From bd8fe29b95ddb0dba0602ab068c41927ede9e2d1 Mon Sep 17 00:00:00 2001 From: Roger Qiu Date: Fri, 15 Oct 2021 18:40:44 +1100 Subject: [PATCH 1/3] Changing to Id derivative of Uint8Array in order to provide operator overloading --- .eslintrc | 2 +- package-lock.json | 12 ++-- src/Id.ts | 35 ++++++++++ src/IdDeterministic.ts | 19 ++--- src/IdRandom.ts | 21 +++--- src/IdSortable.ts | 59 ++++++++-------- src/utils.ts | 86 +++++++++++++++++------ tests/IdDeterministic.test.ts | 40 ++++++++++- tests/IdRandom.test.ts | 35 +++++++++- tests/IdSortable.test.ts | 128 +++++++++++++++++++++++----------- tests/utils.test.ts | 38 ++++++---- tests/utils.ts | 9 ++- 12 files changed, 349 insertions(+), 135 deletions(-) create mode 100644 src/Id.ts diff --git a/.eslintrc b/.eslintrc index 070ec0b..9c4183d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -27,7 +27,6 @@ "no-constant-condition": 0, "no-useless-escape": 0, "no-console": "error", - "eqeqeq": ["error", "smart"], "capitalized-comments": [ "warn", "always", @@ -66,6 +65,7 @@ { "selector": "parameter", "format": ["camelCase"], + "leadingUnderscore": "allowSingleOrDouble", "trailingUnderscore": "allowSingleOrDouble" }, { diff --git a/package-lock.json b/package-lock.json index 48d6bde..5d6765c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -906,9 +906,9 @@ "dev": true }, "@types/node": { - "version": "14.17.21", - "resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.21.tgz", - "integrity": "sha512-zv8ukKci1mrILYiQOwGSV4FpkZhyxQtuFWGya2GujWg+zVAeRQ4qbaMmWp9vb9889CFA8JECH7lkwCL6Ygg8kA==", + "version": "14.17.27", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.27.tgz", + "integrity": "sha512-94+Ahf9IcaDuJTle/2b+wzvjmutxXAEXU6O81JHblYXUg2BDG+dnBy7VxIPHKAyEEDHzCMQydTJuWvrE+Aanzw==", "dev": true }, "@types/prettier": { @@ -3908,9 +3908,9 @@ "dev": true }, "typescript": { - "version": "4.4.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.3.tgz", - "integrity": "sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA==", + "version": "4.4.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.4.tgz", + "integrity": "sha512-DqGhF5IKoBl8WNf8C1gu8q0xZSInh9j1kJJMqT3a94w1JzVaBU4EXOSMrz9yDqMT0xt3selp83fuFMQ0uzv6qA==", "dev": true }, "uglify-js": { diff --git a/src/Id.ts b/src/Id.ts new file mode 100644 index 0000000..ba5aa72 --- /dev/null +++ b/src/Id.ts @@ -0,0 +1,35 @@ +import * as utils from './utils'; + +/** + * IdInternal can be used as a string primitive + * This type hack prevents TS from complaining + * See: https://github.com/microsoft/TypeScript/issues/4538 + */ +type Id = IdInternal & number; + +class IdInternal extends Uint8Array { + public static create(): Id; + public static create(length: number): Id; + public static create(array: ArrayLike | ArrayBufferLike): Id; + public static create( + buffer: ArrayBufferLike, + byteOffset?: number, + length?: number, + ): Id; + public static create(...args: Array): Id { + // @ts-ignore: spreading into Uint8Array constructor + return new IdInternal(...args) as Id; + } + + public [Symbol.toPrimitive](_hint: 'string' | 'number' | 'default'): string { + return utils.toString(this as unknown as Id); + } + + public toString(): string { + return utils.toString(this as unknown as Id); + } +} + +export default IdInternal; + +export type { Id }; diff --git a/src/IdDeterministic.ts b/src/IdDeterministic.ts index 7fb919a..2f1c53c 100644 --- a/src/IdDeterministic.ts +++ b/src/IdDeterministic.ts @@ -1,4 +1,7 @@ +import type { Id } from './Id'; + import { v5 as uuidv5, NIL } from 'uuid'; +import IdInternal from './Id'; /** * This produces deterministic ids based on: @@ -8,7 +11,7 @@ import { v5 as uuidv5, NIL } from 'uuid'; * namespaceUUID is SHA1(NIL UUID + namespace) * ) */ -class IdDeterministic implements IterableIterator { +class IdDeterministic implements IterableIterator { protected namespaceData: Uint8Array; public constructor({ @@ -21,20 +24,20 @@ class IdDeterministic implements IterableIterator { this.namespaceData = namespaceData; } - public get(name?: string): ArrayBuffer { - return this.next(name).value as ArrayBuffer; + public get(name?: string): Id { + return this.next(name).value as Id; } - public next(name: string = ''): IteratorResult { - const idData = new ArrayBuffer(16); - uuidv5(name, this.namespaceData, new Uint8Array(idData)); + public next(name: string = ''): IteratorResult { + const id = IdInternal.create(16); + uuidv5(name, this.namespaceData, id); return { - value: idData, + value: id, done: false, }; } - public [Symbol.iterator](): IterableIterator { + public [Symbol.iterator](): IterableIterator { return this; } } diff --git a/src/IdRandom.ts b/src/IdRandom.ts index b1056a5..0687057 100644 --- a/src/IdRandom.ts +++ b/src/IdRandom.ts @@ -1,7 +1,10 @@ +import type { Id } from './Id'; + import { v4 as uuidv4 } from 'uuid'; import * as utils from './utils'; +import IdInternal from './Id'; -class IdRandom implements IterableIterator { +class IdRandom implements IterableIterator { protected randomSource: (size: number) => Uint8Array; public constructor({ @@ -12,26 +15,26 @@ class IdRandom implements IterableIterator { this.randomSource = randomSource; } - public get(): ArrayBuffer { - return this.next().value as ArrayBuffer; + public get(): Id { + return this.next().value as Id; } - public next(): IteratorResult { - const idData = new ArrayBuffer(16); - // Uuidv4 does mutate the random data + public next(): IteratorResult { + const id = IdInternal.create(16); + // `uuidv4` mutates the random data uuidv4( { rng: () => this.randomSource(16), }, - new Uint8Array(idData), + id, ); return { - value: idData, + value: id, done: false, }; } - public [Symbol.iterator](): IterableIterator { + public [Symbol.iterator](): IterableIterator { return this; } } diff --git a/src/IdSortable.ts b/src/IdSortable.ts index 11239b7..a73672d 100644 --- a/src/IdSortable.ts +++ b/src/IdSortable.ts @@ -1,3 +1,6 @@ +import type { Id } from './Id'; + +import IdInternal from './Id'; import * as utils from './utils'; /** @@ -22,11 +25,12 @@ const msecPrecision = 3; const variantBits = '10'; const versionBits = '0111'; -function extractTs(idBytes: ArrayBuffer): number { +function extractTs(idBytes: Uint8Array): number { // Decode the timestamp from the last id // the timestamp bits is 48 bits or 6 bytes - const idTsBytes = new Uint8Array(idBytes, 0, (unixtsSize + msecSize) / 8); - const idTsBits = utils.bytes2bin(idTsBytes); + // this creates a new zero-copy view + const idTsBytes = idBytes.subarray(0, (unixtsSize + msecSize) / 8); + const idTsBits = utils.bytes2bits(idTsBytes); const unixtsBits = idTsBits.substr(0, unixtsSize); const msecBits = idTsBits.substr(unixtsSize, unixtsSize + msecSize); const unixts = parseInt(unixtsBits, 2); @@ -35,23 +39,21 @@ function extractTs(idBytes: ArrayBuffer): number { return utils.fromFixedPoint([unixts, msec], msecSize, msecPrecision); } -function extractSeq(idBytes: ArrayBuffer): number { - const idSeqBytes = new Uint8Array( - idBytes, +function extractSeq(idBytes: Uint8Array): number { + const idSeqBytes = idBytes.subarray( (unixtsSize + msecSize) / 8, (unixtsSize + msecSize + 4 + seqSize) / 8, ); - const idSeqBits = utils.bytes2bin(idSeqBytes).substr(4, seqSize); + const idSeqBits = utils.bytes2bits(idSeqBytes).substr(4, seqSize); const seq = parseInt(idSeqBits, 2); return seq; } -function extractRand(idBytes: ArrayBuffer): string { - const idRandBytes = new Uint8Array( - idBytes, +function extractRand(idBytes: Uint8Array): string { + const idRandBytes = idBytes.subarray( (unixtsSize + msecSize + 4 + seqSize) / 8, ); - const idRandBits = utils.bytes2bin(idRandBytes).substr(2); + const idRandBits = utils.bytes2bits(idRandBytes).substr(2); return idRandBits; } @@ -63,12 +65,12 @@ function extractRand(idBytes: ArrayBuffer): string { * 12 bits seq enables 4096 ids per millisecond * After 4096, it rolls over */ -class IdSortable implements IterableIterator { +class IdSortable implements IterableIterator { protected randomSource: (size: number) => Uint8Array; protected clock: () => number; protected nodeBits?: string; protected lastTs?: [number, number]; - protected lastId_?: ArrayBuffer; + protected _lastId?: Id; protected seqCounter: number; public constructor({ @@ -77,8 +79,8 @@ class IdSortable implements IterableIterator { timeSource = utils.timeSource, randomSource = utils.randomBytes, }: { - lastId?: ArrayBuffer; - nodeId?: ArrayBuffer; + lastId?: Uint8Array; + nodeId?: Uint8Array; timeSource?: (lastTs?: number) => () => number; randomSource?: (size: number) => Uint8Array; } = {}) { @@ -96,24 +98,24 @@ class IdSortable implements IterableIterator { } } - get lastId(): ArrayBuffer { - if (this.lastId_ == null) { + get lastId(): Id { + if (this._lastId == null) { throw new ReferenceError('lastId has not yet been generated'); } - return this.lastId_; + return this._lastId; } - public get(): ArrayBuffer { - return this.next().value as ArrayBuffer; + public get(): Id { + return this.next().value as Id; } - public next(): IteratorResult { + public next(): IteratorResult { // Clock returns millisecond precision const ts = this.clock() / 10 ** msecPrecision; // Converting to seconds and subseconds const [unixts, msec] = utils.toFixedPoint(ts, msecSize, msecPrecision); - const unixtsBits = utils.dec2bin(unixts, unixtsSize); - const msecBits = utils.dec2bin(msec, msecSize); + const unixtsBits = utils.dec2bits(unixts, unixtsSize); + const msecBits = utils.dec2bits(msec, msecSize); if ( this.lastTs != null && this.lastTs[0] >= unixts && @@ -123,7 +125,7 @@ class IdSortable implements IterableIterator { } else { this.seqCounter = 0; } - const seqBits = utils.dec2bin(this.seqCounter, seqSize); + const seqBits = utils.dec2bits(this.seqCounter, seqSize); // NodeBits can be written to the most significant rand portion let randBits: string; if (this.nodeBits != null) { @@ -137,17 +139,18 @@ class IdSortable implements IterableIterator { } const idBits = unixtsBits + msecBits + versionBits + seqBits + variantBits + randBits; - const idBytes = utils.bin2bytes(idBits); + const idBytes = utils.bits2bytes(idBits); + const id = IdInternal.create(idBytes.buffer); // Save the fixed point timestamp this.lastTs = [unixts, msec]; - this.lastId_ = idBytes.buffer; + this._lastId = id; return { - value: idBytes.buffer, + value: id, done: false, }; } - public [Symbol.iterator](): IterableIterator { + public [Symbol.iterator](): IterableIterator { return this; } } diff --git a/src/utils.ts b/src/utils.ts index b3c9754..6fc54c0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,8 +1,10 @@ import type { Codec } from 'multiformats/bases/base'; +import type { Id } from './Id'; import crypto from 'crypto'; import { performance } from 'perf_hooks'; import { bases } from 'multiformats/basics'; +import IdInternal from './Id'; /** * Gets random bytes as Uint8Array @@ -19,17 +21,16 @@ function randomBits( size: number, ): string { const bytes = randomBytes(Math.ceil(size / 8)); - const bits = [...bytes].map((n) => dec2bin(n, 8)).join(''); + const bits = [...bytes].map((n) => dec2bits(n, 8)).join(''); return bits.substr(0, size); } -function nodeBits(nodeBytes: ArrayBuffer, size: number): string { - const bytes = new Uint8Array( - nodeBytes, +function nodeBits(nodeBytes: Uint8Array, size: number): string { + const bytes = nodeBytes.subarray( 0, Math.min(nodeBytes.byteLength, Math.ceil(size / 8)), ); - const bits = [...bytes].map((n) => dec2bin(n, 8)).join(''); + const bits = [...bytes].map((n) => dec2bits(n, 8)).join(''); return bits.substr(0, size); } @@ -71,8 +72,24 @@ function* take(g: Iterator, l: number): Generator { } } -function toUUID(bytes: ArrayBuffer): string { - const uuidHex = bytes2hex(new Uint8Array(bytes)); +function toString(id: Uint8Array): string { + return String.fromCharCode(...id); + // Const b = Buffer.from(id.buffer, id.byteOffset, id.byteLength); + // return b.toString('binary'); +} + +function fromString(idString: string): Id | undefined { + const id = IdInternal.create(16); + for (let i = 0; i < 16; i++) { + id[i] = idString.charCodeAt(i); + } + return id; + // Const b = Buffer.from(idString, 'binary'); + // return IdInternal.create(b.buffer, b.byteOffset, b.byteLength); +} + +function toUUID(id: Uint8Array): string { + const uuidHex = bytes2hex(id); return [ uuidHex.substr(0, 8), uuidHex.substr(8, 4), @@ -82,12 +99,12 @@ function toUUID(bytes: ArrayBuffer): string { ].join('-'); } -function fromUUID(uuid: string): ArrayBuffer | undefined { +function fromUUID(uuid: string): Id | undefined { const uuidHex = uuid.split('-').join(''); if (uuidHex.length !== 32) { return; } - return hex2bytes(uuidHex).buffer; + return IdInternal.create(hex2bytes(uuidHex).buffer); } type MultibaseFormats = keyof typeof bases; @@ -101,26 +118,47 @@ for (const k in bases) { /** * Encodes an multibase ID string */ -function toMultibase(idBytes: ArrayBuffer, format: MultibaseFormats): string { +function toMultibase(id: Uint8Array, format: MultibaseFormats): string { const codec = bases[format]; - return codec.encode(new Uint8Array(idBytes)); + return codec.encode(id); } /** * Decodes a multibase encoded ID * Do not use this for generic multibase strings */ -function fromMultibase(idString: string): ArrayBuffer | undefined { +function fromMultibase(idString: string): Id | undefined { const prefix = idString[0]; const codec = basesByPrefix[prefix]; if (codec == null) { return; } - const buffer = codec.decode(idString).buffer; + const buffer = codec.decode(idString); if (buffer.byteLength !== 16) { return; } - return buffer; + return IdInternal.create(buffer); +} + +/** + * Encodes as Buffer zero-copy + */ +function toBuffer(id: Uint8Array): Buffer { + return Buffer.from(id.buffer, id.byteOffset, id.byteLength); +} + +/** + * Decodes as Buffer zero-copy + */ +function fromBuffer(idBuffer: Buffer): Id | undefined { + if (idBuffer.byteLength !== 16) { + return; + } + return IdInternal.create( + idBuffer.buffer, + idBuffer.byteOffset, + idBuffer.byteLength, + ); } /** @@ -138,15 +176,15 @@ function hex2bytes(hex: string): Uint8Array { /** * Encodes Uint8Array to bit string */ -function bytes2bin(bytes: Uint8Array): string { - return [...bytes].map((n) => dec2bin(n, 8)).join(''); +function bytes2bits(bytes: Uint8Array): string { + return [...bytes].map((n) => dec2bits(n, 8)).join(''); } /** * Decodes bit string to Uint8Array */ -function bin2bytes(bin: string): Uint8Array { - const numbers = strChunks(bin, 8).map((b) => parseInt(b, 2)); +function bits2bytes(bits: string): Uint8Array { + const numbers = strChunks(bits, 8).map((b) => parseInt(b, 2)); return new Uint8Array(numbers); } @@ -154,7 +192,7 @@ function bin2bytes(bin: string): Uint8Array { * Encodes positive base 10 numbers to bit string * Will output bits in big-endian order */ -function dec2bin(dec: number, size: number): string { +function dec2bits(dec: number, size: number): string { dec %= 2 ** size; // `>>>` coerces dec to unsigned integer return (dec >>> 0).toString(2).padStart(size, '0'); @@ -239,15 +277,19 @@ export { nodeBits, timeSource, take, + toString, + fromString, toUUID, fromUUID, toMultibase, fromMultibase, + toBuffer, + fromBuffer, bytes2hex, hex2bytes, - bytes2bin, - bin2bytes, - dec2bin, + bytes2bits, + bits2bytes, + dec2bits, dec2hex, strChunks, roundPrecise, diff --git a/tests/IdDeterministic.test.ts b/tests/IdDeterministic.test.ts index 297cb5e..ef5e66f 100644 --- a/tests/IdDeterministic.test.ts +++ b/tests/IdDeterministic.test.ts @@ -2,17 +2,24 @@ import IdDeterministic from '@/IdDeterministic'; import * as utils from '@/utils'; describe('IdDeterministic', () => { - test('ids are ArrayBuffer', () => { + test('ids are Uint8Array', () => { const idGen = new IdDeterministic(); const id = idGen.get(); - const idBuf = Buffer.from(id); - expect(idBuf.buffer).toBe(id); + expect(id).toBeInstanceOf(Uint8Array); }); test('ids can be generated', () => { const idGen = new IdDeterministic(); const ids = [...utils.take(idGen, 10)]; expect(ids).toHaveLength(10); }); + test('ids can be encoded and decoded as binary strings', () => { + const idGen = new IdDeterministic(); + const id = idGen.get(); + const encoded = id.toString(); + const id_ = utils.fromString(encoded); + expect(id_).toBeDefined(); + expect(utils.toBuffer(id).equals(utils.toBuffer(id_!))).toBe(true); + }); test('ids can be encoded and decoded with multibase', () => { const idGen = new IdDeterministic(); const id = idGen.get(); @@ -73,4 +80,31 @@ describe('IdDeterministic', () => { const idB2 = Buffer.from(idGen2.get('b')); expect(idB1.equals(idB2)).toBe(true); }); + test('ids can be used as record indexes', () => { + const idGen = new IdDeterministic({ namespace: 'foo' }); + const ids = [ + idGen.get('a'), + idGen.get('b'), + idGen.get('c'), + idGen.get('d'), + ]; + let counter = 0; + const record = {}; + for (const id of ids) { + record[id] = counter; + expect(record[id]).toBe(counter); + counter++; + } + }); + test('ids in strings can be compared for equality', () => { + const idGen = new IdDeterministic({ namespace: 'foo' }); + const id1 = idGen.get('a'); + const id2 = idGen.get('a'); + // Objects will be different + expect(id1 == id2).toBe(false); + // Deterministic ids are the same + expect(id1.toString() == id2.toString()).toBe(true); + expect(id1.toString()).toBe(id2 + ''); + expect(id2.toString()).toBe(String(id1)); + }); }); diff --git a/tests/IdRandom.test.ts b/tests/IdRandom.test.ts index f7936ce..b3d344d 100644 --- a/tests/IdRandom.test.ts +++ b/tests/IdRandom.test.ts @@ -2,17 +2,24 @@ import IdRandom from '@/IdRandom'; import * as utils from '@/utils'; describe('IdRandom', () => { - test('ids are ArrayBuffer', () => { + test('ids are Uint8Array', () => { const idGen = new IdRandom(); const id = idGen.get(); - const idBuf = Buffer.from(id); - expect(idBuf.buffer).toBe(id); + expect(id).toBeInstanceOf(Uint8Array); }); test('ids can be generated', () => { const idGen = new IdRandom(); const ids = [...utils.take(idGen, 10)]; expect(ids).toHaveLength(10); }); + test('ids can be encoded and decoded as binary strings', () => { + const idGen = new IdRandom(); + const id = idGen.get(); + const encoded = id.toString(); + const id_ = utils.fromString(encoded); + expect(id_).toBeDefined(); + expect(utils.toBuffer(id).equals(utils.toBuffer(id_!))).toBe(true); + }); test('ids can be encoded and decoded with multibase', () => { const idGen = new IdRandom(); const id = idGen.get(); @@ -38,4 +45,26 @@ describe('IdRandom', () => { const idSet = new Set(ids); expect(idSet.size).toBe(count); }); + test('ids can be used as record indexes', () => { + const idGen = new IdRandom(); + const ids = [...utils.take(idGen, 10)]; + let counter = 0; + const record = {}; + for (const id of ids) { + record[id] = counter; + expect(record[id]).toBe(counter); + counter++; + } + }); + test('ids in strings can be compared for equality', () => { + const idGen = new IdRandom(); + const id1 = idGen.get(); + const id2 = idGen.get(); + // Objects will be different + expect(id1 == id2).toBe(false); + // Random ids are different + expect(id1.toString() == id2.toString()).toBe(false); + expect(id1.toString()).toBe(id1 + ''); + expect(id2.toString()).toBe(String(id2)); + }); }); diff --git a/tests/IdSortable.test.ts b/tests/IdSortable.test.ts index 9a7966a..9ed4170 100644 --- a/tests/IdSortable.test.ts +++ b/tests/IdSortable.test.ts @@ -1,26 +1,33 @@ import IdSortable, { extractTs, extractSeq, extractRand } from '@/IdSortable'; import * as utils from '@/utils'; -import { sleep } from './utils'; +import { sleep, shuffle } from './utils'; describe('IdSortable', () => { - test('ids are ArrayBuffer', () => { + test('ids are Uint8Array', () => { const idGen = new IdSortable(); const id = idGen.get(); - const idBuf = Buffer.from(id); - expect(idBuf.buffer).toBe(id); + expect(id).toBeInstanceOf(Uint8Array); }); test('ids can be generated', () => { const idGen = new IdSortable(); const ids = [...utils.take(idGen, 10)]; expect(ids).toHaveLength(10); }); + test('ids can be encoded and decoded as binary strings', () => { + const idGen = new IdSortable(); + const id = idGen.get(); + const encoded = id.toString(); + const id_ = utils.fromString(encoded); + expect(id_).toBeDefined(); + expect(utils.toBuffer(id).equals(utils.toBuffer(id_!))).toBe(true); + }); test('ids can be encoded and decoded with multibase', () => { const idGen = new IdSortable(); const id = idGen.get(); const encoded = utils.toMultibase(id, 'base58btc'); const id_ = utils.fromMultibase(encoded); expect(id_).toBeDefined(); - expect(Buffer.from(id).equals(Buffer.from(id_!))).toBe(true); + expect(utils.toBuffer(id).equals(utils.toBuffer(id_!))).toBe(true); }); test('ids can be encoded and decoded with uuid', () => { const idGen = new IdSortable(); @@ -28,38 +35,46 @@ describe('IdSortable', () => { const uuid = utils.toUUID(id); const id_ = utils.fromUUID(uuid); expect(id_).toBeDefined(); - expect(Buffer.from(id).equals(Buffer.from(id_!))).toBe(true); + expect(utils.toBuffer(id).equals(utils.toBuffer(id_!))).toBe(true); }); test('maintains the last id generated', () => { const idGen = new IdSortable(); idGen.get(); idGen.get(); - const id = Buffer.from(idGen.get()); - const id_ = Buffer.from(idGen.lastId); + const id = utils.toBuffer(idGen.get()); + const id_ = utils.toBuffer(idGen.lastId); expect(id.equals(id_)).toBe(true); }); - test('ids are lexically sortable', () => { + test('ids in bytes are lexically sortable', () => { const id = new IdSortable(); - const i1 = Buffer.from(id.get()); - const i2 = Buffer.from(id.get()); - const i3 = Buffer.from(id.get()); + const i1 = utils.toBuffer(id.get()); + const i2 = utils.toBuffer(id.get()); + const i3 = utils.toBuffer(id.get()); const buffers = [i3, i1, i2]; // Comparison is done on the bytes in lexicographic order buffers.sort(Buffer.compare); expect(buffers).toStrictEqual([i1, i2, i3]); }); - test('ids are lexically sortable with time delay', async () => { + test('ids in bytes are lexically sortable with time delay', async () => { const id = new IdSortable(); - const i1 = Buffer.from(id.get()); + const i1 = utils.toBuffer(id.get()); await sleep(250); - const i2 = Buffer.from(id.get()); + const i2 = utils.toBuffer(id.get()); await sleep(500); - const i3 = Buffer.from(id.get()); + const i3 = utils.toBuffer(id.get()); const buffers = [i3, i1, i2]; // Comparison is done on the bytes in lexicographic order buffers.sort(Buffer.compare); expect(buffers).toStrictEqual([i1, i2, i3]); }); + test('encoded id strings are lexically sortable', () => { + const idGen = new IdSortable(); + const idStrings = [...utils.take(idGen, 100)].map((id) => id.toString()); + const idStringsShuffled = idStrings.slice(); + shuffle(idStringsShuffled); + idStringsShuffled.sort(); + expect(idStringsShuffled).toStrictEqual(idStrings); + }); test('encoded uuids are lexically sortable', () => { const id = new IdSortable(); const i1 = utils.toUUID(id.get()); @@ -86,9 +101,9 @@ describe('IdSortable', () => { return () => 0; }, }); - const i1 = Buffer.from(id.get()); - const i2 = Buffer.from(id.get()); - const i3 = Buffer.from(id.get()); + const i1 = utils.toBuffer(id.get()); + const i2 = utils.toBuffer(id.get()); + const i3 = utils.toBuffer(id.get()); // They should not equal expect(i1.equals(i2)).toBe(false); expect(i2.equals(i3)).toBe(false); @@ -96,12 +111,12 @@ describe('IdSortable', () => { // Comparison is done on the bytes in lexicographic order buffers.sort(Buffer.compare); expect(buffers).toStrictEqual([i1, i2, i3]); - expect(extractTs(i1.buffer)).toBe(0); - expect(extractTs(i2.buffer)).toBe(0); - expect(extractTs(i3.buffer)).toBe(0); - expect(extractSeq(i1.buffer)).toBe(0); - expect(extractSeq(i2.buffer)).toBe(1); - expect(extractSeq(i3.buffer)).toBe(2); + expect(extractTs(i1)).toBe(0); + expect(extractTs(i2)).toBe(0); + expect(extractTs(i3)).toBe(0); + expect(extractSeq(i1)).toBe(0); + expect(extractSeq(i2)).toBe(1); + expect(extractSeq(i3)).toBe(2); }); test('ids are monotonic over process restarts', () => { const id = new IdSortable({ @@ -110,39 +125,68 @@ describe('IdSortable', () => { return () => Date.now() + 100000; }, }); - const lastId = Buffer.from(id.get()); + const lastId = utils.toBuffer(id.get()); // Pass a future last id // the default time source should get an older timestamp - const id_ = new IdSortable({ lastId: lastId.buffer }); - const currId = Buffer.from(id_.get()); + const id_ = new IdSortable({ lastId }); + const currId = utils.toBuffer(id_.get()); expect(lastId.equals(currId)).toBe(false); const buffers = [currId, lastId]; buffers.sort(Buffer.compare); expect(buffers).toStrictEqual([lastId, currId]); // Monotonicity is not enforced by seq // but rather the timestamp - expect(extractSeq(lastId.buffer)).toBe(0); - expect(extractSeq(currId.buffer)).toBe(0); + expect(extractSeq(lastId)).toBe(0); + expect(extractSeq(currId)).toBe(0); }); test('ids can have machine id starting from the MSB of rand-section', () => { const nodeId = Buffer.from('abcd', 'utf-8'); - const id = new IdSortable({ - nodeId: nodeId.buffer.slice( - nodeId.byteOffset, - nodeId.byteOffset + nodeId.byteLength, - ), - }); - const i1 = Buffer.from(id.get()); - const i2 = Buffer.from(id.get()); - const i3 = Buffer.from(id.get()); + const id = new IdSortable({ nodeId }); + const i1 = utils.toBuffer(id.get()); + const i2 = utils.toBuffer(id.get()); + const i3 = utils.toBuffer(id.get()); const buffers = [i3, i1, i2]; // Comparison is done on the bytes in lexicographic order buffers.sort(Buffer.compare); expect(buffers).toStrictEqual([i1, i2, i3]); - const randBits = extractRand(i1.buffer); + const randBits = extractRand(i1); expect(randBits.length).toBe(62); - const randBytes = utils.bin2bytes(randBits); + const randBytes = utils.bits2bytes(randBits); const nodeBytes = randBytes.slice(0, 4); - expect(Buffer.from(nodeBytes.buffer).equals(nodeId)).toBe(true); + expect(utils.toBuffer(nodeBytes).equals(nodeId)).toBe(true); + }); + test('ids can be used as record indexes', () => { + const idGen = new IdSortable(); + const ids = [...utils.take(idGen, 10)]; + let counter = 0; + const record = {}; + for (const id of ids) { + record[id] = counter; + expect(record[id]).toBe(counter); + counter++; + } + }); + test('ids can use comparison operators', () => { + const idGen = new IdSortable(); + let idToCompare = idGen.get(); + const ids = [...utils.take(idGen, 100)]; + for (const id of ids) { + expect(idToCompare < id).toBe(true); + expect(idToCompare <= id).toBe(true); + expect(idToCompare > id).toBe(false); + expect(idToCompare >= id).toBe(false); + idToCompare = id; + } + }); + test('ids in strings can be compared for equality', () => { + const idGen = new IdSortable(); + const id1 = idGen.get(); + const id2 = idGen.get(); + // Objects will be different + expect(id1 == id2).toBe(false); + // Sortable ids are different + expect(id1.toString() == id2.toString()).toBe(false); + expect(id1.toString()).toBe(id1 + ''); + expect(id2.toString()).toBe(String(id2)); }); }); diff --git a/tests/utils.test.ts b/tests/utils.test.ts index ce8d5a1..6a098c7 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -23,15 +23,15 @@ describe('utils', () => { }); test('encoding and decoding bytes and bit strings', () => { // 128 size bit string - const bin = + const bits = '00000110000101100010100100001100101001110100010001110000000000001011000111101000111001101100100010110010011110110110110100110011'; const bytes = new Uint8Array([ 6, 22, 41, 12, 167, 68, 112, 0, 177, 232, 230, 200, 178, 123, 109, 51, ]); - expect(utils.bin2bytes(bin)).toStrictEqual(bytes); - expect(utils.bytes2bin(bytes)).toBe(bin); - expect(utils.bytes2bin(utils.bin2bytes(bin))).toBe(bin); - expect(utils.bin2bytes(utils.bytes2bin(bytes))).toStrictEqual(bytes); + expect(utils.bits2bytes(bits)).toStrictEqual(bytes); + expect(utils.bytes2bits(bytes)).toBe(bits); + expect(utils.bytes2bits(utils.bits2bytes(bits))).toBe(bits); + expect(utils.bits2bytes(utils.bytes2bits(bytes))).toStrictEqual(bytes); }); test('encoding and decoding bytes and hex strings', () => { // Uuid hex @@ -45,13 +45,13 @@ describe('utils', () => { expect(utils.hex2bytes(utils.bytes2hex(bytes))).toStrictEqual(bytes); }); test('encoding decimal to bit strings', () => { - expect(utils.dec2bin(0, 8)).toBe('00000000'); - expect(utils.dec2bin(1, 8)).toBe('00000001'); - expect(utils.dec2bin(2, 8)).toBe('00000010'); - expect(utils.dec2bin(255, 8)).toBe('11111111'); + expect(utils.dec2bits(0, 8)).toBe('00000000'); + expect(utils.dec2bits(1, 8)).toBe('00000001'); + expect(utils.dec2bits(2, 8)).toBe('00000010'); + expect(utils.dec2bits(255, 8)).toBe('11111111'); // This should roll back to the beginning - expect(utils.dec2bin(256, 8)).toBe('00000000'); - expect(utils.dec2bin(257, 8)).toBe('00000001'); + expect(utils.dec2bits(256, 8)).toBe('00000000'); + expect(utils.dec2bits(257, 8)).toBe('00000001'); }); test('encoding decimal to hex strings', () => { expect(utils.dec2hex(0, 2)).toBe('00'); @@ -103,7 +103,7 @@ describe('utils', () => { const bytes = new Uint8Array([ 123, 124, 125, 126, 127, 128, 129, 130, 123, 124, 125, 126, 127, 128, 129, 130, - ]).buffer; + ]); const encoded = utils.toMultibase(bytes, 'base58btc'); expect(encoded).toBe('zGFRLUyEszBgw9bRXTeFvu7'); const bytes_ = utils.fromMultibase(encoded); @@ -113,4 +113,18 @@ describe('utils', () => { expect(utils.fromMultibase('aAQ3')).toBeUndefined(); expect(utils.fromMultibase('aAQ333333333333333AAAAAA')).toBeUndefined(); }); + test('buffer encoding and decoding is zero copy', () => { + const bytes = new Uint8Array([ + 123, 124, 125, 126, 127, 128, 129, 130, 123, 124, 125, 126, 127, 128, 129, + 130, + ]); + const buffer = utils.toBuffer(bytes); + buffer[0] = 122; + expect(bytes[0]).toBe(122); + const bytes_ = utils.fromBuffer(buffer); + expect(bytes_).toBeDefined(); + bytes_![0] = 121; + expect(bytes[0]).toBe(121); + expect(buffer[0]).toBe(121); + }); }); diff --git a/tests/utils.ts b/tests/utils.ts index f9d4b34..48160d7 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -2,4 +2,11 @@ async function sleep(ms: number) { return await new Promise((r) => setTimeout(r, ms)); } -export { sleep }; +function shuffle(arr: Array): void { + for (let i = arr.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [arr[i], arr[j]] = [arr[j], arr[i]]; + } +} + +export { sleep, shuffle }; From 3d425fa43c1bd091ea5a373cffb9f9cd0b60d7af Mon Sep 17 00:00:00 2001 From: Roger Qiu Date: Sun, 17 Oct 2021 16:33:56 +1100 Subject: [PATCH 2/3] Updated README to include comparison and string encoding and record index usage --- README.md | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ff81a3f..1eb6628 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,20 @@ const deteGen = new IdDeterministic({ const deteId1 = deteGen.get(); const deteId2 = deteGen.get('bar'); +const deteId3 = deteGen.get('bar'); console.log(utils.toUUID(deteId1)); console.log(utils.toMultibase(deteId2, 'base58btc')); +// Will be cast to string index +const recordOfDeteIds = {}; +recordOfDeteIds[deteId1] = 1; +recordOfDeteIds[deteId2] = 1; +console.log(recordOfDeteIds[deteId1]); + +// Can be checked for equality +console.log(deteId2.toString() === deteId3.toString()); + // Strictly monotonic sortable ids, equivalent to UUIDv7 let lastId = new Uint8Array( @@ -35,7 +45,7 @@ let lastId = new Uint8Array( 0x06, 0x16, 0x3e, 0xf5, 0x6d, 0x8d, 0x70, 0x00, 0x87, 0xc4, 0x65, 0xd5, 0x21, 0x9b, 0x03, 0xd4, ] -).buffer; +); const sortGen = new IdSortable({ lastId }); @@ -44,9 +54,9 @@ const sortId2 = sortGen.get(); const sortId3 = sortGen.get(); const sortIds = [ - Buffer.from(sortId2), - Buffer.from(sortId3), - Buffer.from(sortId1), + utils.toBuffer(sortId2), + utils.toBuffer(sortId3), + utils.toBuffer(sortId1), ]; sortIds.sort(Buffer.compare); @@ -55,6 +65,10 @@ console.log(sortIds); // Save the last id to ensure strict monotonicity across process restarts lastId = sortGen.lastId; + +// Ids can also be compared in order +console.log(sortId1 < sortId2); +console.log(sortId2 < sortId3); ``` ## Installation From 297ebf8acb25d695149caa4ae952756b0ad640d0 Mon Sep 17 00:00:00 2001 From: Roger Qiu Date: Sun, 17 Oct 2021 16:35:07 +1100 Subject: [PATCH 3/3] Updated docs --- docs/assets/js/search.js | 2 +- docs/classes/Id.default.html | 2210 +++++++++++++++++++++ docs/classes/IdDeterministic.default.html | 27 +- docs/classes/IdRandom.default.html | 29 +- docs/classes/IdSortable.default.html | 79 +- docs/index.html | 25 +- docs/modules.html | 4 + docs/modules/Id.html | 168 ++ docs/modules/IdDeterministic.html | 3 + docs/modules/IdRandom.html | 3 + docs/modules/IdSortable.html | 21 +- docs/modules/index.html | 3 + docs/modules/utils.html | 213 +- 13 files changed, 2664 insertions(+), 123 deletions(-) create mode 100644 docs/classes/Id.default.html create mode 100644 docs/modules/Id.html diff --git a/docs/assets/js/search.js b/docs/assets/js/search.js index 1aeb559..05d6a29 100644 --- a/docs/assets/js/search.js +++ b/docs/assets/js/search.js @@ -1 +1 @@ -window.searchData = {"kinds":{"1":"Module","64":"Function","128":"Class","512":"Constructor","1024":"Property","2048":"Method","65536":"Type literal","262144":"Accessor","4194304":"Type alias","16777216":"Reference"},"rows":[{"id":0,"kind":1,"name":"IdDeterministic","url":"modules/IdDeterministic.html","classes":"tsd-kind-module"},{"id":1,"kind":128,"name":"default","url":"classes/IdDeterministic.default.html","classes":"tsd-kind-class tsd-parent-kind-module","parent":"IdDeterministic"},{"id":2,"kind":512,"name":"constructor","url":"classes/IdDeterministic.default.html#constructor","classes":"tsd-kind-constructor tsd-parent-kind-class","parent":"IdDeterministic.default"},{"id":3,"kind":1024,"name":"namespaceData","url":"classes/IdDeterministic.default.html#namespaceData","classes":"tsd-kind-property tsd-parent-kind-class tsd-is-protected","parent":"IdDeterministic.default"},{"id":4,"kind":2048,"name":"get","url":"classes/IdDeterministic.default.html#get","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdDeterministic.default"},{"id":5,"kind":2048,"name":"next","url":"classes/IdDeterministic.default.html#next","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdDeterministic.default"},{"id":6,"kind":2048,"name":"[iterator]","url":"classes/IdDeterministic.default.html#_iterator_","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdDeterministic.default"},{"id":7,"kind":1,"name":"IdRandom","url":"modules/IdRandom.html","classes":"tsd-kind-module"},{"id":8,"kind":128,"name":"default","url":"classes/IdRandom.default.html","classes":"tsd-kind-class tsd-parent-kind-module","parent":"IdRandom"},{"id":9,"kind":512,"name":"constructor","url":"classes/IdRandom.default.html#constructor","classes":"tsd-kind-constructor tsd-parent-kind-class","parent":"IdRandom.default"},{"id":10,"kind":1024,"name":"randomSource","url":"classes/IdRandom.default.html#randomSource","classes":"tsd-kind-property tsd-parent-kind-class tsd-is-protected","parent":"IdRandom.default"},{"id":11,"kind":65536,"name":"__type","url":"classes/IdRandom.default.html#__type","classes":"tsd-kind-type-literal tsd-parent-kind-class","parent":"IdRandom.default"},{"id":12,"kind":2048,"name":"get","url":"classes/IdRandom.default.html#get","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdRandom.default"},{"id":13,"kind":2048,"name":"next","url":"classes/IdRandom.default.html#next","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdRandom.default"},{"id":14,"kind":2048,"name":"[iterator]","url":"classes/IdRandom.default.html#_iterator_","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdRandom.default"},{"id":15,"kind":1,"name":"IdSortable","url":"modules/IdSortable.html","classes":"tsd-kind-module"},{"id":16,"kind":128,"name":"default","url":"classes/IdSortable.default.html","classes":"tsd-kind-class tsd-parent-kind-module","parent":"IdSortable"},{"id":17,"kind":512,"name":"constructor","url":"classes/IdSortable.default.html#constructor","classes":"tsd-kind-constructor tsd-parent-kind-class","parent":"IdSortable.default"},{"id":18,"kind":1024,"name":"randomSource","url":"classes/IdSortable.default.html#randomSource","classes":"tsd-kind-property tsd-parent-kind-class tsd-is-protected","parent":"IdSortable.default"},{"id":19,"kind":65536,"name":"__type","url":"classes/IdSortable.default.html#__type-1","classes":"tsd-kind-type-literal tsd-parent-kind-class","parent":"IdSortable.default"},{"id":20,"kind":1024,"name":"clock","url":"classes/IdSortable.default.html#clock","classes":"tsd-kind-property tsd-parent-kind-class tsd-is-protected","parent":"IdSortable.default"},{"id":21,"kind":65536,"name":"__type","url":"classes/IdSortable.default.html#__type","classes":"tsd-kind-type-literal tsd-parent-kind-class","parent":"IdSortable.default"},{"id":22,"kind":1024,"name":"nodeBits","url":"classes/IdSortable.default.html#nodeBits","classes":"tsd-kind-property tsd-parent-kind-class tsd-is-protected","parent":"IdSortable.default"},{"id":23,"kind":1024,"name":"lastTs","url":"classes/IdSortable.default.html#lastTs","classes":"tsd-kind-property tsd-parent-kind-class tsd-is-protected","parent":"IdSortable.default"},{"id":24,"kind":1024,"name":"lastId_","url":"classes/IdSortable.default.html#lastId_","classes":"tsd-kind-property tsd-parent-kind-class tsd-is-protected","parent":"IdSortable.default"},{"id":25,"kind":1024,"name":"seqCounter","url":"classes/IdSortable.default.html#seqCounter","classes":"tsd-kind-property tsd-parent-kind-class tsd-is-protected","parent":"IdSortable.default"},{"id":26,"kind":262144,"name":"lastId","url":"classes/IdSortable.default.html#lastId","classes":"tsd-kind-get-signature tsd-parent-kind-class","parent":"IdSortable.default"},{"id":27,"kind":2048,"name":"get","url":"classes/IdSortable.default.html#get","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdSortable.default"},{"id":28,"kind":2048,"name":"next","url":"classes/IdSortable.default.html#next","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdSortable.default"},{"id":29,"kind":2048,"name":"[iterator]","url":"classes/IdSortable.default.html#_iterator_","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdSortable.default"},{"id":30,"kind":64,"name":"extractTs","url":"modules/IdSortable.html#extractTs","classes":"tsd-kind-function tsd-parent-kind-module","parent":"IdSortable"},{"id":31,"kind":64,"name":"extractSeq","url":"modules/IdSortable.html#extractSeq","classes":"tsd-kind-function tsd-parent-kind-module","parent":"IdSortable"},{"id":32,"kind":64,"name":"extractRand","url":"modules/IdSortable.html#extractRand","classes":"tsd-kind-function tsd-parent-kind-module","parent":"IdSortable"},{"id":33,"kind":1,"name":"index","url":"modules/index.html","classes":"tsd-kind-module"},{"id":34,"kind":1,"name":"utils","url":"modules/utils.html","classes":"tsd-kind-module"},{"id":35,"kind":64,"name":"randomBytes","url":"modules/utils.html#randomBytes","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":36,"kind":64,"name":"randomBits","url":"modules/utils.html#randomBits","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":37,"kind":64,"name":"nodeBits","url":"modules/utils.html#nodeBits","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":38,"kind":64,"name":"timeSource","url":"modules/utils.html#timeSource","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":39,"kind":64,"name":"take","url":"modules/utils.html#take","classes":"tsd-kind-function tsd-parent-kind-module tsd-has-type-parameter","parent":"utils"},{"id":40,"kind":64,"name":"toUUID","url":"modules/utils.html#toUUID","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":41,"kind":64,"name":"fromUUID","url":"modules/utils.html#fromUUID","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":42,"kind":64,"name":"toMultibase","url":"modules/utils.html#toMultibase","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":43,"kind":64,"name":"fromMultibase","url":"modules/utils.html#fromMultibase","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":44,"kind":64,"name":"bytes2hex","url":"modules/utils.html#bytes2hex","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":45,"kind":64,"name":"hex2bytes","url":"modules/utils.html#hex2bytes","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":46,"kind":64,"name":"bytes2bin","url":"modules/utils.html#bytes2bin","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":47,"kind":64,"name":"bin2bytes","url":"modules/utils.html#bin2bytes","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":48,"kind":64,"name":"dec2bin","url":"modules/utils.html#dec2bin","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":49,"kind":64,"name":"dec2hex","url":"modules/utils.html#dec2hex","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":50,"kind":64,"name":"strChunks","url":"modules/utils.html#strChunks","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":51,"kind":64,"name":"roundPrecise","url":"modules/utils.html#roundPrecise","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":52,"kind":64,"name":"toFixedPoint","url":"modules/utils.html#toFixedPoint","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":53,"kind":64,"name":"fromFixedPoint","url":"modules/utils.html#fromFixedPoint","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":54,"kind":4194304,"name":"MultibaseFormats","url":"modules/utils.html#MultibaseFormats","classes":"tsd-kind-type-alias tsd-parent-kind-module","parent":"utils"},{"id":55,"kind":16777216,"name":"IdRandom","url":"modules/index.html#IdRandom","classes":"tsd-kind-reference tsd-parent-kind-module","parent":"index"},{"id":56,"kind":16777216,"name":"IdDeterministic","url":"modules/index.html#IdDeterministic","classes":"tsd-kind-reference tsd-parent-kind-module","parent":"index"},{"id":57,"kind":16777216,"name":"IdSortable","url":"modules/index.html#IdSortable","classes":"tsd-kind-reference tsd-parent-kind-module","parent":"index"},{"id":58,"kind":16777216,"name":"utils","url":"modules/index.html#utils","classes":"tsd-kind-reference tsd-parent-kind-module","parent":"index"}],"index":{"version":"2.3.9","fields":["name","parent"],"fieldVectors":[["name/0",[0,28.416]],["parent/0",[]],["name/1",[1,28.416]],["parent/1",[0,2.738]],["name/2",[2,28.416]],["parent/2",[3,2.302]],["name/3",[4,36.889]],["parent/3",[3,2.302]],["name/4",[5,28.416]],["parent/4",[3,2.302]],["name/5",[6,28.416]],["parent/5",[3,2.302]],["name/6",[7,28.416]],["parent/6",[3,2.302]],["name/7",[8,28.416]],["parent/7",[]],["name/8",[1,28.416]],["parent/8",[8,2.738]],["name/9",[2,28.416]],["parent/9",[9,2.141]],["name/10",[10,31.781]],["parent/10",[9,2.141]],["name/11",[11,28.416]],["parent/11",[9,2.141]],["name/12",[5,28.416]],["parent/12",[9,2.141]],["name/13",[6,28.416]],["parent/13",[9,2.141]],["name/14",[7,28.416]],["parent/14",[9,2.141]],["name/15",[12,22.225]],["parent/15",[]],["name/16",[1,28.416]],["parent/16",[12,2.141]],["name/17",[2,28.416]],["parent/17",[13,1.437]],["name/18",[10,31.781]],["parent/18",[13,1.437]],["name/19",[11,28.416]],["parent/19",[13,1.437]],["name/20",[14,36.889]],["parent/20",[13,1.437]],["name/21",[11,28.416]],["parent/21",[13,1.437]],["name/22",[15,31.781]],["parent/22",[13,1.437]],["name/23",[16,36.889]],["parent/23",[13,1.437]],["name/24",[17,36.889]],["parent/24",[13,1.437]],["name/25",[18,36.889]],["parent/25",[13,1.437]],["name/26",[19,36.889]],["parent/26",[13,1.437]],["name/27",[5,28.416]],["parent/27",[13,1.437]],["name/28",[6,28.416]],["parent/28",[13,1.437]],["name/29",[7,28.416]],["parent/29",[13,1.437]],["name/30",[20,36.889]],["parent/30",[12,2.141]],["name/31",[21,36.889]],["parent/31",[12,2.141]],["name/32",[22,36.889]],["parent/32",[12,2.141]],["name/33",[23,23.896]],["parent/33",[]],["name/34",[24,9.808]],["parent/34",[]],["name/35",[25,36.889]],["parent/35",[24,0.945]],["name/36",[26,36.889]],["parent/36",[24,0.945]],["name/37",[15,31.781]],["parent/37",[24,0.945]],["name/38",[27,36.889]],["parent/38",[24,0.945]],["name/39",[28,36.889]],["parent/39",[24,0.945]],["name/40",[29,36.889]],["parent/40",[24,0.945]],["name/41",[30,36.889]],["parent/41",[24,0.945]],["name/42",[31,36.889]],["parent/42",[24,0.945]],["name/43",[32,36.889]],["parent/43",[24,0.945]],["name/44",[33,36.889]],["parent/44",[24,0.945]],["name/45",[34,36.889]],["parent/45",[24,0.945]],["name/46",[35,36.889]],["parent/46",[24,0.945]],["name/47",[36,36.889]],["parent/47",[24,0.945]],["name/48",[37,36.889]],["parent/48",[24,0.945]],["name/49",[38,36.889]],["parent/49",[24,0.945]],["name/50",[39,36.889]],["parent/50",[24,0.945]],["name/51",[40,36.889]],["parent/51",[24,0.945]],["name/52",[41,36.889]],["parent/52",[24,0.945]],["name/53",[42,36.889]],["parent/53",[24,0.945]],["name/54",[43,36.889]],["parent/54",[24,0.945]],["name/55",[8,28.416]],["parent/55",[23,2.302]],["name/56",[0,28.416]],["parent/56",[23,2.302]],["name/57",[12,22.225]],["parent/57",[23,2.302]],["name/58",[24,9.808]],["parent/58",[23,2.302]]],"invertedIndex":[["__type",{"_index":11,"name":{"11":{},"19":{},"21":{}},"parent":{}}],["bin2bytes",{"_index":36,"name":{"47":{}},"parent":{}}],["bytes2bin",{"_index":35,"name":{"46":{}},"parent":{}}],["bytes2hex",{"_index":33,"name":{"44":{}},"parent":{}}],["clock",{"_index":14,"name":{"20":{}},"parent":{}}],["constructor",{"_index":2,"name":{"2":{},"9":{},"17":{}},"parent":{}}],["dec2bin",{"_index":37,"name":{"48":{}},"parent":{}}],["dec2hex",{"_index":38,"name":{"49":{}},"parent":{}}],["default",{"_index":1,"name":{"1":{},"8":{},"16":{}},"parent":{}}],["extractrand",{"_index":22,"name":{"32":{}},"parent":{}}],["extractseq",{"_index":21,"name":{"31":{}},"parent":{}}],["extractts",{"_index":20,"name":{"30":{}},"parent":{}}],["fromfixedpoint",{"_index":42,"name":{"53":{}},"parent":{}}],["frommultibase",{"_index":32,"name":{"43":{}},"parent":{}}],["fromuuid",{"_index":30,"name":{"41":{}},"parent":{}}],["get",{"_index":5,"name":{"4":{},"12":{},"27":{}},"parent":{}}],["hex2bytes",{"_index":34,"name":{"45":{}},"parent":{}}],["iddeterministic",{"_index":0,"name":{"0":{},"56":{}},"parent":{"1":{}}}],["iddeterministic.default",{"_index":3,"name":{},"parent":{"2":{},"3":{},"4":{},"5":{},"6":{}}}],["idrandom",{"_index":8,"name":{"7":{},"55":{}},"parent":{"8":{}}}],["idrandom.default",{"_index":9,"name":{},"parent":{"9":{},"10":{},"11":{},"12":{},"13":{},"14":{}}}],["idsortable",{"_index":12,"name":{"15":{},"57":{}},"parent":{"16":{},"30":{},"31":{},"32":{}}}],["idsortable.default",{"_index":13,"name":{},"parent":{"17":{},"18":{},"19":{},"20":{},"21":{},"22":{},"23":{},"24":{},"25":{},"26":{},"27":{},"28":{},"29":{}}}],["index",{"_index":23,"name":{"33":{}},"parent":{"55":{},"56":{},"57":{},"58":{}}}],["iterator",{"_index":7,"name":{"6":{},"14":{},"29":{}},"parent":{}}],["lastid",{"_index":19,"name":{"26":{}},"parent":{}}],["lastid_",{"_index":17,"name":{"24":{}},"parent":{}}],["lastts",{"_index":16,"name":{"23":{}},"parent":{}}],["multibaseformats",{"_index":43,"name":{"54":{}},"parent":{}}],["namespacedata",{"_index":4,"name":{"3":{}},"parent":{}}],["next",{"_index":6,"name":{"5":{},"13":{},"28":{}},"parent":{}}],["nodebits",{"_index":15,"name":{"22":{},"37":{}},"parent":{}}],["randombits",{"_index":26,"name":{"36":{}},"parent":{}}],["randombytes",{"_index":25,"name":{"35":{}},"parent":{}}],["randomsource",{"_index":10,"name":{"10":{},"18":{}},"parent":{}}],["roundprecise",{"_index":40,"name":{"51":{}},"parent":{}}],["seqcounter",{"_index":18,"name":{"25":{}},"parent":{}}],["strchunks",{"_index":39,"name":{"50":{}},"parent":{}}],["take",{"_index":28,"name":{"39":{}},"parent":{}}],["timesource",{"_index":27,"name":{"38":{}},"parent":{}}],["tofixedpoint",{"_index":41,"name":{"52":{}},"parent":{}}],["tomultibase",{"_index":31,"name":{"42":{}},"parent":{}}],["touuid",{"_index":29,"name":{"40":{}},"parent":{}}],["utils",{"_index":24,"name":{"34":{},"58":{}},"parent":{"35":{},"36":{},"37":{},"38":{},"39":{},"40":{},"41":{},"42":{},"43":{},"44":{},"45":{},"46":{},"47":{},"48":{},"49":{},"50":{},"51":{},"52":{},"53":{},"54":{}}}]],"pipeline":[]}} \ No newline at end of file +window.searchData = {"kinds":{"1":"Module","64":"Function","128":"Class","512":"Constructor","1024":"Property","2048":"Method","65536":"Type literal","262144":"Accessor","4194304":"Type alias","16777216":"Reference"},"rows":[{"id":0,"kind":1,"name":"Id","url":"modules/Id.html","classes":"tsd-kind-module"},{"id":1,"kind":128,"name":"default","url":"classes/Id.default.html","classes":"tsd-kind-class tsd-parent-kind-module","parent":"Id"},{"id":2,"kind":2048,"name":"create","url":"classes/Id.default.html#create","classes":"tsd-kind-method tsd-parent-kind-class tsd-is-static","parent":"Id.default"},{"id":3,"kind":512,"name":"constructor","url":"classes/Id.default.html#constructor","classes":"tsd-kind-constructor tsd-parent-kind-class tsd-is-inherited","parent":"Id.default"},{"id":4,"kind":2048,"name":"toString","url":"classes/Id.default.html#toString","classes":"tsd-kind-method tsd-parent-kind-class tsd-is-overwrite","parent":"Id.default"},{"id":5,"kind":2048,"name":"[toPrimitive]","url":"classes/Id.default.html#_toPrimitive_","classes":"tsd-kind-method tsd-parent-kind-class","parent":"Id.default"},{"id":6,"kind":4194304,"name":"Id","url":"modules/Id.html#Id-1","classes":"tsd-kind-type-alias tsd-parent-kind-module","parent":"Id"},{"id":7,"kind":1,"name":"IdDeterministic","url":"modules/IdDeterministic.html","classes":"tsd-kind-module"},{"id":8,"kind":128,"name":"default","url":"classes/IdDeterministic.default.html","classes":"tsd-kind-class tsd-parent-kind-module","parent":"IdDeterministic"},{"id":9,"kind":512,"name":"constructor","url":"classes/IdDeterministic.default.html#constructor","classes":"tsd-kind-constructor tsd-parent-kind-class","parent":"IdDeterministic.default"},{"id":10,"kind":1024,"name":"namespaceData","url":"classes/IdDeterministic.default.html#namespaceData","classes":"tsd-kind-property tsd-parent-kind-class tsd-is-protected","parent":"IdDeterministic.default"},{"id":11,"kind":2048,"name":"get","url":"classes/IdDeterministic.default.html#get","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdDeterministic.default"},{"id":12,"kind":2048,"name":"next","url":"classes/IdDeterministic.default.html#next","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdDeterministic.default"},{"id":13,"kind":2048,"name":"[iterator]","url":"classes/IdDeterministic.default.html#_iterator_","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdDeterministic.default"},{"id":14,"kind":1,"name":"IdRandom","url":"modules/IdRandom.html","classes":"tsd-kind-module"},{"id":15,"kind":128,"name":"default","url":"classes/IdRandom.default.html","classes":"tsd-kind-class tsd-parent-kind-module","parent":"IdRandom"},{"id":16,"kind":512,"name":"constructor","url":"classes/IdRandom.default.html#constructor","classes":"tsd-kind-constructor tsd-parent-kind-class","parent":"IdRandom.default"},{"id":17,"kind":1024,"name":"randomSource","url":"classes/IdRandom.default.html#randomSource","classes":"tsd-kind-property tsd-parent-kind-class tsd-is-protected","parent":"IdRandom.default"},{"id":18,"kind":65536,"name":"__type","url":"classes/IdRandom.default.html#__type","classes":"tsd-kind-type-literal tsd-parent-kind-class","parent":"IdRandom.default"},{"id":19,"kind":2048,"name":"get","url":"classes/IdRandom.default.html#get","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdRandom.default"},{"id":20,"kind":2048,"name":"next","url":"classes/IdRandom.default.html#next","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdRandom.default"},{"id":21,"kind":2048,"name":"[iterator]","url":"classes/IdRandom.default.html#_iterator_","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdRandom.default"},{"id":22,"kind":1,"name":"IdSortable","url":"modules/IdSortable.html","classes":"tsd-kind-module"},{"id":23,"kind":128,"name":"default","url":"classes/IdSortable.default.html","classes":"tsd-kind-class tsd-parent-kind-module","parent":"IdSortable"},{"id":24,"kind":512,"name":"constructor","url":"classes/IdSortable.default.html#constructor","classes":"tsd-kind-constructor tsd-parent-kind-class","parent":"IdSortable.default"},{"id":25,"kind":1024,"name":"randomSource","url":"classes/IdSortable.default.html#randomSource","classes":"tsd-kind-property tsd-parent-kind-class tsd-is-protected","parent":"IdSortable.default"},{"id":26,"kind":65536,"name":"__type","url":"classes/IdSortable.default.html#__type-1","classes":"tsd-kind-type-literal tsd-parent-kind-class","parent":"IdSortable.default"},{"id":27,"kind":1024,"name":"clock","url":"classes/IdSortable.default.html#clock","classes":"tsd-kind-property tsd-parent-kind-class tsd-is-protected","parent":"IdSortable.default"},{"id":28,"kind":65536,"name":"__type","url":"classes/IdSortable.default.html#__type","classes":"tsd-kind-type-literal tsd-parent-kind-class","parent":"IdSortable.default"},{"id":29,"kind":1024,"name":"nodeBits","url":"classes/IdSortable.default.html#nodeBits","classes":"tsd-kind-property tsd-parent-kind-class tsd-is-protected","parent":"IdSortable.default"},{"id":30,"kind":1024,"name":"lastTs","url":"classes/IdSortable.default.html#lastTs","classes":"tsd-kind-property tsd-parent-kind-class tsd-is-protected","parent":"IdSortable.default"},{"id":31,"kind":1024,"name":"_lastId","url":"classes/IdSortable.default.html#_lastId","classes":"tsd-kind-property tsd-parent-kind-class tsd-is-protected","parent":"IdSortable.default"},{"id":32,"kind":1024,"name":"seqCounter","url":"classes/IdSortable.default.html#seqCounter","classes":"tsd-kind-property tsd-parent-kind-class tsd-is-protected","parent":"IdSortable.default"},{"id":33,"kind":262144,"name":"lastId","url":"classes/IdSortable.default.html#lastId","classes":"tsd-kind-get-signature tsd-parent-kind-class","parent":"IdSortable.default"},{"id":34,"kind":2048,"name":"get","url":"classes/IdSortable.default.html#get","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdSortable.default"},{"id":35,"kind":2048,"name":"next","url":"classes/IdSortable.default.html#next","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdSortable.default"},{"id":36,"kind":2048,"name":"[iterator]","url":"classes/IdSortable.default.html#_iterator_","classes":"tsd-kind-method tsd-parent-kind-class","parent":"IdSortable.default"},{"id":37,"kind":64,"name":"extractTs","url":"modules/IdSortable.html#extractTs","classes":"tsd-kind-function tsd-parent-kind-module","parent":"IdSortable"},{"id":38,"kind":64,"name":"extractSeq","url":"modules/IdSortable.html#extractSeq","classes":"tsd-kind-function tsd-parent-kind-module","parent":"IdSortable"},{"id":39,"kind":64,"name":"extractRand","url":"modules/IdSortable.html#extractRand","classes":"tsd-kind-function tsd-parent-kind-module","parent":"IdSortable"},{"id":40,"kind":1,"name":"index","url":"modules/index.html","classes":"tsd-kind-module"},{"id":41,"kind":1,"name":"utils","url":"modules/utils.html","classes":"tsd-kind-module"},{"id":42,"kind":64,"name":"randomBytes","url":"modules/utils.html#randomBytes","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":43,"kind":64,"name":"randomBits","url":"modules/utils.html#randomBits","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":44,"kind":64,"name":"nodeBits","url":"modules/utils.html#nodeBits","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":45,"kind":64,"name":"timeSource","url":"modules/utils.html#timeSource","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":46,"kind":64,"name":"take","url":"modules/utils.html#take","classes":"tsd-kind-function tsd-parent-kind-module tsd-has-type-parameter","parent":"utils"},{"id":47,"kind":64,"name":"toString","url":"modules/utils.html#toString","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":48,"kind":64,"name":"fromString","url":"modules/utils.html#fromString","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":49,"kind":64,"name":"toUUID","url":"modules/utils.html#toUUID","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":50,"kind":64,"name":"fromUUID","url":"modules/utils.html#fromUUID","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":51,"kind":64,"name":"toMultibase","url":"modules/utils.html#toMultibase","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":52,"kind":64,"name":"fromMultibase","url":"modules/utils.html#fromMultibase","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":53,"kind":64,"name":"toBuffer","url":"modules/utils.html#toBuffer","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":54,"kind":64,"name":"fromBuffer","url":"modules/utils.html#fromBuffer","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":55,"kind":64,"name":"bytes2hex","url":"modules/utils.html#bytes2hex","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":56,"kind":64,"name":"hex2bytes","url":"modules/utils.html#hex2bytes","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":57,"kind":64,"name":"bytes2bits","url":"modules/utils.html#bytes2bits","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":58,"kind":64,"name":"bits2bytes","url":"modules/utils.html#bits2bytes","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":59,"kind":64,"name":"dec2bits","url":"modules/utils.html#dec2bits","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":60,"kind":64,"name":"dec2hex","url":"modules/utils.html#dec2hex","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":61,"kind":64,"name":"strChunks","url":"modules/utils.html#strChunks","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":62,"kind":64,"name":"roundPrecise","url":"modules/utils.html#roundPrecise","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":63,"kind":64,"name":"toFixedPoint","url":"modules/utils.html#toFixedPoint","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":64,"kind":64,"name":"fromFixedPoint","url":"modules/utils.html#fromFixedPoint","classes":"tsd-kind-function tsd-parent-kind-module","parent":"utils"},{"id":65,"kind":4194304,"name":"MultibaseFormats","url":"modules/utils.html#MultibaseFormats","classes":"tsd-kind-type-alias tsd-parent-kind-module","parent":"utils"},{"id":66,"kind":16777216,"name":"IdRandom","url":"modules/index.html#IdRandom","classes":"tsd-kind-reference tsd-parent-kind-module","parent":"index"},{"id":67,"kind":16777216,"name":"IdDeterministic","url":"modules/index.html#IdDeterministic","classes":"tsd-kind-reference tsd-parent-kind-module","parent":"index"},{"id":68,"kind":16777216,"name":"IdSortable","url":"modules/index.html#IdSortable","classes":"tsd-kind-reference tsd-parent-kind-module","parent":"index"},{"id":69,"kind":16777216,"name":"utils","url":"modules/index.html#utils","classes":"tsd-kind-reference tsd-parent-kind-module","parent":"index"}],"index":{"version":"2.3.9","fields":["name","parent"],"fieldVectors":[["name/0",[0,27.586]],["parent/0",[]],["name/1",[1,27.586]],["parent/1",[0,2.657]],["name/2",[2,38.572]],["parent/2",[3,2.657]],["name/3",[4,27.586]],["parent/3",[3,2.657]],["name/4",[5,33.464]],["parent/4",[3,2.657]],["name/5",[6,38.572]],["parent/5",[3,2.657]],["name/6",[0,27.586]],["parent/6",[0,2.657]],["name/7",[7,30.099]],["parent/7",[]],["name/8",[1,27.586]],["parent/8",[7,2.899]],["name/9",[4,27.586]],["parent/9",[8,2.463]],["name/10",[9,38.572]],["parent/10",[8,2.463]],["name/11",[10,30.099]],["parent/11",[8,2.463]],["name/12",[11,30.099]],["parent/12",[8,2.463]],["name/13",[12,30.099]],["parent/13",[8,2.463]],["name/14",[13,30.099]],["parent/14",[]],["name/15",[1,27.586]],["parent/15",[13,2.899]],["name/16",[4,27.586]],["parent/16",[14,2.303]],["name/17",[15,33.464]],["parent/17",[14,2.303]],["name/18",[16,30.099]],["parent/18",[14,2.303]],["name/19",[10,30.099]],["parent/19",[14,2.303]],["name/20",[11,30.099]],["parent/20",[14,2.303]],["name/21",[12,30.099]],["parent/21",[14,2.303]],["name/22",[17,23.909]],["parent/22",[]],["name/23",[1,27.586]],["parent/23",[17,2.303]],["name/24",[4,27.586]],["parent/24",[18,1.599]],["name/25",[15,33.464]],["parent/25",[18,1.599]],["name/26",[16,30.099]],["parent/26",[18,1.599]],["name/27",[19,38.572]],["parent/27",[18,1.599]],["name/28",[16,30.099]],["parent/28",[18,1.599]],["name/29",[20,33.464]],["parent/29",[18,1.599]],["name/30",[21,38.572]],["parent/30",[18,1.599]],["name/31",[22,38.572]],["parent/31",[18,1.599]],["name/32",[23,38.572]],["parent/32",[18,1.599]],["name/33",[24,38.572]],["parent/33",[18,1.599]],["name/34",[10,30.099]],["parent/34",[18,1.599]],["name/35",[11,30.099]],["parent/35",[18,1.599]],["name/36",[12,30.099]],["parent/36",[18,1.599]],["name/37",[25,38.572]],["parent/37",[17,2.303]],["name/38",[26,38.572]],["parent/38",[17,2.303]],["name/39",[27,38.572]],["parent/39",[17,2.303]],["name/40",[28,25.579]],["parent/40",[]],["name/41",[29,9.855]],["parent/41",[]],["name/42",[30,38.572]],["parent/42",[29,0.949]],["name/43",[31,38.572]],["parent/43",[29,0.949]],["name/44",[20,33.464]],["parent/44",[29,0.949]],["name/45",[32,38.572]],["parent/45",[29,0.949]],["name/46",[33,38.572]],["parent/46",[29,0.949]],["name/47",[5,33.464]],["parent/47",[29,0.949]],["name/48",[34,38.572]],["parent/48",[29,0.949]],["name/49",[35,38.572]],["parent/49",[29,0.949]],["name/50",[36,38.572]],["parent/50",[29,0.949]],["name/51",[37,38.572]],["parent/51",[29,0.949]],["name/52",[38,38.572]],["parent/52",[29,0.949]],["name/53",[39,38.572]],["parent/53",[29,0.949]],["name/54",[40,38.572]],["parent/54",[29,0.949]],["name/55",[41,38.572]],["parent/55",[29,0.949]],["name/56",[42,38.572]],["parent/56",[29,0.949]],["name/57",[43,38.572]],["parent/57",[29,0.949]],["name/58",[44,38.572]],["parent/58",[29,0.949]],["name/59",[45,38.572]],["parent/59",[29,0.949]],["name/60",[46,38.572]],["parent/60",[29,0.949]],["name/61",[47,38.572]],["parent/61",[29,0.949]],["name/62",[48,38.572]],["parent/62",[29,0.949]],["name/63",[49,38.572]],["parent/63",[29,0.949]],["name/64",[50,38.572]],["parent/64",[29,0.949]],["name/65",[51,38.572]],["parent/65",[29,0.949]],["name/66",[13,30.099]],["parent/66",[28,2.463]],["name/67",[7,30.099]],["parent/67",[28,2.463]],["name/68",[17,23.909]],["parent/68",[28,2.463]],["name/69",[29,9.855]],["parent/69",[28,2.463]]],"invertedIndex":[["__type",{"_index":16,"name":{"18":{},"26":{},"28":{}},"parent":{}}],["_lastid",{"_index":22,"name":{"31":{}},"parent":{}}],["bits2bytes",{"_index":44,"name":{"58":{}},"parent":{}}],["bytes2bits",{"_index":43,"name":{"57":{}},"parent":{}}],["bytes2hex",{"_index":41,"name":{"55":{}},"parent":{}}],["clock",{"_index":19,"name":{"27":{}},"parent":{}}],["constructor",{"_index":4,"name":{"3":{},"9":{},"16":{},"24":{}},"parent":{}}],["create",{"_index":2,"name":{"2":{}},"parent":{}}],["dec2bits",{"_index":45,"name":{"59":{}},"parent":{}}],["dec2hex",{"_index":46,"name":{"60":{}},"parent":{}}],["default",{"_index":1,"name":{"1":{},"8":{},"15":{},"23":{}},"parent":{}}],["extractrand",{"_index":27,"name":{"39":{}},"parent":{}}],["extractseq",{"_index":26,"name":{"38":{}},"parent":{}}],["extractts",{"_index":25,"name":{"37":{}},"parent":{}}],["frombuffer",{"_index":40,"name":{"54":{}},"parent":{}}],["fromfixedpoint",{"_index":50,"name":{"64":{}},"parent":{}}],["frommultibase",{"_index":38,"name":{"52":{}},"parent":{}}],["fromstring",{"_index":34,"name":{"48":{}},"parent":{}}],["fromuuid",{"_index":36,"name":{"50":{}},"parent":{}}],["get",{"_index":10,"name":{"11":{},"19":{},"34":{}},"parent":{}}],["hex2bytes",{"_index":42,"name":{"56":{}},"parent":{}}],["id",{"_index":0,"name":{"0":{},"6":{}},"parent":{"1":{},"6":{}}}],["id.default",{"_index":3,"name":{},"parent":{"2":{},"3":{},"4":{},"5":{}}}],["iddeterministic",{"_index":7,"name":{"7":{},"67":{}},"parent":{"8":{}}}],["iddeterministic.default",{"_index":8,"name":{},"parent":{"9":{},"10":{},"11":{},"12":{},"13":{}}}],["idrandom",{"_index":13,"name":{"14":{},"66":{}},"parent":{"15":{}}}],["idrandom.default",{"_index":14,"name":{},"parent":{"16":{},"17":{},"18":{},"19":{},"20":{},"21":{}}}],["idsortable",{"_index":17,"name":{"22":{},"68":{}},"parent":{"23":{},"37":{},"38":{},"39":{}}}],["idsortable.default",{"_index":18,"name":{},"parent":{"24":{},"25":{},"26":{},"27":{},"28":{},"29":{},"30":{},"31":{},"32":{},"33":{},"34":{},"35":{},"36":{}}}],["index",{"_index":28,"name":{"40":{}},"parent":{"66":{},"67":{},"68":{},"69":{}}}],["iterator",{"_index":12,"name":{"13":{},"21":{},"36":{}},"parent":{}}],["lastid",{"_index":24,"name":{"33":{}},"parent":{}}],["lastts",{"_index":21,"name":{"30":{}},"parent":{}}],["multibaseformats",{"_index":51,"name":{"65":{}},"parent":{}}],["namespacedata",{"_index":9,"name":{"10":{}},"parent":{}}],["next",{"_index":11,"name":{"12":{},"20":{},"35":{}},"parent":{}}],["nodebits",{"_index":20,"name":{"29":{},"44":{}},"parent":{}}],["randombits",{"_index":31,"name":{"43":{}},"parent":{}}],["randombytes",{"_index":30,"name":{"42":{}},"parent":{}}],["randomsource",{"_index":15,"name":{"17":{},"25":{}},"parent":{}}],["roundprecise",{"_index":48,"name":{"62":{}},"parent":{}}],["seqcounter",{"_index":23,"name":{"32":{}},"parent":{}}],["strchunks",{"_index":47,"name":{"61":{}},"parent":{}}],["take",{"_index":33,"name":{"46":{}},"parent":{}}],["timesource",{"_index":32,"name":{"45":{}},"parent":{}}],["tobuffer",{"_index":39,"name":{"53":{}},"parent":{}}],["tofixedpoint",{"_index":49,"name":{"63":{}},"parent":{}}],["tomultibase",{"_index":37,"name":{"51":{}},"parent":{}}],["toprimitive",{"_index":6,"name":{"5":{}},"parent":{}}],["tostring",{"_index":5,"name":{"4":{},"47":{}},"parent":{}}],["touuid",{"_index":35,"name":{"49":{}},"parent":{}}],["utils",{"_index":29,"name":{"41":{},"69":{}},"parent":{"42":{},"43":{},"44":{},"45":{},"46":{},"47":{},"48":{},"49":{},"50":{},"51":{},"52":{},"53":{},"54":{},"55":{},"56":{},"57":{},"58":{},"59":{},"60":{},"61":{},"62":{},"63":{},"64":{},"65":{}}}]],"pipeline":[]}} \ No newline at end of file diff --git a/docs/classes/Id.default.html b/docs/classes/Id.default.html new file mode 100644 index 0000000..bac633f --- /dev/null +++ b/docs/classes/Id.default.html @@ -0,0 +1,2210 @@ + + + + + + default | @matrixai/id + + + + + + +
+
+
+
+ +
+
+ Options +
+
+ All +
    +
  • Public
  • +
  • Public/Protected
  • +
  • All
  • +
+
+ + + + +
+
+ Menu +
+
+
+
+
+
+ +

Class default

+
+
+
+
+
+
+
+

Hierarchy

+
    +
  • + Uint8Array +
      +
    • + default +
    • +
    +
  • +
+
+
+

Index

+
+ +
+
+
+

Constructors

+
+ +

constructor

+
    +
  • new default(length: number): default
  • +
  • new default(array: ArrayLike<number> | ArrayBufferLike): default
  • +
  • new default(buffer: ArrayBufferLike, byteOffset?: number, length?: number): default
  • +
  • new default(elements: Iterable<number>): default
  • +
  • new default(): default
  • +
+
    +
  • + +

    Parameters

    +
      +
    • +
      length: number
      +
    • +
    +

    Returns default

    +
  • +
  • + +

    Parameters

    +
      +
    • +
      array: ArrayLike<number> | ArrayBufferLike
      +
    • +
    +

    Returns default

    +
  • +
  • + +

    Parameters

    +
      +
    • +
      buffer: ArrayBufferLike
      +
    • +
    • +
      Optional byteOffset: number
      +
    • +
    • +
      Optional length: number
      +
    • +
    +

    Returns default

    +
  • +
  • + +

    Parameters

    +
      +
    • +
      elements: Iterable<number>
      +
    • +
    +

    Returns default

    +
  • +
  • + +

    Returns default

    +
  • +
+
+
+
+

Properties

+
+ +

Readonly BYTES_PER_ELEMENT

+
BYTES_PER_ELEMENT: number
+ +
+
+

The size in bytes of each element in the array.

+
+
+
+
+ +

Readonly [toStringTag]

+
[toStringTag]: "Uint8Array"
+ +
+
+ +

Readonly buffer

+
buffer: ArrayBufferLike
+ +
+
+

The ArrayBuffer instance referenced by the array.

+
+
+
+
+ +

Readonly byteLength

+
byteLength: number
+ +
+
+

The length in bytes of the array.

+
+
+
+
+ +

Readonly byteOffset

+
byteOffset: number
+ +
+
+

The offset in bytes of the array.

+
+
+
+
+ +

Readonly length

+
length: number
+ +
+
+

The length of the array.

+
+
+
+
+ +

Static Readonly BYTES_PER_ELEMENT

+
BYTES_PER_ELEMENT: number
+ +
+
+

The size in bytes of each element in the array.

+
+
+
+
+
+

Methods

+
+ +

[iterator]

+
    +
  • [iterator](): IterableIterator<number>
  • +
+
    +
  • + +

    Returns IterableIterator<number>

    +
  • +
+
+
+ +

[toPrimitive]

+
    +
  • [toPrimitive](_hint: "string" | "number" | "default"): string
  • +
+
    +
  • + +

    Parameters

    +
      +
    • +
      _hint: "string" | "number" | "default"
      +
    • +
    +

    Returns string

    +
  • +
+
+
+ +

copyWithin

+
    +
  • copyWithin(target: number, start: number, end?: number): default
  • +
+
    +
  • + +
    +
    +

    Returns the this object after copying a section of the array identified by start and end + to the same array starting at position target

    +
    +
    +

    Parameters

    +
      +
    • +
      target: number
      +
      +

      If target is negative, it is treated as length+target where length is the + length of the array.

      +
      +
    • +
    • +
      start: number
      +
      +

      If start is negative, it is treated as length+start. If end is negative, it + is treated as length+end.

      +
      +
    • +
    • +
      Optional end: number
      +
      +

      If not specified, length of the this object is used as its default value.

      +
      +
    • +
    +

    Returns default

    +
  • +
+
+
+ +

entries

+
    +
  • entries(): IterableIterator<[number, number]>
  • +
+
    +
  • + +
    +
    +

    Returns an array of key, value pairs for every entry in the array

    +
    +
    +

    Returns IterableIterator<[number, number]>

    +
  • +
+
+
+ +

every

+
    +
  • every(predicate: (value: number, index: number, array: Uint8Array) => unknown, thisArg?: any): boolean
  • +
+
    +
  • + +
    +
    +

    Determines whether all the members of an array satisfy the specified test.

    +
    +
    +

    Parameters

    +
      +
    • +
      predicate: (value: number, index: number, array: Uint8Array) => unknown
      +
      +

      A function that accepts up to three arguments. The every method calls + the predicate function for each element in the array until the predicate returns a value + which is coercible to the Boolean value false, or until the end of the array.

      +
      +
        +
      • +
          +
        • (value: number, index: number, array: Uint8Array): unknown
        • +
        +
          +
        • +

          Parameters

          +
            +
          • +
            value: number
            +
          • +
          • +
            index: number
            +
          • +
          • +
            array: Uint8Array
            +
          • +
          +

          Returns unknown

          +
        • +
        +
      • +
      +
    • +
    • +
      Optional thisArg: any
      +
      +

      An object to which the this keyword can refer in the predicate function. + If thisArg is omitted, undefined is used as the this value.

      +
      +
    • +
    +

    Returns boolean

    +
  • +
+
+
+ +

fill

+
    +
  • fill(value: number, start?: number, end?: number): default
  • +
+
    +
  • + +
    +
    +

    Changes all array elements from start to end index to a static value and returns the modified array

    +
    +
    +

    Parameters

    +
      +
    • +
      value: number
      +
      +

      value to fill array section with

      +
      +
    • +
    • +
      Optional start: number
      +
      +

      index to start filling the array at. If start is negative, it is treated as + length+start where length is the length of the array.

      +
      +
    • +
    • +
      Optional end: number
      +
      +

      index to stop filling the array at. If end is negative, it is treated as + length+end.

      +
      +
    • +
    +

    Returns default

    +
  • +
+
+
+ +

filter

+
    +
  • filter(predicate: (value: number, index: number, array: Uint8Array) => any, thisArg?: any): Uint8Array
  • +
+
    +
  • + +
    +
    +

    Returns the elements of an array that meet the condition specified in a callback function.

    +
    +
    +

    Parameters

    +
      +
    • +
      predicate: (value: number, index: number, array: Uint8Array) => any
      +
      +

      A function that accepts up to three arguments. The filter method calls + the predicate function one time for each element in the array.

      +
      +
        +
      • +
          +
        • (value: number, index: number, array: Uint8Array): any
        • +
        +
          +
        • +

          Parameters

          +
            +
          • +
            value: number
            +
          • +
          • +
            index: number
            +
          • +
          • +
            array: Uint8Array
            +
          • +
          +

          Returns any

          +
        • +
        +
      • +
      +
    • +
    • +
      Optional thisArg: any
      +
      +

      An object to which the this keyword can refer in the predicate function. + If thisArg is omitted, undefined is used as the this value.

      +
      +
    • +
    +

    Returns Uint8Array

    +
  • +
+
+
+ +

find

+
    +
  • find(predicate: (value: number, index: number, obj: Uint8Array) => boolean, thisArg?: any): undefined | number
  • +
+
    +
  • + +
    +
    +

    Returns the value of the first element in the array where predicate is true, and undefined + otherwise.

    +
    +
    +

    Parameters

    +
      +
    • +
      predicate: (value: number, index: number, obj: Uint8Array) => boolean
      +
      +

      find calls predicate once for each element of the array, in ascending + order, until it finds one where predicate returns true. If such an element is found, find + immediately returns that element value. Otherwise, find returns undefined.

      +
      +
        +
      • +
          +
        • (value: number, index: number, obj: Uint8Array): boolean
        • +
        +
          +
        • +

          Parameters

          +
            +
          • +
            value: number
            +
          • +
          • +
            index: number
            +
          • +
          • +
            obj: Uint8Array
            +
          • +
          +

          Returns boolean

          +
        • +
        +
      • +
      +
    • +
    • +
      Optional thisArg: any
      +
      +

      If provided, it will be used as the this value for each invocation of + predicate. If it is not provided, undefined is used instead.

      +
      +
    • +
    +

    Returns undefined | number

    +
  • +
+
+
+ +

findIndex

+
    +
  • findIndex(predicate: (value: number, index: number, obj: Uint8Array) => boolean, thisArg?: any): number
  • +
+
    +
  • + +
    +
    +

    Returns the index of the first element in the array where predicate is true, and -1 + otherwise.

    +
    +
    +

    Parameters

    +
      +
    • +
      predicate: (value: number, index: number, obj: Uint8Array) => boolean
      +
      +

      find calls predicate once for each element of the array, in ascending + order, until it finds one where predicate returns true. If such an element is found, + findIndex immediately returns that element index. Otherwise, findIndex returns -1.

      +
      +
        +
      • +
          +
        • (value: number, index: number, obj: Uint8Array): boolean
        • +
        +
          +
        • +

          Parameters

          +
            +
          • +
            value: number
            +
          • +
          • +
            index: number
            +
          • +
          • +
            obj: Uint8Array
            +
          • +
          +

          Returns boolean

          +
        • +
        +
      • +
      +
    • +
    • +
      Optional thisArg: any
      +
      +

      If provided, it will be used as the this value for each invocation of + predicate. If it is not provided, undefined is used instead.

      +
      +
    • +
    +

    Returns number

    +
  • +
+
+
+ +

forEach

+
    +
  • forEach(callbackfn: (value: number, index: number, array: Uint8Array) => void, thisArg?: any): void
  • +
+
    +
  • + +
    +
    +

    Performs the specified action for each element in an array.

    +
    +
    +

    Parameters

    +
      +
    • +
      callbackfn: (value: number, index: number, array: Uint8Array) => void
      +
      +

      A function that accepts up to three arguments. forEach calls the + callbackfn function one time for each element in the array.

      +
      +
        +
      • +
          +
        • (value: number, index: number, array: Uint8Array): void
        • +
        +
          +
        • +

          Parameters

          +
            +
          • +
            value: number
            +
          • +
          • +
            index: number
            +
          • +
          • +
            array: Uint8Array
            +
          • +
          +

          Returns void

          +
        • +
        +
      • +
      +
    • +
    • +
      Optional thisArg: any
      +
      +

      An object to which the this keyword can refer in the callbackfn function. + If thisArg is omitted, undefined is used as the this value.

      +
      +
    • +
    +

    Returns void

    +
  • +
+
+
+ +

includes

+
    +
  • includes(searchElement: number, fromIndex?: number): boolean
  • +
+
    +
  • + +
    +
    +

    Determines whether an array includes a certain element, returning true or false as appropriate.

    +
    +
    +

    Parameters

    +
      +
    • +
      searchElement: number
      +
      +

      The element to search for.

      +
      +
    • +
    • +
      Optional fromIndex: number
      +
      +

      The position in this array at which to begin searching for searchElement.

      +
      +
    • +
    +

    Returns boolean

    +
  • +
+
+
+ +

indexOf

+
    +
  • indexOf(searchElement: number, fromIndex?: number): number
  • +
+
    +
  • + +
    +
    +

    Returns the index of the first occurrence of a value in an array.

    +
    +
    +

    Parameters

    +
      +
    • +
      searchElement: number
      +
      +

      The value to locate in the array.

      +
      +
    • +
    • +
      Optional fromIndex: number
      +
      +

      The array index at which to begin the search. If fromIndex is omitted, the + search starts at index 0.

      +
      +
    • +
    +

    Returns number

    +
  • +
+
+
+ +

join

+
    +
  • join(separator?: string): string
  • +
+
    +
  • + +
    +
    +

    Adds all the elements of an array separated by the specified separator string.

    +
    +
    +

    Parameters

    +
      +
    • +
      Optional separator: string
      +
      +

      A string used to separate one element of an array from the next in the + resulting String. If omitted, the array elements are separated with a comma.

      +
      +
    • +
    +

    Returns string

    +
  • +
+
+
+ +

keys

+
    +
  • keys(): IterableIterator<number>
  • +
+
    +
  • + +
    +
    +

    Returns an list of keys in the array

    +
    +
    +

    Returns IterableIterator<number>

    +
  • +
+
+
+ +

lastIndexOf

+
    +
  • lastIndexOf(searchElement: number, fromIndex?: number): number
  • +
+
    +
  • + +
    +
    +

    Returns the index of the last occurrence of a value in an array.

    +
    +
    +

    Parameters

    +
      +
    • +
      searchElement: number
      +
      +

      The value to locate in the array.

      +
      +
    • +
    • +
      Optional fromIndex: number
      +
      +

      The array index at which to begin the search. If fromIndex is omitted, the + search starts at index 0.

      +
      +
    • +
    +

    Returns number

    +
  • +
+
+
+ +

map

+
    +
  • map(callbackfn: (value: number, index: number, array: Uint8Array) => number, thisArg?: any): Uint8Array
  • +
+
    +
  • + +
    +
    +

    Calls a defined callback function on each element of an array, and returns an array that + contains the results.

    +
    +
    +

    Parameters

    +
      +
    • +
      callbackfn: (value: number, index: number, array: Uint8Array) => number
      +
      +

      A function that accepts up to three arguments. The map method calls the + callbackfn function one time for each element in the array.

      +
      +
        +
      • +
          +
        • (value: number, index: number, array: Uint8Array): number
        • +
        +
          +
        • +

          Parameters

          +
            +
          • +
            value: number
            +
          • +
          • +
            index: number
            +
          • +
          • +
            array: Uint8Array
            +
          • +
          +

          Returns number

          +
        • +
        +
      • +
      +
    • +
    • +
      Optional thisArg: any
      +
      +

      An object to which the this keyword can refer in the callbackfn function. + If thisArg is omitted, undefined is used as the this value.

      +
      +
    • +
    +

    Returns Uint8Array

    +
  • +
+
+
+ +

reduce

+
    +
  • reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number): number
  • +
  • reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number, initialValue: number): number
  • +
  • reduce<U>(callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: Uint8Array) => U, initialValue: U): U
  • +
