simpleflakes 3.0.1 → 3.0.2

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/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "simpleflakes",
3
- "version": "3.0.1",
3
+ "version": "3.0.2",
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
  ],
@@ -19,7 +18,7 @@
19
18
  "test": "npm run build && tape tests/**/*.js | faucet",
20
19
  "test:compatibility": "npm run build && node test-compatibility.js",
21
20
  "test:ci": "npm run test:coverage && npm run test:compatibility",
22
- "test:coverage": "nyc npm test",
21
+ "test:coverage": "npm run build && nyc --instrument --source-map=false tape tests/**/*.js",
23
22
  "test:coverage:report": "nyc report --reporter=html && echo '\nHTML coverage report generated in ./coverage/index.html'",
24
23
  "test:coverage:ci": "nyc npm test && cat ./coverage/lcov.info",
25
24
  "test:coverage:clean": "rimraf coverage .nyc_output",
@@ -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
- };