fantomid 1.0.1 → 1.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.
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Generates an JavaScript-friendly unique ID.
3
+ *
4
+ * ```js
5
+ * import fantomId from "fantomid";
6
+ * fantomid(); //=> 5895736349537828
7
+ * ```
8
+ *
9
+ * @returns A random integer.
10
+ */
11
+ declare function fantomid(): number;
12
+
13
+ export { fantomid as default };
@@ -8,4 +8,6 @@
8
8
  *
9
9
  * @returns A random integer.
10
10
  */
11
- export default function fantomid(): number;
11
+ declare function fantomid(): number;
12
+
13
+ export { fantomid as default };
package/dist/index.js ADDED
@@ -0,0 +1,27 @@
1
+ import { random } from '@lukeed/csprng';
2
+
3
+ const EPOCH = /* @__PURE__ */ new Date("2024-09-08");
4
+ function fantomid() {
5
+ const TIMESTAMP_LENGTH = 36;
6
+ const RANDOM_BITS_LENGTH = 17;
7
+ const TIMESTAMP_PRECISION_DIVIDER = 100;
8
+ const bits = new Array(TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH);
9
+ const timestamp = Math.round(
10
+ // Use a more recent base date instead of the Unix Epoch to
11
+ // increase the amount of time we can represent in the timestamp.
12
+ (/* @__PURE__ */ new Date() - EPOCH) / // Decrease the precision of the timestamp to
13
+ // increase the amount of bits we can fit in a key.
14
+ TIMESTAMP_PRECISION_DIVIDER
15
+ );
16
+ for (let i = 0; i < TIMESTAMP_LENGTH; i++) {
17
+ bits[TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH - 1 - i] = timestamp >> i & 1;
18
+ }
19
+ const randomBytes = random(Math.ceil(RANDOM_BITS_LENGTH / 8));
20
+ for (let i = 0; i < RANDOM_BITS_LENGTH; i++) {
21
+ bits[RANDOM_BITS_LENGTH - 1 - i] = randomBytes[Math.floor(i / 8)] >> i % 8 & 1;
22
+ }
23
+ return Number("0b" + bits.reduce((id, bit) => id + bit, String()));
24
+ }
25
+
26
+ export { fantomid as default };
27
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
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 = new 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,IAAI,KAAK,CAAC,gBAAgB,GAAG,kBAAkB,CAAC;AAC/D,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,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 ADDED
@@ -0,0 +1,27 @@
1
+ import { random } from '@lukeed/csprng';
2
+
3
+ const EPOCH = /* @__PURE__ */ new Date("2024-09-08");
4
+ function fantomid() {
5
+ const TIMESTAMP_LENGTH = 36;
6
+ const RANDOM_BITS_LENGTH = 17;
7
+ const TIMESTAMP_PRECISION_DIVIDER = 100;
8
+ const bits = new Array(TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH);
9
+ const timestamp = Math.round(
10
+ // Use a more recent base date instead of the Unix Epoch to
11
+ // increase the amount of time we can represent in the timestamp.
12
+ (/* @__PURE__ */ new Date() - EPOCH) / // Decrease the precision of the timestamp to
13
+ // increase the amount of bits we can fit in a key.
14
+ TIMESTAMP_PRECISION_DIVIDER
15
+ );
16
+ for (let i = 0; i < TIMESTAMP_LENGTH; i++) {
17
+ bits[TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH - 1 - i] = timestamp >> i & 1;
18
+ }
19
+ const randomBytes = random(Math.ceil(RANDOM_BITS_LENGTH / 8));
20
+ for (let i = 0; i < RANDOM_BITS_LENGTH; i++) {
21
+ bits[RANDOM_BITS_LENGTH - 1 - i] = randomBytes[Math.floor(i / 8)] >> i % 8 & 1;
22
+ }
23
+ return Number("0b" + bits.reduce((id, bit) => id + bit, String()));
24
+ }
25
+
26
+ export { fantomid as default };
27
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +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 = new 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,IAAI,KAAK,CAAC,gBAAgB,GAAG,kBAAkB,CAAC;AAC/D,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,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.1",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
5
  "description": "A tiny, universal ID generator compatible with JavaScript integers",
6
6
  "repository": "th3rius/fantomid",
package/.husky/pre-commit DELETED
@@ -1,2 +0,0 @@
1
- prettier $(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g') -uw
2
- git update-index --again
package/src/fantomid.js DELETED
@@ -1,37 +0,0 @@
1
- import { random } from "@lukeed/csprng";
2
-
3
- const EPOCH = new Date("2024-09-08");
4
-
5
- export function fantomid() {
6
- const TIMESTAMP_LENGTH = 36;
7
- const RANDOM_BITS_LENGTH = 17;
8
- const TIMESTAMP_PRECISION_DIVIDER = 100;
9
-
10
- const bits = new Array(TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH);
11
-
12
- // Generates a timestamp to be part of the key. This is
13
- // inspired by how UUID v7 works: the idea is that this
14
- // will guarantee keys are generated evenly across time.
15
- const timestamp = Math.round(
16
- // Use a more recent base date instead of the Unix Epoch to
17
- // increase the amount of time we can represent in the timestamp.
18
- (new Date() - EPOCH) /
19
- // Decrease the precision of the timestamp to
20
- // increase the amount of bits we can fit in a key.
21
- TIMESTAMP_PRECISION_DIVIDER,
22
- );
23
- for (let i = 0; i < TIMESTAMP_LENGTH; i++) {
24
- bits[TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH - 1 - i] = (timestamp >> i) & 1;
25
- }
26
-
27
- // Generates a randomized part of the key. If multiple machines generate a
28
- // key at the exact same time (limited by the precision of the timestamp),
29
- // we are ensured to have a chance of collision of `1` in `2 ** RANDOM_BITS_LENGTH`.
30
- const randomBytes = random(Math.ceil(RANDOM_BITS_LENGTH / 8));
31
- for (let i = 0; i < RANDOM_BITS_LENGTH; i++) {
32
- bits[RANDOM_BITS_LENGTH - 1 - i] =
33
- (randomBytes[Math.floor(i / 8)] >> i % 8) & 1;
34
- }
35
-
36
- return Number("0b" + bits.reduce((id, bit) => id + bit, String()));
37
- }
@@ -1,22 +0,0 @@
1
- import { test, mock, before, it } from "node:test";
2
- import assert from "assert/strict";
3
-
4
- test("fantomid", () => {
5
- let randomMock = mock.fn();
6
- let fantomid;
7
-
8
- before(async () => {
9
- mock.module("@lukeed/csprng", { namedExports: { random: randomMock } });
10
- ({ fantomid } = await import("./fantomid.js"));
11
- });
12
-
13
- it("should generate an id", () => {
14
- randomMock.mock.mockImplementationOnce(() =>
15
- Buffer.from([0xa2, 0xd2, 0xff, 0x04, 0xc5]),
16
- );
17
- console.log(Buffer.from([0xa2, 0xd2, 0xff, 0x04, 0xc5]));
18
- mock.timers.enable({ now: new Date("2025-11-24T19:13:45.994Z") });
19
- const id = fantomid();
20
- assert.equal(id, 8209108814378532);
21
- });
22
- });
package/src/index.js DELETED
@@ -1,2 +0,0 @@
1
- import { fantomid } from "./fantomid.js";
2
- export default fantomid;