+
    +
  • + +
    +
    +

    Calls the specified callback function for all the elements in an array. The return value of + the callback function is the accumulated result, and is provided as an argument in the next + call to the callback function.

    +
    +
    +

    Parameters

    +
      +
    • +
      callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number
      +
      +

      A function that accepts up to four arguments. The reduce method calls the + callbackfn function one time for each element in the array.

      +
      +
        +
      • +
          +
        • (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array): number
        • +
        +
          +
        • +

          Parameters

          +
            +
          • +
            previousValue: number
            +
          • +
          • +
            currentValue: number
            +
          • +
          • +
            currentIndex: number
            +
          • +
          • +
            array: Uint8Array
            +
          • +
          +

          Returns number

          +
        • +
        +
      • +
      +
    • +
    +

    Returns number

    +
  • +
  • + +

    Parameters

    +
      +
    • +
      callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number
      +
        +
      • +
          +
        • (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array): number
        • +
        +
          +
        • +

          Parameters

          +
            +
          • +
            previousValue: number
            +
          • +
          • +
            currentValue: number
            +
          • +
          • +
            currentIndex: number
            +
          • +
          • +
            array: Uint8Array
            +
          • +
          +

          Returns number

          +
        • +
        +
      • +
      +
    • +
    • +
      initialValue: number
      +
    • +
    +

    Returns number

    +
  • +
  • + +
    +
    +

    Calls the specified callback function for all the elements in an array. The return value of + the callback function is the accumulated result, and is provided as an argument in the next + call to the callback function.

    +
    +
    +

    Type parameters

    +
      +
    • +

      U

      +
    • +
    +

    Parameters

    +
      +
    • +
      callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: Uint8Array) => U
      +
      +

      A function that accepts up to four arguments. The reduce method calls the + callbackfn function one time for each element in the array.

      +
      +
        +
      • +
          +
        • (previousValue: U, currentValue: number, currentIndex: number, array: Uint8Array): U
        • +
        +
          +
        • +

          Parameters

          +
            +
          • +
            previousValue: U
            +
          • +
          • +
            currentValue: number
            +
          • +
          • +
            currentIndex: number
            +
          • +
          • +
            array: Uint8Array
            +
          • +
          +

          Returns U

          +
        • +
        +
      • +
      +
    • +
    • +
      initialValue: U
      +
      +

      If initialValue is specified, it is used as the initial value to start + the accumulation. The first call to the callbackfn function provides this value as an argument + instead of an array value.

      +
      +
    • +
    +

    Returns U

    +
  • +
