fantomid 1.0.7 → 1.0.9

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.
@@ -1,27 +1,29 @@
1
- import { random } from '@lukeed/csprng';
1
+ 'use strict';
2
+
3
+ var csprng = require('@lukeed/csprng');
2
4
 
3
5
  const EPOCH = /* @__PURE__ */ new Date("2024-09-08");
6
+ const TIMESTAMP_LENGTH = 36;
7
+ const RANDOM_BITS_LENGTH = 17;
8
+ const TIMESTAMP_PRECISION_DIVIDER = 100;
4
9
  function fantomid() {
5
- const TIMESTAMP_LENGTH = 36;
6
- const RANDOM_BITS_LENGTH = 17;
7
- const TIMESTAMP_PRECISION_DIVIDER = 100;
8
10
  const bits = Array(TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH);
9
11
  const timestamp = Math.round(
10
12
  // Use a more recent base date instead of the Unix Epoch to
11
13
  // increase the amount of time we can represent in the timestamp.
12
- (/* @__PURE__ */ new Date() - EPOCH) / // Decrease the precision of the timestamp to
14
+ (Date.now() - EPOCH) / // Decrease the precision of the timestamp to
13
15
  // increase the amount of bits we can fit in a key.
14
16
  TIMESTAMP_PRECISION_DIVIDER
15
17
  );
16
18
  for (let i = 0; i < TIMESTAMP_LENGTH; i++) {
17
19
  bits[TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH - 1 - i] = timestamp >> i & 1;
18
20
  }
19
- const randomBytes = random(Math.ceil(RANDOM_BITS_LENGTH / 8));
21
+ const randomBytes = csprng.random(Math.ceil(RANDOM_BITS_LENGTH / 8));
20
22
  for (let i = 0; i < RANDOM_BITS_LENGTH; i++) {
21
23
  bits[RANDOM_BITS_LENGTH - 1 - i] = randomBytes[Math.floor(i / 8)] >> i % 8 & 1;
22
24
  }
23
25
  return Number("0b" + bits.reduce((id, bit) => id + bit, String()));
24
26
  }
25
27
 
26
- export { fantomid as default };
27
- //# sourceMappingURL=index.js.map
28
+ module.exports = fantomid;
29
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../src/fantomid.js"],"sourcesContent":["import { random } from \"@lukeed/csprng\";\n\nconst EPOCH = new Date(\"2024-09-08\");\nconst TIMESTAMP_LENGTH = 36;\nconst RANDOM_BITS_LENGTH = 17;\nconst TIMESTAMP_PRECISION_DIVIDER = 100;\n\nexport function fantomid() {\n const bits = Array(TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH);\n\n // Generates a timestamp to be part of the key. This is\n // inspired by how UUID v7 works: the idea is that this\n // will guarantee keys are generated evenly across time.\n const timestamp = Math.round(\n // Use a more recent base date instead of the Unix Epoch to\n // increase the amount of time we can represent in the timestamp.\n (Date.now() - EPOCH) /\n // Decrease the precision of the timestamp to\n // increase the amount of bits we can fit in a key.\n TIMESTAMP_PRECISION_DIVIDER,\n );\n for (let i = 0; i < TIMESTAMP_LENGTH; i++) {\n bits[TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH - 1 - i] = (timestamp >> i) & 1;\n }\n\n // Generates a randomized part of the key. If multiple machines generate a\n // key at the exact same time (limited by the precision of the timestamp),\n // we are ensured to have a chance of collision of `1` in `2 ** RANDOM_BITS_LENGTH`.\n const randomBytes = random(Math.ceil(RANDOM_BITS_LENGTH / 8));\n for (let i = 0; i < RANDOM_BITS_LENGTH; i++) {\n bits[RANDOM_BITS_LENGTH - 1 - i] =\n (randomBytes[Math.floor(i / 8)] >> i % 8) & 1;\n }\n\n return Number(\"0b\" + bits.reduce((id, bit) => id + bit, String()));\n}\n"],"names":["random"],"mappings":";;;;AACA,MAAM,KAAK,mBAAmB,IAAI,IAAI,CAAC,YAAY,CAAC;AACpD,MAAM,gBAAgB,GAAG,EAAE;AAC3B,MAAM,kBAAkB,GAAG,EAAE;AAC7B,MAAM,2BAA2B,GAAG,GAAG;AAChC,SAAS,QAAQ,GAAG;AAC3B,EAAE,MAAM,IAAI,GAAG,KAAK,CAAC,gBAAgB,GAAG,kBAAkB,CAAC;AAC3D,EAAE,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK;AAC9B;AACA;AACA,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK;AACvB;AACA,IAAI;AACJ,GAAG;AACH,EAAE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,gBAAgB,EAAE,CAAC,EAAE,EAAE;AAC7C,IAAI,IAAI,CAAC,gBAAgB,GAAG,kBAAkB,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,SAAS,IAAI,CAAC,GAAG,CAAC;AAC5E,EAAE;AACF,EAAE,MAAM,WAAW,GAAGA,aAAM,CAAC,IAAI,CAAC,IAAI,CAAC,kBAAkB,GAAG,CAAC,CAAC,CAAC;AAC/D,EAAE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,kBAAkB,EAAE,CAAC,EAAE,EAAE;AAC/C,IAAI,IAAI,CAAC,kBAAkB,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;AAClF,EAAE;AACF,EAAE,OAAO,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,GAAG,KAAK,EAAE,GAAG,GAAG,EAAE,MAAM,EAAE,CAAC,CAAC;AACpE;;;;"}
package/dist/index.mjs CHANGED
@@ -1,15 +1,15 @@
1
1
  import { random } from '@lukeed/csprng';
