@thi.ng/ksuid
Advanced tools
Comparing version 0.4.0 to 1.0.1
@@ -11,2 +11,3 @@ import { BaseN } from "@thi.ng/base-n"; | ||
readonly size: number; | ||
readonly encodedSize: number; | ||
readonly base: BaseN; | ||
@@ -13,0 +14,0 @@ readonly epoch: number; |
@@ -16,3 +16,4 @@ import { assert } from "@thi.ng/api"; | ||
this.size = this.epochSize + opts.bytes; | ||
this.pad = padLeft(this.base.size(2 ** (this.size * 8) - 1), this.base.base[0]); | ||
this.encodedSize = this.base.size(2 ** (this.size * 8) - 1); | ||
this.pad = padLeft(this.encodedSize, this.base.base[0]); | ||
} | ||
@@ -19,0 +20,0 @@ next() { |
@@ -9,2 +9,3 @@ import type { BaseN } from "@thi.ng/base-n"; | ||
readonly size: number; | ||
readonly encodedSize: number; | ||
readonly base: BaseN; | ||
@@ -11,0 +12,0 @@ readonly epoch: number; |
@@ -6,2 +6,10 @@ # Change Log | ||
## [1.0.1](https://github.com/thi-ng/umbrella/compare/@thi.ng/ksuid@0.4.0...@thi.ng/ksuid@1.0.1) (2021-08-08) | ||
**Note:** Version bump only for package @thi.ng/ksuid | ||
# [0.4.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/ksuid@0.3.0...@thi.ng/ksuid@0.4.0) (2021-08-07) | ||
@@ -8,0 +16,0 @@ |
@@ -12,4 +12,4 @@ import { AKSUID } from "./aksuid"; | ||
/** | ||
* Creates and returns a new 32bit epoch KSUID instance (w/ second time | ||
* precision). | ||
* Creates and returns a new 32bit epoch KSUID generator instance (w/ second | ||
* time precision). | ||
* | ||
@@ -16,0 +16,0 @@ * @param opts |
@@ -22,4 +22,4 @@ import { AKSUID } from "./aksuid"; | ||
/** | ||
* Creates and returns a new 32bit epoch KSUID instance (w/ second time | ||
* precision). | ||
* Creates and returns a new 32bit epoch KSUID generator instance (w/ second | ||
* time precision). | ||
* | ||
@@ -26,0 +26,0 @@ * @param opts |
@@ -12,4 +12,4 @@ import { AKSUID } from "./aksuid"; | ||
/** | ||
* Creates and returns a new 64bit epoch KSUID instance (w/ millisecond time | ||
* precision). | ||
* Creates and returns a new 64bit epoch KSUID generator instance (w/ | ||
* millisecond time precision). | ||
* | ||
@@ -16,0 +16,0 @@ * @param opts |
@@ -33,4 +33,4 @@ import { AKSUID } from "./aksuid"; | ||
/** | ||
* Creates and returns a new 64bit epoch KSUID instance (w/ millisecond time | ||
* precision). | ||
* Creates and returns a new 64bit epoch KSUID generator instance (w/ | ||
* millisecond time precision). | ||
* | ||
@@ -37,0 +37,0 @@ * @param opts |
@@ -17,3 +17,4 @@ 'use strict'; | ||
this.size = this.epochSize + opts.bytes; | ||
this.pad = strings.padLeft(this.base.size(2 ** (this.size * 8) - 1), this.base.base[0]); | ||
this.encodedSize = this.base.size(2 ** (this.size * 8) - 1); | ||
this.pad = strings.padLeft(this.encodedSize, this.base.base[0]); | ||
} | ||
@@ -20,0 +21,0 @@ next() { |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@thi.ng/api"),require("@thi.ng/base-n"),require("@thi.ng/random"),require("@thi.ng/strings")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/api","@thi.ng/base-n","@thi.ng/random","@thi.ng/strings"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).thi=e.thi||{},e.thi.ng=e.thi.ng||{},e.thi.ng.ksuid={}),e.thi.ng.api,e.thi.ng.baseN,e.thi.ng.random,e.thi.ng.strings)}(this,(function(e,t,s,i,n){"use strict";class r{constructor(e,t){this.epochSize=e,this.base=t.base||s.BASE62,this.rnd=t.rnd,this.epoch=t.epoch,this.size=this.epochSize+t.bytes,this.pad=n.padLeft(this.base.size(2**(8*this.size)-1),this.base.base[0])}next(){return this.format(this.nextBinary())}nextBinary(){const e=this.timeOnlyBinary();return this.rnd?i.randomBytesFrom(this.rnd,e,this.epochSize):i.randomBytes(e,this.epochSize)}timeOnly(e){return this.format(this.timeOnlyBinary(e))}format(e){return this.ensureSize(e),this.pad(this.base.encodeBytes(e))}ensureSize(e){return t.assert(e.length==this.size,`illegal KSUID size, expected ${this.size} bytes`),e}ensureTime(e){return t.assert(e>=0,"configured base epoch must be in the past"),e}u32(e,t=0){return(e[t]<<24|e[t+1]<<16|e[t+2]<<8|e[t+3])>>>0}}class h extends r{constructor(e){super(4,Object.assign({epoch:16e8,bytes:16},e))}timeOnlyBinary(e=Date.now()){const t=new Uint8Array(this.size),s=this.ensureTime(e/1e3-this.epoch|0);return t.set([s>>>24,s>>16&255,s>>8&255,255&s]),t}parse(e){const t=new Uint8Array(this.size);return this.base.decodeBytes(e,t),{epoch:1e3*(this.u32(t)+this.epoch),id:t.slice(4)}}}class o extends r{constructor(e){super(8,Object.assign({epoch:16e11,bytes:12},e))}timeOnlyBinary(e=Date.now()){const t=new Uint8Array(this.size),s=this.ensureTime(e-this.epoch),i=s/4294967296>>>0,n=(4294967295&s)>>>0;return t.set([i>>>24,i>>16&255,i>>8&255,255&i,n>>>24,n>>16&255,n>>8&255,255&n]),t}parse(e){const t=new Uint8Array(this.size);return this.base.decodeBytes(e,t),{epoch:4294967296*this.u32(t)+this.u32(t,4)+this.epoch,id:t.slice(8)}}}class a extends r{constructor(e){super(6,Object.assign({epoch:0,bytes:10,base:s.BASE32_CROCKFORD},e))}timeOnlyBinary(e=Date.now()){const t=new Uint8Array(this.size),s=this.ensureTime(e-this.epoch),i=s/4294967296>>>0,n=(4294967295&s)>>>0;return t.set([i>>8&255,255&i,n>>>24,n>>16&255,n>>8&255,255&n]),t}parse(e){const t=new Uint8Array(this.size);return this.base.decodeBytes(e,t),{epoch:4294967296*(t[0]<<8|t[1])+this.u32(t,2)+this.epoch,id:t.slice(6)}}}e.AKSUID=r,e.KSUID32=h,e.KSUID64=o,e.ULID=a,e.defKSUID32=e=>new h(e),e.defKSUID64=e=>new o(e),e.defULID=e=>new a(e),Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@thi.ng/api"),require("@thi.ng/base-n"),require("@thi.ng/random"),require("@thi.ng/strings")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/api","@thi.ng/base-n","@thi.ng/random","@thi.ng/strings"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).thi=e.thi||{},e.thi.ng=e.thi.ng||{},e.thi.ng.ksuid={}),e.thi.ng.api,e.thi.ng.baseN,e.thi.ng.random,e.thi.ng.strings)}(this,(function(e,t,s,i,n){"use strict";class r{constructor(e,t){this.epochSize=e,this.base=t.base||s.BASE62,this.rnd=t.rnd,this.epoch=t.epoch,this.size=this.epochSize+t.bytes,this.encodedSize=this.base.size(2**(8*this.size)-1),this.pad=n.padLeft(this.encodedSize,this.base.base[0])}next(){return this.format(this.nextBinary())}nextBinary(){const e=this.timeOnlyBinary();return this.rnd?i.randomBytesFrom(this.rnd,e,this.epochSize):i.randomBytes(e,this.epochSize)}timeOnly(e){return this.format(this.timeOnlyBinary(e))}format(e){return this.ensureSize(e),this.pad(this.base.encodeBytes(e))}ensureSize(e){return t.assert(e.length==this.size,`illegal KSUID size, expected ${this.size} bytes`),e}ensureTime(e){return t.assert(e>=0,"configured base epoch must be in the past"),e}u32(e,t=0){return(e[t]<<24|e[t+1]<<16|e[t+2]<<8|e[t+3])>>>0}}class h extends r{constructor(e){super(4,Object.assign({epoch:16e8,bytes:16},e))}timeOnlyBinary(e=Date.now()){const t=new Uint8Array(this.size),s=this.ensureTime(e/1e3-this.epoch|0);return t.set([s>>>24,s>>16&255,s>>8&255,255&s]),t}parse(e){const t=new Uint8Array(this.size);return this.base.decodeBytes(e,t),{epoch:1e3*(this.u32(t)+this.epoch),id:t.slice(4)}}}class o extends r{constructor(e){super(8,Object.assign({epoch:16e11,bytes:12},e))}timeOnlyBinary(e=Date.now()){const t=new Uint8Array(this.size),s=this.ensureTime(e-this.epoch),i=s/4294967296>>>0,n=(4294967295&s)>>>0;return t.set([i>>>24,i>>16&255,i>>8&255,255&i,n>>>24,n>>16&255,n>>8&255,255&n]),t}parse(e){const t=new Uint8Array(this.size);return this.base.decodeBytes(e,t),{epoch:4294967296*this.u32(t)+this.u32(t,4)+this.epoch,id:t.slice(8)}}}class a extends r{constructor(e){super(6,Object.assign({epoch:0,bytes:10,base:s.BASE32_CROCKFORD},e))}timeOnlyBinary(e=Date.now()){const t=new Uint8Array(this.size),s=this.ensureTime(e-this.epoch),i=s/4294967296>>>0,n=(4294967295&s)>>>0;return t.set([i>>8&255,255&i,n>>>24,n>>16&255,n>>8&255,255&n]),t}parse(e){const t=new Uint8Array(this.size);return this.base.decodeBytes(e,t),{epoch:4294967296*(t[0]<<8|t[1])+this.u32(t,2)+this.epoch,id:t.slice(6)}}}e.AKSUID=r,e.KSUID32=h,e.KSUID64=o,e.ULID=a,e.defKSUID32=e=>new h(e),e.defKSUID64=e=>new o(e),e.defULID=e=>new a(e),Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@thi.ng/ksuid", | ||
"version": "0.4.0", | ||
"version": "1.0.1", | ||
"description": "Configurable K-sortable unique IDs, ULIDs, binary & base-N encoded, 32/48/64bit time resolutions", | ||
@@ -83,5 +83,6 @@ "module": "./index.js", | ||
], | ||
"status": "stable", | ||
"year": 2020 | ||
}, | ||
"gitHead": "fe6cee7534b8534ee97aa52b159979dba6b90461" | ||
"gitHead": "dea912b6ee09890a44ed2f1ede436f55af479e01" | ||
} |
@@ -24,3 +24,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
Configurable sortable unique IDs, ULIDs, binary & base-N encoded, 32/48/64bit time resolution. | ||
Configurable K-sortable unique IDs, ULIDs, binary & base-N encoded, 32/48/64bit time resolutions. | ||
@@ -81,3 +81,3 @@ Idea based on [segmentio/ksuid](https://github.com/segmentio/ksuid), though the | ||
Package sizes (gzipped, pre-treeshake): ESM: 803 bytes / CJS: 879 bytes / UMD: 974 bytes | ||
Package sizes (gzipped, pre-treeshake): ESM: 812 bytes / CJS: 888 bytes / UMD: 983 bytes | ||
@@ -84,0 +84,0 @@ ## Dependencies |
@@ -12,4 +12,4 @@ import { AKSUID } from "./aksuid"; | ||
/** | ||
* Creates and returns a new ULID instance (w/ 48bit epoch millisecond time | ||
* precision). | ||
* Creates and returns a new ULID generator instance (w/ 48bit epoch millisecond | ||
* time precision). | ||
* | ||
@@ -16,0 +16,0 @@ * @remarks |
@@ -34,4 +34,4 @@ import { BASE32_CROCKFORD } from "@thi.ng/base-n"; | ||
/** | ||
* Creates and returns a new ULID instance (w/ 48bit epoch millisecond time | ||
* precision). | ||
* Creates and returns a new ULID generator instance (w/ 48bit epoch millisecond | ||
* time precision). | ||
* | ||
@@ -38,0 +38,0 @@ * @remarks |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
51678
478
1