+
+
+ +

reduceRight

+
    +
  • reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number): number
  • +
  • reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number, initialValue: number): number
  • +
  • reduceRight<U>(callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: Uint8Array) => U, initialValue: U): U
  • +
+
    +
  • + +
    +
    +

    Calls the specified callback function for all the elements in an array, in descending order. + The return value of the callback function is the accumulated result, and is provided as an + argument in the next call to the callback function.

    +
    +
    +

    Parameters

    +
      +
    • +
      callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number
      +
      +

      A function that accepts up to four arguments. The reduceRight method calls + the callbackfn function one time for each element in the array.

      +
      +
        +
      • +
          +
        • (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array): number
        • +
        +
          +
        • +

          Parameters

          +
            +
          • +
            previousValue: number
            +
          • +
          • +
            currentValue: number
            +
          • +
          • +
            currentIndex: number
            +
          • +
          • +
            array: Uint8Array
            +
          • +
          +

          Returns number

          +
        • +
        +
      • +
      +
    • +
    +

    Returns number

    +
  • +
  • + +

    Parameters

    +
      +
    • +
      callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number
      +
        +
      • +
          +
        • (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array): number
        • +
        +
          +
        • +

          Parameters

          +
            +
          • +
            previousValue: number
            +
          • +
          • +
            currentValue: number
            +
          • +
          • +
            currentIndex: number
            +
          • +
          • +
            array: Uint8Array
            +
          • +
          +

          Returns number

          +
        • +
        +
      • +
      +
    • +
    • +
      initialValue: number
      +
    • +
    +

    Returns number

    +
  • +
  • + +
    +
    +

    Calls the specified callback function for all the elements in an array, in descending order. + The return value of the callback function is the accumulated result, and is provided as an + argument in the next call to the callback function.

    +
    +
    +

    Type parameters

    +
      +
    • +

      U

      +
    • +
    +

    Parameters

    +
      +
    • +
      callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: Uint8Array) => U
      +
      +

      A function that accepts up to four arguments. The reduceRight method calls + the callbackfn function one time for each element in the array.

      +
      +
        +
      • +
          +
        • (previousValue: U, currentValue: number, currentIndex: number, array: Uint8Array): U
        • +
        +
          +
        • +

          Parameters

          +
            +
          • +
            previousValue: U
            +
          • +
          • +
            currentValue: number
            +
          • +
          • +
            currentIndex: number
            +
          • +
          • +
            array: Uint8Array
            +
          • +
          +

          Returns U

          +
        • +
        +
      • +
      +
    • +
    • +
      initialValue: U
      +
      +

      If initialValue is specified, it is used as the initial value to start + the accumulation. The first call to the callbackfn function provides this value as an argument + instead of an array value.

      +
      +
    • +
    +

    Returns U

    +
  • +
