Skip to content

Commit 19fc4aa

Browse files
authored
Merge pull request #21 from danakt/release/v4
Vesion 4.0
2 parents 7e96598 + 7fe2e79 commit 19fc4aa

File tree

12 files changed

+2402
-798
lines changed

12 files changed

+2402
-798
lines changed

.eslintrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"node": true
5+
},
6+
"parserOptions": {
7+
"ecmaVersion": 5
8+
},
9+
"ignorePatterns": "tests"
10+
}

.github/workflows/node.js.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Node.js CI
2+
3+
on:
4+
push:
5+
branches: ['master']
6+
pull_request:
7+
branches: ['master']
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [12.x, 14.x, 16.x]
16+
17+
steps:
18+
- uses: actions/checkout@v3
19+
- name: Use Node.js ${{ matrix.node-version }}
20+
uses: actions/setup-node@v3
21+
with:
22+
node-version: ${{ matrix.node-version }}
23+
cache: 'npm'
24+
- run: npm i
25+
- run: npm test

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
language: node_js
22
node_js:
3-
- '8'
3+
- '14'

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2016 Danakt Frost
3+
Copyright (c) 2020 Danakt Saushkin
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const uuidV5Hash = getUuid('Hello world!', 5);
4545
`getUuid(name [, namespace, version]);`
4646

4747
- `name` — hashing target
48-
- `namespace` _Optional_namespace in which generation occurs
48+
- `namespace` _Optional_UUID namespace
4949
- `version` _Optional_ — 3 or 5, version of UUID
5050

5151
## License

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "uuid-by-string",
3-
"version": "3.0.7",
3+
"version": "4.0.0",
44
"description": "Generating uuid-hash from string",
55
"main": "src/index.js",
66
"typings": "index.d.ts",
77
"scripts": {
88
"test": "jest",
9-
"prepublishOnly": "jest"
9+
"lint": "eslint",
10+
"prepublishOnly": "npm run lint && npm run test"
1011
},
1112
"repository": {
1213
"type": "git",
@@ -23,7 +24,7 @@
2324
"md5",
2425
"sha-1"
2526
],
26-
"author": "Danakt Frost",
27+
"author": "Danakt Saushkin",
2728
"license": "MIT",
2829
"bugs": {
2930
"url": "https://github.com/Danakt/uuid-by-string/issues"
@@ -43,6 +44,7 @@
4344
"js-sha1": "^0.6.0"
4445
},
4546
"devDependencies": {
47+
"eslint": "^8.23.0",
4648
"jest": "^27.0.1"
4749
}
4850
}

src/index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@ function generateUuid(target, namespace, version) {
3131
}
3232

3333
// Parsing target chars
34-
var charBuffer = lib.stringToCharBuffer(target);
35-
36-
// TODO: Test namespace for uuid and parse to buffer
37-
var namespaceCharBuffer = typeof namespace === 'string' ? lib.stringToCharBuffer(namespace) : EMPTY_UINT8_ARRAY;
34+
var targetCharBuffer = lib.stringToCharBuffer(target);
35+
var namespaceCharBuffer = typeof namespace === 'string' ? lib.parseUuid(namespace) : EMPTY_UINT8_ARRAY;
3836

3937
// Concatenation two buffers of strings to one
40-
var buffer = lib.concatBuffers(namespaceCharBuffer, charBuffer);
38+
var buffer = lib.concatBuffers(namespaceCharBuffer, targetCharBuffer);
4139

4240
// Getting hash
4341
var hash = version === 3 ? lib.md5Hash(buffer) : lib.sha1Hash(buffer);

src/lib.js

Lines changed: 93 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,31 @@ var sha1 = require('js-sha1');
44
/** List of hex digit for fast accessing by index */
55
var HEX_DIGITS = '0123456789abcdef'.split('');
66

