uuid-by-string 3.0.7 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "env": {
3
+ "browser": true,
4
+ "node": true
5
+ },
6
+ "parserOptions": {
7
+ "ecmaVersion": 5
8
+ },
9
+ "ignorePatterns": "tests"
10
+ }
@@ -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
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2016 Danakt Frost
3
+ Copyright (c) 2020 Danakt Saushkin
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -45,7 +45,7 @@ const uuidV5Hash = getUuid('Hello world!', 5);
45
45
  `getUuid(name [, namespace, version]);`
46
46
 
47
47
  - `name` — hashing target
48
- - `namespace` _Optional_ — namespace in which generation occurs
48
+ - `namespace` _Optional_ — UUID namespace
49
49
  - `version` _Optional_ — 3 or 5, version of UUID
50
50
 
51
51
  ## License
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "uuid-by-string",
3
- "version": "3.0.7",
3
+ "version": "4.0.0",
4
4
  "description": "Generating uuid-hash from string",
5
5
  "main": "src/index.js",
6
6
  "typings": "index.d.ts",
7
7
  "scripts": {
8
8
  "test": "jest",
9
- "prepublishOnly": "jest"
9
+ "lint": "eslint",
10
+ "prepublishOnly": "npm run lint && npm run test"
10
11
  },
11
12
  "repository": {
12
13
  "type": "git",
@@ -23,7 +24,7 @@
23
24
  "md5",
24
25
  "sha-1"
25
26
  ],
26
- "author": "Danakt Frost",
27
+ "author": "Danakt Saushkin",
27
28
  "license": "MIT",
28
29
  "bugs": {
29
30
  "url": "https://github.com/Danakt/uuid-by-string/issues"
@@ -43,6 +44,7 @@
43
44
  "js-sha1": "^0.6.0"
44
45
  },
45
46
  "devDependencies": {
47
+ "eslint": "^8.23.0",
46
48
  "jest": "^27.0.1"
47
49
  }
48
50
  }
package/src/index.js CHANGED
@@ -31,13 +31,11 @@ function generateUuid(target, namespace, version) {
31
31
  }
32
32
 
33
33
  // 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;
38
36
 
39
37
  // Concatenation two buffers of strings to one
40
- var buffer = lib.concatBuffers(namespaceCharBuffer, charBuffer);
38
+ var buffer = lib.concatBuffers(namespaceCharBuffer, targetCharBuffer);
41
39
 
42
40
  // Getting hash
43
41
  var hash = version === 3 ? lib.md5Hash(buffer) : lib.sha1Hash(buffer);
package/src/lib.js CHANGED
@@ -4,12 +4,31 @@ var sha1 = require('js-sha1');
4
4
  /** List of hex digit for fast accessing by index */
5
5
  var HEX_DIGITS = '0123456789abcdef'.split('');
6
6
 
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
+
7
26
  /**
8
27
  * Converts unsigned byte to hex representation
9
28
  * @param {number} ubyte The unsigned byte to convert
10
29
  * @returns {string} The hex representation
11
30
  */
12
- var uint8ToHex = function(ubyte) {
31
+ var uint8ToHex = function (ubyte) {
13
32
  var first = ubyte >> 4;
14
33
  var second = ubyte - (first << 4);
15
34
 
@@ -21,14 +40,14 @@ var uint8ToHex = function(ubyte) {
21
40
  * @param {Uint8Array} buf The unsigned bytes buffer
22
41
  * @returns {string} The hex string representation
23
42
  */
24
- var uint8ArrayToHex = function(buf) {
25
- var out = [];
43
+ var uint8ArrayToHex = function (buf) {
44
+ var out = '';
26
45
 
27
46
  for (var i = 0; i < buf.length; i++) {
28
- out.push(uint8ToHex(buf[i]));
47
+ out += uint8ToHex(buf[i]);
29
48
  }
30
49
 
31
- return out.join('');
50
+ return out;
32
51
  };
33
52
 
34
53
  /**
@@ -36,11 +55,12 @@ var uint8ArrayToHex = function(buf) {
36
55
  * @param {string} str The string to parse
37
56
  * @returns {Uint8Array} Buffer of char codes
38
57
  */
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);
41
61
 
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);
44
64
  }
45
65
 
46
66
  return buffer;
@@ -51,7 +71,7 @@ var stringToCharBuffer = function(str) {
51
71
  * @param {Uint8Array} buf Buffer of char codes
52
72
  * @returns {Uint8Array} MD5 hash buffer
53
73
  */
54
- var md5Hash = function(buf) {
74
+ var md5Hash = function (buf) {
55
75
  return new Uint8Array(md5.arrayBuffer(buf));
56
76
  };
57
77
 
@@ -60,7 +80,7 @@ var md5Hash = function(buf) {
60
80
  * @param {Uint8Array} buf Buffer of char codes
61
81
  * @returns {Uint8Array} SHA-1 hash buffer
62
82
  */
63
- var sha1Hash = function(buf) {
83
+ var sha1Hash = function (buf) {
64
84
  return new Uint8Array(sha1.arrayBuffer(buf));
65
85
  };
66
86
 
@@ -70,7 +90,7 @@ var sha1Hash = function(buf) {
70
90
  * @param {Uint8Array} buf2 The second buffer to concatenate
71
91
  * @returns {Uint8Array} Concatenation result
72
92
  */
73
- var concatBuffers = function(buf1, buf2) {
93
+ var concatBuffers = function (buf1, buf2) {
74
94
  var out = new Uint8Array(buf1.length + buf2.length);
75
95
 
76
96
  out.set(new Uint8Array(buf1), 0);
@@ -79,44 +99,81 @@ var concatBuffers = function(buf1, buf2) {
79
99
  return out;
80
100
  };
81
101
 
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
+
82
141
  /**
83
142
  * Creates uuid from hash buffer
84
143
  * @param {Uint8Array} hashBuffer Hash buffer
85
144
  * @param {3|5} version Version of uuid
86
145
  * @returns {string} The uuid
87
146
  */
88
- var hashToUuid = function(hashBuffer, version) {
89
- return [
147
+ var hashToUuid = function (hashBuffer, version) {
148
+ return (
90
149
  // The low field of the timestamp
91
- uint8ArrayToHex(hashBuffer.slice(0, 4)),
92
- '-',
93
-
150
+ uint8ArrayToHex(hashBuffer.slice(0, 4)) +
151
+ '-' +
94
152
  // The middle field of the timestamp
95
- uint8ArrayToHex(hashBuffer.slice(4, 6)),
96
- '-',
97
-
153
+ uint8ArrayToHex(hashBuffer.slice(4, 6)) +
154
+ '-' +
98
155
  // 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
+ '-' +
103
159
  // The high field of the clock sequence multiplexed with the variant
104
- uint8ToHex((hashBuffer[8] & 0x3f) | 0x80),
160
+ uint8ToHex((hashBuffer[8] & 0x3f) | 0x80) +
105
161
  // The low field of the clock sequence
106
- uint8ToHex(hashBuffer[9]),
107
- '-',
162
+ uint8ToHex(hashBuffer[9]) +
163
+ '-' +
108
164
  // The spatially unique node identifier
109
-
110
165
  uint8ArrayToHex(hashBuffer.slice(10, 16))
111
- ].join('');
166
+ );
112
167
  };
113
168
 
114
169
  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,
122
179
  };