+
+
+ +

reverse

+
    +
  • reverse(): Uint8Array
  • +
+
    +
  • + +
    +
    +

    Reverses the elements in an Array.

    +
    +
    +

    Returns Uint8Array

    +
  • +
+
+
+ +

set

+
    +
  • set(array: ArrayLike<number>, offset?: number): void
  • +
+
    +
  • + +
    +
    +

    Sets a value or an array of values.

    +
    +
    +

    Parameters

    +
      +
    • +
      array: ArrayLike<number>
      +
      +

      A typed or untyped array of values to set.

      +
      +
    • +
    • +
      Optional offset: number
      +
      +

      The index in the current array at which the values are to be written.

      +
      +
    • +
    +

    Returns void

    +
  • +
+
+
+ +

slice

+
    +
  • slice(start?: number, end?: number): Uint8Array
  • +
+
    +
  • + +
    +
    +

    Returns a section of an array.

    +
    +
    +

    Parameters

    +
      +
    • +
      Optional start: number
      +
      +

      The beginning of the specified portion of the array.

      +
      +
    • +
    • +
      Optional end: number
      +
      +

      The end of the specified portion of the array. This is exclusive of the element at the index 'end'.

      +
      +
    • +
    +

    Returns Uint8Array

    +
  • +
+
+
+ +

