@valkyriestudios/utils 12.44.0 → 12.45.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/README.md +21 -0
- package/array/dedupe.d.ts +2 -1
- package/cjs/array/dedupe.js +18 -21
- package/cjs/array/mapFn.js +8 -9
- package/cjs/hash/guid.js +18 -24
- package/cjs/hash/hexId.js +28 -0
- package/cjs/hash/index.js +5 -1
- package/cjs/hash/uuidv7.js +42 -0
- package/cjs/object/is.js +1 -1
- package/cjs/object/isNotEmpty.js +1 -1
- package/esm/array/dedupe.js +18 -21
- package/esm/array/mapFn.js +8 -9
- package/esm/hash/guid.js +18 -24
- package/esm/hash/hexId.js +25 -0
- package/esm/hash/index.js +2 -0
- package/esm/hash/uuidv7.js +39 -0
- package/esm/object/is.js +1 -1
- package/esm/object/isNotEmpty.js +1 -1
- package/hash/hexId.d.ts +7 -0
- package/hash/index.d.ts +2 -0
- package/hash/uuidv7.d.ts +5 -0
- package/index.d.ts +15 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -994,6 +994,27 @@ import guid from '@valkyriestudios/utils/hash/guid';
|
|
|
994
994
|
guid(); // 245caf1a-86af-11e7-bb31-be2e44b06b34
|
|
995
995
|
```
|
|
996
996
|
|
|
997
|
+
### hash/hexId(size:number)
|
|
998
|
+
Generate a cryptographically secure random hex string.
|
|
999
|
+
|
|
1000
|
+
⚠️ The size parameter is in bytes, the resulting string will be size * 2 characters long
|
|
1001
|
+
```typescript
|
|
1002
|
+
import hexId from '@valkyriestudios/utils/hash/hexId';
|
|
1003
|
+
|
|
1004
|
+
hexId(8); // "7a3c1b9f8e2d4c71"
|
|
1005
|
+
hexId(16); // "3f6e1b2c4a7d9f0e2b6c8d1a3e4f7b9c"
|
|
1006
|
+
```
|
|
1007
|
+
|
|
1008
|
+
### hash/uuidv7()
|
|
1009
|
+
Generate a unique uuidv7 identifier according to [RFC 9562](https://www.rfc-editor.org/rfc/rfc9562)
|
|
1010
|
+
|
|
1011
|
+
These IDs are **time-ordered** while still being 128-bit random identifiers.
|
|
1012
|
+
```typescript
|
|
1013
|
+
import uuidv7 from '@valkyriestudios/utils/hash/uuidv7';
|
|
1014
|
+
|
|
1015
|
+
uuidv7(); // "018f4d5b-9c61-7d2f-8e3b-3a90b5f78c2d"
|
|
1016
|
+
```
|
|
1017
|
+
|
|
997
1018
|
### hash/djb2(val:unknown)
|
|
998
1019
|
Generate a djb2 hash from an object/array/primitive/...
|
|
999
1020
|
```typescript
|
package/array/dedupe.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
type DedupeFilterFn<T> = (el: T) => boolean;
|
|
1
2
|
type DedupeOptionsBase<T> = {
|
|
2
3
|
/**
|
|
3
4
|
* Pass a custom filter function which will be run in O(n) while deduping is going on
|
|
4
5
|
*/
|
|
5
|
-
filter_fn?:
|
|
6
|
+
filter_fn?: DedupeFilterFn<T>;
|
|
6
7
|
};
|
|
7
8
|
type DedupeOptionsWithKey<T extends Record<string, unknown>> = DedupeOptionsBase<T> & {
|
|
8
9
|
/**
|
package/cjs/array/dedupe.js
CHANGED
|
@@ -2,40 +2,37 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.dedupe = dedupe;
|
|
4
4
|
exports.default = dedupe;
|
|
5
|
-
const utils_1 = require("../hash/utils");
|
|
6
5
|
function dedupe(val, opts) {
|
|
7
6
|
if (!Array.isArray(val))
|
|
8
7
|
return [];
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const set = new Set();
|
|
8
|
+
const filter = opts?.filter_fn;
|
|
9
|
+
const key = opts?.key;
|
|
12
10
|
const acc = [];
|
|
13
|
-
|
|
11
|
+
const seenPrimitives = new Set();
|
|
14
12
|
const len = val.length;
|
|
15
|
-
if (
|
|
16
|
-
const CUSTOM_FILTER_FN = typeof FILTER_FN === 'function'
|
|
17
|
-
? (el) => el && Object.prototype.toString.call(el) === '[object Object]' && FILTER_FN(el)
|
|
18
|
-
: (el) => el && Object.prototype.toString.call(el) === '[object Object]';
|
|
13
|
+
if (filter) {
|
|
19
14
|
for (let i = 0; i < len; i++) {
|
|
20
15
|
const el = val[i];
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
16
|
+
const raw = key ? el?.[key] : el;
|
|
17
|
+
if (raw != null && filter(el)) {
|
|
18
|
+
const hash = typeof raw !== 'object' ? raw : JSON.stringify(raw);
|
|
19
|
+
if (!seenPrimitives.has(hash)) {
|
|
20
|
+
seenPrimitives.add(hash);
|
|
21
|
+
acc.push(el);
|
|
22
|
+
}
|
|
27
23
|
}
|
|
28
24
|
}
|
|
29
25
|
}
|
|
30
26
|
else {
|
|
31
27
|
for (let i = 0; i < len; i++) {
|
|
32
28
|
const el = val[i];
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
29
|
+
const raw = key ? el?.[key] : el;
|
|
30
|
+
if (raw != null) {
|
|
31
|
+
const hash = typeof raw !== 'object' ? raw : JSON.stringify(raw);
|
|
32
|
+
if (!seenPrimitives.has(hash)) {
|
|
33
|
+
seenPrimitives.add(hash);
|
|
34
|
+
acc.push(el);
|
|
35
|
+
}
|
|
39
36
|
}
|
|
40
37
|
}
|
|
41
38
|
}
|
package/cjs/array/mapFn.js
CHANGED
|
@@ -4,21 +4,20 @@ exports.mapFn = mapFn;
|
|
|
4
4
|
exports.default = mapFn;
|
|
5
5
|
const merge_1 = require("../object/merge");
|
|
6
6
|
function mapFn(arr, fn, opts) {
|
|
7
|
-
if (
|
|
8
|
-
typeof fn !== 'function')
|
|
7
|
+
if (!Array.isArray(arr) || typeof fn !== 'function')
|
|
9
8
|
return {};
|
|
10
9
|
const MERGE = opts?.merge === true;
|
|
11
10
|
const TRANSFORM_FN = opts?.transform_fn;
|
|
12
11
|
const map = {};
|
|
13
12
|
for (let i = 0; i < arr.length; i++) {
|
|
14
13
|
const el = arr[i];
|
|
15
|
-
if (Object.prototype.toString.call(el)
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
if (Object.prototype.toString.call(el) === '[object Object]') {
|
|
15
|
+
let hash = fn(el);
|
|
16
|
+
if (Number.isFinite(hash) || (typeof hash === 'string' && hash.length)) {
|
|
17
|
+
hash = String(hash);
|
|
18
|
+
const transformed = TRANSFORM_FN ? TRANSFORM_FN(el) : el;
|
|
19
|
+
map[hash] = MERGE && hash in map ? (0, merge_1.merge)(map[hash], transformed, { union: true }) : transformed;
|
|
20
|
+
}
|
|
22
21
|
}
|
|
23
22
|
}
|
|
24
23
|
return map;
|
package/cjs/hash/guid.js
CHANGED
|
@@ -6,29 +6,23 @@ const HEX = [];
|
|
|
6
6
|
for (let i = 0; i < 256; i++) {
|
|
7
7
|
HEX[i] = (i < 16 ? '0' : '') + i.toString(16);
|
|
8
8
|
}
|
|
9
|
+
let pool = new Uint8Array(0);
|
|
10
|
+
let poolIdx = 0;
|
|
11
|
+
function refill(size = 16 * 1024) {
|
|
12
|
+
pool = new Uint8Array(size);
|
|
13
|
+
crypto.getRandomValues(pool);
|
|
14
|
+
poolIdx = 0;
|
|
15
|
+
}
|
|
9
16
|
function guid() {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
HEX[
|
|
18
|
-
'-' +
|
|
19
|
-
HEX[
|
|
20
|
-
HEX[
|
|
21
|
-
'-' +
|
|
22
|
-
HEX[((d1 >> 16) & 0x0f) | 0x40] +
|
|
23
|
-
HEX[(d1 >> 24) & 0xff] +
|
|
24
|
-
'-' +
|
|
25
|
-
HEX[(d2 & 0x3f) | 0x80] +
|
|
26
|
-
HEX[(d2 >> 8) & 0xff] +
|
|
27
|
-
'-' +
|
|
28
|
-
HEX[(d2 >> 16) & 0xff] +
|
|
29
|
-
HEX[(d2 >> 24) & 0xff] +
|
|
30
|
-
HEX[d3 & 0xff] +
|
|
31
|
-
HEX[(d3 >> 8) & 0xff] +
|
|
32
|
-
HEX[(d3 >> 16) & 0xff] +
|
|
33
|
-
HEX[(d3 >> 24) & 0xff];
|
|
17
|
+
if (poolIdx + 16 > pool.length)
|
|
18
|
+
refill();
|
|
19
|
+
const buf = pool.subarray(poolIdx, poolIdx + 16);
|
|
20
|
+
poolIdx += 16;
|
|
21
|
+
buf[6] = (buf[6] & 0x0f) | 0x40;
|
|
22
|
+
buf[8] = (buf[8] & 0x3f) | 0x80;
|
|
23
|
+
return (HEX[buf[0]] + HEX[buf[1]] + HEX[buf[2]] + HEX[buf[3]] + '-' +
|
|
24
|
+
HEX[buf[4]] + HEX[buf[5]] + '-' +
|
|
25
|
+
HEX[buf[6]] + HEX[buf[7]] + '-' +
|
|
26
|
+
HEX[buf[8]] + HEX[buf[9]] + '-' +
|
|
27
|
+
HEX[buf[10]] + HEX[buf[11]] + HEX[buf[12]] + HEX[buf[13]] + HEX[buf[14]] + HEX[buf[15]]);
|
|
34
28
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hexId = hexId;
|
|
4
|
+
exports.default = hexId;
|
|
5
|
+
const HEX = [];
|
|
6
|
+
for (let i = 0; i < 256; i++) {
|
|
7
|
+
HEX[i] = (i < 16 ? '0' : '') + i.toString(16);
|
|
8
|
+
}
|
|
9
|
+
let pool = new Uint8Array(0);
|
|
10
|
+
let poolIdx = 0;
|
|
11
|
+
function refill(size = 16 * 1024) {
|
|
12
|
+
pool = new Uint8Array(size);
|
|
13
|
+
crypto.getRandomValues(pool);
|
|
14
|
+
poolIdx = 0;
|
|
15
|
+
}
|
|
16
|
+
function hexId(size) {
|
|
17
|
+
if (!Number.isInteger(size) || size <= 0)
|
|
18
|
+
return '';
|
|
19
|
+
if (poolIdx + size > pool.length)
|
|
20
|
+
refill();
|
|
21
|
+
const buf = pool.subarray(poolIdx, poolIdx + size);
|
|
22
|
+
poolIdx += size;
|
|
23
|
+
let out = '';
|
|
24
|
+
for (let i = 0; i < buf.length; i++) {
|
|
25
|
+
out += HEX[buf[i]];
|
|
26
|
+
}
|
|
27
|
+
return out;
|
|
28
|
+
}
|
package/cjs/hash/index.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.guid = exports.fnv1A = exports.djb2 = void 0;
|
|
3
|
+
exports.uuidv7 = exports.hexId = exports.guid = exports.fnv1A = exports.djb2 = void 0;
|
|
4
4
|
var djb2_1 = require("./djb2");
|
|
5
5
|
Object.defineProperty(exports, "djb2", { enumerable: true, get: function () { return djb2_1.djb2; } });
|
|
6
6
|
var fnv1A_1 = require("./fnv1A");
|
|
7
7
|
Object.defineProperty(exports, "fnv1A", { enumerable: true, get: function () { return fnv1A_1.fnv1A; } });
|
|
8
8
|
var guid_1 = require("./guid");
|
|
9
9
|
Object.defineProperty(exports, "guid", { enumerable: true, get: function () { return guid_1.guid; } });
|
|
10
|
+
var hexId_1 = require("./hexId");
|
|
11
|
+
Object.defineProperty(exports, "hexId", { enumerable: true, get: function () { return hexId_1.hexId; } });
|
|
12
|
+
var uuidv7_1 = require("./uuidv7");
|
|
13
|
+
Object.defineProperty(exports, "uuidv7", { enumerable: true, get: function () { return uuidv7_1.uuidv7; } });
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uuidv7 = uuidv7;
|
|
4
|
+
exports.default = uuidv7;
|
|
5
|
+
const HEX = [];
|
|
6
|
+
for (let i = 0; i < 256; i++) {
|
|
7
|
+
HEX[i] = (i < 16 ? '0' : '') + i.toString(16);
|
|
8
|
+
}
|
|
9
|
+
let pool = new Uint8Array(0);
|
|
10
|
+
let poolIdx = 0;
|
|
11
|
+
function refill(size = 16 * 1024) {
|
|
12
|
+
pool = new Uint8Array(size);
|
|
13
|
+
crypto.getRandomValues(pool);
|
|
14
|
+
poolIdx = 0;
|
|
15
|
+
}
|
|
16
|
+
function uuidv7() {
|
|
17
|
+
if (poolIdx + 10 > pool.length)
|
|
18
|
+
refill();
|
|
19
|
+
const rand = pool.subarray(poolIdx, poolIdx + 10);
|
|
20
|
+
poolIdx += 10;
|
|
21
|
+
const time = BigInt(Date.now());
|
|
22
|
+
return (HEX[Number((time >> 40n) & 0xffn)] +
|
|
23
|
+
HEX[Number((time >> 32n) & 0xffn)] +
|
|
24
|
+
HEX[Number((time >> 24n) & 0xffn)] +
|
|
25
|
+
HEX[Number((time >> 16n) & 0xffn)] +
|
|
26
|
+
'-' +
|
|
27
|
+
HEX[Number((time >> 8n) & 0xffn)] +
|
|
28
|
+
HEX[Number(time & 0xffn)] +
|
|
29
|
+
'-' +
|
|
30
|
+
HEX[(rand[0] & 0x0f) | 0x70] +
|
|
31
|
+
HEX[rand[1]] +
|
|
32
|
+
'-' +
|
|
33
|
+
HEX[(rand[2] & 0x3f) | 0x80] +
|
|
34
|
+
HEX[rand[3]] +
|
|
35
|
+
'-' +
|
|
36
|
+
HEX[rand[4]] +
|
|
37
|
+
HEX[rand[5]] +
|
|
38
|
+
HEX[rand[6]] +
|
|
39
|
+
HEX[rand[7]] +
|
|
40
|
+
HEX[rand[8]] +
|
|
41
|
+
HEX[rand[9]]);
|
|
42
|
+
}
|
package/cjs/object/is.js
CHANGED
|
@@ -3,5 +3,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.isObject = isObject;
|
|
4
4
|
exports.default = isObject;
|
|
5
5
|
function isObject(val) {
|
|
6
|
-
return Object.prototype.toString.call(val) === '[object Object]';
|
|
6
|
+
return typeof val === 'object' && Object.prototype.toString.call(val) === '[object Object]';
|
|
7
7
|
}
|
package/cjs/object/isNotEmpty.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.isNotEmptyObject = isNotEmptyObject;
|
|
4
4
|
exports.default = isNotEmptyObject;
|
|
5
5
|
function isNotEmptyObject(val) {
|
|
6
|
-
if (Object.prototype.toString.call(val) !== '[object Object]')
|
|
6
|
+
if (typeof val !== 'object' || Object.prototype.toString.call(val) !== '[object Object]')
|
|
7
7
|
return false;
|
|
8
8
|
for (const _ in val) {
|
|
9
9
|
return true;
|
package/esm/array/dedupe.js
CHANGED
|
@@ -1,37 +1,34 @@
|
|
|
1
|
-
import { toString } from '../hash/utils';
|
|
2
1
|
function dedupe(val, opts) {
|
|
3
2
|
if (!Array.isArray(val))
|
|
4
3
|
return [];
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const set = new Set();
|
|
4
|
+
const filter = opts?.filter_fn;
|
|
5
|
+
const key = opts?.key;
|
|
8
6
|
const acc = [];
|
|
9
|
-
|
|
7
|
+
const seenPrimitives = new Set();
|
|
10
8
|
const len = val.length;
|
|
11
|
-
if (
|
|
12
|
-
const CUSTOM_FILTER_FN = typeof FILTER_FN === 'function'
|
|
13
|
-
? (el) => el && Object.prototype.toString.call(el) === '[object Object]' && FILTER_FN(el)
|
|
14
|
-
: (el) => el && Object.prototype.toString.call(el) === '[object Object]';
|
|
9
|
+
if (filter) {
|
|
15
10
|
for (let i = 0; i < len; i++) {
|
|
16
11
|
const el = val[i];
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
const raw = key ? el?.[key] : el;
|
|
13
|
+
if (raw != null && filter(el)) {
|
|
14
|
+
const hash = typeof raw !== 'object' ? raw : JSON.stringify(raw);
|
|
15
|
+
if (!seenPrimitives.has(hash)) {
|
|
16
|
+
seenPrimitives.add(hash);
|
|
17
|
+
acc.push(el);
|
|
18
|
+
}
|
|
23
19
|
}
|
|
24
20
|
}
|
|
25
21
|
}
|
|
26
22
|
else {
|
|
27
23
|
for (let i = 0; i < len; i++) {
|
|
28
24
|
const el = val[i];
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
25
|
+
const raw = key ? el?.[key] : el;
|
|
26
|
+
if (raw != null) {
|
|
27
|
+
const hash = typeof raw !== 'object' ? raw : JSON.stringify(raw);
|
|
28
|
+
if (!seenPrimitives.has(hash)) {
|
|
29
|
+
seenPrimitives.add(hash);
|
|
30
|
+
acc.push(el);
|
|
31
|
+
}
|
|
35
32
|
}
|
|
36
33
|
}
|
|
37
34
|
}
|
package/esm/array/mapFn.js
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
import { merge } from '../object/merge';
|
|
2
2
|
function mapFn(arr, fn, opts) {
|
|
3
|
-
if (
|
|
4
|
-
typeof fn !== 'function')
|
|
3
|
+
if (!Array.isArray(arr) || typeof fn !== 'function')
|
|
5
4
|
return {};
|
|
6
5
|
const MERGE = opts?.merge === true;
|
|
7
6
|
const TRANSFORM_FN = opts?.transform_fn;
|
|
8
7
|
const map = {};
|
|
9
8
|
for (let i = 0; i < arr.length; i++) {
|
|
10
9
|
const el = arr[i];
|
|
11
|
-
if (Object.prototype.toString.call(el)
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
10
|
+
if (Object.prototype.toString.call(el) === '[object Object]') {
|
|
11
|
+
let hash = fn(el);
|
|
12
|
+
if (Number.isFinite(hash) || (typeof hash === 'string' && hash.length)) {
|
|
13
|
+
hash = String(hash);
|
|
14
|
+
const transformed = TRANSFORM_FN ? TRANSFORM_FN(el) : el;
|
|
15
|
+
map[hash] = MERGE && hash in map ? merge(map[hash], transformed, { union: true }) : transformed;
|
|
16
|
+
}
|
|
18
17
|
}
|
|
19
18
|
}
|
|
20
19
|
return map;
|
package/esm/hash/guid.js
CHANGED
|
@@ -2,30 +2,24 @@ const HEX = [];
|
|
|
2
2
|
for (let i = 0; i < 256; i++) {
|
|
3
3
|
HEX[i] = (i < 16 ? '0' : '') + i.toString(16);
|
|
4
4
|
}
|
|
5
|
+
let pool = new Uint8Array(0);
|
|
6
|
+
let poolIdx = 0;
|
|
7
|
+
function refill(size = 16 * 1024) {
|
|
8
|
+
pool = new Uint8Array(size);
|
|
9
|
+
crypto.getRandomValues(pool);
|
|
10
|
+
poolIdx = 0;
|
|
11
|
+
}
|
|
5
12
|
function guid() {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
HEX[
|
|
14
|
-
'-' +
|
|
15
|
-
HEX[
|
|
16
|
-
HEX[
|
|
17
|
-
'-' +
|
|
18
|
-
HEX[((d1 >> 16) & 0x0f) | 0x40] +
|
|
19
|
-
HEX[(d1 >> 24) & 0xff] +
|
|
20
|
-
'-' +
|
|
21
|
-
HEX[(d2 & 0x3f) | 0x80] +
|
|
22
|
-
HEX[(d2 >> 8) & 0xff] +
|
|
23
|
-
'-' +
|
|
24
|
-
HEX[(d2 >> 16) & 0xff] +
|
|
25
|
-
HEX[(d2 >> 24) & 0xff] +
|
|
26
|
-
HEX[d3 & 0xff] +
|
|
27
|
-
HEX[(d3 >> 8) & 0xff] +
|
|
28
|
-
HEX[(d3 >> 16) & 0xff] +
|
|
29
|
-
HEX[(d3 >> 24) & 0xff];
|
|
13
|
+
if (poolIdx + 16 > pool.length)
|
|
14
|
+
refill();
|
|
15
|
+
const buf = pool.subarray(poolIdx, poolIdx + 16);
|
|
16
|
+
poolIdx += 16;
|
|
17
|
+
buf[6] = (buf[6] & 0x0f) | 0x40;
|
|
18
|
+
buf[8] = (buf[8] & 0x3f) | 0x80;
|
|
19
|
+
return (HEX[buf[0]] + HEX[buf[1]] + HEX[buf[2]] + HEX[buf[3]] + '-' +
|
|
20
|
+
HEX[buf[4]] + HEX[buf[5]] + '-' +
|
|
21
|
+
HEX[buf[6]] + HEX[buf[7]] + '-' +
|
|
22
|
+
HEX[buf[8]] + HEX[buf[9]] + '-' +
|
|
23
|
+
HEX[buf[10]] + HEX[buf[11]] + HEX[buf[12]] + HEX[buf[13]] + HEX[buf[14]] + HEX[buf[15]]);
|
|
30
24
|
}
|
|
31
25
|
export { guid, guid as default };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const HEX = [];
|
|
2
|
+
for (let i = 0; i < 256; i++) {
|
|
3
|
+
HEX[i] = (i < 16 ? '0' : '') + i.toString(16);
|
|
4
|
+
}
|
|
5
|
+
let pool = new Uint8Array(0);
|
|
6
|
+
let poolIdx = 0;
|
|
7
|
+
function refill(size = 16 * 1024) {
|
|
8
|
+
pool = new Uint8Array(size);
|
|
9
|
+
crypto.getRandomValues(pool);
|
|
10
|
+
poolIdx = 0;
|
|
11
|
+
}
|
|
12
|
+
function hexId(size) {
|
|
13
|
+
if (!Number.isInteger(size) || size <= 0)
|
|
14
|
+
return '';
|
|
15
|
+
if (poolIdx + size > pool.length)
|
|
16
|
+
refill();
|
|
17
|
+
const buf = pool.subarray(poolIdx, poolIdx + size);
|
|
18
|
+
poolIdx += size;
|
|
19
|
+
let out = '';
|
|
20
|
+
for (let i = 0; i < buf.length; i++) {
|
|
21
|
+
out += HEX[buf[i]];
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
export { hexId, hexId as default };
|
package/esm/hash/index.js
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const HEX = [];
|
|
2
|
+
for (let i = 0; i < 256; i++) {
|
|
3
|
+
HEX[i] = (i < 16 ? '0' : '') + i.toString(16);
|
|
4
|
+
}
|
|
5
|
+
let pool = new Uint8Array(0);
|
|
6
|
+
let poolIdx = 0;
|
|
7
|
+
function refill(size = 16 * 1024) {
|
|
8
|
+
pool = new Uint8Array(size);
|
|
9
|
+
crypto.getRandomValues(pool);
|
|
10
|
+
poolIdx = 0;
|
|
11
|
+
}
|
|
12
|
+
function uuidv7() {
|
|
13
|
+
if (poolIdx + 10 > pool.length)
|
|
14
|
+
refill();
|
|
15
|
+
const rand = pool.subarray(poolIdx, poolIdx + 10);
|
|
16
|
+
poolIdx += 10;
|
|
17
|
+
const time = BigInt(Date.now());
|
|
18
|
+
return (HEX[Number((time >> 40n) & 0xffn)] +
|
|
19
|
+
HEX[Number((time >> 32n) & 0xffn)] +
|
|
20
|
+
HEX[Number((time >> 24n) & 0xffn)] +
|
|
21
|
+
HEX[Number((time >> 16n) & 0xffn)] +
|
|
22
|
+
'-' +
|
|
23
|
+
HEX[Number((time >> 8n) & 0xffn)] +
|
|
24
|
+
HEX[Number(time & 0xffn)] +
|
|
25
|
+
'-' +
|
|
26
|
+
HEX[(rand[0] & 0x0f) | 0x70] +
|
|
27
|
+
HEX[rand[1]] +
|
|
28
|
+
'-' +
|
|
29
|
+
HEX[(rand[2] & 0x3f) | 0x80] +
|
|
30
|
+
HEX[rand[3]] +
|
|
31
|
+
'-' +
|
|
32
|
+
HEX[rand[4]] +
|
|
33
|
+
HEX[rand[5]] +
|
|
34
|
+
HEX[rand[6]] +
|
|
35
|
+
HEX[rand[7]] +
|
|
36
|
+
HEX[rand[8]] +
|
|
37
|
+
HEX[rand[9]]);
|
|
38
|
+
}
|
|
39
|
+
export { uuidv7, uuidv7 as default };
|
package/esm/object/is.js
CHANGED
package/esm/object/isNotEmpty.js
CHANGED
package/hash/hexId.d.ts
ADDED
package/hash/index.d.ts
CHANGED
package/hash/uuidv7.d.ts
ADDED
package/index.d.ts
CHANGED
|
@@ -2,12 +2,10 @@ declare module "equal" {
|
|
|
2
2
|
function equal(a: any, b: any): boolean;
|
|
3
3
|
export { equal, equal as default };
|
|
4
4
|
}
|
|
5
|
-
declare module "hash/utils" {
|
|
6
|
-
export function toString(raw: unknown): string;
|
|
7
|
-
}
|
|
8
5
|
declare module "array/dedupe" {
|
|
6
|
+
type DedupeFilterFn<T> = (el: T) => boolean;
|
|
9
7
|
type DedupeOptionsBase<T> = {
|
|
10
|
-
filter_fn?:
|
|
8
|
+
filter_fn?: DedupeFilterFn<T>;
|
|
11
9
|
};
|
|
12
10
|
type DedupeOptionsWithKey<T extends Record<string, unknown>> = DedupeOptionsBase<T> & {
|
|
13
11
|
key: keyof T;
|
|
@@ -643,6 +641,9 @@ declare module "is" {
|
|
|
643
641
|
}>;
|
|
644
642
|
export { Is, Is as default };
|
|
645
643
|
}
|
|
644
|
+
declare module "hash/utils" {
|
|
645
|
+
export function toString(raw: unknown): string;
|
|
646
|
+
}
|
|
646
647
|
declare module "hash/djb2" {
|
|
647
648
|
function djb2(data: unknown): string;
|
|
648
649
|
export { djb2, djb2 as default };
|
|
@@ -708,10 +709,20 @@ declare module "hash/guid" {
|
|
|
708
709
|
function guid(): string;
|
|
709
710
|
export { guid, guid as default };
|
|
710
711
|
}
|
|
712
|
+
declare module "hash/hexId" {
|
|
713
|
+
function hexId(size: number): string;
|
|
714
|
+
export { hexId, hexId as default };
|
|
715
|
+
}
|
|
716
|
+
declare module "hash/uuidv7" {
|
|
717
|
+
function uuidv7(): string;
|
|
718
|
+
export { uuidv7, uuidv7 as default };
|
|
719
|
+
}
|
|
711
720
|
declare module "hash/index" {
|
|
712
721
|
export { djb2 } from "hash/djb2";
|
|
713
722
|
export { fnv1A } from "hash/fnv1A";
|
|
714
723
|
export { guid } from "hash/guid";
|
|
724
|
+
export { hexId } from "hash/hexId";
|
|
725
|
+
export { uuidv7 } from "hash/uuidv7";
|
|
715
726
|
}
|
|
716
727
|
declare module "modules/PubSub" {
|
|
717
728
|
type SyncFn = (data: unknown) => void;
|