2
2
 
3
3
  const EPOCH = /* @__PURE__ */ new Date("2024-09-08");
4
+ const TIMESTAMP_LENGTH = 36;
5
+ const RANDOM_BITS_LENGTH = 17;
6
+ const TIMESTAMP_PRECISION_DIVIDER = 100;
4
7
  function fantomid() {
5
- const TIMESTAMP_LENGTH = 36;
6
- const RANDOM_BITS_LENGTH = 17;
7
- const TIMESTAMP_PRECISION_DIVIDER = 100;
8
8
  const bits = Array(TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH);
9
9
  const timestamp = Math.round(
10
10
  // Use a more recent base date instead of the Unix Epoch to
11
11
  // increase the amount of time we can represent in the timestamp.
12
- (/* @__PURE__ */ new Date() - EPOCH) / // Decrease the precision of the timestamp to
12
+ (Date.now() - EPOCH) / // Decrease the precision of the timestamp to
13
13
  // increase the amount of bits we can fit in a key.
14
14
  TIMESTAMP_PRECISION_DIVIDER
15
15
  );
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/fantomid.js"],"sourcesContent":["import { random } from \"@lukeed/csprng\";\n\nconst EPOCH = new Date(\"2024-09-08\");\n\nexport function fantomid() {\n const TIMESTAMP_LENGTH = 36;\n const RANDOM_BITS_LENGTH = 17;\n const TIMESTAMP_PRECISION_DIVIDER = 100;\n\n const bits = Array(TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH);\n\n // Generates a timestamp to be part of the key. This is\n // inspired by how UUID v7 works: the idea is that this\n // will guarantee keys are generated evenly across time.\n const timestamp = Math.round(\n // Use a more recent base date instead of the Unix Epoch to\n // increase the amount of time we can represent in the timestamp.\n (new Date() - EPOCH) /\n // Decrease the precision of the timestamp to\n // increase the amount of bits we can fit in a key.\n TIMESTAMP_PRECISION_DIVIDER,\n );\n for (let i = 0; i < TIMESTAMP_LENGTH; i++) {\n bits[TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH - 1 - i] = (timestamp >> i) & 1;\n }\n\n // Generates a randomized part of the key. If multiple machines generate a\n // key at the exact same time (limited by the precision of the timestamp),\n // we are ensured to have a chance of collision of `1` in `2 ** RANDOM_BITS_LENGTH`.\n const randomBytes = random(Math.ceil(RANDOM_BITS_LENGTH / 8));\n for (let i = 0; i < RANDOM_BITS_LENGTH; i++) {\n bits[RANDOM_BITS_LENGTH - 1 - i] =\n (randomBytes[Math.floor(i / 8)] >> i % 8) & 1;\n }\n\n return Number(\"0b\" + bits.reduce((id, bit) => id + bit, String()));\n}\n"],"names":[],"mappings":";;AACA,MAAM,K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EPOCH = new Date(\"2024-09-08\");\nconst TIMESTAMP_LENGTH = 36;\nconst RANDOM_BITS_LENGTH = 17;\nconst TIMESTAMP_PRECISION_DIVIDER = 100;\n\nexport function fantomid() {\n const bits = Array(TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH);\n\n // Generates a timestamp to be part of the key. This is\n // inspired by how UUID v7 works: the idea is that this\n // will guarantee keys are generated evenly across time.\n const timestamp = Math.round(\n // Use a more recent base date instead of the Unix Epoch to\n // increase the amount of time we can represent in the timestamp.\n (Date.now() - EPOCH) /\n // Decrease the precision of the timestamp to\n // increase the amount of bits we can fit in a key.\n TIMESTAMP_PRECISION_DIVIDER,\n );\n for (let i = 0; i < TIMESTAMP_LENGTH; i++) {\n bits[TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH - 1 - i] = (timestamp >> i) & 1;\n }\n\n // Generates a randomized part of the key. If multiple machines generate a\n // key at the exact same time (limited by the precision of the timestamp),\n // we are ensured to have a chance of collision of `1` in `2 ** RANDOM_BITS_LENGTH`.\n const randomBytes = random(Math.ceil(RANDOM_BITS_LENGTH / 8));\n for (let i = 0; i < RANDOM_BITS_LENGTH; i++) {\n bits[RANDOM_BITS_LENGTH - 1 - i] =\n (randomBytes[Math.floor(i / 8)] >> i % 8) & 1;\n }\n\n return Number(\"0b\" + bits.reduce((id, bit) => id + bit, String()));\n}\n"],"names":[],"mappings":";;AACA,MAAM,KAAK,mBAAmB,IAAI,IAAI,CAAC,YAAY,CAAC;AACpD,MAAM,gBAAgB,GAAG,EAAE;AAC3B,MAAM,kBAAkB,GAAG,EAAE;AAC7B,MAAM,2BAA2B,GAAG,GAAG;AAChC,SAAS,QAAQ,GAAG;AAC3B,EAAE,MAAM,IAAI,GAAG,KAAK,CAAC,gBAAgB,GAAG,kBAAkB,CAAC;AAC3D,EAAE,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK;AAC9B;AACA;AACA,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK;AACvB;AACA,IAAI;AACJ,GAAG;AACH,EAAE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,gBAAgB,EAAE,CAAC,EAAE,EAAE;AAC7C,IAAI,IAAI,CAAC,gBAAgB,GAAG,kBAAkB,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,SAAS,IAAI,CAAC,GAAG,CAAC;AAC5E,EAAE;AACF,EAAE,MAAM,WAAW,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,kBAAkB,GAAG,CAAC,CAAC,CAAC;AAC/D,EAAE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,kBAAkB,EAAE,CAAC,EAAE,EAAE;AAC/C,IAAI,IAAI,CAAC,kBAAkB,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;AAClF,EAAE;AACF,EAAE,OAAO,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,GAAG,KAAK,EAAE,GAAG,GAAG,EAAE,MAAM,EAAE,CAAC,CAAC;AACpE;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fantomid",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "type": "module",
5
5
  "description": "A tiny, universal ID generator compatible with JavaScript integers",