7+
/** Length of string containing uuid */
8+
var UUID_LENGTH = 36;
9+
10+
/** Regular expression for uuid testing */
11+
var UUID_REGEXP = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
12+
13+
/** Map matching of hex number and corresponding byte */
14+
var HEX_TO_BYTE_MAP = (function () {
15+
var map = {};
16+
17+
for (var i = 0; i < 256; i++) {
18+
var hex = i.toString(16);
19+
20+
map[hex.length === 1 ? '0' + hex : hex] = i;
21+
}
22+
23+
return map;
24+
})();
25+
726
/**
827
* Converts unsigned byte to hex representation
928
* @param {number} ubyte The unsigned byte to convert
1029
* @returns {string} The hex representation
1130
*/
12-
var uint8ToHex = function(ubyte) {
31+
var uint8ToHex = function (ubyte) {
1332
var first = ubyte >> 4;
1433
var second = ubyte - (first << 4);
1534

@@ -21,26 +40,27 @@ var uint8ToHex = function(ubyte) {
2140
* @param {Uint8Array} buf The unsigned bytes buffer
2241
* @returns {string} The hex string representation
2342
*/
24-
var uint8ArrayToHex = function(buf) {
25-
var out = [];
43+
var uint8ArrayToHex = function (buf) {
44+
var out = '';
2645

2746
for (var i = 0; i < buf.length; i++) {
28-
out.push(uint8ToHex(buf[i]));
47+
out += uint8ToHex(buf[i]);
2948
}
3049

31-
return out.join('');
50+
return out;
3251
};
3352

3453
/**
3554
* Converts string to buffer of char codes
3655
* @param {string} str The string to parse
3756
* @returns {Uint8Array} Buffer of char codes
3857
*/
39-
var stringToCharBuffer = function(str) {
40-
var buffer = new Uint8Array(str.length);
58+
var stringToCharBuffer = function (str) {
59+
var escapedStr = unescape(encodeURIComponent(str));
60+
var buffer = new Uint8Array(escapedStr.length);
4161

42-
for (var i = 0; i < str.length; i++) {
43-
buffer[i] = str[i].charCodeAt(0);
62+
for (var i = 0; i < escapedStr.length; i++) {
63+
buffer[i] = escapedStr[i].charCodeAt(0);
4464
}
4565

4666
return buffer;
@@ -51,7 +71,7 @@ var stringToCharBuffer = function(str) {
5171
* @param {Uint8Array} buf Buffer of char codes
5272
* @returns {Uint8Array} MD5 hash buffer
5373
*/
54-
var md5Hash = function(buf) {
74+
var md5Hash = function (buf) {
5575
return new Uint8Array(md5.arrayBuffer(buf));
5676
};
5777

@@ -60,7 +80,7 @@ var md5Hash = function(buf) {
6080
* @param {Uint8Array} buf Buffer of char codes
6181
* @returns {Uint8Array} SHA-1 hash buffer
6282
*/
63-
var sha1Hash = function(buf) {
83+
var sha1Hash = function (buf) {
6484
return new Uint8Array(sha1.arrayBuffer(buf));
6585
};
6686

@@ -70,7 +90,7 @@ var sha1Hash = function(buf) {
7090
* @param {Uint8Array} buf2 The second buffer to concatenate
7191
* @returns {Uint8Array} Concatenation result
7292
*/
73-
var concatBuffers = function(buf1, buf2) {
93+
var concatBuffers = function (buf1, buf2) {
7494
var out = new Uint8Array(buf1.length + buf2.length);
7595

7696
out.set(new Uint8Array(buf1), 0);
@@ -79,44 +99,81 @@ var concatBuffers = function(buf1, buf2) {
7999
return out;
80100
};
81101

102+
/**
103+
* Validates UUID
104+
* @param {string} uuid UUID to validate
105+
* @return {boolean} Validation result
106+
*/
107+
var validateUuid = function (uuid) {
108+
return typeof uuid === 'string' && uuid.length === UUID_LENGTH && UUID_REGEXP.test(uuid);
109+
};
110+
111+
/**
112+
* Parses UUID into a buffer
113+
* @param {string} uuid UUID to parse
114+
* @returns {Uint8Array} Ready buffer
115+
*/
116+
var parseUuid = function (uuid) {
117+
if (!validateUuid(uuid)) {
118+
throw TypeError('Invalid UUID');
119+
}
120+
121+
var buf = new Uint8Array(16);
122+
var strIndex = 0;
123+
var bufIndex = 0;
124+
125+
while (strIndex < uuid.length) {
126+
if (uuid[strIndex] === '-') {
127+
strIndex++;
128+
continue;
129+
}
130+
131+
var oct = (uuid[strIndex] + uuid[strIndex + 1]).toLowerCase();
132+
buf[bufIndex] = HEX_TO_BYTE_MAP[oct];
133+
134+
bufIndex++;
135+
strIndex += 2;
136+
}
137+
138+
return buf;
139+
};
140+
82141
/**
83142
* Creates uuid from hash buffer
84143
* @param {Uint8Array} hashBuffer Hash buffer
85144
* @param {3|5} version Version of uuid
86145
* @returns {string} The uuid
87146
*/
88-
var hashToUuid = function(hashBuffer, version) {
89-
return [
147+
var hashToUuid = function (hashBuffer, version) {
148+
return (
90149
// The low field of the timestamp
91-
uint8ArrayToHex(hashBuffer.slice(0, 4)),
92-
'-',
93-
150+
uint8ArrayToHex(hashBuffer.slice(0, 4)) +
151+
'-' +
94152
// The middle field of the timestamp
95-
uint8ArrayToHex(hashBuffer.slice(4, 6)),
96-
'-',
97-
153+
uint8ArrayToHex(hashBuffer.slice(4, 6)) +
154+
'-' +
98155
// The high field of the timestamp multiplexed with the version number
99-
uint8ToHex((hashBuffer[6] & 0x0f) | parseInt(version * 10, 16)),
100-
uint8ToHex(hashBuffer[7]),
101-
'-',
102-
156+
uint8ToHex((hashBuffer[6] & 0x0f) | parseInt(version * 10, 16)) +
157+
uint8ToHex(hashBuffer[7]) +
158+
'-' +
103159
// The high field of the clock sequence multiplexed with the variant
104-
uint8ToHex((hashBuffer[8] & 0x3f) | 0x80),
160+
uint8ToHex((hashBuffer[8] & 0x3f) | 0x80) +
105161
// The low field of the clock sequence
106-
uint8ToHex(hashBuffer[9]),
107-
'-',
162+
uint8ToHex(hashBuffer[9]) +
163+
'-' +
108164
// The spatially unique node identifier
109-
110165
uint8ArrayToHex(hashBuffer.slice(10, 16))
111-
].join('');
166+
);
112167
};
113168

114169
module.exports = {
115-
uint8ToHex,
116-
uint8ArrayToHex,
117-
stringToCharBuffer,
118-
md5Hash,
119-
sha1Hash,
120-
concatBuffers,
121-
hashToUuid
170+
uint8ToHex: uint8ToHex,
171+
uint8ArrayToHex: uint8ArrayToHex,
172+
stringToCharBuffer: stringToCharBuffer,
173+
md5Hash: md5Hash,
174+
sha1Hash: sha1Hash,
175+
concatBuffers: concatBuffers,
176+
validateUuid: validateUuid,
177+
parseUuid: parseUuid,
178+
hashToUuid: hashToUuid,
122179
};

tests/__mock__/samples.js

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

0 commit comments

Comments
 (0)