some

+
    +
  • some(predicate: (value: number, index: number, array: Uint8Array) => unknown, thisArg?: any): boolean
  • +
+
    +
  • + +
    +
    +

    Determines whether the specified callback function returns true for any element of an array.

    +
    +
    +

    Parameters

    +
      +
    • +
      predicate: (value: number, index: number, array: Uint8Array) => unknown
      +
      +

      A function that accepts up to three arguments. The some method calls + the predicate function for each element in the array until the predicate returns a value + which is coercible to the Boolean value true, or until the end of the array.

      +
      +
        +
      • +
          +
        • (value: number, index: number, array: Uint8Array): unknown
        • +
        +
          +
        • +

          Parameters

          +
            +
          • +
            value: number
            +
          • +
          • +
            index: number
            +
          • +
          • +
            array: Uint8Array
            +
          • +
          +

          Returns unknown

          +
        • +
        +
      • +
      +
    • +
    • +
      Optional thisArg: any
      +
      +

      An object to which the this keyword can refer in the predicate function. + If thisArg is omitted, undefined is used as the this value.

      +
      +
    • +
    +

    Returns boolean

    +
  • +
+
+
+ +

sort

+
    +
  • sort(compareFn?: (a: number, b: number) => number): default
  • +
+
    +
  • + +
    +
    +

    Sorts an array.

    +
    +
    +

    Parameters

    +
      +
    • +
      Optional compareFn: (a: number, b: number) => number
      +
      +

      Function used to determine the order of the elements. It is expected to return + a negative value if first argument is less than second argument, zero if they're equal and a positive + value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.

      +
      [11,2,22,1].sort((a, b) => a - b)
      +
      +
      +
        +
      • +
          +
        • (a: number, b: number): number
        • +
        +
          +
        • +

          Parameters

          +
            +
          • +
            a: number
            +
          • +
          • +
            b: number
            +
          • +
          +

          Returns number

          +
        • +
        +
      • +
      +
    • +
    +

    Returns default

    +
  • +
