detritus-utils
Advanced tools
| import * as Timers from './timers'; | ||
| export declare class BaseCollectionMixin<K, V> { | ||
| readonly length: number; | ||
| readonly size: number; | ||
| get length(): number; | ||
| get size(): number; | ||
| clear(): void; | ||
@@ -23,3 +23,3 @@ entries(): IterableIterator<[K, V]>; | ||
| [Symbol.iterator](): IterableIterator<[K, V]>; | ||
| readonly [Symbol.toStringTag]: string; | ||
| get [Symbol.toStringTag](): string; | ||
| } | ||
@@ -39,4 +39,4 @@ export interface BaseCollectionOptions { | ||
| constructor({ expire, intervalTime, limit }?: BaseCollectionOptions); | ||
| readonly lastUsed: Map<K, number>; | ||
| readonly shouldStartInterval: boolean; | ||
| get lastUsed(): Map<K, number>; | ||
| get shouldStartInterval(): boolean; | ||
| setExpire(value: number): this; | ||
@@ -46,3 +46,3 @@ setIntervalTimeout(value: number): this; | ||
| stopInterval(): void; | ||
| readonly size: number; | ||
| get size(): number; | ||
| clear(): void; | ||
@@ -58,3 +58,3 @@ clone(): BaseCollection<K, V>; | ||
| [Symbol.iterator](): IterableIterator<[K, V]>; | ||
| readonly [Symbol.toStringTag]: string; | ||
| get [Symbol.toStringTag](): string; | ||
| } |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.BaseCollection = exports.BaseCollectionMixin = void 0; | ||
| const Timers = require("./timers"); | ||
@@ -242,2 +243,10 @@ class BaseCollectionMixin { | ||
| } | ||
| if (this.limit !== Infinity) { | ||
| if (this.limit <= this.cache.size) { | ||
| for (let [key, value] of this.cache) { | ||
| this.delete(key); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| return this; | ||
@@ -244,0 +253,0 @@ } |
+2
-2
| export declare class BaseSet<V> extends Set<V> { | ||
| readonly length: number; | ||
| get length(): number; | ||
| clone(): BaseSet<V>; | ||
@@ -16,3 +16,3 @@ every(func: any): boolean; | ||
| toString(): string; | ||
| readonly [Symbol.toStringTag]: string; | ||
| get [Symbol.toStringTag](): string; | ||
| } |
+1
-0
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.BaseSet = void 0; | ||
| class BaseSet extends Set { | ||
@@ -4,0 +5,0 @@ get length() { |
+2
-1
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.DISCORD_TOKEN_EPOCH = exports.DISCORD_SNOWFLAKE_EPOCH = exports.Package = void 0; | ||
| exports.Package = Object.freeze({ | ||
| URL: 'https://github.com/detritusjs/utils', | ||
| VERSION: '0.2.6', | ||
| VERSION: '0.3.0', | ||
| }); | ||
| exports.DISCORD_SNOWFLAKE_EPOCH = 1420070400000; | ||
| exports.DISCORD_TOKEN_EPOCH = 1293840000000; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.EventSubscription = exports.EventSpewer = void 0; | ||
| const events_1 = require("events"); | ||
@@ -4,0 +5,0 @@ let Emitter = events_1.EventEmitter; |
+14
-6
| "use strict"; | ||
| function __export(m) { | ||
| for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
| } | ||
| var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
| }) : (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| o[k2] = m[k]; | ||
| })); | ||
| var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
| for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.Tools = exports.Timers = exports.Snowflake = exports.Constants = void 0; | ||
| const Constants = require("./constants"); | ||
@@ -14,4 +22,4 @@ exports.Constants = Constants; | ||
| exports.Tools = Tools; | ||
| __export(require("./basecollection")); | ||
| __export(require("./baseset")); | ||
| __export(require("./eventspewer")); | ||
| __exportStar(require("./basecollection"), exports); | ||
| __exportStar(require("./baseset"), exports); | ||
| __exportStar(require("./eventspewer"), exports); |
| export interface Snowflake { | ||
| binary: string; | ||
| id: string; | ||
@@ -20,6 +19,3 @@ processId: number; | ||
| } | ||
| export interface SnowflakeDeconstructOptionsRequired { | ||
| epoch: number; | ||
| } | ||
| export declare function deconstruct(id: string, options?: SnowflakeDeconstructOptions): Snowflake; | ||
| export declare function timestamp(id: string, options?: SnowflakeDeconstructOptions): number; |
+54
-80
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.timestamp = exports.deconstruct = exports.generate = void 0; | ||
| const constants_1 = require("./constants"); | ||
| const bits = Object.freeze({ | ||
| processId: 5, | ||
| workerId: 5, | ||
| sequence: 12, | ||
| timestamp: 42n, | ||
| workerId: 5n, | ||
| processId: 5n, | ||
| sequence: 12n, | ||
| }); | ||
| const shift = Object.freeze({ | ||
| timestamp: bits.processId + bits.workerId + bits.sequence, | ||
| workerId: bits.workerId + bits.sequence, | ||
| processId: bits.sequence, | ||
| sequence: 0n, | ||
| }); | ||
| const max = Object.freeze({ | ||
| timestamp: 0x40000000000, | ||
| middle: -1 ^ (-1 << (bits.workerId + bits.processId + bits.sequence)), | ||
| processId: -1 ^ (-1 << bits.processId), | ||
| sequence: -1 ^ (-1 << bits.sequence), | ||
| workerId: -1 ^ (-1 << bits.workerId), | ||
| timestamp: 0x40000000000n, | ||
| processId: -1n ^ (-1n << bits.processId), | ||
| sequence: -1n ^ (-1n << bits.sequence), | ||
| workerId: -1n ^ (-1n << bits.workerId), | ||
| }); | ||
| const cache = { | ||
| sequence: 0, | ||
| sequence: 0n, | ||
| }; | ||
| function generate(options = {}) { | ||
| options = Object.assign({}, options); | ||
| if (options.epoch === undefined) { | ||
| options.epoch = constants_1.DISCORD_SNOWFLAKE_EPOCH; | ||
| options = Object.assign({ | ||
| epoch: constants_1.DISCORD_SNOWFLAKE_EPOCH, | ||
| processId: 0, | ||
| timestamp: Date.now(), | ||
| workerId: 0, | ||
| }, options); | ||
| const epoch = BigInt(options.epoch); | ||
| const processId = BigInt(options.processId); | ||
| const timestamp = BigInt(options.timestamp); | ||
| const workerId = BigInt(options.workerId); | ||
| let sequence; | ||
| if (options.sequence === undefined) { | ||
| sequence = cache.sequence = ++cache.sequence & max.sequence; | ||
| } | ||
| if (options.processId === undefined) { | ||
| options.processId = 0; | ||
| else { | ||
| sequence = BigInt(options.sequence) & max.sequence; | ||
| } | ||
| if (options.timestamp === undefined) { | ||
| options.timestamp = Date.now(); | ||
| } | ||
| if (options.workerId === undefined) { | ||
| options.workerId = 0; | ||
| } | ||
| const snowflake = { | ||
| binary: '', | ||
| id: '', | ||
| processId: options.processId & max.processId, | ||
| sequence: 0, | ||
| timestamp: (options.timestamp - options.epoch) % max.timestamp, | ||
| workerId: options.workerId & max.workerId, | ||
| processId: Number(processId & max.processId), | ||
| sequence: Number(sequence), | ||
| timestamp: Number((timestamp - epoch) % max.timestamp), | ||
| workerId: Number(workerId & max.workerId), | ||
| }; | ||
| if (options.sequence === undefined) { | ||
| snowflake.sequence = cache.sequence = ++cache.sequence & max.sequence; | ||
| } | ||
| else { | ||
| snowflake.sequence = options.sequence & max.sequence; | ||
| } | ||
| let processId = snowflake.processId.toString(2).padStart(5, '0'); | ||
| let sequence = snowflake.sequence.toString(2).padStart(12, '0'); | ||
| let timestamp = snowflake.timestamp.toString(2).padStart(42, '0'); | ||
| let workerId = snowflake.workerId.toString(2).padStart(5, '0'); | ||
| let binary = snowflake.binary = timestamp + workerId + processId + sequence; | ||
| //thanks discord.js | ||
| while (binary.length > 50) { | ||
| const high = parseInt(binary.slice(0, -32), 2); | ||
| const low = parseInt((high % 10).toString(2) + binary.slice(-32), 2); | ||
| snowflake.id = (low % 10).toString() + snowflake.id; | ||
| binary = Math.floor(high / 10).toString(2) + Math.floor(low / 10).toString(2).padStart(32, '0'); | ||
| } | ||
| let binaryNumber = parseInt(binary, 2); | ||
| while (0 < binaryNumber) { | ||
| snowflake.id = (binaryNumber % 10).toString() + snowflake.id; | ||
| binaryNumber = Math.floor(binaryNumber / 10); | ||
| } | ||
| snowflake.id = String((timestamp << shift.timestamp) | | ||
| (processId << shift.processId) | | ||
| (workerId << shift.workerId) | | ||
| (sequence << shift.sequence)); | ||
| return snowflake; | ||
@@ -68,40 +59,23 @@ } | ||
| function deconstruct(id, options = {}) { | ||
| options = Object.assign({}, options); | ||
| if (options.epoch === undefined) { | ||
| options.epoch = constants_1.DISCORD_SNOWFLAKE_EPOCH; | ||
| } | ||
| const snowflake = { | ||
| binary: '', | ||
| options = Object.assign({ | ||
| epoch: constants_1.DISCORD_SNOWFLAKE_EPOCH, | ||
| }, options); | ||
| const epoch = BigInt(options.epoch); | ||
| const snowflake = BigInt(id); | ||
| return { | ||
| id, | ||
| processId: 0, | ||
| sequence: 0, | ||
| timestamp: 0, | ||
| workerId: 0, | ||
| processId: Number((snowflake & 0x1f000n) >> shift.processId), | ||
| sequence: Number(snowflake & 0xfffn), | ||
| timestamp: Number((snowflake >> shift.timestamp) + epoch), | ||
| workerId: Number((snowflake & 0x3e0000n) >> shift.workerId), | ||
| }; | ||
| //thanks discord.js | ||
| let high = parseInt(id.slice(0, -10)) || 0; | ||
| let low = parseInt(id.slice(-10)); | ||
| while (low > 0 || high > 0) { | ||
| snowflake.binary = (low & 1).toString() + snowflake.binary; | ||
| low = Math.floor(low / 2); | ||
| if (high > 0) { | ||
| low += 5000000000 * (high % 2); | ||
| high = Math.floor(high / 2); | ||
| } | ||
| } | ||
| snowflake.binary = snowflake.binary.padStart(64, '0'); | ||
| snowflake.timestamp = parseInt(snowflake.binary.slice(0, 42), 2) + options.epoch; | ||
| snowflake.workerId = parseInt(snowflake.binary.slice(42, 47), 2); | ||
| snowflake.processId = parseInt(snowflake.binary.slice(47, 52), 2); | ||
| snowflake.sequence = parseInt(snowflake.binary.slice(52, 64), 2); | ||
| return snowflake; | ||
| } | ||
| exports.deconstruct = deconstruct; | ||
| function timestamp(id, options = {}) { | ||
| options = Object.assign({}, options); | ||
| if (options.epoch === undefined) { | ||
| options.epoch = constants_1.DISCORD_SNOWFLAKE_EPOCH; | ||
| } | ||
| options = Object.assign({ | ||
| epoch: constants_1.DISCORD_SNOWFLAKE_EPOCH, | ||
| }, options); | ||
| const epoch = BigInt(options.epoch); | ||
| if (id) { | ||
| return Math.round((parseInt(id) / max.middle) + options.epoch); | ||
| return Number((BigInt(id) >> shift.timestamp) + epoch); | ||
| } | ||
@@ -108,0 +82,0 @@ return 0; |
+2
-2
| export declare class Interval { | ||
| shouldRef: boolean; | ||
| reference: any; | ||
| readonly hasStarted: boolean; | ||
| get hasStarted(): boolean; | ||
| hasRef(): boolean; | ||
@@ -14,3 +14,3 @@ ref(): void; | ||
| reference: any; | ||
| readonly hasStarted: boolean; | ||
| get hasStarted(): boolean; | ||
| hasRef(): boolean; | ||
@@ -17,0 +17,0 @@ ref(): void; |
+1
-0
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.sleep = exports.Timeout = exports.Interval = void 0; | ||
| class Interval { | ||
@@ -4,0 +5,0 @@ constructor() { |
+1
-0
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.URIEncodeWrap = exports.guildIdToShardId = exports.normalize = void 0; | ||
| function normalize(object) { | ||
@@ -4,0 +5,0 @@ for (const key in object) { |
+3
-3
| { | ||
| "name": "detritus-utils", | ||
| "version": "0.2.6", | ||
| "version": "0.3.0", | ||
| "description": "Detritus Utils", | ||
@@ -12,6 +12,6 @@ "main": "lib/index.js", | ||
| "devDependencies": { | ||
| "@types/node": "^12.11.7" | ||
| "@types/node": "^14.0.27" | ||
| }, | ||
| "peerDependencies": { | ||
| "eventemitter3": "^4.0.0" | ||
| "eventemitter3": "^4.0.4" | ||
| }, | ||
@@ -18,0 +18,0 @@ "peerDependenciesMeta": { |
25966
-0.66%858
-0.92%