Skip to content

Commit 7626b7c

Browse files
authored
Jordan/optimize utils (#231)
* Add license headers, add sprintf to utils * Bump logging/utils to 0.1.2 and use internal sprintf function
1 parent 78ba995 commit 7626b7c

File tree

12 files changed

+198
-93
lines changed

12 files changed

+198
-93
lines changed

packages/logging/package-lock.json

Lines changed: 1 addition & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/logging/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@optimizely/js-sdk-logging",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Optimizely Full Stack Core Logging",
55
"author": "jordangarcia <[email protected]>",
66
"homepage": "https://github.com/optimizely/javascript-sdk",
@@ -20,12 +20,10 @@
2020
"prepublishOnly": "jest && npm run tsc"
2121
},
2222
"dependencies": {
23-
"@optimizely/js-sdk-utils": "^0.1.1",
24-
"sprintf-js": "^1.1.1"
23+
"@optimizely/js-sdk-utils": "^0.1.2"
2524
},
2625
"devDependencies": {
2726
"@types/jest": "^23.3.12",
28-
"@types/sprintf-js": "^1.1.1",
2927
"jest": "^23.6.0",
3028
"ts-jest": "^23.10.5"
3129
}

packages/logging/src/errorHandler.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Copyright 2019, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
/**
217
* @export
318
* @interface ErrorHandler

packages/logging/src/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Copyright 2019, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
export * from './errorHandler'
217
export * from './models'
318
export * from './logger'

packages/logging/src/logger.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
import { sprintf } from 'sprintf-js'
1+
/**
2+
* Copyright 2019, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
216
import { getErrorHandler } from './errorHandler'
3-
import { isValidEnum } from '@optimizely/js-sdk-utils'
17+
import { isValidEnum, sprintf } from '@optimizely/js-sdk-utils'
418

519
import { LogLevel, LoggerFacade, LogManager, LogHandler } from './models'
620

packages/logging/src/models.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Copyright 2019, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
export enum LogLevel {
217
NOTSET = 0,
318
DEBUG = 1,

packages/optimizely-sdk/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/utils/__tests__/utils.spec.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// <reference types="jest" />
2-
import { isValidEnum, groupBy, objectValues, find, keyBy } from '../src/utils'
2+
import { isValidEnum, groupBy, objectValues, find, keyBy, sprintf } from '../src'
33

44
describe('utils', () => {
55
describe('isValidEnum', () => {
@@ -84,4 +84,34 @@ describe('utils', () => {
8484
})
8585
})
8686
})
87+
88+
describe('sprintf', () => {
89+
it('sprintf(msg)', () => {
90+
expect(sprintf('this is my message')).toBe('this is my message')
91+
})
92+
93+
it('sprintf(msg, arg1)', () => {
94+
expect(sprintf('hi %s', 'jordan')).toBe('hi jordan')
95+
})
96+
97+
it('sprintf(msg, arg1, arg2)', () => {
98+
expect(sprintf('hi %s its %s', 'jordan', 'jon')).toBe('hi jordan its jon')
99+
})
100+
101+
it('should print undefined if an argument is missing', () => {
102+
expect(sprintf('hi %s its %s', 'jordan')).toBe('hi jordan its undefined')
103+
})
104+
105+
it('should evaluate a function', () => {
106+
expect(sprintf('hi %s its %s', 'jordan', () => 'a function')).toBe('hi jordan its a function')
107+
})
108+
109+
it('should work with numbers', () => {
110+
expect(sprintf('hi %s', 123)).toBe('hi 123')
111+
})
112+
113+
it('should not error when passed an object', () => {
114+
expect(sprintf('hi %s', { foo: 'bar' })).toBe('hi [object Object]')
115+
})
116+
})
87117
})

packages/utils/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@optimizely/js-sdk-utils",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Optimizely Full Stack Utils",
55
"author": "jordangarcia <[email protected]>",
66
"homepage": "https://github.com/optimizely/javascript-sdk",

packages/utils/src/index.ts

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,100 @@
1-
export * from './utils'
1+
/**
2+
* Copyright 2019, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { v4 } from 'uuid'
17+
18+
export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>
19+
20+
export function getTimestamp(): number {
21+
return new Date().getTime()
22+
}
23+
24+
export function generateUUID(): string {
25+
return v4()
26+
}
27+
28+
/**
29+
* Validates a value is a valid TypeScript enum
30+
*
31+
* @export
32+
* @param {object} enumToCheck
33+
* @param {*} value
34+
* @returns {boolean}
35+
*/
36+
export function isValidEnum(enumToCheck: { [key: string]: any }, value: any): boolean {
37+
let found = false
38+
39+
const keys = Object.keys(enumToCheck)
40+
for (let index = 0; index < keys.length; index++) {
41+
if (value === enumToCheck[keys[index]]) {
42+
found = true
43+
break
44+
}
45+
}
46+
return found
47+
}
48+
49+
export function groupBy<K>(arr: K[], grouperFn: (item: K) => string): Array<K[]> {
50+
const grouper: { [key: string]: K[] } = {}
51+
52+
arr.forEach(item => {
53+
const key = grouperFn(item)
54+
grouper[key] = grouper[key] || []
55+
grouper[key].push(item)
56+
})
57+
58+
return objectValues(grouper)
59+
}
60+
61+
export function objectValues<K>(obj: { [key: string]: K }): K[] {
62+
return Object.keys(obj).map(key => obj[key])
63+
}
64+
65+
export function find<K>(arr: K[], cond: (arg: K) => boolean): K | undefined {
66+
let found
67+
68+
for (let item of arr) {
69+
if (cond(item)) {
70+
found = item
71+
break
72+
}
73+
}
74+
75+
return found
76+
}
77+
78+
export function keyBy<K>(arr: K[], keyByFn: (item: K) => string): { [key: string]: K } {
79+
let map: { [key: string]: K } = {}
80+
arr.forEach(item => {
81+
const key = keyByFn(item)
82+
map[key] = item
83+
})
84+
return map
85+
}
86+
87+
export function sprintf(format: string, ...args: any[]): string {
88+
var i = 0
89+
return format.replace(/%s/g, function() {
90+
const arg = args[i++]
91+
const type = typeof arg
92+
if (type === 'function') {
93+
return arg()
94+
} else if (type === 'string') {
95+
return arg
96+
} else {
97+
return String(arg)
98+
}
99+
})
100+
}

packages/utils/src/utils.ts

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)