detritus-utils 0.5.0-beta.2 → 0.5.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,298 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.BaseCollection = exports.BaseCollectionMixin = void 0;
37
- const Timers = __importStar(require("./timers"));
38
- class BaseCollectionMixin {
39
- get length() {
40
- return this.size;
41
- }
42
- get size() {
43
- return 0;
44
- }
45
- clear() {
46
- }
47
- entries() {
48
- return this[Symbol.iterator]();
49
- }
50
- every(func) {
51
- for (let [key, value] of this) {
52
- if (!func(value, key)) {
53
- return false;
54
- }
55
- }
56
- return true;
57
- }
58
- filter(func) {
59
- const map = [];
60
- for (let [key, value] of this) {
61
- if (func(value, key)) {
62
- map.push(value);
63
- }
64
- }
65
- return map;
66
- }
67
- find(func) {
68
- for (let [key, value] of this) {
69
- if (func(value, key)) {
70
- return value;
71
- }
72
- }
73
- return undefined;
74
- }
75
- first() {
76
- for (let [key, value] of this) {
77
- return value;
78
- }
79
- }
80
- forEach(func, thisArg) {
81
- }
82
- join(separator) {
83
- return this.toArray().join(separator);
84
- }
85
- map(func) {
86
- const map = [];
87
- for (let [key, value] of this) {
88
- map.push(func(value, key));
89
- }
90
- return map;
91
- }
92
- reduce(func, initialValue) {
93
- let reduced = initialValue;
94
- for (let [key, value] of this) {
95
- reduced = func(reduced, value);
96
- }
97
- return reduced;
98
- }
99
- some(func) {
100
- for (let [key, value] of this) {
101
- if (func(value, key)) {
102
- return true;
103
- }
104
- }
105
- return false;
106
- }
107
- sort(func) {
108
- return this.toArray().sort(func);
109
- }
110
- toArray() {
111
- return Array.from(this.values());
112
- }
113
- toJSON() {
114
- return this.toArray();
115
- }
116
- toString() {
117
- return this[Symbol.toStringTag];
118
- }
119
- *keys() {
120
- }
121
- *values() {
122
- }
123
- *[Symbol.iterator]() {
124
- }
125
- get [Symbol.toStringTag]() {
126
- return 'BaseCollection';
127
- }
128
- }
129
- exports.BaseCollectionMixin = BaseCollectionMixin;
130
- class BaseCollection extends BaseCollectionMixin {
131
- cache = new Map();
132
- _lastUsed;
133
- expire;
134
- interval = undefined;
135
- intervalTime = 5000;
136
- limit = Infinity;
137
- constructor({ expire, intervalTime, limit } = {}) {
138
- super();
139
- if (expire !== undefined) {
140
- this.expire = expire;
141
- }
142
- if (intervalTime !== undefined) {
143
- this.intervalTime = intervalTime;
144
- }
145
- if (limit !== undefined) {
146
- this.limit = limit;
147
- }
148
- if (this.expire) {
149
- this.intervalTime = Math.min(this.expire, this.intervalTime);
150
- }
151
- Object.defineProperties(this, {
152
- _lastUsed: { enumerable: false, writable: true },
153
- cache: { enumerable: false },
154
- expire: { configurable: true, writable: false },
155
- interval: { enumerable: false, writeable: true },
156
- intervalTime: { configurable: true, enumerable: false, writable: false },
157
- limit: { enumerable: false },
158
- });
159
- }
160
- get lastUsed() {
161
- if (this._lastUsed) {
162
- return this._lastUsed;
163
- }
164
- if (this.expire) {
165
- return this._lastUsed = new Map();
166
- }
167
- return new Map();
168
- }
169
- get shouldStartInterval() {
170
- return !!this.intervalTime && (!this.interval || !!this.interval.hasStarted);
171
- }
172
- setExpire(value) {
173
- Object.defineProperty(this, 'expire', { value });
174
- if (value) {
175
- if (this.size) {
176
- this.startInterval();
177
- }
178
- }
179
- else {
180
- this.stopInterval();
181
- }
182
- return this;
183
- }
184
- setIntervalTimeout(value) {
185
- Object.defineProperty(this, 'intervalTime', { value });
186
- if (value) {
187
- if (this.size) {
188
- this.startInterval();
189
- }
190
- }
191
- else {
192
- this.stopInterval();
193
- }
194
- return this;
195
- }
196
- startInterval() {
197
- if (this.intervalTime && this.expire) {
198
- if (!this.interval) {
199
- this.interval = new Timers.Interval();
200
- }
201
- this.interval.start(this.intervalTime, () => {
202
- const expire = this.expire;
203
- if (expire) {
204
- const now = Date.now();
205
- for (let [key, value] of this.cache) {
206
- const lastUsed = this.lastUsed.get(key);
207
- if (lastUsed && expire < now - lastUsed) {
208
- this.delete(key);
209
- }
210
- }
211
- }
212
- else {
213
- this.stopInterval();
214
- }
215
- });
216
- }
217
- else {
218
- this.stopInterval();
219
- }
220
- }
221
- stopInterval() {
222
- if (this.interval) {
223
- this.interval.stop();
224
- }
225
- this.interval = undefined;
226
- }
227
- get size() {
228
- return this.cache.size;
229
- }
230
- clear() {
231
- this.stopInterval();
232
- this.cache.clear();
233
- if (this._lastUsed) {
234
- this._lastUsed.clear();
235
- }
236
- }
237
- clone() {
238
- const collection = new BaseCollection(this);
239
- for (let [key, value] of this) {
240
- collection.set(key, value);
241
- }
242
- return collection;
243
- }
244
- delete(key) {
245
- const deleted = this.cache.delete(key);
246
- if (this._lastUsed) {
247
- this._lastUsed.delete(key);
248
- }
249
- if (!this.cache.size) {
250
- this.stopInterval();
251
- }
252
- return deleted;
253
- }
254
- forEach(func, thisArg) {
255
- return this.cache.forEach(func, thisArg);
256
- }
257
- get(key) {
258
- const value = this.cache.get(key);
259
- if (this.expire && value) {
260
- this.lastUsed.set(key, Date.now());
261
- }
262
- return value;
263
- }
264
- has(key) {
265
- return this.cache.has(key);
266
- }
267
- keys() {
268
- return this.cache.keys();
269
- }
270
- set(key, value) {
271
- this.cache.set(key, value);
272
- if (this.expire) {
273
- this.lastUsed.set(key, Date.now());
274
- if (this.shouldStartInterval) {
275
- this.startInterval();
276
- }
277
- }
278
- if (this.limit !== Infinity) {
279
- if (this.limit <= this.cache.size) {
280
- for (let [key, value] of this.cache) {
281
- this.delete(key);
282
- break;
283
- }
284
- }
285
- }
286
- return this;
287
- }
288
- values() {
289
- return this.cache.values();
290
- }
291
- [Symbol.iterator]() {
292
- return this.cache[Symbol.iterator]();
293
- }
294
- get [Symbol.toStringTag]() {
295
- return `BaseCollection (${this.size.toLocaleString()} items)`;
296
- }
297
- }
298
- exports.BaseCollection = BaseCollection;
@@ -1,17 +0,0 @@
1
- export declare class BaseSet<V> extends Set<V> {
2
- get length(): number;
3
- clone(): BaseSet<V>;
4
- every(func: any): boolean;
5
- filter(func: (v: V) => boolean): Array<V>;
6
- find(func: (v: V) => boolean): undefined | V;
7
- first(): undefined | V;
8
- join(separator?: string): string;
9
- map(func: (v: V) => any): Array<any>;
10
- reduce(cb: any, initialValue?: any): any;
11
- some(func: (v: V) => boolean): boolean;
12
- sort(func?: (x: V, y: V) => number): Array<V>;
13
- toArray(): Array<V>;
14
- toJSON(): Array<V>;
15
- toString(): string;
16
- get [Symbol.toStringTag](): string;
17
- }
@@ -1,70 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BaseSet = void 0;
4
- class BaseSet extends Set {
5
- get length() {
6
- return this.size;
7
- }
8
- clone() {
9
- return new BaseSet(this.values());
10
- }
11
- every(func) {
12
- return this.toArray().every(func);
13
- }
14
- filter(func) {
15
- const map = [];
16
- for (let value of this) {
17
- if (func(value)) {
18
- map.push(value);
19
- }
20
- }
21
- return map;
22
- }
23
- find(func) {
24
- for (let value of this) {
25
- if (func(value)) {
26
- return value;
27
- }
28
- }
29
- }
30
- first() {
31
- return this.values().next().value;
32
- }
33
- join(separator) {
34
- return this.toArray().join(separator);
35
- }
36
- map(func) {
37
- const map = [];
38
- for (let value of this) {
39
- map.push(func(value));
40
- }
41
- return map;
42
- }
43
- reduce(cb, initialValue) {
44
- return this.toArray().reduce(cb, initialValue);
45
- }
46
- some(func) {
47
- for (let value of this) {
48
- if (func(value)) {
49
- return true;
50
- }
51
- }
52
- return false;
53
- }
54
- sort(func) {
55
- return this.toArray().sort(func);
56
- }
57
- toArray() {
58
- return Array.from(this.values());
59
- }
60
- toJSON() {
61
- return this.toArray();
62
- }
63
- toString() {
64
- return this[Symbol.toStringTag];
65
- }
66
- get [Symbol.toStringTag]() {
67
- return `BaseSet (${this.size.toLocaleString()} items)`;
68
- }
69
- }
70
- exports.BaseSet = BaseSet;
@@ -1,6 +0,0 @@
1
- export declare const Package: Readonly<{
2
- URL: "https://github.com/detritusjs/utils";
3
- VERSION: "0.4.0";
4
- }>;
5
- export declare const DISCORD_SNOWFLAKE_EPOCH = 1420070400000;
6
- export declare const DISCORD_TOKEN_EPOCH = 1293840000000;
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DISCORD_TOKEN_EPOCH = exports.DISCORD_SNOWFLAKE_EPOCH = exports.Package = void 0;
4
- exports.Package = Object.freeze({
5
- URL: 'https://github.com/detritusjs/utils',
6
- VERSION: '0.4.0',
7
- });
8
- exports.DISCORD_SNOWFLAKE_EPOCH = 1420070400000;
9
- exports.DISCORD_TOKEN_EPOCH = 1293840000000;
@@ -1,28 +0,0 @@
1
- import { EventEmitter } from 'events';
2
- declare let Emitter: any;
3
- export declare class EventSpewer extends Emitter implements EventEmitter {
4
- /**
5
- * @ignore
6
- */
7
- _events: {
8
- [key: string]: any;
9
- } | undefined;
10
- /**
11
- * @ignore
12
- */
13
- _subscriptions: Array<EventSubscription> | undefined;
14
- constructor();
15
- hasEventListener(name: string | symbol): boolean;
16
- subscribe(name: string | symbol, listener: (...args: any[]) => void): EventSubscription;
17
- removeSubscription(subscription: EventSubscription): void;
18
- removeAllListeners(name?: string | symbol): this;
19
- removeAllSubscriptions(): this;
20
- }
21
- export declare class EventSubscription {
22
- listener: ((...args: any[]) => void) | null;
23
- name: string | symbol;
24
- spewer: EventSpewer | null;
25
- constructor(spewer: EventSpewer, name: string | symbol, listener: (...args: any[]) => void);
26
- remove(): void;
27
- }
28
- export {};
@@ -1,110 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EventSubscription = exports.EventSpewer = void 0;
4
- const events_1 = require("events");
5
- let Emitter = events_1.EventEmitter;
6
- try {
7
- Emitter = require('eventemitter3');
8
- }
9
- catch (e) { }
10
- class EventSpewer extends Emitter {
11
- /**
12
- * @ignore
13
- */
14
- _events;
15
- /**
16
- * @ignore
17
- */
18
- _subscriptions = undefined;
19
- constructor() {
20
- super();
21
- Object.defineProperties(this, {
22
- _events: { enumerable: false },
23
- _eventsCount: { enumerable: false },
24
- _maxListeners: { enumerable: false },
25
- _subscriptions: { enumerable: false },
26
- });
27
- }
28
- hasEventListener(name) {
29
- return !!this._events && (name in this._events);
30
- }
31
- subscribe(name, listener) {
32
- const subscription = new EventSubscription(this, name, listener);
33
- this.on(name, listener);
34
- if (!this._subscriptions) {
35
- this._subscriptions = [];
36
- }
37
- this._subscriptions.push(subscription);
38
- return subscription;
39
- }
40
- removeSubscription(subscription) {
41
- if (subscription.listener) {
42
- this.removeListener(subscription.name, subscription.listener);
43
- }
44
- if (this._subscriptions) {
45
- const index = this._subscriptions.indexOf(subscription);
46
- if (index !== 1) {
47
- if (this._subscriptions.length === 1) {
48
- this._subscriptions.pop();
49
- }
50
- else {
51
- this._subscriptions.splice(index, 1);
52
- }
53
- }
54
- if (!this._subscriptions.length) {
55
- this._subscriptions = undefined;
56
- }
57
- }
58
- }
59
- removeAllListeners(name) {
60
- if (this._subscriptions) {
61
- if (name !== undefined) {
62
- for (let subscription of this._subscriptions) {
63
- if (subscription.name === name) {
64
- subscription.remove();
65
- }
66
- }
67
- }
68
- else {
69
- while (this._subscriptions.length) {
70
- const subscription = this._subscriptions.shift();
71
- if (subscription) {
72
- subscription.listener = null;
73
- subscription.spewer = null;
74
- }
75
- }
76
- }
77
- }
78
- return super.removeAllListeners(name);
79
- }
80
- removeAllSubscriptions() {
81
- if (this._subscriptions) {
82
- while (this._subscriptions.length) {
83
- const subscription = this._subscriptions.shift();
84
- if (subscription) {
85
- subscription.remove();
86
- }
87
- }
88
- }
89
- return this;
90
- }
91
- }
92
- exports.EventSpewer = EventSpewer;
93
- class EventSubscription {
94
- listener;
95
- name;
96
- spewer;
97
- constructor(spewer, name, listener) {
98
- this.name = name;
99
- this.listener = listener;
100
- this.spewer = spewer;
101
- }
102
- remove() {
103
- if (this.spewer) {
104
- this.spewer.removeSubscription(this);
105
- }
106
- this.listener = null;
107
- this.spewer = null;
108
- }
109
- }
110
- exports.EventSubscription = EventSubscription;
@@ -1,8 +0,0 @@
1
- import * as Constants from './constants';
2
- import * as Snowflake from './snowflake';
3
- import * as Timers from './timers';
4
- import * as Tools from './tools';
5
- export { Constants, Snowflake, Timers, Tools };
6
- export * from './basecollection';
7
- export * from './baseset';
8
- export * from './eventspewer';
package/lib/src/index.js DELETED
@@ -1,50 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
36
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.Tools = exports.Timers = exports.Snowflake = exports.Constants = void 0;
40
- const Constants = __importStar(require("./constants"));
41
- exports.Constants = Constants;
42
- const Snowflake = __importStar(require("./snowflake"));
43
- exports.Snowflake = Snowflake;
44
- const Timers = __importStar(require("./timers"));
45
- exports.Timers = Timers;
46
- const Tools = __importStar(require("./tools"));
47
- exports.Tools = Tools;
48
- __exportStar(require("./basecollection"), exports);
49
- __exportStar(require("./baseset"), exports);
50
- __exportStar(require("./eventspewer"), exports);
@@ -1,20 +0,0 @@
1
- export interface Snowflake {
2
- id: string;
3
- processId: number;
4
- sequence: number;
5
- timestamp: number;
6
- workerId: number;
7
- }
8
- export interface SnowflakeGenerateOptions {
9
- epoch?: number;
10
- processId?: number;
11
- sequence?: number;
12
- timestamp?: number;
13
- workerId?: number;
14
- }
15
- export declare function generate(options?: SnowflakeGenerateOptions): Snowflake;
16
- export interface SnowflakeDeconstructOptions {
17
- epoch?: number;
18
- }
19
- export declare function deconstruct(id: string, options?: SnowflakeDeconstructOptions): Snowflake;
20
- export declare function timestamp(id: string, options?: SnowflakeDeconstructOptions): number;