-
-
Notifications
You must be signed in to change notification settings - Fork 903
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: add parse/stringify/validate/version/NIL APIs #479
Merged
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ddb7ee9
test: parsing non RFC uuid values (#455)
awwit 3d24e58
Version 9 improvements (#464)
broofa 82ef52b
Merge branch 'master' into v9
broofa bb21fcf
Merge remote-tracking branch 'origin/master' into v9
ctavan 7e2a384
feat: add NIL_UUID export, fixes #475
broofa 2585728
chore: cleanup REGEX export
broofa b3e9ec9
chore: rename NIL_UUID -> NIL
broofa 3116a9e
chore: throw if stringify UUID is not valid
broofa 13db698
chore: rename UUID_NIL -> NIL
broofa 5d5f23b
docs: v9 docs
broofa df6fed7
Merge branch 'v9' of github.com:uuidjs/uuid into v9
broofa b84bfbe
fix: browser spec for nil UUID
broofa 44cc707
fix: remove nil uuid browser tests
broofa 2ee8068
chore: tweak README
broofa a9791a6
fix: readme
broofa af334d3
fix: bundlewatch
broofa 39a07c9
fix: readme
ctavan f15caed
fix: language
ctavan 40cf16a
fix: add uuidNIL
broofa ccc161e
Merge branch 'v9' of github.com:uuidjs/uuid into v9
broofa 91c1599
fix: simplify stringify validation
broofa c16ebbd
fix: rename NIL -> nil
broofa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
{ | ||
"files": [ | ||
{ "path": "./examples/browser-rollup/dist/v1-size.js", "maxSize": "0.8 kB" }, | ||
{ "path": "./examples/browser-rollup/dist/v3-size.js", "maxSize": "1.8 kB" }, | ||
{ "path": "./examples/browser-rollup/dist/v4-size.js", "maxSize": "0.5 kB" }, | ||
{ "path": "./examples/browser-rollup/dist/v5-size.js", "maxSize": "1.2 kB" }, | ||
{ "path": "./examples/browser-rollup/dist/v1-size.js", "maxSize": "1.0 kB" }, | ||
{ "path": "./examples/browser-rollup/dist/v3-size.js", "maxSize": "2.1 kB" }, | ||
{ "path": "./examples/browser-rollup/dist/v4-size.js", "maxSize": "0.7 kB" }, | ||
{ "path": "./examples/browser-rollup/dist/v5-size.js", "maxSize": "1.5 kB" }, | ||
|
||
{ "path": "./examples/browser-webpack/dist/v1-size.js", "maxSize": "1.0 kB" }, | ||
{ "path": "./examples/browser-webpack/dist/v3-size.js", "maxSize": "2.0 kB" }, | ||
{ "path": "./examples/browser-webpack/dist/v4-size.js", "maxSize": "0.7 kB" }, | ||
{ "path": "./examples/browser-webpack/dist/v5-size.js", "maxSize": "1.4 kB" } | ||
{ "path": "./examples/browser-webpack/dist/v1-size.js", "maxSize": "1.3 kB" }, | ||
{ "path": "./examples/browser-webpack/dist/v3-size.js", "maxSize": "2.5 kB" }, | ||
{ "path": "./examples/browser-webpack/dist/v4-size.js", "maxSize": "1.0 kB" }, | ||
{ "path": "./examples/browser-webpack/dist/v5-size.js", "maxSize": "1.9 kB" } | ||
] | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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 @@ | ||
export default '00000000-0000-0000-0000-000000000000'; |
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,41 @@ | ||
import validate from './validate.js'; | ||
|
||
function parse(uuid) { | ||
if (!validate(uuid)) { | ||
throw TypeError('Invalid UUID'); | ||
} | ||
|
||
let v; | ||
const arr = new Uint8Array(16); | ||
|
||
// Parse ########-....-....-....-............ | ||
arr[0] = (v = parseInt(uuid.slice(0, 8), 16)) >>> 24; | ||
arr[1] = (v >>> 16) & 0xff; | ||
arr[2] = (v >>> 8) & 0xff; | ||
arr[3] = v & 0xff; | ||
|
||
// Parse ........-####-....-....-............ | ||
arr[4] = (v = parseInt(uuid.slice(9, 13), 16)) >>> 8; | ||
arr[5] = v & 0xff; | ||
|
||
// Parse ........-....-####-....-............ | ||
arr[6] = (v = parseInt(uuid.slice(14, 18), 16)) >>> 8; | ||
arr[7] = v & 0xff; | ||
|
||
// Parse ........-....-....-####-............ | ||
arr[8] = (v = parseInt(uuid.slice(19, 23), 16)) >>> 8; | ||
arr[9] = v & 0xff; | ||
|
||
// Parse ........-....-....-....-############ | ||
// (Use "/" to avoid 32-bit truncation when bit-shifting high-order bytes) | ||
arr[10] = ((v = parseInt(uuid.slice(24, 36), 16)) / 0x10000000000) & 0xff; | ||
arr[11] = (v / 0x100000000) & 0xff; | ||
arr[12] = (v >>> 24) & 0xff; | ||
arr[13] = (v >>> 16) & 0xff; | ||
arr[14] = (v >>> 8) & 0xff; | ||
arr[15] = v & 0xff; | ||
|
||
return arr; | ||
} | ||
|
||
export default parse; |
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 @@ | ||
export default /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i; |
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,60 @@ | ||
import validate from './validate.js'; | ||
|
||
/** | ||
* Convert array of 16 byte values to UUID string format of the form: | ||
* XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX | ||
*/ | ||
const byteToHex = []; | ||
|
||
for (let i = 0; i < 256; ++i) { | ||
byteToHex.push((i + 0x100).toString(16).substr(1)); | ||
} | ||
|
||
function stringify(arr, offset = 0) { | ||
// Note: Be careful editing this code! It's been tuned for performance | ||
// and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434 | ||
const uuid = ( | ||
byteToHex[arr[offset + 0]] + | ||
byteToHex[arr[offset + 1]] + | ||
byteToHex[arr[offset + 2]] + | ||
byteToHex[arr[offset + 3]] + | ||
'-' + | ||
byteToHex[arr[offset + 4]] + | ||
byteToHex[arr[offset + 5]] + | ||
'-' + | ||
byteToHex[arr[offset + 6]] + | ||
byteToHex[arr[offset + 7]] + | ||
'-' + | ||
byteToHex[arr[offset + 8]] + | ||
byteToHex[arr[offset + 9]] + | ||
'-' + | ||
byteToHex[arr[offset + 10]] + | ||
byteToHex[arr[offset + 11]] + | ||
byteToHex[arr[offset + 12]] + | ||
byteToHex[arr[offset + 13]] + | ||
byteToHex[arr[offset + 14]] + | ||
byteToHex[arr[offset + 15]] | ||
).toLowerCase(); | ||
|
||
// Sanity check for valid UUID. This works because if any of | ||
// the input array values don't map to a defined hex octet, the string length | ||
// will get blown out (e.g. "74af23d8-85undefined2c44-...") | ||
// | ||
// This is a somewhat crude check, but avoids having to check each value | ||
// individually. | ||
if (uuid.length !== 36) { | ||
throw new TypeError( | ||
'Stringified UUID is invalid (All input values must be integers between 0 and 255)', | ||
); | ||
} | ||
broofa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (!validate(uuid)) { | ||
throw TypeError( | ||
'Stringified UUID is invalid (Confirm the RFC `version` and `variant` fields are valid in the input values)', | ||
); | ||
} | ||
|
||
return uuid; | ||
} | ||
|
||
export default stringify; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Will these tests run in parallel? Shouldn't we rather ensure that they are run in series?
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.
I believe Benchmark.js runs synchronously, so these do run in series. (At least, that's what I see when I do
cd examples/benchmark; node benchmark.js
.)