-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(Utility): expose UUID generator #303
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ describe(`retryWithBackoff`, function() { | |
const error = new Error('bar'); | ||
const seenValues: {[idx: number]: number} = {}; | ||
|
||
// tslint:disable-next-line deprecation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, so I will assume There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're actually just getting around a TSLint bug. The deprecation rule is triggered even though There is an open PR to fix this which would allow us to remove all of these disables. But since TSLint itself is deprecated I'm not holding my breath on it getting fixed. |
||
of(1, 2, 3).pipe( | ||
tap(v => { | ||
if (!seenValues[v]) { | ||
|
@@ -33,6 +34,7 @@ describe(`retryWithBackoff`, function() { | |
retries: 2, | ||
delayCalculator: linearBackoff, | ||
}), | ||
// tslint:disable-next-line no-any | ||
).subscribe(() => {}, (err: any) => { | ||
expect(err).toEqual(error); | ||
expect(seenValues).toEqual({ | ||
|
@@ -48,6 +50,7 @@ describe(`retryWithBackoff`, function() { | |
const error = new Error('bar'); | ||
const seenValues: {[idx: number]: number} = {}; | ||
|
||
// tslint:disable-next-line deprecation | ||
of(1, 2, 3, 4).pipe( | ||
tap(v => { | ||
if (!seenValues[v]) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { uuidRegex } from '@terminus/ngx-tools/regex'; | ||
|
||
import { generateUUID } from './uuid'; | ||
|
||
|
||
describe(`uuid`, function() { | ||
const testUUIDGenerator = function(generator: () => string, iterations = 1, done: jest.DoneCallback): Promise<void> { | ||
const uuidstore: Record<string, string> = {}; | ||
let i; | ||
let newUuid; | ||
|
||
for (i = 0; i < iterations; i++) { | ||
newUuid = generator(); | ||
|
||
// Test Validity | ||
if (!uuidRegex.test(newUuid)) { | ||
done.fail(new Error(`Not a valid UUID: ${newUuid}`)); | ||
} | ||
|
||
// Test Collision | ||
if (uuidstore[newUuid]) { | ||
done.fail(new Error(`Collision on ${newUuid}`)); | ||
} | ||
uuidstore[newUuid] = newUuid; | ||
} | ||
|
||
return Promise.resolve(); | ||
}; | ||
|
||
|
||
test(`should create UUIDs that do not collide`, function(done) { | ||
testUUIDGenerator(generateUUID, 100, done).then(() => { | ||
done(); | ||
}); | ||
}); | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* eslint-disable no-magic-numbers */ | ||
/** | ||
* Generate a canonically formatted UUID that is Version 1 through 5 and is the appropriate Variant as per RFC4122. | ||
* | ||
* @example | ||
* generateUUID() // Returns: `f4ee5eed-ed19-3681-713e-907a23ed7858` | ||
* | ||
* @return The UUID | ||
*/ | ||
export function generateUUID(): string { | ||
const buf = new Uint16Array(8); | ||
window.crypto.getRandomValues(buf); | ||
|
||
const S4 = function(num: number) { | ||
let ret = num.toString(16); | ||
while (ret.length < 4) { | ||
ret = `0${ret}`; | ||
} | ||
return ret; | ||
}; | ||
|
||
return (`${S4(buf[0]) + S4(buf[1])}-${S4(buf[2])}-${S4(buf[3])}-${S4(buf[4])}-${S4(buf[5])}${S4(buf[6])}${S4(buf[7])}`); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -220,7 +220,8 @@ | |
"tsickle": "^0.34.0", | ||
"tslint": "^5.16.0", | ||
"typescript": "~3.2.1", | ||
"validate-commit-msg": "^2.14.0" | ||
"validate-commit-msg": "^2.14.0", | ||
"window-crypto": "^1.1.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need this to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only for testing. Jest doesn't have a polyfill for |
||
}, | ||
"husky": { | ||
"hooks": { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,42 @@ | ||
// Polyfill `window.crypto` | ||
import 'window-crypto'; | ||
|
||
// tslint:disable no-any no-unsafe-any | ||
const mock = () => { | ||
let storage: Record<string, any> = {}; | ||
return { | ||
getItem: (key: string): any => key in storage ? storage[key] : null, | ||
getItem: (key: string): any => (key in storage ? storage[key] : null), | ||
setItem: (key: string, value: any) => { | ||
storage[key] = value || ''; | ||
return storage[key]; | ||
}, | ||
removeItem: (key: string) => delete storage[key], | ||
clear: () => storage = {}, | ||
clear: () => (storage = {}), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are the parenthesis needed or for better readability? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kind of. Creating an assignment in a return statement can be confusing so we have a lint rule warning about this. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A better way for me to phrase it: It is not more readable, but the intent should be more clear. |
||
}; | ||
}; | ||
// tslint:enable no-any no-unsafe-any | ||
|
||
Object.defineProperty(window, 'localStorage', {value: mock()}); | ||
Object.defineProperty(window, 'sessionStorage', {value: mock()}); | ||
Object.defineProperty(window, 'getComputedStyle', { | ||
value: () => ['-webkit-appearance'], | ||
}); | ||
Object.defineProperty(window, 'getComputedStyle', {value: () => ['-webkit-appearance']}); | ||
Object.defineProperty(window, 'CSS', {value: () => ({})}); | ||
|
||
|
||
|
||
|
||
/** | ||
* Patches for Material | ||
*/ | ||
const WARN_SUPPRESSING_PATTERNS = [ | ||
/Could not find Angular Material core theme/, | ||
/Could not find HammerJS/, | ||
/Could not find Angular Material core theme/, | ||
/Could not find HammerJS/, | ||
]; | ||
// eslint-disable-next-line no-console | ||
const warn = console.warn; | ||
Object.defineProperty(console, 'warn', { | ||
value: (...params: string[]) => { | ||
if (!WARN_SUPPRESSING_PATTERNS.some((pattern) => pattern.test(params[0]))) { | ||
warn(...params); | ||
} | ||
}, | ||
value: (...params: string[]) => { | ||
if (!WARN_SUPPRESSING_PATTERNS.some(pattern => pattern.test(params[0]))) { | ||
warn(...params); | ||
} | ||
}, | ||
}); | ||
Object.defineProperty(window, 'matchMedia', { | ||
value: () => ( | ||
|
@@ -48,9 +48,8 @@ Object.defineProperty(window, 'matchMedia', { | |
), | ||
}); | ||
Object.defineProperty(document.body.style, 'transform', { | ||
value: () => | ||
({ | ||
enumerable: true, | ||
configurable: true, | ||
}), | ||
value: () => ({ | ||
enumerable: true, | ||
configurable: true, | ||
}), | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is getting deprecated, here, 118, 136, and 155?