+
+
+ +

subarray

+
    +
  • subarray(begin?: number, end?: number): Uint8Array
  • +
+
    +
  • + +
    +
    +

    Gets a new Uint8Array view of the ArrayBuffer store for this array, referencing the elements + at begin, inclusive, up to end, exclusive.

    +
    +
    +

    Parameters

    +
      +
    • +
      Optional begin: number
      +
      +

      The index of the beginning of the array.

      +
      +
    • +
    • +
      Optional end: number
      +
      +

      The index of the end of the array.

      +
      +
    • +
    +

    Returns Uint8Array

    +
  • +
+
+
+ +

toLocaleString

+
    +
  • toLocaleString(): string
  • +
+
    +
  • + +
    +
    +

    Converts a number to a string by using the current locale.

    +
    +
    +

    Returns string

    +
  • +
+
+
+ +

toString

+
    +
  • toString(): string
  • +
+
    +
  • + +

    Returns string

    +
  • +
+
+
+ +

valueOf

+
    +
  • valueOf(): Uint8Array
  • +
+
    +
  • + +
    +
    +

    Returns the primitive value of the specified object.

    +
    +
    +

    Returns Uint8Array

    +
  • +
+
+
+ +

values

+
    +
  • values(): IterableIterator<number>
  • +
+
    +
  • + +
    +
    +

    Returns an list of values in the array

    +
    +
    +

    Returns IterableIterator<number>

    +
  • +
