@stryke/unique-id 0.3.2 → 0.3.3
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/dist/cuid.cjs +13 -13
- package/dist/cuid.d.ts +3 -0
- package/dist/cuid.mjs +1 -1
- package/dist/snowflake.cjs +16 -15
- package/dist/snowflake.d.ts +2 -2
- package/dist/snowflake.mjs +1 -1
- package/package.json +4 -3
package/dist/cuid.cjs
CHANGED
|
@@ -6,25 +6,25 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.cuid = cuid;
|
|
7
7
|
var _hash = require("@stryke/hash");
|
|
8
8
|
var _random = require("./random.cjs");
|
|
9
|
-
const
|
|
9
|
+
const l = 476782367,
|
|
10
10
|
t = 36,
|
|
11
11
|
c = 1,
|
|
12
|
-
|
|
13
|
-
function
|
|
12
|
+
g = Math.floor(Math.random() * l) + c;
|
|
13
|
+
function i(o = 4, r = Math.random) {
|
|
14
14
|
let n = "";
|
|
15
|
-
for (; n.length < o;) n += Math.floor(
|
|
15
|
+
for (; n.length < o;) n += Math.floor(r() * t).toString(t);
|
|
16
16
|
return n;
|
|
17
17
|
}
|
|
18
|
-
function
|
|
19
|
-
const
|
|
20
|
-
n = Object.keys(
|
|
21
|
-
|
|
22
|
-
return (0, _hash.hash)(
|
|
18
|
+
function h(o) {
|
|
19
|
+
const r = (o?.globalObj ?? typeof globalThis > "u") && typeof globalThis > "u" ? {} : globalThis,
|
|
20
|
+
n = Object.keys(r).toString(),
|
|
21
|
+
e = n.length > 0 ? n + i(t, Math.random) : i(t, Math.random);
|
|
22
|
+
return (0, _hash.hash)(e).slice(0, Math.max(0, t));
|
|
23
23
|
}
|
|
24
24
|
function cuid() {
|
|
25
25
|
const o = Date.now().toString(t),
|
|
26
|
-
|
|
27
|
-
n =
|
|
28
|
-
|
|
29
|
-
return `${(0, _random.randomLetter)() +
|
|
26
|
+
r = g.toString(t),
|
|
27
|
+
n = i(t, Math.random),
|
|
28
|
+
e = (0, _hash.hash)(`${o + n + r + h()}`);
|
|
29
|
+
return `${(0, _random.randomLetter)() + e.slice(1, Math.min(e.length - 1, t))}`;
|
|
30
30
|
}
|
package/dist/cuid.d.ts
CHANGED
package/dist/cuid.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{hash as
|
|
1
|
+
import{hash as a}from"@stryke/hash";import{randomLetter as s}from"./random";const l=476782367,t=36,c=1,g=Math.floor(Math.random()*l)+c;function i(o=4,r=Math.random){let n="";for(;n.length<o;)n+=Math.floor(r()*t).toString(t);return n}function h(o){const r=(o?.globalObj??typeof globalThis>"u")&&typeof globalThis>"u"?{}:globalThis,n=Object.keys(r).toString(),e=n.length>0?n+i(t,Math.random):i(t,Math.random);return a(e).slice(0,Math.max(0,t))}export function cuid(){const o=Date.now().toString(t),r=g.toString(t),n=i(t,Math.random),e=a(`${o+n+r+h()}`);return`${s()+e.slice(1,Math.min(e.length-1,t))}`}
|
package/dist/snowflake.cjs
CHANGED
|
@@ -9,30 +9,31 @@ exports.isValidSnowflake = isValidSnowflake;
|
|
|
9
9
|
exports.snowflake = snowflake;
|
|
10
10
|
const DEFAULT_SHARD_ID = exports.DEFAULT_SHARD_ID = 1,
|
|
11
11
|
DEFAULT_EPOCH = exports.DEFAULT_EPOCH = Date.UTC(1970, 0, 1).valueOf();
|
|
12
|
-
let
|
|
13
|
-
function
|
|
12
|
+
let s = 1;
|
|
13
|
+
function a(e) {
|
|
14
14
|
const t = "0000000000000000000000000000000000000000000000000000000000000000",
|
|
15
15
|
n = BigInt(e).toString(2);
|
|
16
16
|
return n.length < 64 ? t.slice(0, Math.max(0, 64 - n.length)) + n : n;
|
|
17
17
|
}
|
|
18
|
-
function
|
|
19
|
-
return Number.parseInt(n ?
|
|
18
|
+
function i(e, t, n) {
|
|
19
|
+
return Number.parseInt(n ? a(e).slice(t, t + n) : a(e).slice(Math.max(0, t)), 2);
|
|
20
20
|
}
|
|
21
|
-
function snowflake({
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
function snowflake(e = {}) {
|
|
22
|
+
const {
|
|
23
|
+
shardId: t,
|
|
24
|
+
epoch: n,
|
|
25
|
+
timestamp: r
|
|
26
|
+
} = e ?? {};
|
|
27
|
+
let o = BigInt(r ? r instanceof Date ? r.valueOf() : new Date(r).valueOf() : Date.now()) - BigInt(n ?? DEFAULT_EPOCH) << BigInt(22);
|
|
28
|
+
return o |= BigInt((t ?? 1) % 1024) << BigInt(12), o |= BigInt(s++ % 4096), o.toString();
|
|
28
29
|
}
|
|
29
30
|
function deconstructSnowflake(e) {
|
|
30
|
-
const t =
|
|
31
|
+
const t = a(e);
|
|
31
32
|
return {
|
|
32
33
|
snowflake: e,
|
|
33
|
-
timestamp:
|
|
34
|
-
shard_id:
|
|
35
|
-
sequence:
|
|
34
|
+
timestamp: i(e, 1, 41),
|
|
35
|
+
shard_id: i(e, 42, 10),
|
|
36
|
+
sequence: i(e, 52),
|
|
36
37
|
binary: t
|
|
37
38
|
};
|
|
38
39
|
}
|
package/dist/snowflake.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Options passed to the `generate` function to create a snowflake identifier.
|
|
3
3
|
*/
|
|
4
|
-
export interface
|
|
4
|
+
export interface SnowflakeGeneratorOptions {
|
|
5
5
|
/**
|
|
6
6
|
* The id of the shard running this generator.
|
|
7
7
|
*
|
|
@@ -81,7 +81,7 @@ export declare const DEFAULT_EPOCH: number;
|
|
|
81
81
|
* @param options - The options to use when generating the snowflake
|
|
82
82
|
* @returns A snowflake
|
|
83
83
|
*/
|
|
84
|
-
export declare function snowflake(
|
|
84
|
+
export declare function snowflake(options?: SnowflakeGeneratorOptions): string;
|
|
85
85
|
/**
|
|
86
86
|
* Deconstruct a snowflake to its values using the `epoch`.
|
|
87
87
|
*
|
package/dist/snowflake.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const DEFAULT_SHARD_ID=1,DEFAULT_EPOCH=Date.UTC(1970,0,1).valueOf();let
|
|
1
|
+
export const DEFAULT_SHARD_ID=1,DEFAULT_EPOCH=Date.UTC(1970,0,1).valueOf();let s=1;function a(e){const t="0000000000000000000000000000000000000000000000000000000000000000",n=BigInt(e).toString(2);return n.length<64?t.slice(0,Math.max(0,64-n.length))+n:n}function i(e,t,n){return Number.parseInt(n?a(e).slice(t,t+n):a(e).slice(Math.max(0,t)),2)}export function snowflake(e={}){const{shardId:t,epoch:n,timestamp:r}=e??{};let o=BigInt(r?r instanceof Date?r.valueOf():new Date(r).valueOf():Date.now())-BigInt(n??DEFAULT_EPOCH)<<BigInt(22);return o|=BigInt((t??1)%1024)<<BigInt(12),o|=BigInt(s++%4096),o.toString()}export function deconstructSnowflake(e){const t=a(e);return{snowflake:e,timestamp:i(e,1,41),shard_id:i(e,42,10),sequence:i(e,52),binary:t}}export function isValidSnowflake(e){if(!/^\d{19}$/.test(e))return!1;try{return deconstructSnowflake(e),!0}catch{return!1}}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stryke/unique-id",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A package containing various helper functions to generate unique identifier strings",
|
|
6
6
|
"repository": {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
},
|
|
11
11
|
"private": false,
|
|
12
12
|
"publishConfig": { "access": "public" },
|
|
13
|
-
"dependencies": { "@stryke/hash": "^0.9.
|
|
13
|
+
"dependencies": { "@stryke/hash": "^0.9.7" },
|
|
14
14
|
"devDependencies": {},
|
|
15
15
|
"sideEffects": false,
|
|
16
16
|
"files": ["dist/**/*"],
|
|
@@ -127,5 +127,6 @@
|
|
|
127
127
|
},
|
|
128
128
|
"main": "./dist/index.cjs",
|
|
129
129
|
"module": "./dist/index.mjs",
|
|
130
|
-
"types": "./dist/index.d.ts"
|
|
130
|
+
"types": "./dist/index.d.ts",
|
|
131
|
+
"gitHead": "a5ed5552046d1a8b313d88aed4d8bce7f33caec8"
|
|
131
132
|
}
|