6
6
  "repository": "th3rius/fantomid",
@@ -16,13 +16,13 @@
16
16
  "build": "rimraf dist && pkgroll --sourcemap",
17
17
  "test": "node --test --experimental-test-module-mocks **/*.spec.js"
18
18
  },
19
- "main": "./dist/index.js",
19
+ "main": "./dist/index.cjs",
20
20
  "module": "./dist/index.mjs",
21
- "types": "./dist/index.d.ts",
21
+ "types": "./dist/index.d.cts",
22
22
  "exports": {
23
23
  "require": {
24
- "types": "./dist/index.d.ts",
25
- "default": "./dist/index.js"
24
+ "types": "./dist/index.d.cts",
25
+ "default": "./dist/index.cjs"
26
26
  },
27
27
  "import": {
28
28
  "types": "./dist/index.d.mts",
package/src/fantomid.js CHANGED
@@ -1,12 +1,11 @@
1
1
  import { random } from "@lukeed/csprng";
2
2
 
3
3
  const EPOCH = new Date("2024-09-08");
4
+ const TIMESTAMP_LENGTH = 36;
5
+ const RANDOM_BITS_LENGTH = 17;
6
+ const TIMESTAMP_PRECISION_DIVIDER = 100;
4
7
 
5
8
  export function fantomid() {
6
- const TIMESTAMP_LENGTH = 36;
7
- const RANDOM_BITS_LENGTH = 17;
8
- const TIMESTAMP_PRECISION_DIVIDER = 100;
9
-
10
9
  const bits = Array(TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH);
11
10
 
12
11
  // Generates a timestamp to be part of the key. This is
@@ -15,7 +14,7 @@ export function fantomid() {
15
14
  const timestamp = Math.round(
16
15
  // Use a more recent base date instead of the Unix Epoch to
17
16
  // increase the amount of time we can represent in the timestamp.
18
- (new Date() - EPOCH) /
17
+ (Date.now() - EPOCH) /
19
18
  // Decrease the precision of the timestamp to
20
19
  // increase the amount of bits we can fit in a key.
21
20
  TIMESTAMP_PRECISION_DIVIDER,
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":["../src/fantomid.js"],"sourcesContent":["import { random } from \"@lukeed/csprng\";\n\nconst EPOCH = new Date(\"2024-09-08\");\n\nexport function fantomid() {\n const TIMESTAMP_LENGTH = 36;\n const RANDOM_BITS_LENGTH = 17;\n const TIMESTAMP_PRECISION_DIVIDER = 100;\n\n const bits = Array(TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH);\n\n // Generates a timestamp to be part of the key. This is\n // inspired by how UUID v7 works: the idea is that this\n // will guarantee keys are generated evenly across time.\n const timestamp = Math.round(\n // Use a more recent base date instead of the Unix Epoch to\n // increase the amount of time we can represent in the timestamp.\n (new Date() - EPOCH) /\n // Decrease the precision of the timestamp to\n // increase the amount of bits we can fit in a key.\n TIMESTAMP_PRECISION_DIVIDER,\n );\n for (let i = 0; i < TIMESTAMP_LENGTH; i++) {\n bits[TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH - 1 - i] = (timestamp >> i) & 1;\n }\n\n // Generates a randomized part of the key. If multiple machines generate a\n // key at the exact same time (limited by the precision of the timestamp),\n // we are ensured to have a chance of collision of `1` in `2 ** RANDOM_BITS_LENGTH`.\n const randomBytes = random(Math.ceil(RANDOM_BITS_LENGTH / 8));\n for (let i = 0; i < RANDOM_BITS_LENGTH; i++) {\n bits[RANDOM_BITS_LENGTH - 1 - i] =\n (randomBytes[Math.floor(i / 8)] >> i % 8) & 1;\n }\n\n return Number(\"0b\" + bits.reduce((id, bit) => id + bit, String()));\n}\n"],"names":[],"mappings":";;AACA,MAAM,KAAK,mBAAmB,IAAI,IAAI,CAAC,YAAY,CAAC;AAC7C,SAAS,QAAQ,GAAG;AAC3B,EAAE,MAAM,gBAAgB,GAAG,EAAE;AAC7B,EAAE,MAAM,kBAAkB,GAAG,EAAE;AAC/B,EAAE,MAAM,2BAA2B,GAAG,GAAG;AACzC,EAAE,MAAM,IAAI,GAAG,KAAK,CAAC,gBAAgB,GAAG,kBAAkB,CAAC;AAC3D,EAAE,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK;AAC9B;AACA;AACA,IAAI,iBAAiB,IAAI,IAAI,EAAE,GAAG,KAAK;AACvC;AACA,IAAI;AACJ,GAAG;AACH,EAAE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,gBAAgB,EAAE,CAAC,EAAE,EAAE;AAC7C,IAAI,IAAI,CAAC,gBAAgB,GAAG,kBAAkB,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,SAAS,IAAI,CAAC,GAAG,CAAC;AAC5E,EAAE;AACF,EAAE,MAAM,WAAW,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,kBAAkB,GAAG,CAAC,CAAC,CAAC;AAC/D,EAAE,KAAK,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