+
+
+ +

Static create

+
    +
  • create(): Id
  • +
  • create(length: number): Id
  • +
  • create(array: ArrayLike<number> | ArrayBufferLike): Id
  • +
  • create(buffer: ArrayBufferLike, byteOffset?: number, length?: number): Id
  • +
+
    +
  • + +

    Returns Id

    +
  • +
  • + +

    Parameters

    +
      +
    • +
      length: number
      +
    • +
    +

    Returns Id

    +
  • +
  • + +

    Parameters

    +
      +
    • +
      array: ArrayLike<number> | ArrayBufferLike
      +
    • +
    +

    Returns Id

    +
  • +
  • + +

    Parameters

    +
      +
    • +
      buffer: ArrayBufferLike
      +
    • +
    • +
      Optional byteOffset: number
      +
    • +
    • +
      Optional length: number
      +
    • +
    +

    Returns Id

    +
  • +
+
+
+ +

Static from

+
    +
  • from(arrayLike: ArrayLike<number>): Uint8Array
  • +
  • from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Uint8Array
  • +
  • from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint8Array
  • +
+
    +
  • + +
    +
    +

    Creates an array from an array-like or iterable object.

    +
    +
    +

    Parameters

    +
      +
    • +
      arrayLike: ArrayLike<number>
      +
      +

      An array-like or iterable object to convert to an array.

      +
      +
    • +
    +

    Returns Uint8Array

    +
  • +
  • + +
    +
    +

    Creates an array from an array-like or iterable object.

    +
    +
    +

    Type parameters

    +
      +
    • +

      T

      +
    • +
    +

    Parameters

    +
      +
    • +
      arrayLike: ArrayLike<T>
      +
      +

      An array-like or iterable object to convert to an array.

      +
      +
    • +
    • +
      mapfn: (v: T, k: number) => number
      +
      +

      A mapping function to call on every element of the array.

      +
      +
        +
      • +
          +
        • (v: T, k: number): number
        • +
        +
          +
        • +

          Parameters

          +
            +
          • +
            v: T
            +
          • +
          • +
            k: number
            +
          • +
          +

          Returns number

          +
        • +
        +
      • +
      +
    • +
    • +
      Optional thisArg: any
      +
      +

      Value of 'this' used to invoke the mapfn.

      +
      +
    • +
    +

    Returns Uint8Array

    +
  • +
  • + +
    +
    +

    Creates an array from an array-like or iterable object.

    +
    +
    +

    Parameters

    +
      +
    • +
      arrayLike: Iterable<number>
      +
      +

      An array-like or iterable object to convert to an array.

      +
      +
    • +
    • +
      Optional mapfn: (v: number, k: number) => number
      +
      +

      A mapping function to call on every element of the array.

      +
      +
        +
      • +
          +
        • (v: number, k: number): number
        • +
        +
          +
        • +

          Parameters

          +
            +
          • +
            v: number
            +
          • +
          • +
            k: number
            +
          • +
          +

          Returns number

          +
        • +
        +
      • +
      +
    • +
    • +
      Optional thisArg: any
      +
      +

      Value of 'this' used to invoke the mapfn.

      +
      +
    • +
    +

    Returns Uint8Array

    +
  • +
