-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Utility): expose UUID generator
ISSUES CLOSED: #91
- Loading branch information
1 parent
f323396
commit eb08352
Showing
7 changed files
with
103 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('This is the error')); | ||
} | ||
|
||
// 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(); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters