simpleflakes 3.0.1 → 3.0.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/README.md +1 -4
- package/package.json +5 -7
- package/lib/simpleflakes.js +0 -64
package/README.md
CHANGED
|
@@ -314,10 +314,7 @@ npm test
|
|
|
314
314
|
# Run with coverage
|
|
315
315
|
npm run test:coverage
|
|
316
316
|
|
|
317
|
-
#
|
|
318
|
-
npm run test:compatibility
|
|
319
|
-
|
|
320
|
-
# Run all CI tests (coverage + compatibility)
|
|
317
|
+
# Run all CI tests (coverage)
|
|
321
318
|
npm run test:ci
|
|
322
319
|
|
|
323
320
|
# Type checking
|
package/package.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "simpleflakes",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.3",
|
|
4
4
|
"description": "Fast, and reliable, distributed 64-bit ID generation, in pure JavaScript, for Node.js.",
|
|
5
5
|
"main": "dist/simpleflakes.js",
|
|
6
6
|
"types": "dist/simpleflakes.d.ts",
|
|
7
7
|
"files": [
|
|
8
8
|
"dist/",
|
|
9
|
-
"lib/",
|
|
10
9
|
"README.md",
|
|
11
10
|
"LICENSE"
|
|
12
11
|
],
|
|
@@ -17,13 +16,12 @@
|
|
|
17
16
|
"postbuild": "echo 'TypeScript compilation complete. Output in ./dist/'",
|
|
18
17
|
"type-check": "tsc --noEmit",
|
|
19
18
|
"test": "npm run build && tape tests/**/*.js | faucet",
|
|
20
|
-
"test:
|
|
21
|
-
"test:
|
|
22
|
-
"test:coverage": "nyc npm test",
|
|
19
|
+
"test:ci": "npm run test:coverage",
|
|
20
|
+
"test:coverage": "npm run build && nyc --instrument --source-map=false tape tests/**/*.js",
|
|
23
21
|
"test:coverage:report": "nyc report --reporter=html && echo '\nHTML coverage report generated in ./coverage/index.html'",
|
|
24
|
-
"test:coverage:ci": "
|
|
22
|
+
"test:coverage:ci": "npm run test:coverage && cat ./coverage/lcov.info",
|
|
25
23
|
"test:coverage:clean": "rimraf coverage .nyc_output",
|
|
26
|
-
"benchmark": "npm run build && node
|
|
24
|
+
"benchmark": "npm run build && node benchmark/run.js",
|
|
27
25
|
"clean": "npm run build:clean && npm run test:coverage:clean"
|
|
28
26
|
},
|
|
29
27
|
"repository": {
|
package/lib/simpleflakes.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
const SIMPLEFLAKE_EPOCH = 946684800000; // Date.UTC(2000, 0, 1) == epoch ms, since 1 Jan 2000 00:00
|
|
2
|
-
const UNSIGNED_23BIT_MAX = 8388607; // (Math.pow(2, 23) - 1) >> 0
|
|
3
|
-
|
|
4
|
-
const SIMPLEFLAKE_TIMESTAMP_LENGTH = 41n;
|
|
5
|
-
const SIMPLEFLAKE_RANDOM_LENGTH = 23n;
|
|
6
|
-
|
|
7
|
-
const SIMPLEFLAKE_RANDOM_SHIFT = 0n;
|
|
8
|
-
const SIMPLEFLAKE_TIMESTAMP_SHIFT = 23n;
|
|
9
|
-
|
|
10
|
-
const CACHE_64_BIT_ZEROS = '0000000000000000000000000000000000000000000000000000000000000000';
|
|
11
|
-
|
|
12
|
-
function simpleflake(ts = Date.now(), randomBits, epoch = SIMPLEFLAKE_EPOCH) {
|
|
13
|
-
return ((BigInt(ts) - BigInt(epoch)) << SIMPLEFLAKE_TIMESTAMP_SHIFT) +
|
|
14
|
-
BigInt(randomBits ?? Math.round(Math.random() * UNSIGNED_23BIT_MAX));
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function binary(value, padding = true) {
|
|
18
|
-
const binValue = BigInt(value).toString(2);
|
|
19
|
-
return padding && binValue.length < 64
|
|
20
|
-
? CACHE_64_BIT_ZEROS.substr(0, 64 - binValue.length) + binValue
|
|
21
|
-
: binValue;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function extractBits(data, shift, length) {
|
|
25
|
-
const shiftN = BigInt(shift);
|
|
26
|
-
const bitmask = ((1n << BigInt(length)) - 1n) << shiftN;
|
|
27
|
-
return (BigInt(data) & bitmask) >> shiftN;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function parseSimpleflake(flake) {
|
|
31
|
-
return new SimpleFlakeStruct(
|
|
32
|
-
// timestamp
|
|
33
|
-
(extractBits(flake, SIMPLEFLAKE_TIMESTAMP_SHIFT, SIMPLEFLAKE_TIMESTAMP_LENGTH)
|
|
34
|
-
+ BigInt(SIMPLEFLAKE_EPOCH)).toString(10),
|
|
35
|
-
// random bits
|
|
36
|
-
extractBits(flake, SIMPLEFLAKE_RANDOM_SHIFT, SIMPLEFLAKE_RANDOM_LENGTH).toString(10)
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function SimpleFlakeStruct(timestamp, randomBits) {
|
|
41
|
-
if (this instanceof SimpleFlakeStruct) {
|
|
42
|
-
if (timestamp == null || randomBits == null) {
|
|
43
|
-
throw new Error('Missing argument for SimpleFlakeStruct.');
|
|
44
|
-
}
|
|
45
|
-
this.timestamp = timestamp;
|
|
46
|
-
this.randomBits = randomBits;
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
return new SimpleFlakeStruct(timestamp, randomBits);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
module.exports = {
|
|
54
|
-
// Enhancements
|
|
55
|
-
SimpleFlakeStruct: SimpleFlakeStruct,
|
|
56
|
-
|
|
57
|
-
// original API
|
|
58
|
-
simpleflakeStruct: SimpleFlakeStruct,
|
|
59
|
-
extractBits: extractBits,
|
|
60
|
-
parseSimpleflake: parseSimpleflake,
|
|
61
|
-
binary: binary,
|
|
62
|
-
SIMPLEFLAKE_EPOCH: SIMPLEFLAKE_EPOCH,
|
|
63
|
-
simpleflake: simpleflake
|
|
64
|
-
};
|