+
+
+ +

Static of

+
    +
  • of(...items: number[]): Uint8Array
  • +
+
    +
  • + +
    +
    +

    Returns a new array from a set of elements.

    +
    +
    +

    Parameters

    +
      +
    • +
      Rest ...items: number[]
      +
      +

      A set of elements to include in the new array object.

      +
      +
    • +
    +

    Returns Uint8Array

    +
  • +
+
+
+
+ +
+
+
+
+

Legend

+
+
    +
  • Function
  • +
  • Function with type parameter
  • +
  • Type alias
  • +
+
    +
  • Class
  • +
  • Method
  • +
+
    +
  • Inherited constructor
  • +
+
    +
  • Static method
  • +
+
+
+
+
+

Generated using TypeDoc

+
+
+ + + \ No newline at end of file diff --git a/docs/classes/IdDeterministic.default.html b/docs/classes/IdDeterministic.default.html index bcf5790..3f51407 100644 --- a/docs/classes/IdDeterministic.default.html +++ b/docs/classes/IdDeterministic.default.html @@ -91,7 +91,7 @@

Hierarchy

Implements

    -
  • IterableIterator<ArrayBuffer>
  • +
  • IterableIterator<Id>
@@ -133,7 +133,7 @@

constructor

  • Parameters

    @@ -160,7 +160,7 @@

    Protected namespaceData<
    namespaceData: Uint8Array

  • @@ -171,17 +171,17 @@

    Methods

    [iterator]

      -
    • [iterator](): IterableIterator<ArrayBuffer>
    • +
    • [iterator](): IterableIterator<Id>
    @@ -189,13 +189,13 @@

    Returns IterableI

    get

      -
    • get(name?: string): ArrayBuffer
    • +
    • get(name?: string): Id
    -

    Returns ArrayBuffer

    +

    Returns Id

    @@ -212,14 +212,14 @@

    Returns ArrayBuff

    next

      -
    • next(name?: string): IteratorResult<ArrayBuffer, void>
    • +
    • next(name?: string): IteratorResult<Id, void>
    -

    Returns IteratorResult<ArrayBuffer, void>

    +

    Returns IteratorResult<Id, void>

    @@ -240,6 +240,9 @@

    Returns IteratorR
  • Modules
  • +
  • + Id +
  • IdDeterministic
  • diff --git a/docs/classes/IdRandom.default.html b/docs/classes/IdRandom.default.html index 9f8d3d8..deb6041 100644 --- a/docs/classes/IdRandom.default.html +++ b/docs/classes/IdRandom.default.html @@ -79,7 +79,7 @@

    Hierarchy

    Implements

      -
    • IterableIterator<ArrayBuffer>
    • +
    • IterableIterator<Id>
    @@ -121,7 +121,7 @@

    constructor

  • Parameters

    @@ -138,7 +138,7 @@
    randomSource: function
  • Parameters

    @@ -167,7 +167,7 @@

    Protected randomSourcerandomSource: (size: number) => Uint8Array
    @@ -199,17 +199,17 @@

    Methods

    [iterator]

      -
    • [iterator](): IterableIterator<ArrayBuffer>
    • +
    • [iterator](): IterableIterator<Id>
    • -

      Returns IterableIterator<ArrayBuffer>

      +

      Returns IterableIterator<Id>

  • @@ -217,16 +217,16 @@

    Returns IterableI

    get

      -
    • get(): ArrayBuffer
    • +
    • get(): Id
    @@ -234,17 +234,17 @@

    Returns ArrayBuff

    next

      -
    • next(): IteratorResult<ArrayBuffer, void>
    • +
    • next(): IteratorResult<Id, void>
    • -

      Returns IteratorResult<ArrayBuffer, void>

      +

      Returns IteratorResult<Id, void>

    @@ -256,6 +256,9 @@

    Returns IteratorR
  • Modules
  • +
  • + Id +
  • IdDeterministic
  • diff --git a/docs/classes/IdSortable.default.html b/docs/classes/IdSortable.default.html index 23d2bf5..1c6ea98 100644 --- a/docs/classes/IdSortable.default.html +++ b/docs/classes/IdSortable.default.html @@ -91,7 +91,7 @@

    Hierarchy

    Implements

      -
    • IterableIterator<ArrayBuffer>
    • +
    • IterableIterator<Id>
    @@ -107,8 +107,8 @@

    Constructors

    Properties

    @@ -285,7 +285,7 @@

    Protected randomSourcerandomSource: (size: number) => Uint8Array
    @@ -316,7 +316,7 @@

    Protected seqCounter

    seqCounter: number

    @@ -327,16 +327,16 @@

    Accessors

    lastId

      -
    • get lastId(): ArrayBuffer
    • +
    • get lastId(): Id
    @@ -347,17 +347,17 @@

    Methods

    [iterator]

      -
    • [iterator](): IterableIterator<ArrayBuffer>
    • +
    • [iterator](): IterableIterator<Id>
    @@ -365,16 +365,16 @@

    Returns IterableI

    get

      -
    • get(): ArrayBuffer
    • +
    • get(): Id
    @@ -382,17 +382,17 @@

    Returns ArrayBuff

    next

      -
    • next(): IteratorResult<ArrayBuffer, void>
    • +
    • next(): IteratorResult<Id, void>
    @@ -404,6 +404,9 @@

    Returns IteratorR
  • Modules
  • +
  • + Id +
  • IdDeterministic
  • @@ -432,10 +435,10 @@

    Returns IteratorR constructor
  • - clock + _lastId
  • - lastId_ + clock
  • lastTs diff --git a/docs/index.html b/docs/index.html index 22165c4..3c17a18 100644 --- a/docs/index.html +++ b/docs/index.html @@ -81,10 +81,20 @@

    js-id

    const deteId1 = deteGen.get(); const deteId2 = deteGen.get('bar'); +const deteId3 = deteGen.get('bar'); console.log(utils.toUUID(deteId1)); console.log(utils.toMultibase(deteId2, 'base58btc')); +// Will be cast to string index +const recordOfDeteIds = {}; +recordOfDeteIds[deteId1] = 1; +recordOfDeteIds[deteId2] = 1; +console.log(recordOfDeteIds[deteId1]); + +// Can be checked for equality +console.log(deteId2.toString() === deteId3.toString()); + // Strictly monotonic sortable ids, equivalent to UUIDv7 let lastId = new Uint8Array( @@ -92,7 +102,7 @@

    js-id

    0x06, 0x16, 0x3e, 0xf5, 0x6d, 0x8d, 0x70, 0x00, 0x87, 0xc4, 0x65, 0xd5, 0x21, 0x9b, 0x03, 0xd4, ] -).buffer; +); const sortGen = new IdSortable({ lastId }); @@ -101,9 +111,9 @@

    js-id

    const sortId3 = sortGen.get(); const sortIds = [ - Buffer.from(sortId2), - Buffer.from(sortId3), - Buffer.from(sortId1), + utils.toBuffer(sortId2), + utils.toBuffer(sortId3), + utils.toBuffer(sortId1), ]; sortIds.sort(Buffer.compare); @@ -112,6 +122,10 @@

    js-id

    // Save the last id to ensure strict monotonicity across process restarts lastId = sortGen.lastId; + +// Ids can also be compared in order +console.log(sortId1 < sortId2); +console.log(sortId2 < sortId3);

    Installation

    @@ -159,6 +173,9 @@

    Publishing

  • Modules
  • +
  • + Id +
  • IdDeterministic
  • diff --git a/docs/modules.html b/docs/modules.html index 1026330..b0e637c 100644 --- a/docs/modules.html +++ b/docs/modules.html @@ -64,6 +64,7 @@

    Index

    Modules

      +
    • Id
    • IdDeterministic
    • IdRandom
    • IdSortable
    • @@ -81,6 +82,9 @@

      Modules

    • Modules
    • +
    • + Id +
    • IdDeterministic
    • diff --git a/docs/modules/Id.html b/docs/modules/Id.html new file mode 100644 index 0000000..b9815c8 --- /dev/null +++ b/docs/modules/Id.html @@ -0,0 +1,168 @@ + + + + + + Id | @matrixai/id + + + + + + +
      +
      +
      +
      + +
      +
      + Options +
      +
      + All +
        +
      • Public
      • +
      • Public/Protected
      • +
      • All
      • +
      +
      + + + + +
      +
      + Menu +
      +
      +
      +
      +
      +
      + +

      Module Id

      +
      +
      +
      +
      +
      +
      +
      +

      Index

      +
      +
      +
      +

      Classes

      + +
      +
      +

      Type aliases

      + +
      +
      +
      +
      +
      +

      Type aliases

      +
      + +

      Id

      +
      Id: default & number
      + +
      +
      +

      IdInternal can be used as a string primitive + This type hack prevents TS from complaining + See: https://github.com/microsoft/TypeScript/issues/4538

      +
      +
      +
      +
      +
      + +
      +
      +
      +
      +

      Legend

      +
      +
        +
      • Function
      • +
      • Function with type parameter
      • +
      • Type alias
      • +
      +
        +
      • Class
      • +
      +
      +
      +
      +
      +

      Generated using TypeDoc

      +
      +
      + + + \ No newline at end of file diff --git a/docs/modules/IdDeterministic.html b/docs/modules/IdDeterministic.html index 6f9aa13..751422f 100644 --- a/docs/modules/IdDeterministic.html +++ b/docs/modules/IdDeterministic.html @@ -85,6 +85,9 @@

      Classes

    • Modules
    • +
    • + Id +
    • IdDeterministic
    • diff --git a/docs/modules/IdRandom.html b/docs/modules/IdRandom.html index 24bff12..d063609 100644 --- a/docs/modules/IdRandom.html +++ b/docs/modules/IdRandom.html @@ -85,6 +85,9 @@

      Classes

    • Modules
    • +
    • + Id +
    • IdDeterministic
    • diff --git a/docs/modules/IdSortable.html b/docs/modules/IdSortable.html index 8069887..97a0571 100644 --- a/docs/modules/IdSortable.html +++ b/docs/modules/IdSortable.html @@ -92,19 +92,19 @@

      Functions

      extractRand

        -
      • extractRand(idBytes: ArrayBuffer): string
      • +
      • extractRand(idBytes: Uint8Array): string
    @@ -118,16 +122,16 @@

    MultibaseFormats

    Functions

    - -

    bin2bytes

    + +

    bits2bytes

      -
    • bin2bytes(bin: string): Uint8Array
    • +
    • bits2bytes(bits: string): Uint8Array
    • @@ -138,7 +142,7 @@

      bin2bytes

      Parameters

      • -
        bin: string
        +
        bits: string

      Returns Uint8Array

      @@ -146,16 +150,16 @@

      Returns Uint8Arra

    - -

    bytes2bin

    + +

    bytes2bits

      -
    • bytes2bin(bytes: Uint8Array): string
    • +
    • bytes2bits(bytes: Uint8Array): string
    - -

    dec2bin

    + +

    dec2bits

      -
    • dec2bin(dec: number, size: number): string
    • +
    • dec2bits(dec: number, size: number): string
    +
    + +

    fromBuffer

    +
      +
    • fromBuffer(idBuffer: Buffer): Id | undefined
    • +
    +
      +
    • + +
      +
      +

      Decodes as Buffer zero-copy

      +
      +
      +

      Parameters

      +
        +
      • +
        idBuffer: Buffer
        +
      • +
      +

      Returns Id | undefined

      +
    • +
    +

    fromFixedPoint

    @@ -275,7 +307,7 @@

    fromFixedPoint

  • @@ -305,13 +337,13 @@

    Returns number

    fromMultibase

      -
    • fromMultibase(idString: string): ArrayBuffer | undefined
    • +
    • fromMultibase(idString: string): Id | undefined
    -

    Returns ArrayBuffer | undefined

    +

    Returns Id | undefined

    +

  • + +
    +
    + +

    fromString

    +
      +
    • fromString(idString: string): Id | undefined
    • +
    +
      +
    • + +

      Parameters

      +
        +
      • +
        idString: string
        +
      • +
      +

      Returns Id | undefined

    @@ -334,13 +389,13 @@

    Returns ArrayBuff

    fromUUID

      -
    • fromUUID(uuid: string): ArrayBuffer | undefined
    • +
    • fromUUID(uuid: string): Id | undefined
    -

    Returns ArrayBuffer | undefined

    +

    Returns Id | undefined

    @@ -363,7 +418,7 @@

    hex2bytes

  • Parameters

    @@ -380,19 +435,19 @@

    Returns Uint8Arra

    nodeBits

      -
    • nodeBits(nodeBytes: ArrayBuffer, size: number): string
    • +
    • nodeBits(nodeBytes: Uint8Array, size: number): string