@stryke/unique-id 0.3.43 → 0.3.45
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/CHANGELOG.md +12 -0
- package/dist/_virtual/rolldown_runtime.cjs +29 -1
- package/dist/convert/src/neutral.cjs +3 -1
- package/dist/convert/src/neutral.mjs +5 -1
- package/dist/convert/src/parse-type-definition.cjs +1 -1
- package/dist/convert/src/parse-type-definition.mjs +3 -1
- package/dist/convert/src/string-to-utf8-array.cjs +5 -1
- package/dist/convert/src/string-to-utf8-array.mjs +5 -1
- package/dist/convert/src/string-to-utf8-array.mjs.map +1 -1
- package/dist/convert/src/utf8-array-to-string.cjs +5 -1
- package/dist/convert/src/utf8-array-to-string.mjs +5 -1
- package/dist/convert/src/utf8-array-to-string.mjs.map +1 -1
- package/dist/cuid.cjs +64 -1
- package/dist/cuid.mjs +64 -1
- package/dist/cuid.mjs.map +1 -1
- package/dist/fs/src/list-files.cjs +4 -1
- package/dist/fs/src/list-files.mjs +5 -1
- package/dist/hash/src/digest.cjs +1 -1
- package/dist/hash/src/digest.mjs +3 -1
- package/dist/hash/src/hash-files.cjs +2 -1
- package/dist/hash/src/hash-files.mjs +4 -1
- package/dist/hash/src/murmurhash.cjs +22 -1
- package/dist/hash/src/murmurhash.mjs +21 -1
- package/dist/hash/src/murmurhash.mjs.map +1 -1
- package/dist/hash/src/neutral.cjs +4 -1
- package/dist/hash/src/neutral.mjs +6 -1
- package/dist/hash/src/xx-hash.cjs +1 -1
- package/dist/hash/src/xx-hash.mjs +3 -1
- package/dist/index.cjs +18 -1
- package/dist/index.mjs +7 -1
- package/dist/nanoid-client.cjs +22 -1
- package/dist/nanoid-client.mjs +22 -1
- package/dist/nanoid-client.mjs.map +1 -1
- package/dist/random.cjs +52 -1
- package/dist/random.mjs +48 -1
- package/dist/random.mjs.map +1 -1
- package/dist/snowflake.cjs +100 -1
- package/dist/snowflake.mjs +95 -1
- package/dist/snowflake.mjs.map +1 -1
- package/dist/type-checks/src/index.cjs +3 -1
- package/dist/type-checks/src/index.mjs +5 -1
- package/dist/type-checks/src/is-buffer.cjs +12 -1
- package/dist/type-checks/src/is-buffer.mjs +11 -1
- package/dist/type-checks/src/is-buffer.mjs.map +1 -1
- package/dist/type-checks/src/is-collection.cjs +1 -1
- package/dist/type-checks/src/is-collection.mjs +3 -1
- package/dist/type-checks/src/type-detect.cjs +15 -1
- package/dist/type-checks/src/type-detect.mjs +16 -1
- package/dist/type-checks/src/type-detect.mjs.map +1 -1
- package/dist/uuid.cjs +45 -1
- package/dist/uuid.mjs +44 -1
- package/dist/uuid.mjs.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
# Changelog for Stryke - Unique ID
|
|
4
4
|
|
|
5
|
+
## [0.3.44](https://github.com/storm-software/stryke/releases/tag/unique-id%400.3.44) (01/16/2026)
|
|
6
|
+
|
|
7
|
+
### Updated Dependencies
|
|
8
|
+
|
|
9
|
+
- Updated **hash** to **v0.12.39**
|
|
10
|
+
|
|
11
|
+
## [0.3.43](https://github.com/storm-software/stryke/releases/tag/unique-id%400.3.43) (01/16/2026)
|
|
12
|
+
|
|
13
|
+
### Updated Dependencies
|
|
14
|
+
|
|
15
|
+
- Updated **hash** to **v0.12.38**
|
|
16
|
+
|
|
5
17
|
## [0.3.42](https://github.com/storm-software/stryke/releases/tag/unique-id%400.3.42) (01/16/2026)
|
|
6
18
|
|
|
7
19
|
### Updated Dependencies
|
|
@@ -1 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
|
|
29
|
+
exports.__toESM = __toESM;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
require(
|
|
1
|
+
require('../../type-checks/src/index.cjs');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string-to-utf8-array.mjs","names":[],"sources":["../../../../convert/src/string-to-utf8-array.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nconst encoder = new TextEncoder();\n\n/**\n * Convert a string to a utf-8 array\n *\n * @param input - The string to convert\n * @returns The converted utf-8 array\n */\nexport function stringToUtf8Array(input: string): Uint8Array {\n return encoder.encode(input);\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"string-to-utf8-array.mjs","names":[],"sources":["../../../../convert/src/string-to-utf8-array.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nconst encoder = new TextEncoder();\n\n/**\n * Convert a string to a utf-8 array\n *\n * @param input - The string to convert\n * @returns The converted utf-8 array\n */\nexport function stringToUtf8Array(input: string): Uint8Array {\n return encoder.encode(input);\n}\n"],"mappings":";AAkBA,MAAM,UAAU,IAAI,aAAa"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utf8-array-to-string.mjs","names":[],"sources":["../../../../convert/src/utf8-array-to-string.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nconst decoder = new TextDecoder();\n\n/**\n * Convert a utf-8 array to string\n *\n * @param input - Utf-8 Array\n * @returns The converted string\n */\nexport function utf8ArrayToString(\n input: NodeJS.ArrayBufferView | ArrayBuffer\n): string {\n return decoder.decode(input);\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"utf8-array-to-string.mjs","names":[],"sources":["../../../../convert/src/utf8-array-to-string.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nconst decoder = new TextDecoder();\n\n/**\n * Convert a utf-8 array to string\n *\n * @param input - Utf-8 Array\n * @returns The converted string\n */\nexport function utf8ArrayToString(\n input: NodeJS.ArrayBufferView | ArrayBuffer\n): string {\n return decoder.decode(input);\n}\n"],"mappings":";AAkBA,MAAM,UAAU,IAAI,aAAa"}
|
package/dist/cuid.cjs
CHANGED
|
@@ -1 +1,64 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_murmurhash = require('./hash/src/murmurhash.cjs');
|
|
2
|
+
require('./hash/src/neutral.cjs');
|
|
3
|
+
const require_random = require('./random.cjs');
|
|
4
|
+
|
|
5
|
+
//#region src/cuid.ts
|
|
6
|
+
/**
|
|
7
|
+
* ~22k hosts before 50% chance of initial counter collision with a remaining counter range of 9.0e+15 in JavaScript.
|
|
8
|
+
*/
|
|
9
|
+
const INITIAL_COUNT_MAX = 476782367;
|
|
10
|
+
/**
|
|
11
|
+
* The length of the CUID fingerprint.
|
|
12
|
+
*/
|
|
13
|
+
const CUID_LARGE_LENGTH = 36;
|
|
14
|
+
/**
|
|
15
|
+
* The counter used to help prevent collisions.
|
|
16
|
+
*/
|
|
17
|
+
const counter = Math.floor(Math.random() * INITIAL_COUNT_MAX) + 1;
|
|
18
|
+
/**
|
|
19
|
+
* Generate a random letter
|
|
20
|
+
*
|
|
21
|
+
* @param length - The length of the random string to generate
|
|
22
|
+
* @param random - The random number generator
|
|
23
|
+
* @returns A random letter
|
|
24
|
+
*/
|
|
25
|
+
function createEntropy(length = 4, random = Math.random) {
|
|
26
|
+
let entropy = "";
|
|
27
|
+
while (entropy.length < length) entropy += Math.floor(random() * CUID_LARGE_LENGTH).toString(CUID_LARGE_LENGTH);
|
|
28
|
+
return entropy;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* This is a fingerprint of the host environment. It is used to help prevent collisions when generating ids in a distributed system.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* If no global object is available, you can pass in your own, or fall back on a random string.
|
|
35
|
+
*
|
|
36
|
+
* @param options - Options
|
|
37
|
+
* @returns The environment's Fingerprint
|
|
38
|
+
*/
|
|
39
|
+
function fingerprint(options) {
|
|
40
|
+
const globalObj = options?.globalObj ?? typeof globalThis === "undefined" ? typeof globalThis === "undefined" ? {} : globalThis : globalThis;
|
|
41
|
+
const globals = Object.keys(globalObj).toString();
|
|
42
|
+
return require_murmurhash.murmurhash(globals.length > 0 ? globals + createEntropy(CUID_LARGE_LENGTH, Math.random) : createEntropy(CUID_LARGE_LENGTH, Math.random)).slice(0, Math.max(0, CUID_LARGE_LENGTH));
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Generate a random CUID
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
*
|
|
50
|
+
* // Generate a random CUID
|
|
51
|
+
* const id = cuid();
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @returns A random CUID string
|
|
55
|
+
*/
|
|
56
|
+
function cuid() {
|
|
57
|
+
const time = Date.now().toString(CUID_LARGE_LENGTH);
|
|
58
|
+
const count = counter.toString(CUID_LARGE_LENGTH);
|
|
59
|
+
const hashed = require_murmurhash.murmurhash(`${time + createEntropy(CUID_LARGE_LENGTH, Math.random) + count + fingerprint()}`);
|
|
60
|
+
return `${require_random.randomLetter() + hashed.slice(1, Math.min(hashed.length - 1, CUID_LARGE_LENGTH))}`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
exports.cuid = cuid;
|
package/dist/cuid.mjs
CHANGED
|
@@ -1,2 +1,65 @@
|
|
|
1
|
-
import{murmurhash
|
|
1
|
+
import { murmurhash } from "./hash/src/murmurhash.mjs";
|
|
2
|
+
import "./hash/src/neutral.mjs";
|
|
3
|
+
import { randomLetter } from "./random.mjs";
|
|
4
|
+
|
|
5
|
+
//#region src/cuid.ts
|
|
6
|
+
/**
|
|
7
|
+
* ~22k hosts before 50% chance of initial counter collision with a remaining counter range of 9.0e+15 in JavaScript.
|
|
8
|
+
*/
|
|
9
|
+
const INITIAL_COUNT_MAX = 476782367;
|
|
10
|
+
/**
|
|
11
|
+
* The length of the CUID fingerprint.
|
|
12
|
+
*/
|
|
13
|
+
const CUID_LARGE_LENGTH = 36;
|
|
14
|
+
/**
|
|
15
|
+
* The counter used to help prevent collisions.
|
|
16
|
+
*/
|
|
17
|
+
const counter = Math.floor(Math.random() * INITIAL_COUNT_MAX) + 1;
|
|
18
|
+
/**
|
|
19
|
+
* Generate a random letter
|
|
20
|
+
*
|
|
21
|
+
* @param length - The length of the random string to generate
|
|
22
|
+
* @param random - The random number generator
|
|
23
|
+
* @returns A random letter
|
|
24
|
+
*/
|
|
25
|
+
function createEntropy(length = 4, random = Math.random) {
|
|
26
|
+
let entropy = "";
|
|
27
|
+
while (entropy.length < length) entropy += Math.floor(random() * CUID_LARGE_LENGTH).toString(CUID_LARGE_LENGTH);
|
|
28
|
+
return entropy;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* This is a fingerprint of the host environment. It is used to help prevent collisions when generating ids in a distributed system.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* If no global object is available, you can pass in your own, or fall back on a random string.
|
|
35
|
+
*
|
|
36
|
+
* @param options - Options
|
|
37
|
+
* @returns The environment's Fingerprint
|
|
38
|
+
*/
|
|
39
|
+
function fingerprint(options) {
|
|
40
|
+
const globalObj = options?.globalObj ?? typeof globalThis === "undefined" ? typeof globalThis === "undefined" ? {} : globalThis : globalThis;
|
|
41
|
+
const globals = Object.keys(globalObj).toString();
|
|
42
|
+
return murmurhash(globals.length > 0 ? globals + createEntropy(CUID_LARGE_LENGTH, Math.random) : createEntropy(CUID_LARGE_LENGTH, Math.random)).slice(0, Math.max(0, CUID_LARGE_LENGTH));
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Generate a random CUID
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
*
|
|
50
|
+
* // Generate a random CUID
|
|
51
|
+
* const id = cuid();
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @returns A random CUID string
|
|
55
|
+
*/
|
|
56
|
+
function cuid() {
|
|
57
|
+
const time = Date.now().toString(CUID_LARGE_LENGTH);
|
|
58
|
+
const count = counter.toString(CUID_LARGE_LENGTH);
|
|
59
|
+
const hashed = murmurhash(`${time + createEntropy(CUID_LARGE_LENGTH, Math.random) + count + fingerprint()}`);
|
|
60
|
+
return `${randomLetter() + hashed.slice(1, Math.min(hashed.length - 1, CUID_LARGE_LENGTH))}`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
export { cuid };
|
|
2
65
|
//# sourceMappingURL=cuid.mjs.map
|
package/dist/cuid.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cuid.mjs","names":[],"sources":["../src/cuid.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { murmurhash } from \"@stryke/hash/neutral\";\nimport { randomLetter } from \"./random\";\n\n/**\n * ~22k hosts before 50% chance of initial counter collision with a remaining counter range of 9.0e+15 in JavaScript.\n */\nconst INITIAL_COUNT_MAX = 476_782_367;\n\n/**\n * The length of the CUID fingerprint.\n */\nconst CUID_LARGE_LENGTH = 36;\n\n/**\n * The sequence of the current running generator.\n *\n * @defaultValue 1\n */\nconst sequence = 1;\n\n/**\n * The counter used to help prevent collisions.\n */\nconst counter = Math.floor(Math.random() * INITIAL_COUNT_MAX) + sequence;\n\n/**\n * Generate a random letter\n *\n * @param length - The length of the random string to generate\n * @param random - The random number generator\n * @returns A random letter\n */\nfunction createEntropy(length = 4, random = Math.random) {\n let entropy = \"\";\n\n while (entropy.length < length) {\n entropy += Math.floor(random() * CUID_LARGE_LENGTH).toString(\n CUID_LARGE_LENGTH\n );\n }\n return entropy;\n}\n\nexport interface FingerprintOptions {\n globalObj?: any;\n}\n\n/**\n * This is a fingerprint of the host environment. It is used to help prevent collisions when generating ids in a distributed system.\n *\n * @remarks\n * If no global object is available, you can pass in your own, or fall back on a random string.\n *\n * @param options - Options\n * @returns The environment's Fingerprint\n */\nfunction fingerprint(options?: FingerprintOptions): string {\n const globalObj =\n (options?.globalObj ?? typeof globalThis === \"undefined\")\n ? typeof globalThis === \"undefined\"\n ? {}\n : globalThis\n : globalThis;\n\n const globals = Object.keys(globalObj).toString();\n const sourceString =\n globals.length > 0\n ? globals + createEntropy(CUID_LARGE_LENGTH, Math.random)\n : createEntropy(CUID_LARGE_LENGTH, Math.random);\n\n return murmurhash(sourceString).slice(0, Math.max(0, CUID_LARGE_LENGTH));\n}\n\n/**\n * Generate a random CUID\n *\n * @example\n * ```typescript\n *\n * // Generate a random CUID\n * const id = cuid();\n * ```\n *\n * @returns A random CUID string\n */\nexport function cuid(): string {\n // If we're lucky, the `.toString(36)` calls may reduce hashing rounds\n // by shortening the input to the hash function a little.\n const time = Date.now().toString(CUID_LARGE_LENGTH);\n const count = counter.toString(CUID_LARGE_LENGTH);\n\n // The salt should be long enough to be globally unique across the full\n // length of the hash. For simplicity, we use the same length as the\n // intended id output.\n const salt = createEntropy(CUID_LARGE_LENGTH, Math.random);\n\n const hashed = murmurhash(`${time + salt + count + fingerprint()}`);\n\n return `${\n randomLetter() +\n hashed.slice(1, Math.min(hashed.length - 1, CUID_LARGE_LENGTH))\n }`;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"cuid.mjs","names":[],"sources":["../src/cuid.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { murmurhash } from \"@stryke/hash/neutral\";\nimport { randomLetter } from \"./random\";\n\n/**\n * ~22k hosts before 50% chance of initial counter collision with a remaining counter range of 9.0e+15 in JavaScript.\n */\nconst INITIAL_COUNT_MAX = 476_782_367;\n\n/**\n * The length of the CUID fingerprint.\n */\nconst CUID_LARGE_LENGTH = 36;\n\n/**\n * The sequence of the current running generator.\n *\n * @defaultValue 1\n */\nconst sequence = 1;\n\n/**\n * The counter used to help prevent collisions.\n */\nconst counter = Math.floor(Math.random() * INITIAL_COUNT_MAX) + sequence;\n\n/**\n * Generate a random letter\n *\n * @param length - The length of the random string to generate\n * @param random - The random number generator\n * @returns A random letter\n */\nfunction createEntropy(length = 4, random = Math.random) {\n let entropy = \"\";\n\n while (entropy.length < length) {\n entropy += Math.floor(random() * CUID_LARGE_LENGTH).toString(\n CUID_LARGE_LENGTH\n );\n }\n return entropy;\n}\n\nexport interface FingerprintOptions {\n globalObj?: any;\n}\n\n/**\n * This is a fingerprint of the host environment. It is used to help prevent collisions when generating ids in a distributed system.\n *\n * @remarks\n * If no global object is available, you can pass in your own, or fall back on a random string.\n *\n * @param options - Options\n * @returns The environment's Fingerprint\n */\nfunction fingerprint(options?: FingerprintOptions): string {\n const globalObj =\n (options?.globalObj ?? typeof globalThis === \"undefined\")\n ? typeof globalThis === \"undefined\"\n ? {}\n : globalThis\n : globalThis;\n\n const globals = Object.keys(globalObj).toString();\n const sourceString =\n globals.length > 0\n ? globals + createEntropy(CUID_LARGE_LENGTH, Math.random)\n : createEntropy(CUID_LARGE_LENGTH, Math.random);\n\n return murmurhash(sourceString).slice(0, Math.max(0, CUID_LARGE_LENGTH));\n}\n\n/**\n * Generate a random CUID\n *\n * @example\n * ```typescript\n *\n * // Generate a random CUID\n * const id = cuid();\n * ```\n *\n * @returns A random CUID string\n */\nexport function cuid(): string {\n // If we're lucky, the `.toString(36)` calls may reduce hashing rounds\n // by shortening the input to the hash function a little.\n const time = Date.now().toString(CUID_LARGE_LENGTH);\n const count = counter.toString(CUID_LARGE_LENGTH);\n\n // The salt should be long enough to be globally unique across the full\n // length of the hash. For simplicity, we use the same length as the\n // intended id output.\n const salt = createEntropy(CUID_LARGE_LENGTH, Math.random);\n\n const hashed = murmurhash(`${time + salt + count + fingerprint()}`);\n\n return `${\n randomLetter() +\n hashed.slice(1, Math.min(hashed.length - 1, CUID_LARGE_LENGTH))\n }`;\n}\n"],"mappings":";;;;;;;;AAwBA,MAAM,oBAAoB;;;;AAK1B,MAAM,oBAAoB;;;;AAY1B,MAAM,UAAU,KAAK,MAAM,KAAK,QAAQ,GAAG,kBAAkB,GAL5C;;;;;;;;AAcjB,SAAS,cAAc,SAAS,GAAG,SAAS,KAAK,QAAQ;CACvD,IAAI,UAAU;AAEd,QAAO,QAAQ,SAAS,OACtB,YAAW,KAAK,MAAM,QAAQ,GAAG,kBAAkB,CAAC,SAClD,kBACD;AAEH,QAAO;;;;;;;;;;;AAgBT,SAAS,YAAY,SAAsC;CACzD,MAAM,YACH,SAAS,aAAa,OAAO,eAAe,cACzC,OAAO,eAAe,cACpB,EAAE,GACF,aACF;CAEN,MAAM,UAAU,OAAO,KAAK,UAAU,CAAC,UAAU;AAMjD,QAAO,WAJL,QAAQ,SAAS,IACb,UAAU,cAAc,mBAAmB,KAAK,OAAO,GACvD,cAAc,mBAAmB,KAAK,OAAO,CAEpB,CAAC,MAAM,GAAG,KAAK,IAAI,GAAG,kBAAkB,CAAC;;;;;;;;;;;;;;AAe1E,SAAgB,OAAe;CAG7B,MAAM,OAAO,KAAK,KAAK,CAAC,SAAS,kBAAkB;CACnD,MAAM,QAAQ,QAAQ,SAAS,kBAAkB;CAOjD,MAAM,SAAS,WAAW,GAAG,OAFhB,cAAc,mBAAmB,KAAK,OAAO,GAEf,QAAQ,aAAa,GAAG;AAEnE,QAAO,GACL,cAAc,GACd,OAAO,MAAM,GAAG,KAAK,IAAI,OAAO,SAAS,GAAG,kBAAkB,CAAC"}
|
|
@@ -1 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
require('../../type-checks/src/index.cjs');
|
|
3
|
+
require("defu");
|
|
4
|
+
require("glob");
|
package/dist/hash/src/digest.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
require(
|
|
1
|
+
require('../../convert/src/neutral.cjs');
|
package/dist/hash/src/digest.mjs
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
require(
|
|
1
|
+
require('../../fs/src/list-files.cjs');
|
|
2
|
+
require('./murmurhash.cjs');
|
|
@@ -1 +1,22 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let ohash = require("ohash");
|
|
3
|
+
|
|
4
|
+
//#region ../hash/src/murmurhash.ts
|
|
5
|
+
/**
|
|
6
|
+
* Use a [MurmurHash3](https://en.wikipedia.org/wiki/MurmurHash) based algorithm to hash any JS value into a string.
|
|
7
|
+
*
|
|
8
|
+
* @see https://github.com/ohash/ohash
|
|
9
|
+
* @see https://en.wikipedia.org/wiki/MurmurHash
|
|
10
|
+
*
|
|
11
|
+
* @param content - The value to hash
|
|
12
|
+
* @param options - Hashing options
|
|
13
|
+
* @returns A hashed string value
|
|
14
|
+
*/
|
|
15
|
+
function murmurhash(content, options) {
|
|
16
|
+
const result = (0, ohash.hash)(content);
|
|
17
|
+
const maxLength = options?.maxLength ?? 32;
|
|
18
|
+
return result.length > maxLength ? result.slice(0, maxLength) : result;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
exports.murmurhash = murmurhash;
|
|
@@ -1,2 +1,22 @@
|
|
|
1
|
-
import{hash
|
|
1
|
+
import { hash } from "ohash";
|
|
2
|
+
|
|
3
|
+
//#region ../hash/src/murmurhash.ts
|
|
4
|
+
/**
|
|
5
|
+
* Use a [MurmurHash3](https://en.wikipedia.org/wiki/MurmurHash) based algorithm to hash any JS value into a string.
|
|
6
|
+
*
|
|
7
|
+
* @see https://github.com/ohash/ohash
|
|
8
|
+
* @see https://en.wikipedia.org/wiki/MurmurHash
|
|
9
|
+
*
|
|
10
|
+
* @param content - The value to hash
|
|
11
|
+
* @param options - Hashing options
|
|
12
|
+
* @returns A hashed string value
|
|
13
|
+
*/
|
|
14
|
+
function murmurhash(content, options) {
|
|
15
|
+
const result = hash(content);
|
|
16
|
+
const maxLength = options?.maxLength ?? 32;
|
|
17
|
+
return result.length > maxLength ? result.slice(0, maxLength) : result;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
export { murmurhash };
|
|
2
22
|
//# sourceMappingURL=murmurhash.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"murmurhash.mjs","names":["ohash"],"sources":["../../../../hash/src/murmurhash.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { hash as ohash } from \"ohash\";\n\nexport interface HashOptions {\n /**\n * The maximum length of the hash\n *\n * @defaultValue 32\n */\n maxLength?: number;\n}\n\n/**\n * Use a [MurmurHash3](https://en.wikipedia.org/wiki/MurmurHash) based algorithm to hash any JS value into a string.\n *\n * @see https://github.com/ohash/ohash\n * @see https://en.wikipedia.org/wiki/MurmurHash\n *\n * @param content - The value to hash\n * @param options - Hashing options\n * @returns A hashed string value\n */\nexport function murmurhash(content: any, options?: HashOptions): string {\n const result = ohash(content);\n const maxLength = options?.maxLength ?? 32;\n\n return result.length > maxLength ? result.slice(0, maxLength) : result;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"murmurhash.mjs","names":["ohash"],"sources":["../../../../hash/src/murmurhash.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { hash as ohash } from \"ohash\";\n\nexport interface HashOptions {\n /**\n * The maximum length of the hash\n *\n * @defaultValue 32\n */\n maxLength?: number;\n}\n\n/**\n * Use a [MurmurHash3](https://en.wikipedia.org/wiki/MurmurHash) based algorithm to hash any JS value into a string.\n *\n * @see https://github.com/ohash/ohash\n * @see https://en.wikipedia.org/wiki/MurmurHash\n *\n * @param content - The value to hash\n * @param options - Hashing options\n * @returns A hashed string value\n */\nexport function murmurhash(content: any, options?: HashOptions): string {\n const result = ohash(content);\n const maxLength = options?.maxLength ?? 32;\n\n return result.length > maxLength ? result.slice(0, maxLength) : result;\n}\n"],"mappings":";;;;;;;;;;;;;AAuCA,SAAgB,WAAW,SAAc,SAA+B;CACtE,MAAM,SAASA,KAAM,QAAQ;CAC7B,MAAM,YAAY,SAAS,aAAa;AAExC,QAAO,OAAO,SAAS,YAAY,OAAO,MAAM,GAAG,UAAU,GAAG"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
require(
|
|
1
|
+
require("js-xxhash");
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1,18 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_random = require('./random.cjs');
|
|
2
|
+
const require_cuid = require('./cuid.cjs');
|
|
3
|
+
const require_nanoid_client = require('./nanoid-client.cjs');
|
|
4
|
+
const require_snowflake = require('./snowflake.cjs');
|
|
5
|
+
const require_uuid = require('./uuid.cjs');
|
|
6
|
+
|
|
7
|
+
exports.DEFAULT_EPOCH = require_snowflake.DEFAULT_EPOCH;
|
|
8
|
+
exports.DEFAULT_SHARD_ID = require_snowflake.DEFAULT_SHARD_ID;
|
|
9
|
+
exports.cuid = require_cuid.cuid;
|
|
10
|
+
exports.deconstructSnowflake = require_snowflake.deconstructSnowflake;
|
|
11
|
+
exports.getNonCryptoRandomValues = require_random.getNonCryptoRandomValues;
|
|
12
|
+
exports.isValidSnowflake = require_snowflake.isValidSnowflake;
|
|
13
|
+
exports.nanoid = require_nanoid_client.nanoid;
|
|
14
|
+
exports.randomCharacters = require_random.randomCharacters;
|
|
15
|
+
exports.randomInteger = require_random.randomInteger;
|
|
16
|
+
exports.randomLetter = require_random.randomLetter;
|
|
17
|
+
exports.snowflake = require_snowflake.snowflake;
|
|
18
|
+
exports.uuid = require_uuid.uuid;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
-
import{getNonCryptoRandomValues
|
|
1
|
+
import { getNonCryptoRandomValues, randomCharacters, randomInteger, randomLetter } from "./random.mjs";
|
|
2
|
+
import { cuid } from "./cuid.mjs";
|
|
3
|
+
import { nanoid } from "./nanoid-client.mjs";
|
|
4
|
+
import { DEFAULT_EPOCH, DEFAULT_SHARD_ID, deconstructSnowflake, isValidSnowflake, snowflake } from "./snowflake.mjs";
|
|
5
|
+
import { uuid } from "./uuid.mjs";
|
|
6
|
+
|
|
7
|
+
export { DEFAULT_EPOCH, DEFAULT_SHARD_ID, cuid, deconstructSnowflake, getNonCryptoRandomValues, isValidSnowflake, nanoid, randomCharacters, randomInteger, randomLetter, snowflake, uuid };
|
package/dist/nanoid-client.cjs
CHANGED
|
@@ -1 +1,22 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_random = require('./random.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/nanoid-client.ts
|
|
4
|
+
/**
|
|
5
|
+
* A wrapper around the [nanoid](https://github.com/ai/nanoid) package with some modifications.
|
|
6
|
+
*
|
|
7
|
+
* @param size - The size of the string to generate
|
|
8
|
+
* @returns A unique identifier following the nanoid format
|
|
9
|
+
*/
|
|
10
|
+
function nanoid(size = 21) {
|
|
11
|
+
return require_random.getNonCryptoRandomValues(new Uint8Array(size)).reduce((id, byte) => {
|
|
12
|
+
byte &= 63;
|
|
13
|
+
if (byte < 36) id += byte.toString(36);
|
|
14
|
+
else if (byte < 62) id += (byte - 26).toString(36).toUpperCase();
|
|
15
|
+
else if (byte > 62) id += "-";
|
|
16
|
+
else id += "_";
|
|
17
|
+
return id;
|
|
18
|
+
}, "");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
exports.nanoid = nanoid;
|
package/dist/nanoid-client.mjs
CHANGED
|
@@ -1,2 +1,23 @@
|
|
|
1
|
-
import{getNonCryptoRandomValues
|
|
1
|
+
import { getNonCryptoRandomValues } from "./random.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/nanoid-client.ts
|
|
4
|
+
/**
|
|
5
|
+
* A wrapper around the [nanoid](https://github.com/ai/nanoid) package with some modifications.
|
|
6
|
+
*
|
|
7
|
+
* @param size - The size of the string to generate
|
|
8
|
+
* @returns A unique identifier following the nanoid format
|
|
9
|
+
*/
|
|
10
|
+
function nanoid(size = 21) {
|
|
11
|
+
return getNonCryptoRandomValues(new Uint8Array(size)).reduce((id, byte) => {
|
|
12
|
+
byte &= 63;
|
|
13
|
+
if (byte < 36) id += byte.toString(36);
|
|
14
|
+
else if (byte < 62) id += (byte - 26).toString(36).toUpperCase();
|
|
15
|
+
else if (byte > 62) id += "-";
|
|
16
|
+
else id += "_";
|
|
17
|
+
return id;
|
|
18
|
+
}, "");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
export { nanoid };
|
|
2
23
|
//# sourceMappingURL=nanoid-client.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nanoid-client.mjs","names":[],"sources":["../src/nanoid-client.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { getNonCryptoRandomValues } from \"./random\";\n\n// This is taken from https://github.com/ai/nanoid/blob/main/index.browser.js We\n// copy this because we want to use `--platform=neutral` which doesn't work with\n// the npm package.\n// Also we changed the random number generator to use Math.random() for compat\n// with React Native.\n\n/**\n * A wrapper around the [nanoid](https://github.com/ai/nanoid) package with some modifications.\n *\n * @param size - The size of the string to generate\n * @returns A unique identifier following the nanoid format\n */\nexport function nanoid(size = 21): string {\n // Use our custom getRandomValues function to fill a Uint8Array with random values.\n const randomBytes = getNonCryptoRandomValues(new Uint8Array(size));\n\n return randomBytes.reduce((id, byte) => {\n // It is incorrect to use bytes exceeding the alphabet size.\n // The following mask reduces the random byte in the 0-255 value\n // range to the 0-63 value range. Therefore, adding hacks, such\n // as empty string fallback or magic numbers, is unnecessary because\n // the bitmask trims bytes down to the alphabet size.\n byte &= 63;\n if (byte < 36) {\n // `0-9a-z`\n id += byte.toString(36);\n } else if (byte < 62) {\n // `A-Z`\n id += (byte - 26).toString(36).toUpperCase();\n } else if (byte > 62) {\n id += \"-\";\n } else {\n id += \"_\";\n }\n return id;\n }, \"\");\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"nanoid-client.mjs","names":[],"sources":["../src/nanoid-client.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { getNonCryptoRandomValues } from \"./random\";\n\n// This is taken from https://github.com/ai/nanoid/blob/main/index.browser.js We\n// copy this because we want to use `--platform=neutral` which doesn't work with\n// the npm package.\n// Also we changed the random number generator to use Math.random() for compat\n// with React Native.\n\n/**\n * A wrapper around the [nanoid](https://github.com/ai/nanoid) package with some modifications.\n *\n * @param size - The size of the string to generate\n * @returns A unique identifier following the nanoid format\n */\nexport function nanoid(size = 21): string {\n // Use our custom getRandomValues function to fill a Uint8Array with random values.\n const randomBytes = getNonCryptoRandomValues(new Uint8Array(size));\n\n return randomBytes.reduce((id, byte) => {\n // It is incorrect to use bytes exceeding the alphabet size.\n // The following mask reduces the random byte in the 0-255 value\n // range to the 0-63 value range. Therefore, adding hacks, such\n // as empty string fallback or magic numbers, is unnecessary because\n // the bitmask trims bytes down to the alphabet size.\n byte &= 63;\n if (byte < 36) {\n // `0-9a-z`\n id += byte.toString(36);\n } else if (byte < 62) {\n // `A-Z`\n id += (byte - 26).toString(36).toUpperCase();\n } else if (byte > 62) {\n id += \"-\";\n } else {\n id += \"_\";\n }\n return id;\n }, \"\");\n}\n"],"mappings":";;;;;;;;;AAgCA,SAAgB,OAAO,OAAO,IAAY;AAIxC,QAFoB,yBAAyB,IAAI,WAAW,KAAK,CAAC,CAE/C,QAAQ,IAAI,SAAS;AAMtC,UAAQ;AACR,MAAI,OAAO,GAET,OAAM,KAAK,SAAS,GAAG;WACd,OAAO,GAEhB,QAAO,OAAO,IAAI,SAAS,GAAG,CAAC,aAAa;WACnC,OAAO,GAChB,OAAM;MAEN,OAAM;AAER,SAAO;IACN,GAAG"}
|
package/dist/random.cjs
CHANGED
|
@@ -1 +1,52 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
//#region src/random.ts
|
|
3
|
+
const alphabet = Array.from({ length: 26 }, (_x, i) => String.fromCodePoint(i + 97));
|
|
4
|
+
/**
|
|
5
|
+
* Generate a random integer
|
|
6
|
+
*
|
|
7
|
+
* @param maximum - The maximum value (inclusive)
|
|
8
|
+
* @param minimum - The minimum value (inclusive)
|
|
9
|
+
* @returns A random integer
|
|
10
|
+
*/
|
|
11
|
+
const randomInteger = (maximum, minimum = 0) => Math.floor(Math.random() * (maximum - minimum + 1) + minimum);
|
|
12
|
+
/**
|
|
13
|
+
* Generate a random letter
|
|
14
|
+
*
|
|
15
|
+
* @param random - The random number generator
|
|
16
|
+
* @returns A random letter
|
|
17
|
+
*/
|
|
18
|
+
const randomLetter = (random = Math.random) => alphabet[Math.floor(random() * alphabet.length)];
|
|
19
|
+
/**
|
|
20
|
+
* Generate a random string
|
|
21
|
+
*
|
|
22
|
+
* @param array - The array to fill with random values
|
|
23
|
+
* @returns The array filled with random values
|
|
24
|
+
*/
|
|
25
|
+
function getNonCryptoRandomValues(array) {
|
|
26
|
+
if (array === null) throw new TypeError("array cannot be null");
|
|
27
|
+
for (let i = 0; i < array.length; i++) array[i] = Math.floor(Math.random() * 256);
|
|
28
|
+
return array;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Generate a series of random characters
|
|
32
|
+
*
|
|
33
|
+
* @param length - The length of the random characters
|
|
34
|
+
* @returns A series of random characters
|
|
35
|
+
*/
|
|
36
|
+
function randomCharacters(length) {
|
|
37
|
+
let result = "";
|
|
38
|
+
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
39
|
+
const charactersLength = 62;
|
|
40
|
+
let counter = 0;
|
|
41
|
+
while (counter < length) {
|
|
42
|
+
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
43
|
+
counter += 1;
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
exports.getNonCryptoRandomValues = getNonCryptoRandomValues;
|
|
50
|
+
exports.randomCharacters = randomCharacters;
|
|
51
|
+
exports.randomInteger = randomInteger;
|
|
52
|
+
exports.randomLetter = randomLetter;
|
package/dist/random.mjs
CHANGED
|
@@ -1,2 +1,49 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/random.ts
|
|
2
|
+
const alphabet = Array.from({ length: 26 }, (_x, i) => String.fromCodePoint(i + 97));
|
|
3
|
+
/**
|
|
4
|
+
* Generate a random integer
|
|
5
|
+
*
|
|
6
|
+
* @param maximum - The maximum value (inclusive)
|
|
7
|
+
* @param minimum - The minimum value (inclusive)
|
|
8
|
+
* @returns A random integer
|
|
9
|
+
*/
|
|
10
|
+
const randomInteger = (maximum, minimum = 0) => Math.floor(Math.random() * (maximum - minimum + 1) + minimum);
|
|
11
|
+
/**
|
|
12
|
+
* Generate a random letter
|
|
13
|
+
*
|
|
14
|
+
* @param random - The random number generator
|
|
15
|
+
* @returns A random letter
|
|
16
|
+
*/
|
|
17
|
+
const randomLetter = (random = Math.random) => alphabet[Math.floor(random() * alphabet.length)];
|
|
18
|
+
/**
|
|
19
|
+
* Generate a random string
|
|
20
|
+
*
|
|
21
|
+
* @param array - The array to fill with random values
|
|
22
|
+
* @returns The array filled with random values
|
|
23
|
+
*/
|
|
24
|
+
function getNonCryptoRandomValues(array) {
|
|
25
|
+
if (array === null) throw new TypeError("array cannot be null");
|
|
26
|
+
for (let i = 0; i < array.length; i++) array[i] = Math.floor(Math.random() * 256);
|
|
27
|
+
return array;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Generate a series of random characters
|
|
31
|
+
*
|
|
32
|
+
* @param length - The length of the random characters
|
|
33
|
+
* @returns A series of random characters
|
|
34
|
+
*/
|
|
35
|
+
function randomCharacters(length) {
|
|
36
|
+
let result = "";
|
|
37
|
+
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
38
|
+
const charactersLength = 62;
|
|
39
|
+
let counter = 0;
|
|
40
|
+
while (counter < length) {
|
|
41
|
+
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
42
|
+
counter += 1;
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
//#endregion
|
|
48
|
+
export { getNonCryptoRandomValues, randomCharacters, randomInteger, randomLetter };
|
|
2
49
|
//# sourceMappingURL=random.mjs.map
|
package/dist/random.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"random.mjs","names":[],"sources":["../src/random.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nconst alphabet = Array.from({ length: 26 }, (_x, i) =>\n String.fromCodePoint(i + 97)\n);\n\n/**\n * Generate a random integer\n *\n * @param maximum - The maximum value (inclusive)\n * @param minimum - The minimum value (inclusive)\n * @returns A random integer\n */\nexport const randomInteger = (maximum: number, minimum = 0) =>\n Math.floor(Math.random() * (maximum - minimum + 1) + minimum);\n\n/**\n * Generate a random letter\n *\n * @param random - The random number generator\n * @returns A random letter\n */\nexport const randomLetter = (random: () => number = Math.random) =>\n alphabet[Math.floor(random() * alphabet.length)];\n\n/**\n * Generate a random string\n *\n * @param array - The array to fill with random values\n * @returns The array filled with random values\n */\nexport function getNonCryptoRandomValues(array: Uint8Array) {\n if (array === null) {\n throw new TypeError(\"array cannot be null\");\n }\n\n // Fill the array with random values\n for (let i = 0; i < array.length; i++) {\n array[i] = Math.floor(Math.random() * 256); // Random byte (0-255)\n }\n\n return array;\n}\n\n/**\n * Generate a series of random characters\n *\n * @param length - The length of the random characters\n * @returns A series of random characters\n */\nexport function randomCharacters(length: number) {\n let result = \"\";\n const characters =\n \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\";\n const charactersLength = characters.length;\n let counter = 0;\n while (counter < length) {\n result += characters.charAt(Math.floor(Math.random() * charactersLength));\n counter += 1;\n }\n return result;\n}\n"],"mappings":"AAkBA,MAAM,
|
|
1
|
+
{"version":3,"file":"random.mjs","names":[],"sources":["../src/random.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nconst alphabet = Array.from({ length: 26 }, (_x, i) =>\n String.fromCodePoint(i + 97)\n);\n\n/**\n * Generate a random integer\n *\n * @param maximum - The maximum value (inclusive)\n * @param minimum - The minimum value (inclusive)\n * @returns A random integer\n */\nexport const randomInteger = (maximum: number, minimum = 0) =>\n Math.floor(Math.random() * (maximum - minimum + 1) + minimum);\n\n/**\n * Generate a random letter\n *\n * @param random - The random number generator\n * @returns A random letter\n */\nexport const randomLetter = (random: () => number = Math.random) =>\n alphabet[Math.floor(random() * alphabet.length)];\n\n/**\n * Generate a random string\n *\n * @param array - The array to fill with random values\n * @returns The array filled with random values\n */\nexport function getNonCryptoRandomValues(array: Uint8Array) {\n if (array === null) {\n throw new TypeError(\"array cannot be null\");\n }\n\n // Fill the array with random values\n for (let i = 0; i < array.length; i++) {\n array[i] = Math.floor(Math.random() * 256); // Random byte (0-255)\n }\n\n return array;\n}\n\n/**\n * Generate a series of random characters\n *\n * @param length - The length of the random characters\n * @returns A series of random characters\n */\nexport function randomCharacters(length: number) {\n let result = \"\";\n const characters =\n \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\";\n const charactersLength = characters.length;\n let counter = 0;\n while (counter < length) {\n result += characters.charAt(Math.floor(Math.random() * charactersLength));\n counter += 1;\n }\n return result;\n}\n"],"mappings":";AAkBA,MAAM,WAAW,MAAM,KAAK,EAAE,QAAQ,IAAI,GAAG,IAAI,MAC/C,OAAO,cAAc,IAAI,GAAG,CAC7B;;;;;;;;AASD,MAAa,iBAAiB,SAAiB,UAAU,MACvD,KAAK,MAAM,KAAK,QAAQ,IAAI,UAAU,UAAU,KAAK,QAAQ;;;;;;;AAQ/D,MAAa,gBAAgB,SAAuB,KAAK,WACvD,SAAS,KAAK,MAAM,QAAQ,GAAG,SAAS,OAAO;;;;;;;AAQjD,SAAgB,yBAAyB,OAAmB;AAC1D,KAAI,UAAU,KACZ,OAAM,IAAI,UAAU,uBAAuB;AAI7C,MAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,IAChC,OAAM,KAAK,KAAK,MAAM,KAAK,QAAQ,GAAG,IAAI;AAG5C,QAAO;;;;;;;;AAST,SAAgB,iBAAiB,QAAgB;CAC/C,IAAI,SAAS;CACb,MAAM,aACJ;CACF,MAAM,mBAAmB;CACzB,IAAI,UAAU;AACd,QAAO,UAAU,QAAQ;AACvB,YAAU,WAAW,OAAO,KAAK,MAAM,KAAK,QAAQ,GAAG,iBAAiB,CAAC;AACzE,aAAW;;AAEb,QAAO"}
|
package/dist/snowflake.cjs
CHANGED
|
@@ -1 +1,100 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
//#region src/snowflake.ts
|
|
3
|
+
const DEFAULT_SHARD_ID = 1;
|
|
4
|
+
const DEFAULT_EPOCH = Date.UTC(1970, 0, 1).valueOf();
|
|
5
|
+
/**
|
|
6
|
+
* The sequence of the current running generator.
|
|
7
|
+
*
|
|
8
|
+
* @defaultValue 1
|
|
9
|
+
*/
|
|
10
|
+
let sequence = 1;
|
|
11
|
+
/**
|
|
12
|
+
* Transform a snowflake into its 64Bit binary string.
|
|
13
|
+
*
|
|
14
|
+
* @param snowflake - Snowflake to transform
|
|
15
|
+
* @returns A 64Bit binary string
|
|
16
|
+
*/
|
|
17
|
+
function ToBinaryString(snowflake$1) {
|
|
18
|
+
const cached64BitZeros = "0000000000000000000000000000000000000000000000000000000000000000";
|
|
19
|
+
const binValue = BigInt(snowflake$1).toString(2);
|
|
20
|
+
return binValue.length < 64 ? cached64BitZeros.slice(0, Math.max(0, 64 - binValue.length)) + binValue : binValue;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Extract bits and their values from a snowflake.
|
|
24
|
+
*
|
|
25
|
+
* @param snowflake - Snowflake to extract from
|
|
26
|
+
* @param start - The starting index to extract bits from
|
|
27
|
+
* @param length - Number of bits to extract before stopping
|
|
28
|
+
* @returns A bigint value of the extracted bits
|
|
29
|
+
*/
|
|
30
|
+
function extractBits(snowflake$1, start, length) {
|
|
31
|
+
return Number.parseInt(length ? ToBinaryString(snowflake$1).slice(start, start + length) : ToBinaryString(snowflake$1).slice(Math.max(0, start)), 2);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Generate a snowflake identifier.
|
|
35
|
+
*
|
|
36
|
+
* @remarks
|
|
37
|
+
* Snowflakes are 64-bit unsigned integers that are roughly time-ordered.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
*
|
|
42
|
+
* // Generate a snowflake with the default options
|
|
43
|
+
* const id1 = snowflake();
|
|
44
|
+
*
|
|
45
|
+
* // Generate a snowflake with a custom shard id
|
|
46
|
+
* const id2 = snowflake({ shardId: 2 });
|
|
47
|
+
*
|
|
48
|
+
* // Generate a snowflake with a custom shard id and timestamp
|
|
49
|
+
* const id3 = snowflake({ shardId: 3, timestamp: new Date("2021-01-01") });
|
|
50
|
+
*
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @param options - The options to use when generating the snowflake
|
|
54
|
+
* @returns A snowflake
|
|
55
|
+
*/
|
|
56
|
+
function snowflake(options = {}) {
|
|
57
|
+
const { shardId, epoch, timestamp } = options ?? {};
|
|
58
|
+
let result = BigInt(timestamp ? timestamp instanceof Date ? timestamp.valueOf() : new Date(timestamp).valueOf() : Date.now()) - BigInt(epoch ?? DEFAULT_EPOCH) << BigInt(22);
|
|
59
|
+
result |= BigInt((shardId ?? DEFAULT_SHARD_ID) % 1024) << BigInt(12);
|
|
60
|
+
result |= BigInt(sequence++ % 4096);
|
|
61
|
+
return result.toString();
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Deconstruct a snowflake to its values using the `epoch`.
|
|
65
|
+
*
|
|
66
|
+
* @param snowflake - Snowflake to deconstruct
|
|
67
|
+
* @returns Either the DeconstructedSnowflake object
|
|
68
|
+
*/
|
|
69
|
+
function deconstructSnowflake(snowflake$1) {
|
|
70
|
+
const binary = ToBinaryString(snowflake$1);
|
|
71
|
+
return {
|
|
72
|
+
snowflake: snowflake$1,
|
|
73
|
+
timestamp: extractBits(snowflake$1, 1, 41),
|
|
74
|
+
shard_id: extractBits(snowflake$1, 42, 10),
|
|
75
|
+
sequence: extractBits(snowflake$1, 52),
|
|
76
|
+
binary
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Check if a snowflake string Id is valid.
|
|
81
|
+
*
|
|
82
|
+
* @param snowflake - Snowflake to check
|
|
83
|
+
* @returns Whether the snowflake is valid
|
|
84
|
+
*/
|
|
85
|
+
function isValidSnowflake(snowflake$1) {
|
|
86
|
+
if (!/^\d{19}$/.test(snowflake$1)) return false;
|
|
87
|
+
try {
|
|
88
|
+
deconstructSnowflake(snowflake$1);
|
|
89
|
+
return true;
|
|
90
|
+
} catch {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
//#endregion
|
|
96
|
+
exports.DEFAULT_EPOCH = DEFAULT_EPOCH;
|
|
97
|
+
exports.DEFAULT_SHARD_ID = DEFAULT_SHARD_ID;
|
|
98
|
+
exports.deconstructSnowflake = deconstructSnowflake;
|
|
99
|
+
exports.isValidSnowflake = isValidSnowflake;
|
|
100
|
+
exports.snowflake = snowflake;
|
package/dist/snowflake.mjs
CHANGED
|
@@ -1,2 +1,96 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/snowflake.ts
|
|
2
|
+
const DEFAULT_SHARD_ID = 1;
|
|
3
|
+
const DEFAULT_EPOCH = Date.UTC(1970, 0, 1).valueOf();
|
|
4
|
+
/**
|
|
5
|
+
* The sequence of the current running generator.
|
|
6
|
+
*
|
|
7
|
+
* @defaultValue 1
|
|
8
|
+
*/
|
|
9
|
+
let sequence = 1;
|
|
10
|
+
/**
|
|
11
|
+
* Transform a snowflake into its 64Bit binary string.
|
|
12
|
+
*
|
|
13
|
+
* @param snowflake - Snowflake to transform
|
|
14
|
+
* @returns A 64Bit binary string
|
|
15
|
+
*/
|
|
16
|
+
function ToBinaryString(snowflake$1) {
|
|
17
|
+
const cached64BitZeros = "0000000000000000000000000000000000000000000000000000000000000000";
|
|
18
|
+
const binValue = BigInt(snowflake$1).toString(2);
|
|
19
|
+
return binValue.length < 64 ? cached64BitZeros.slice(0, Math.max(0, 64 - binValue.length)) + binValue : binValue;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Extract bits and their values from a snowflake.
|
|
23
|
+
*
|
|
24
|
+
* @param snowflake - Snowflake to extract from
|
|
25
|
+
* @param start - The starting index to extract bits from
|
|
26
|
+
* @param length - Number of bits to extract before stopping
|
|
27
|
+
* @returns A bigint value of the extracted bits
|
|
28
|
+
*/
|
|
29
|
+
function extractBits(snowflake$1, start, length) {
|
|
30
|
+
return Number.parseInt(length ? ToBinaryString(snowflake$1).slice(start, start + length) : ToBinaryString(snowflake$1).slice(Math.max(0, start)), 2);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Generate a snowflake identifier.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* Snowflakes are 64-bit unsigned integers that are roughly time-ordered.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
*
|
|
41
|
+
* // Generate a snowflake with the default options
|
|
42
|
+
* const id1 = snowflake();
|
|
43
|
+
*
|
|
44
|
+
* // Generate a snowflake with a custom shard id
|
|
45
|
+
* const id2 = snowflake({ shardId: 2 });
|
|
46
|
+
*
|
|
47
|
+
* // Generate a snowflake with a custom shard id and timestamp
|
|
48
|
+
* const id3 = snowflake({ shardId: 3, timestamp: new Date("2021-01-01") });
|
|
49
|
+
*
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @param options - The options to use when generating the snowflake
|
|
53
|
+
* @returns A snowflake
|
|
54
|
+
*/
|
|
55
|
+
function snowflake(options = {}) {
|
|
56
|
+
const { shardId, epoch, timestamp } = options ?? {};
|
|
57
|
+
let result = BigInt(timestamp ? timestamp instanceof Date ? timestamp.valueOf() : new Date(timestamp).valueOf() : Date.now()) - BigInt(epoch ?? DEFAULT_EPOCH) << BigInt(22);
|
|
58
|
+
result |= BigInt((shardId ?? DEFAULT_SHARD_ID) % 1024) << BigInt(12);
|
|
59
|
+
result |= BigInt(sequence++ % 4096);
|
|
60
|
+
return result.toString();
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Deconstruct a snowflake to its values using the `epoch`.
|
|
64
|
+
*
|
|
65
|
+
* @param snowflake - Snowflake to deconstruct
|
|
66
|
+
* @returns Either the DeconstructedSnowflake object
|
|
67
|
+
*/
|
|
68
|
+
function deconstructSnowflake(snowflake$1) {
|
|
69
|
+
const binary = ToBinaryString(snowflake$1);
|
|
70
|
+
return {
|
|
71
|
+
snowflake: snowflake$1,
|
|
72
|
+
timestamp: extractBits(snowflake$1, 1, 41),
|
|
73
|
+
shard_id: extractBits(snowflake$1, 42, 10),
|
|
74
|
+
sequence: extractBits(snowflake$1, 52),
|
|
75
|
+
binary
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Check if a snowflake string Id is valid.
|
|
80
|
+
*
|
|
81
|
+
* @param snowflake - Snowflake to check
|
|
82
|
+
* @returns Whether the snowflake is valid
|
|
83
|
+
*/
|
|
84
|
+
function isValidSnowflake(snowflake$1) {
|
|
85
|
+
if (!/^\d{19}$/.test(snowflake$1)) return false;
|
|
86
|
+
try {
|
|
87
|
+
deconstructSnowflake(snowflake$1);
|
|
88
|
+
return true;
|
|
89
|
+
} catch {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
//#endregion
|
|
95
|
+
export { DEFAULT_EPOCH, DEFAULT_SHARD_ID, deconstructSnowflake, isValidSnowflake, snowflake };
|
|
2
96
|
//# sourceMappingURL=snowflake.mjs.map
|
package/dist/snowflake.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snowflake.mjs","names":["DEFAULT_EPOCH: number","snowflake"],"sources":["../src/snowflake.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\n/**\n * Options passed to the `generate` function to create a snowflake identifier.\n */\nexport interface SnowflakeGeneratorOptions {\n /**\n * The id of the shard running this generator.\n *\n * @defaultValue 1\n */\n shardId?: number;\n\n /**\n * The epoch to use for the snowflake.\n *\n * @remarks\n * This is the time in milliseconds since 1 January 1970 00:00:00 UTC.\n *\n * @defaultValue 1420070400000 (Date.UTC(1970, 0, 1).valueOf())\n */\n epoch?: number;\n\n /**\n * The current timestamp to use for the snowflake.\n *\n * @defaultValue Date.now()\n */\n timestamp?: number | Date;\n}\n\n/**\n * Resolvable value types for a valid Snowflake:\n * string\n * number\n * bigint\n */\nexport type SnowflakeResolvable = string;\n\n/**\n * A deconstructed snowflake and the details around it's creation.\n */\nexport interface DeconstructedSnowflake {\n /**\n * Snowflake deconstructed from\n */\n snowflake: SnowflakeResolvable;\n\n /**\n * The timestamp the snowflake was generated\n */\n timestamp: number;\n\n /**\n * The shard_id used when generating\n */\n shard_id: number;\n\n /**\n * The increment of this snowflake\n */\n sequence: number;\n\n /**\n * The 64Bit snowflake binary string\n */\n binary: string;\n}\n\nexport const DEFAULT_SHARD_ID = 1;\nexport const DEFAULT_EPOCH: number = Date.UTC(1970, 0, 1).valueOf();\n\n/**\n * The sequence of the current running generator.\n *\n * @defaultValue 1\n */\nlet sequence = 1;\n\n/**\n * Transform a snowflake into its 64Bit binary string.\n *\n * @param snowflake - Snowflake to transform\n * @returns A 64Bit binary string\n */\nfunction ToBinaryString(snowflake: SnowflakeResolvable): string {\n const cached64BitZeros =\n \"0000000000000000000000000000000000000000000000000000000000000000\";\n const binValue = BigInt(snowflake).toString(2);\n\n return binValue.length < 64\n ? cached64BitZeros.slice(0, Math.max(0, 64 - binValue.length)) + binValue\n : binValue;\n}\n\n/**\n * Extract bits and their values from a snowflake.\n *\n * @param snowflake - Snowflake to extract from\n * @param start - The starting index to extract bits from\n * @param length - Number of bits to extract before stopping\n * @returns A bigint value of the extracted bits\n */\nfunction extractBits(\n snowflake: SnowflakeResolvable,\n start: number,\n length?: number\n): number {\n return Number.parseInt(\n length\n ? ToBinaryString(snowflake).slice(start, start + length)\n : ToBinaryString(snowflake).slice(Math.max(0, start)),\n 2\n );\n}\n\n/**\n * Generate a snowflake identifier.\n *\n * @remarks\n * Snowflakes are 64-bit unsigned integers that are roughly time-ordered.\n *\n * @example\n * ```typescript\n *\n * // Generate a snowflake with the default options\n * const id1 = snowflake();\n *\n * // Generate a snowflake with a custom shard id\n * const id2 = snowflake({ shardId: 2 });\n *\n * // Generate a snowflake with a custom shard id and timestamp\n * const id3 = snowflake({ shardId: 3, timestamp: new Date(\"2021-01-01\") });\n *\n * ```\n *\n * @param options - The options to use when generating the snowflake\n * @returns A snowflake\n */\nexport function snowflake(options: SnowflakeGeneratorOptions = {}): string {\n const { shardId, epoch, timestamp } = options ?? {};\n\n let result =\n (BigInt(\n timestamp\n ? timestamp instanceof Date\n ? timestamp.valueOf()\n : new Date(timestamp).valueOf()\n : Date.now()\n ) -\n BigInt(epoch ?? DEFAULT_EPOCH)) <<\n BigInt(22);\n\n result |= BigInt((shardId ?? DEFAULT_SHARD_ID) % 1024) << BigInt(12);\n result |= BigInt(sequence++ % 4096);\n\n return result.toString();\n}\n\n/**\n * Deconstruct a snowflake to its values using the `epoch`.\n *\n * @param snowflake - Snowflake to deconstruct\n * @returns Either the DeconstructedSnowflake object\n */\nexport function deconstructSnowflake(\n snowflake: SnowflakeResolvable\n): DeconstructedSnowflake {\n const binary = ToBinaryString(snowflake);\n\n return {\n snowflake,\n timestamp: extractBits(snowflake, 1, 41),\n shard_id: extractBits(snowflake, 42, 10),\n sequence: extractBits(snowflake, 52),\n binary\n };\n}\n\n/**\n * Check if a snowflake string Id is valid.\n *\n * @param snowflake - Snowflake to check\n * @returns Whether the snowflake is valid\n */\nexport function isValidSnowflake(snowflake: string): boolean {\n if (!/^\\d{19}$/.test(snowflake)) {\n return false;\n }\n\n try {\n deconstructSnowflake(snowflake);\n\n return true;\n } catch {\n return false;\n }\n}\n"],"mappings":"AAqFA,MAAa,
|
|
1
|
+
{"version":3,"file":"snowflake.mjs","names":["DEFAULT_EPOCH: number","snowflake"],"sources":["../src/snowflake.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\n/**\n * Options passed to the `generate` function to create a snowflake identifier.\n */\nexport interface SnowflakeGeneratorOptions {\n /**\n * The id of the shard running this generator.\n *\n * @defaultValue 1\n */\n shardId?: number;\n\n /**\n * The epoch to use for the snowflake.\n *\n * @remarks\n * This is the time in milliseconds since 1 January 1970 00:00:00 UTC.\n *\n * @defaultValue 1420070400000 (Date.UTC(1970, 0, 1).valueOf())\n */\n epoch?: number;\n\n /**\n * The current timestamp to use for the snowflake.\n *\n * @defaultValue Date.now()\n */\n timestamp?: number | Date;\n}\n\n/**\n * Resolvable value types for a valid Snowflake:\n * string\n * number\n * bigint\n */\nexport type SnowflakeResolvable = string;\n\n/**\n * A deconstructed snowflake and the details around it's creation.\n */\nexport interface DeconstructedSnowflake {\n /**\n * Snowflake deconstructed from\n */\n snowflake: SnowflakeResolvable;\n\n /**\n * The timestamp the snowflake was generated\n */\n timestamp: number;\n\n /**\n * The shard_id used when generating\n */\n shard_id: number;\n\n /**\n * The increment of this snowflake\n */\n sequence: number;\n\n /**\n * The 64Bit snowflake binary string\n */\n binary: string;\n}\n\nexport const DEFAULT_SHARD_ID = 1;\nexport const DEFAULT_EPOCH: number = Date.UTC(1970, 0, 1).valueOf();\n\n/**\n * The sequence of the current running generator.\n *\n * @defaultValue 1\n */\nlet sequence = 1;\n\n/**\n * Transform a snowflake into its 64Bit binary string.\n *\n * @param snowflake - Snowflake to transform\n * @returns A 64Bit binary string\n */\nfunction ToBinaryString(snowflake: SnowflakeResolvable): string {\n const cached64BitZeros =\n \"0000000000000000000000000000000000000000000000000000000000000000\";\n const binValue = BigInt(snowflake).toString(2);\n\n return binValue.length < 64\n ? cached64BitZeros.slice(0, Math.max(0, 64 - binValue.length)) + binValue\n : binValue;\n}\n\n/**\n * Extract bits and their values from a snowflake.\n *\n * @param snowflake - Snowflake to extract from\n * @param start - The starting index to extract bits from\n * @param length - Number of bits to extract before stopping\n * @returns A bigint value of the extracted bits\n */\nfunction extractBits(\n snowflake: SnowflakeResolvable,\n start: number,\n length?: number\n): number {\n return Number.parseInt(\n length\n ? ToBinaryString(snowflake).slice(start, start + length)\n : ToBinaryString(snowflake).slice(Math.max(0, start)),\n 2\n );\n}\n\n/**\n * Generate a snowflake identifier.\n *\n * @remarks\n * Snowflakes are 64-bit unsigned integers that are roughly time-ordered.\n *\n * @example\n * ```typescript\n *\n * // Generate a snowflake with the default options\n * const id1 = snowflake();\n *\n * // Generate a snowflake with a custom shard id\n * const id2 = snowflake({ shardId: 2 });\n *\n * // Generate a snowflake with a custom shard id and timestamp\n * const id3 = snowflake({ shardId: 3, timestamp: new Date(\"2021-01-01\") });\n *\n * ```\n *\n * @param options - The options to use when generating the snowflake\n * @returns A snowflake\n */\nexport function snowflake(options: SnowflakeGeneratorOptions = {}): string {\n const { shardId, epoch, timestamp } = options ?? {};\n\n let result =\n (BigInt(\n timestamp\n ? timestamp instanceof Date\n ? timestamp.valueOf()\n : new Date(timestamp).valueOf()\n : Date.now()\n ) -\n BigInt(epoch ?? DEFAULT_EPOCH)) <<\n BigInt(22);\n\n result |= BigInt((shardId ?? DEFAULT_SHARD_ID) % 1024) << BigInt(12);\n result |= BigInt(sequence++ % 4096);\n\n return result.toString();\n}\n\n/**\n * Deconstruct a snowflake to its values using the `epoch`.\n *\n * @param snowflake - Snowflake to deconstruct\n * @returns Either the DeconstructedSnowflake object\n */\nexport function deconstructSnowflake(\n snowflake: SnowflakeResolvable\n): DeconstructedSnowflake {\n const binary = ToBinaryString(snowflake);\n\n return {\n snowflake,\n timestamp: extractBits(snowflake, 1, 41),\n shard_id: extractBits(snowflake, 42, 10),\n sequence: extractBits(snowflake, 52),\n binary\n };\n}\n\n/**\n * Check if a snowflake string Id is valid.\n *\n * @param snowflake - Snowflake to check\n * @returns Whether the snowflake is valid\n */\nexport function isValidSnowflake(snowflake: string): boolean {\n if (!/^\\d{19}$/.test(snowflake)) {\n return false;\n }\n\n try {\n deconstructSnowflake(snowflake);\n\n return true;\n } catch {\n return false;\n }\n}\n"],"mappings":";AAqFA,MAAa,mBAAmB;AAChC,MAAaA,gBAAwB,KAAK,IAAI,MAAM,GAAG,EAAE,CAAC,SAAS;;;;;;AAOnE,IAAI,WAAW;;;;;;;AAQf,SAAS,eAAe,aAAwC;CAC9D,MAAM,mBACJ;CACF,MAAM,WAAW,OAAOC,YAAU,CAAC,SAAS,EAAE;AAE9C,QAAO,SAAS,SAAS,KACrB,iBAAiB,MAAM,GAAG,KAAK,IAAI,GAAG,KAAK,SAAS,OAAO,CAAC,GAAG,WAC/D;;;;;;;;;;AAWN,SAAS,YACP,aACA,OACA,QACQ;AACR,QAAO,OAAO,SACZ,SACI,eAAeA,YAAU,CAAC,MAAM,OAAO,QAAQ,OAAO,GACtD,eAAeA,YAAU,CAAC,MAAM,KAAK,IAAI,GAAG,MAAM,CAAC,EACvD,EACD;;;;;;;;;;;;;;;;;;;;;;;;;AA0BH,SAAgB,UAAU,UAAqC,EAAE,EAAU;CACzE,MAAM,EAAE,SAAS,OAAO,cAAc,WAAW,EAAE;CAEnD,IAAI,SACD,OACC,YACI,qBAAqB,OACnB,UAAU,SAAS,GACnB,IAAI,KAAK,UAAU,CAAC,SAAS,GAC/B,KAAK,KAAK,CACf,GACC,OAAO,SAAS,cAAc,IAChC,OAAO,GAAG;AAEZ,WAAU,QAAQ,WAAW,oBAAoB,KAAK,IAAI,OAAO,GAAG;AACpE,WAAU,OAAO,aAAa,KAAK;AAEnC,QAAO,OAAO,UAAU;;;;;;;;AAS1B,SAAgB,qBACd,aACwB;CACxB,MAAM,SAAS,eAAeA,YAAU;AAExC,QAAO;EACL;EACA,WAAW,YAAYA,aAAW,GAAG,GAAG;EACxC,UAAU,YAAYA,aAAW,IAAI,GAAG;EACxC,UAAU,YAAYA,aAAW,GAAG;EACpC;EACD;;;;;;;;AASH,SAAgB,iBAAiB,aAA4B;AAC3D,KAAI,CAAC,WAAW,KAAKA,YAAU,CAC7B,QAAO;AAGT,KAAI;AACF,uBAAqBA,YAAU;AAE/B,SAAO;SACD;AACN,SAAO"}
|
|
@@ -1 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
//#region ../type-checks/src/is-buffer.ts
|
|
3
|
+
const isBufferExists = typeof Buffer !== "undefined";
|
|
4
|
+
/**
|
|
5
|
+
* Check if the provided value's type is `Buffer`
|
|
6
|
+
*/
|
|
7
|
+
const isBuffer = isBufferExists ? Buffer.isBuffer.bind(Buffer) : function isBuffer$1(value) {
|
|
8
|
+
return false;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
//#endregion
|
|
12
|
+
exports.isBufferExists = isBufferExists;
|
|
@@ -1,2 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
//#region ../type-checks/src/is-buffer.ts
|
|
2
|
+
const isBufferExists = typeof Buffer !== "undefined";
|
|
3
|
+
/**
|
|
4
|
+
* Check if the provided value's type is `Buffer`
|
|
5
|
+
*/
|
|
6
|
+
const isBuffer = isBufferExists ? Buffer.isBuffer.bind(Buffer) : function isBuffer$1(value) {
|
|
7
|
+
return false;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
export { isBufferExists };
|
|
2
12
|
//# sourceMappingURL=is-buffer.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-buffer.mjs","names":["isBuffer: typeof Buffer.isBuffer","isBuffer"],"sources":["../../../../type-checks/src/is-buffer.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nexport const isBufferExists = typeof Buffer !== \"undefined\";\n\n/**\n * Check if the provided value's type is `Buffer`\n */\nexport const isBuffer: typeof Buffer.isBuffer = isBufferExists\n ? Buffer.isBuffer.bind(Buffer)\n : /**\n * Check if the provided value's type is `Buffer`\n\n * @param value - The value to type check\n * @returns An indicator specifying if the value provided is of type `Buffer`\n */\n function isBuffer(\n value: Parameters<typeof Buffer.isBuffer>[0]\n ): value is Buffer {\n return false;\n };\n"],"mappings":"AAkBA,MAAa,
|
|
1
|
+
{"version":3,"file":"is-buffer.mjs","names":["isBuffer: typeof Buffer.isBuffer","isBuffer"],"sources":["../../../../type-checks/src/is-buffer.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nexport const isBufferExists = typeof Buffer !== \"undefined\";\n\n/**\n * Check if the provided value's type is `Buffer`\n */\nexport const isBuffer: typeof Buffer.isBuffer = isBufferExists\n ? Buffer.isBuffer.bind(Buffer)\n : /**\n * Check if the provided value's type is `Buffer`\n\n * @param value - The value to type check\n * @returns An indicator specifying if the value provided is of type `Buffer`\n */\n function isBuffer(\n value: Parameters<typeof Buffer.isBuffer>[0]\n ): value is Buffer {\n return false;\n };\n"],"mappings":";AAkBA,MAAa,iBAAiB,OAAO,WAAW;;;;AAKhD,MAAaA,WAAmC,iBAC5C,OAAO,SAAS,KAAK,OAAO,GAO5B,SAASC,WACP,OACiB;AACjB,QAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
require(
|
|
1
|
+
require('./type-detect.cjs');
|
|
@@ -1 +1,15 @@
|
|
|
1
|
-
require(
|
|
1
|
+
require('./is-buffer.cjs');
|
|
2
|
+
|
|
3
|
+
//#region ../type-checks/src/type-detect.ts
|
|
4
|
+
const globalObject = ((Obj) => {
|
|
5
|
+
if (typeof globalThis === "object") return globalThis;
|
|
6
|
+
Object.defineProperty(Obj, "typeDetectGlobalObject", {
|
|
7
|
+
get() {
|
|
8
|
+
return this;
|
|
9
|
+
},
|
|
10
|
+
configurable: true
|
|
11
|
+
});
|
|
12
|
+
return globalThis;
|
|
13
|
+
})(Object.prototype);
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
@@ -1,2 +1,17 @@
|
|
|
1
|
-
import"./is-buffer.mjs";
|
|
1
|
+
import "./is-buffer.mjs";
|
|
2
|
+
|
|
3
|
+
//#region ../type-checks/src/type-detect.ts
|
|
4
|
+
const globalObject = ((Obj) => {
|
|
5
|
+
if (typeof globalThis === "object") return globalThis;
|
|
6
|
+
Object.defineProperty(Obj, "typeDetectGlobalObject", {
|
|
7
|
+
get() {
|
|
8
|
+
return this;
|
|
9
|
+
},
|
|
10
|
+
configurable: true
|
|
11
|
+
});
|
|
12
|
+
return globalThis;
|
|
13
|
+
})(Object.prototype);
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { };
|
|
2
17
|
//# sourceMappingURL=type-detect.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type-detect.mjs","names":[],"sources":["../../../../type-checks/src/type-detect.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isBuffer } from \"./is-buffer\";\n\nconst globalObject = (Obj => {\n if (typeof globalThis === \"object\") {\n return globalThis;\n }\n Object.defineProperty(Obj, \"typeDetectGlobalObject\", {\n get() {\n return this;\n },\n configurable: true\n });\n\n // // biome-ignore lint/correctness/noUndeclaredVariables: <explanation>\n // const global = typeDetectGlobalObject;\n\n // // biome-ignore lint/performance/noDelete: <explanation>\n // delete Obj.typeDetectGlobalObject;\n return globalThis;\n})(Object.prototype);\n\nexport function typeDetect(obj: unknown): string {\n // NOTE: isBuffer must execute before type-detect,\n // because type-detect returns 'Uint8Array'.\n if (isBuffer(obj)) {\n return \"Buffer\";\n }\n\n const typeofObj = typeof obj;\n if (typeofObj !== \"object\") {\n return typeofObj;\n }\n\n if (obj === null) {\n return \"null\";\n }\n\n if (obj === globalObject) {\n return \"global\";\n }\n\n if (\n Array.isArray(obj) &&\n (Symbol.toStringTag === undefined || !(Symbol.toStringTag in obj))\n ) {\n return \"Array\";\n }\n\n // https://html.spec.whatwg.org/multipage/browsers.html#location\n if (typeof globalThis === \"object\" && globalThis !== null) {\n if (\n typeof (globalThis as any).location === \"object\" &&\n obj === (globalThis as any).location\n ) {\n return \"Location\";\n }\n\n // https://html.spec.whatwg.org/#document\n if (\n typeof (globalThis as any).document === \"object\" &&\n obj === (globalThis as any).document\n ) {\n return \"Document\";\n }\n\n // https://html.spec.whatwg.org/multipage/webappapis.html#mimetypearray\n if (typeof (globalThis as any).navigator === \"object\") {\n if (\n typeof (globalThis as any).navigator.mimeTypes === \"object\" &&\n obj === (globalThis as any).navigator.mimeTypes\n ) {\n return \"MimeTypeArray\";\n }\n\n // https://html.spec.whatwg.org/multipage/webappapis.html#pluginarray\n if (\n typeof (globalThis as any).navigator.plugins === \"object\" &&\n obj === (globalThis as any).navigator.plugins\n ) {\n return \"PluginArray\";\n }\n }\n\n // https://html.spec.whatwg.org/multipage/webappapis.html#pluginarray\n if (\n (typeof (globalThis as any).HTMLElement === \"function\" ||\n typeof (globalThis as any).HTMLElement === \"object\") &&\n obj instanceof (globalThis as any).HTMLElement\n ) {\n if ((obj as any).tagName === \"BLOCKQUOTE\") {\n return \"HTMLQuoteElement\";\n }\n\n // https://html.spec.whatwg.org/#htmltabledatacellelement\n if ((obj as any).tagName === \"TD\") {\n return \"HTMLTableDataCellElement\";\n }\n\n // https://html.spec.whatwg.org/#htmltableheadercellelement\n if ((obj as any).tagName === \"TH\") {\n return \"HTMLTableHeaderCellElement\";\n }\n }\n }\n\n const stringTag =\n Symbol.toStringTag !== undefined && (obj as any)[Symbol.toStringTag];\n if (typeof stringTag === \"string\") {\n return stringTag;\n }\n\n const objPrototype = Object.getPrototypeOf(obj);\n if (objPrototype === RegExp.prototype) {\n return \"RegExp\";\n }\n\n if (objPrototype === Date.prototype) {\n return \"Date\";\n }\n\n // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-promise.prototype-@@tostringtag\n if (typeof Promise !== \"undefined\" && objPrototype === Promise.prototype) {\n return \"Promise\";\n }\n\n if (typeof Set !== \"undefined\" && objPrototype === Set.prototype) {\n return \"Set\";\n }\n\n if (typeof Map !== \"undefined\" && objPrototype === Map.prototype) {\n return \"Map\";\n }\n\n if (typeof WeakSet !== \"undefined\" && objPrototype === WeakSet.prototype) {\n return \"WeakSet\";\n }\n\n if (typeof WeakMap !== \"undefined\" && objPrototype === WeakMap.prototype) {\n return \"WeakMap\";\n }\n\n // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-dataview.prototype-@@tostringtag\n if (typeof DataView !== \"undefined\" && objPrototype === DataView.prototype) {\n return \"DataView\";\n }\n\n // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%mapiteratorprototype%-@@tostringtag\n if (\n typeof Map !== \"undefined\" &&\n objPrototype === Object.getPrototypeOf(new Map().entries())\n ) {\n return \"Map Iterator\";\n }\n\n // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%setiteratorprototype%-@@tostringtag\n if (\n typeof Set !== \"undefined\" &&\n objPrototype === Object.getPrototypeOf(new Set().entries())\n ) {\n return \"Set Iterator\";\n }\n\n // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%arrayiteratorprototype%-@@tostringtag\n if (\n typeof Array.prototype[Symbol.iterator] === \"function\" &&\n objPrototype === Object.getPrototypeOf([][Symbol.iterator]())\n ) {\n return \"Array Iterator\";\n }\n\n // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%stringiteratorprototype%-@@tostringtag\n if (\n Symbol.iterator !== undefined &&\n typeof String.prototype[Symbol.iterator] === \"function\" &&\n Object.getPrototypeOf(\"\"[Symbol.iterator]()) &&\n objPrototype === Object.getPrototypeOf(\"\"[Symbol.iterator]())\n ) {\n return \"String Iterator\";\n }\n\n if (objPrototype === null) {\n return \"Object\";\n }\n\n return Object.prototype.toString.call(obj).slice(8, -1);\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"type-detect.mjs","names":[],"sources":["../../../../type-checks/src/type-detect.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isBuffer } from \"./is-buffer\";\n\nconst globalObject = (Obj => {\n if (typeof globalThis === \"object\") {\n return globalThis;\n }\n Object.defineProperty(Obj, \"typeDetectGlobalObject\", {\n get() {\n return this;\n },\n configurable: true\n });\n\n // // biome-ignore lint/correctness/noUndeclaredVariables: <explanation>\n // const global = typeDetectGlobalObject;\n\n // // biome-ignore lint/performance/noDelete: <explanation>\n // delete Obj.typeDetectGlobalObject;\n return globalThis;\n})(Object.prototype);\n\nexport function typeDetect(obj: unknown): string {\n // NOTE: isBuffer must execute before type-detect,\n // because type-detect returns 'Uint8Array'.\n if (isBuffer(obj)) {\n return \"Buffer\";\n }\n\n const typeofObj = typeof obj;\n if (typeofObj !== \"object\") {\n return typeofObj;\n }\n\n if (obj === null) {\n return \"null\";\n }\n\n if (obj === globalObject) {\n return \"global\";\n }\n\n if (\n Array.isArray(obj) &&\n (Symbol.toStringTag === undefined || !(Symbol.toStringTag in obj))\n ) {\n return \"Array\";\n }\n\n // https://html.spec.whatwg.org/multipage/browsers.html#location\n if (typeof globalThis === \"object\" && globalThis !== null) {\n if (\n typeof (globalThis as any).location === \"object\" &&\n obj === (globalThis as any).location\n ) {\n return \"Location\";\n }\n\n // https://html.spec.whatwg.org/#document\n if (\n typeof (globalThis as any).document === \"object\" &&\n obj === (globalThis as any).document\n ) {\n return \"Document\";\n }\n\n // https://html.spec.whatwg.org/multipage/webappapis.html#mimetypearray\n if (typeof (globalThis as any).navigator === \"object\") {\n if (\n typeof (globalThis as any).navigator.mimeTypes === \"object\" &&\n obj === (globalThis as any).navigator.mimeTypes\n ) {\n return \"MimeTypeArray\";\n }\n\n // https://html.spec.whatwg.org/multipage/webappapis.html#pluginarray\n if (\n typeof (globalThis as any).navigator.plugins === \"object\" &&\n obj === (globalThis as any).navigator.plugins\n ) {\n return \"PluginArray\";\n }\n }\n\n // https://html.spec.whatwg.org/multipage/webappapis.html#pluginarray\n if (\n (typeof (globalThis as any).HTMLElement === \"function\" ||\n typeof (globalThis as any).HTMLElement === \"object\") &&\n obj instanceof (globalThis as any).HTMLElement\n ) {\n if ((obj as any).tagName === \"BLOCKQUOTE\") {\n return \"HTMLQuoteElement\";\n }\n\n // https://html.spec.whatwg.org/#htmltabledatacellelement\n if ((obj as any).tagName === \"TD\") {\n return \"HTMLTableDataCellElement\";\n }\n\n // https://html.spec.whatwg.org/#htmltableheadercellelement\n if ((obj as any).tagName === \"TH\") {\n return \"HTMLTableHeaderCellElement\";\n }\n }\n }\n\n const stringTag =\n Symbol.toStringTag !== undefined && (obj as any)[Symbol.toStringTag];\n if (typeof stringTag === \"string\") {\n return stringTag;\n }\n\n const objPrototype = Object.getPrototypeOf(obj);\n if (objPrototype === RegExp.prototype) {\n return \"RegExp\";\n }\n\n if (objPrototype === Date.prototype) {\n return \"Date\";\n }\n\n // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-promise.prototype-@@tostringtag\n if (typeof Promise !== \"undefined\" && objPrototype === Promise.prototype) {\n return \"Promise\";\n }\n\n if (typeof Set !== \"undefined\" && objPrototype === Set.prototype) {\n return \"Set\";\n }\n\n if (typeof Map !== \"undefined\" && objPrototype === Map.prototype) {\n return \"Map\";\n }\n\n if (typeof WeakSet !== \"undefined\" && objPrototype === WeakSet.prototype) {\n return \"WeakSet\";\n }\n\n if (typeof WeakMap !== \"undefined\" && objPrototype === WeakMap.prototype) {\n return \"WeakMap\";\n }\n\n // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-dataview.prototype-@@tostringtag\n if (typeof DataView !== \"undefined\" && objPrototype === DataView.prototype) {\n return \"DataView\";\n }\n\n // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%mapiteratorprototype%-@@tostringtag\n if (\n typeof Map !== \"undefined\" &&\n objPrototype === Object.getPrototypeOf(new Map().entries())\n ) {\n return \"Map Iterator\";\n }\n\n // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%setiteratorprototype%-@@tostringtag\n if (\n typeof Set !== \"undefined\" &&\n objPrototype === Object.getPrototypeOf(new Set().entries())\n ) {\n return \"Set Iterator\";\n }\n\n // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%arrayiteratorprototype%-@@tostringtag\n if (\n typeof Array.prototype[Symbol.iterator] === \"function\" &&\n objPrototype === Object.getPrototypeOf([][Symbol.iterator]())\n ) {\n return \"Array Iterator\";\n }\n\n // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%stringiteratorprototype%-@@tostringtag\n if (\n Symbol.iterator !== undefined &&\n typeof String.prototype[Symbol.iterator] === \"function\" &&\n Object.getPrototypeOf(\"\"[Symbol.iterator]()) &&\n objPrototype === Object.getPrototypeOf(\"\"[Symbol.iterator]())\n ) {\n return \"String Iterator\";\n }\n\n if (objPrototype === null) {\n return \"Object\";\n }\n\n return Object.prototype.toString.call(obj).slice(8, -1);\n}\n"],"mappings":";;;AAoBA,MAAM,iBAAgB,QAAO;AAC3B,KAAI,OAAO,eAAe,SACxB,QAAO;AAET,QAAO,eAAe,KAAK,0BAA0B;EACnD,MAAM;AACJ,UAAO;;EAET,cAAc;EACf,CAAC;AAOF,QAAO;GACN,OAAO,UAAU"}
|
package/dist/uuid.cjs
CHANGED
|
@@ -1 +1,45 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
//#region src/uuid.ts
|
|
3
|
+
let index = 256;
|
|
4
|
+
const hex = [];
|
|
5
|
+
let buffer;
|
|
6
|
+
while (index--) hex[index] = (index + 256).toString(16).slice(1);
|
|
7
|
+
/**
|
|
8
|
+
* A utility function that returns a [UUID (Universally Unique Identifier)](https://www.cockroachlabs.com/blog/what-is-a-uuid/) string.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* This function generates a random UUID (Universally Unique Identifier) using a cryptographically secure pseudo-random number generator.
|
|
12
|
+
*
|
|
13
|
+
* This implementation was based on https://github.com/lukeed/uuid.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Generate a UUID string
|
|
18
|
+
* const id = uuid();
|
|
19
|
+
* // => "f47ac10b-58cc-4372-a567-0e02b2c3d479"
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @returns A UUID unique identifier.
|
|
23
|
+
*/
|
|
24
|
+
function uuid() {
|
|
25
|
+
let i = 0;
|
|
26
|
+
let num;
|
|
27
|
+
let out = "";
|
|
28
|
+
if (!buffer || index + 16 > 256) {
|
|
29
|
+
buffer = Array.from({ length: i = 256 });
|
|
30
|
+
while (i--) buffer[i] = Math.trunc(256 * Math.random());
|
|
31
|
+
i = index = 0;
|
|
32
|
+
}
|
|
33
|
+
for (; i < 16; i++) {
|
|
34
|
+
num = buffer[index + i];
|
|
35
|
+
if (i === 6) out += hex[num & 15 | 64];
|
|
36
|
+
else if (i === 8) out += hex[num & 63 | 128];
|
|
37
|
+
else out += hex[num];
|
|
38
|
+
if (i & 1 && i > 1 && i < 11) out += "-";
|
|
39
|
+
}
|
|
40
|
+
index++;
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
exports.uuid = uuid;
|
package/dist/uuid.mjs
CHANGED
|
@@ -1,2 +1,45 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/uuid.ts
|
|
2
|
+
let index = 256;
|
|
3
|
+
const hex = [];
|
|
4
|
+
let buffer;
|
|
5
|
+
while (index--) hex[index] = (index + 256).toString(16).slice(1);
|
|
6
|
+
/**
|
|
7
|
+
* A utility function that returns a [UUID (Universally Unique Identifier)](https://www.cockroachlabs.com/blog/what-is-a-uuid/) string.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* This function generates a random UUID (Universally Unique Identifier) using a cryptographically secure pseudo-random number generator.
|
|
11
|
+
*
|
|
12
|
+
* This implementation was based on https://github.com/lukeed/uuid.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* // Generate a UUID string
|
|
17
|
+
* const id = uuid();
|
|
18
|
+
* // => "f47ac10b-58cc-4372-a567-0e02b2c3d479"
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @returns A UUID unique identifier.
|
|
22
|
+
*/
|
|
23
|
+
function uuid() {
|
|
24
|
+
let i = 0;
|
|
25
|
+
let num;
|
|
26
|
+
let out = "";
|
|
27
|
+
if (!buffer || index + 16 > 256) {
|
|
28
|
+
buffer = Array.from({ length: i = 256 });
|
|
29
|
+
while (i--) buffer[i] = Math.trunc(256 * Math.random());
|
|
30
|
+
i = index = 0;
|
|
31
|
+
}
|
|
32
|
+
for (; i < 16; i++) {
|
|
33
|
+
num = buffer[index + i];
|
|
34
|
+
if (i === 6) out += hex[num & 15 | 64];
|
|
35
|
+
else if (i === 8) out += hex[num & 63 | 128];
|
|
36
|
+
else out += hex[num];
|
|
37
|
+
if (i & 1 && i > 1 && i < 11) out += "-";
|
|
38
|
+
}
|
|
39
|
+
index++;
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
export { uuid };
|
|
2
45
|
//# sourceMappingURL=uuid.mjs.map
|
package/dist/uuid.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uuid.mjs","names":["buffer!: number[]","num!: number"],"sources":["../src/uuid.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nlet index = 256;\nconst hex = [] as string[];\nlet buffer!: number[];\n\nwhile (index--) {\n hex[index] = (index + 256).toString(16).slice(1);\n}\n\n/**\n * A utility function that returns a [UUID (Universally Unique Identifier)](https://www.cockroachlabs.com/blog/what-is-a-uuid/) string.\n *\n * @remarks\n * This function generates a random UUID (Universally Unique Identifier) using a cryptographically secure pseudo-random number generator.\n *\n * This implementation was based on https://github.com/lukeed/uuid.\n *\n * @example\n * ```typescript\n * // Generate a UUID string\n * const id = uuid();\n * // => \"f47ac10b-58cc-4372-a567-0e02b2c3d479\"\n * ```\n *\n * @returns A UUID unique identifier.\n */\nexport function uuid() {\n let i = 0;\n let num!: number;\n let out = \"\";\n\n if (!buffer || index + 16 > 256) {\n buffer = Array.from({ length: (i = 256) });\n while (i--) {\n buffer[i] = Math.trunc(256 * Math.random());\n }\n // eslint-disable-next-line no-multi-assign\n i = index = 0;\n }\n\n for (; i < 16; i++) {\n num = buffer[index + i]!;\n if (i === 6) {\n out += hex[(num & 15) | 64];\n } else if (i === 8) {\n out += hex[(num & 63) | 128];\n } else {\n out += hex[num];\n }\n\n if (i & 1 && i > 1 && i < 11) {\n out += \"-\";\n }\n }\n\n index++;\n return out;\n}\n"],"mappings":"AAkBA,IAAI,
|
|
1
|
+
{"version":3,"file":"uuid.mjs","names":["buffer!: number[]","num!: number"],"sources":["../src/uuid.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nlet index = 256;\nconst hex = [] as string[];\nlet buffer!: number[];\n\nwhile (index--) {\n hex[index] = (index + 256).toString(16).slice(1);\n}\n\n/**\n * A utility function that returns a [UUID (Universally Unique Identifier)](https://www.cockroachlabs.com/blog/what-is-a-uuid/) string.\n *\n * @remarks\n * This function generates a random UUID (Universally Unique Identifier) using a cryptographically secure pseudo-random number generator.\n *\n * This implementation was based on https://github.com/lukeed/uuid.\n *\n * @example\n * ```typescript\n * // Generate a UUID string\n * const id = uuid();\n * // => \"f47ac10b-58cc-4372-a567-0e02b2c3d479\"\n * ```\n *\n * @returns A UUID unique identifier.\n */\nexport function uuid() {\n let i = 0;\n let num!: number;\n let out = \"\";\n\n if (!buffer || index + 16 > 256) {\n buffer = Array.from({ length: (i = 256) });\n while (i--) {\n buffer[i] = Math.trunc(256 * Math.random());\n }\n // eslint-disable-next-line no-multi-assign\n i = index = 0;\n }\n\n for (; i < 16; i++) {\n num = buffer[index + i]!;\n if (i === 6) {\n out += hex[(num & 15) | 64];\n } else if (i === 8) {\n out += hex[(num & 63) | 128];\n } else {\n out += hex[num];\n }\n\n if (i & 1 && i > 1 && i < 11) {\n out += \"-\";\n }\n }\n\n index++;\n return out;\n}\n"],"mappings":";AAkBA,IAAI,QAAQ;AACZ,MAAM,MAAM,EAAE;AACd,IAAIA;AAEJ,OAAO,QACL,KAAI,UAAU,QAAQ,KAAK,SAAS,GAAG,CAAC,MAAM,EAAE;;;;;;;;;;;;;;;;;;AAoBlD,SAAgB,OAAO;CACrB,IAAI,IAAI;CACR,IAAIC;CACJ,IAAI,MAAM;AAEV,KAAI,CAAC,UAAU,QAAQ,KAAK,KAAK;AAC/B,WAAS,MAAM,KAAK,EAAE,QAAS,IAAI,KAAM,CAAC;AAC1C,SAAO,IACL,QAAO,KAAK,KAAK,MAAM,MAAM,KAAK,QAAQ,CAAC;AAG7C,MAAI,QAAQ;;AAGd,QAAO,IAAI,IAAI,KAAK;AAClB,QAAM,OAAO,QAAQ;AACrB,MAAI,MAAM,EACR,QAAO,IAAK,MAAM,KAAM;WACf,MAAM,EACf,QAAO,IAAK,MAAM,KAAM;MAExB,QAAO,IAAI;AAGb,MAAI,IAAI,KAAK,IAAI,KAAK,IAAI,GACxB,QAAO;;AAIX;AACA,QAAO"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stryke/unique-id",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.45",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A package containing various helper functions to generate unique identifier strings",
|
|
6
6
|
"repository": {
|
|
@@ -32,5 +32,5 @@
|
|
|
32
32
|
"types": "./dist/index.d.cts",
|
|
33
33
|
"devDependencies": { "tsdown": "^0.17.2" },
|
|
34
34
|
"publishConfig": { "access": "public" },
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "a31823cb064cdedba8187c7a25956f1863d035c8"
|
|
36
36
|
}
|