@oscarpalmer/atoms 0.189.1 → 0.190.0
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.
- package/dist/beacon.d.mts +51 -31
- package/dist/beacon.mjs +163 -141
- package/dist/color/index.d.mts +1 -2
- package/dist/color/index.mjs +2 -2
- package/dist/color/instance.d.mts +3 -167
- package/dist/color/instance.mjs +58 -217
- package/dist/color/misc/get.d.mts +1 -2
- package/dist/color/misc/get.mjs +3 -3
- package/dist/color/misc/index.d.mts +4 -4
- package/dist/color/misc/index.mjs +9 -9
- package/dist/color/misc/is.d.mts +1 -2
- package/dist/color/models.d.mts +158 -2
- package/dist/function/memoize.d.mts +3 -9
- package/dist/function/memoize.mjs +43 -90
- package/dist/function/once.mjs +16 -4
- package/dist/herald.d.mts +19 -15
- package/dist/herald.mjs +79 -89
- package/dist/index.d.mts +173 -162
- package/dist/index.mjs +750 -995
- package/dist/internal/result.d.mts +6 -6
- package/dist/internal/result.mjs +1 -1
- package/dist/internal/value/has.d.mts +2 -2
- package/dist/logger.d.mts +7 -7
- package/dist/logger.mjs +72 -51
- package/dist/promise/helpers.d.mts +2 -2
- package/dist/promise/helpers.mjs +2 -2
- package/dist/queue.d.mts +24 -12
- package/dist/queue.mjs +288 -346
- package/dist/string/fuzzy.d.mts +4 -13
- package/dist/string/fuzzy.mjs +15 -41
- package/package.json +2 -2
- package/src/beacon.ts +262 -160
- package/src/color/index.ts +8 -8
- package/src/color/instance.ts +82 -258
- package/src/color/misc/get.ts +4 -4
- package/src/color/misc/index.ts +10 -11
- package/src/color/models.ts +180 -2
- package/src/function/memoize.ts +74 -65
- package/src/function/once.ts +4 -0
- package/src/herald.ts +136 -94
- package/src/internal/result.ts +6 -6
- package/src/internal/value/has.ts +2 -2
- package/src/logger.ts +93 -42
- package/src/promise/helpers.ts +2 -2
- package/src/queue.ts +480 -375
- package/src/string/fuzzy.ts +36 -31
|
@@ -2,95 +2,6 @@ import { isPlainObject } from "../internal/is.mjs";
|
|
|
2
2
|
import { getString, join } from "../internal/string.mjs";
|
|
3
3
|
import { SizedMap } from "../sized/map.mjs";
|
|
4
4
|
//#region src/function/memoize.ts
|
|
5
|
-
/**
|
|
6
|
-
* A _Memoized_ function instance, caching and retrieving results based on the its parameters _(or a custom cache key)_
|
|
7
|
-
*/
|
|
8
|
-
var Memoized = class {
|
|
9
|
-
#state;
|
|
10
|
-
/**
|
|
11
|
-
* Maximum cache size
|
|
12
|
-
*
|
|
13
|
-
* @returns Maximum cache size _(or `Number.NaN` if the instance has been destroyed)_
|
|
14
|
-
*/
|
|
15
|
-
get maximum() {
|
|
16
|
-
return this.#state.cache?.maximum ?? NaN;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Current cache size
|
|
20
|
-
*
|
|
21
|
-
* @returns Current cache size _(or `Number.NaN` if the instance has been destroyed)_
|
|
22
|
-
*/
|
|
23
|
-
get size() {
|
|
24
|
-
return this.#state.cache?.size ?? NaN;
|
|
25
|
-
}
|
|
26
|
-
constructor(callback, options) {
|
|
27
|
-
const cache = new SizedMap(options.cacheSize);
|
|
28
|
-
const getter = (...parameters) => {
|
|
29
|
-
const key = options.cacheKey?.(...parameters) ?? (parameters.length === 1 ? parameters[0] : join(parameters.map(getString), SEPARATOR));
|
|
30
|
-
if (cache.has(key)) return cache.get(key);
|
|
31
|
-
const value = callback(...parameters);
|
|
32
|
-
cache.set(key, value);
|
|
33
|
-
return value;
|
|
34
|
-
};
|
|
35
|
-
this.#state = {
|
|
36
|
-
cache,
|
|
37
|
-
getter
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Clear the cache
|
|
42
|
-
*/
|
|
43
|
-
clear() {
|
|
44
|
-
this.#state.cache?.clear();
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Delete a result from the cache
|
|
48
|
-
*
|
|
49
|
-
* @param key Key to delete
|
|
50
|
-
* @returns `true` if the key existed and was removed, otherwise `false`
|
|
51
|
-
*/
|
|
52
|
-
delete(key) {
|
|
53
|
-
return this.#state.cache?.delete(key) ?? false;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Destroy the instance
|
|
57
|
-
*
|
|
58
|
-
* _(When a Memoized instance is destroyed, its cache and callback are removed, and calls to `run` will throw an error)_
|
|
59
|
-
*/
|
|
60
|
-
destroy() {
|
|
61
|
-
this.#state.cache?.clear();
|
|
62
|
-
this.#state.cache = void 0;
|
|
63
|
-
this.#state.getter = void 0;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Get a result from the cache
|
|
67
|
-
*
|
|
68
|
-
* @param key Key to get
|
|
69
|
-
* @returns Cached result or `undefined` if it does not exist
|
|
70
|
-
*/
|
|
71
|
-
get(key) {
|
|
72
|
-
return this.#state.cache?.get(key);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Does the result exist?
|
|
76
|
-
*
|
|
77
|
-
* @param key Key to check
|
|
78
|
-
* @returns `true` if the result exists, otherwise `false`
|
|
79
|
-
*/
|
|
80
|
-
has(key) {
|
|
81
|
-
return this.#state.cache?.has(key) ?? false;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Run the callback with the provided parameters
|
|
85
|
-
*
|
|
86
|
-
* @param parameters Parameters to pass to the callback
|
|
87
|
-
* @returns Cached or computed _(then cached)_ result
|
|
88
|
-
*/
|
|
89
|
-
run(...parameters) {
|
|
90
|
-
if (this.#state.cache == null || this.#state.getter == null) throw new Error(MEMOIZED_ERROR_DESTROYED);
|
|
91
|
-
return this.#state.getter(...parameters);
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
5
|
function getMemoizationOptions(input) {
|
|
95
6
|
const { cacheKey, cacheSize } = isPlainObject(input) ? input : {};
|
|
96
7
|
return {
|
|
@@ -107,7 +18,49 @@ function getMemoizationOptions(input) {
|
|
|
107
18
|
*/
|
|
108
19
|
function memoize(callback, options) {
|
|
109
20
|
if (typeof callback !== "function") throw new TypeError(MEMOIZED_ERROR_CALLBACK);
|
|
110
|
-
|
|
21
|
+
const state = { options: getMemoizationOptions(options) };
|
|
22
|
+
state.cache = new SizedMap(state.options.cacheSize);
|
|
23
|
+
state.getter = (...parameters) => {
|
|
24
|
+
const key = state.options.cacheKey?.(...parameters) ?? (parameters.length === 1 ? parameters[0] : join(parameters.map(getString), SEPARATOR));
|
|
25
|
+
if (state.cache.has(key)) return state.cache.get(key);
|
|
26
|
+
const value = callback(...parameters);
|
|
27
|
+
state.cache?.set(key, value);
|
|
28
|
+
return value;
|
|
29
|
+
};
|
|
30
|
+
const instance = {
|
|
31
|
+
clear: () => {
|
|
32
|
+
state.cache?.clear();
|
|
33
|
+
},
|
|
34
|
+
delete: (key) => {
|
|
35
|
+
return state.cache?.delete(key) ?? false;
|
|
36
|
+
},
|
|
37
|
+
destroy: () => {
|
|
38
|
+
state.cache?.clear();
|
|
39
|
+
state.cache = void 0;
|
|
40
|
+
state.getter = void 0;
|
|
41
|
+
},
|
|
42
|
+
get: (key) => {
|
|
43
|
+
return state.cache?.get(key);
|
|
44
|
+
},
|
|
45
|
+
has: (key) => {
|
|
46
|
+
return state.cache?.has(key) ?? false;
|
|
47
|
+
},
|
|
48
|
+
run: (...parameters) => {
|
|
49
|
+
if (state.cache == null || state.getter == null) throw new Error(MEMOIZED_ERROR_DESTROYED);
|
|
50
|
+
return state.getter(...parameters);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
Object.defineProperties(instance, {
|
|
54
|
+
maximum: {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
get: () => state.cache?.maximum ?? NaN
|
|
57
|
+
},
|
|
58
|
+
size: {
|
|
59
|
+
enumerable: true,
|
|
60
|
+
get: () => state.cache?.size ?? NaN
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
return Object.freeze(instance);
|
|
111
64
|
}
|
|
112
65
|
const DEFAULT_CACHE_SIZE = 1024;
|
|
113
66
|
const MEMOIZED_ERROR_CALLBACK = "Memoized requires a callback function";
|
package/dist/function/once.mjs
CHANGED
|
@@ -41,10 +41,22 @@ function asyncOnce(callback) {
|
|
|
41
41
|
});
|
|
42
42
|
};
|
|
43
43
|
Object.defineProperties(fn, {
|
|
44
|
-
called: {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
called: {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
get: () => state.called
|
|
47
|
+
},
|
|
48
|
+
cleared: {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
get: () => state.cleared
|
|
51
|
+
},
|
|
52
|
+
error: {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
get: () => state.error
|
|
55
|
+
},
|
|
56
|
+
finished: {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
get: () => state.finished
|
|
59
|
+
}
|
|
48
60
|
});
|
|
49
61
|
fn.clear = () => {
|
|
50
62
|
if (!state.called || !state.finished || state.cleared) return;
|
package/dist/herald.d.mts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { GenericCallback } from "./models.mjs";
|
|
2
2
|
//#region src/herald.d.ts
|
|
3
|
-
|
|
4
|
-
#private;
|
|
5
|
-
constructor(herald: Herald<Map>);
|
|
3
|
+
type Events<Map extends Record<string, GenericCallback>> = {
|
|
6
4
|
/**
|
|
7
5
|
* Subscribe to an event with a callback
|
|
8
6
|
*
|
|
@@ -19,17 +17,9 @@ declare class Events<Map extends Record<string, GenericCallback>> {
|
|
|
19
17
|
* @returns Unsubscriber function
|
|
20
18
|
*/
|
|
21
19
|
unsubscribe<Event extends keyof Map>(event: Event, callback?: Map[Event]): void;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
* A _Herald_ is an announcer for named events, allowing emission, subscription, and unsubscription of events
|
|
25
|
-
*/
|
|
26
|
-
declare class Herald<Map extends Record<string, GenericCallback>> {
|
|
27
|
-
#private;
|
|
28
|
-
/**
|
|
29
|
-
* Events interface for subscribing to and unsubscribing from events
|
|
30
|
-
*/
|
|
20
|
+
};
|
|
21
|
+
type Herald<Map extends Record<string, GenericCallback>> = {
|
|
31
22
|
readonly events: Events<Map>;
|
|
32
|
-
constructor(names: (keyof Map)[]);
|
|
33
23
|
/**
|
|
34
24
|
* Remove all event subscribers
|
|
35
25
|
*/
|
|
@@ -56,7 +46,7 @@ declare class Herald<Map extends Record<string, GenericCallback>> {
|
|
|
56
46
|
* @param callback Callback function
|
|
57
47
|
*/
|
|
58
48
|
unsubscribe<Event extends keyof Map>(event: Event, callback?: Map[Event]): void;
|
|
59
|
-
}
|
|
49
|
+
};
|
|
60
50
|
type Unsubscriber = () => void;
|
|
61
51
|
/**
|
|
62
52
|
* Create a _Herald_ for announcing named events
|
|
@@ -65,5 +55,19 @@ type Unsubscriber = () => void;
|
|
|
65
55
|
* @returns _Herald_ instance
|
|
66
56
|
*/
|
|
67
57
|
declare function herald<Events extends Record<string, GenericCallback>>(names: (keyof Events)[]): Herald<Events>;
|
|
58
|
+
/**
|
|
59
|
+
* Is the value events for a herald?
|
|
60
|
+
*
|
|
61
|
+
* @param value Value to check
|
|
62
|
+
* @returns `true` if the value is events for a herald, otherwise `false`
|
|
63
|
+
*/
|
|
64
|
+
declare function isEvents<Map extends Record<string, GenericCallback> = Record<string, GenericCallback>>(value: unknown): value is Events<Map>;
|
|
65
|
+
/**
|
|
66
|
+
* Is the value a herald?
|
|
67
|
+
*
|
|
68
|
+
* @param value Value to check
|
|
69
|
+
* @returns `true` if the value is a herald, otherwise `false`
|
|
70
|
+
*/
|
|
71
|
+
declare function isHerald<Events extends Record<string, GenericCallback> = Record<string, GenericCallback>>(value: unknown): value is Herald<Events>;
|
|
68
72
|
//#endregion
|
|
69
|
-
export {
|
|
73
|
+
export { Events, Herald, Unsubscriber, herald, isEvents, isHerald };
|
package/dist/herald.mjs
CHANGED
|
@@ -1,92 +1,16 @@
|
|
|
1
1
|
import { noop } from "./internal/function/misc.mjs";
|
|
2
2
|
//#region src/herald.ts
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*/
|
|
15
|
-
subscribe(event, callback) {
|
|
16
|
-
return this.#herald.subscribe(event, callback);
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Unsubscribe from an event with a callback _(or all callbacks, if no callback is provided)_
|
|
20
|
-
*
|
|
21
|
-
* @param event Event name
|
|
22
|
-
* @param callback Callback function
|
|
23
|
-
* @returns Unsubscriber function
|
|
24
|
-
*/
|
|
25
|
-
unsubscribe(event, callback) {
|
|
26
|
-
return this.#herald.unsubscribe(event, callback);
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
/**
|
|
30
|
-
* A _Herald_ is an announcer for named events, allowing emission, subscription, and unsubscription of events
|
|
31
|
-
*/
|
|
32
|
-
var Herald = class {
|
|
33
|
-
#names;
|
|
34
|
-
#subscribers = /* @__PURE__ */ new Map();
|
|
35
|
-
constructor(names) {
|
|
36
|
-
this.#names = new Set(names);
|
|
37
|
-
Object.defineProperty(this, "events", { value: new Events(this) });
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Remove all event subscribers
|
|
41
|
-
*/
|
|
42
|
-
clear() {
|
|
43
|
-
this.#subscribers.clear();
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Emit an event with parameters
|
|
47
|
-
*
|
|
48
|
-
* @param event Event name
|
|
49
|
-
* @param parameters Event parameters
|
|
50
|
-
*/
|
|
51
|
-
emit(event, ...parameters) {
|
|
52
|
-
const subscribers = this.#subscribers.get(event);
|
|
53
|
-
if (subscribers == null) return;
|
|
54
|
-
for (const callback of subscribers) callback(...parameters);
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Subscribe to an event with a callback
|
|
58
|
-
*
|
|
59
|
-
* @param event Event name
|
|
60
|
-
* @param callback Callback function
|
|
61
|
-
* @returns Unsubscriber function
|
|
62
|
-
*/
|
|
63
|
-
subscribe(event, callback) {
|
|
64
|
-
if (!this.#names.has(event) || typeof callback !== "function") return noop;
|
|
65
|
-
let subscribers = this.#subscribers.get(event);
|
|
66
|
-
if (subscribers == null) {
|
|
67
|
-
subscribers = /* @__PURE__ */ new Set();
|
|
68
|
-
this.#subscribers.set(event, subscribers);
|
|
69
|
-
}
|
|
70
|
-
subscribers.add(callback);
|
|
71
|
-
return () => {
|
|
72
|
-
subscribers?.delete(callback);
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Unsubscribe from an event with a callback _(or all callbacks, if no callback is provided)_
|
|
77
|
-
*
|
|
78
|
-
* @param event Event name
|
|
79
|
-
* @param callback Callback function
|
|
80
|
-
*/
|
|
81
|
-
unsubscribe(event, callback) {
|
|
82
|
-
if (!this.#names.has(event) || (callback != null ? typeof callback !== "function" : false)) return;
|
|
83
|
-
const subscribers = this.#subscribers.get(event);
|
|
84
|
-
if (subscribers == null) return;
|
|
85
|
-
if (callback == null) subscribers.clear();
|
|
86
|
-
else subscribers.delete(callback);
|
|
87
|
-
if (callback == null || subscribers.size === 0) this.#subscribers.delete(event);
|
|
88
|
-
}
|
|
89
|
-
};
|
|
3
|
+
function getEvents(herald) {
|
|
4
|
+
const events = {
|
|
5
|
+
subscribe: (event, callback) => herald.subscribe(event, callback),
|
|
6
|
+
unsubscribe: (event, callback) => herald.unsubscribe(event, callback)
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(events, KEY_HERALD, {
|
|
9
|
+
enumerable: false,
|
|
10
|
+
value: NAME_EVENTS
|
|
11
|
+
});
|
|
12
|
+
return Object.freeze(events);
|
|
13
|
+
}
|
|
90
14
|
/**
|
|
91
15
|
* Create a _Herald_ for announcing named events
|
|
92
16
|
*
|
|
@@ -95,8 +19,74 @@ var Herald = class {
|
|
|
95
19
|
*/
|
|
96
20
|
function herald(names) {
|
|
97
21
|
if (!Array.isArray(names) || names.length === 0 || !names.every((name) => typeof name === "string")) throw new Error(MESSAGE);
|
|
98
|
-
|
|
22
|
+
const set = new Set(names);
|
|
23
|
+
const subscribers = /* @__PURE__ */ new Map();
|
|
24
|
+
const instance = {
|
|
25
|
+
clear() {
|
|
26
|
+
subscribers.clear();
|
|
27
|
+
},
|
|
28
|
+
emit(event, ...parameters) {
|
|
29
|
+
const callbacks = subscribers.get(event);
|
|
30
|
+
if (callbacks == null) return;
|
|
31
|
+
for (const callback of callbacks) callback(...parameters);
|
|
32
|
+
},
|
|
33
|
+
subscribe(event, callback) {
|
|
34
|
+
if (!set.has(event) || typeof callback !== "function") return noop;
|
|
35
|
+
let eventSubscribers = subscribers.get(event);
|
|
36
|
+
if (eventSubscribers == null) {
|
|
37
|
+
eventSubscribers = /* @__PURE__ */ new Set();
|
|
38
|
+
subscribers.set(event, eventSubscribers);
|
|
39
|
+
}
|
|
40
|
+
eventSubscribers.add(callback);
|
|
41
|
+
return () => {
|
|
42
|
+
eventSubscribers?.delete(callback);
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
unsubscribe(event, callback) {
|
|
46
|
+
if (!set.has(event) || (callback != null ? typeof callback !== "function" : false)) return;
|
|
47
|
+
const eventSubscribers = subscribers.get(event);
|
|
48
|
+
if (eventSubscribers == null) return;
|
|
49
|
+
if (callback == null) eventSubscribers.clear();
|
|
50
|
+
else eventSubscribers.delete(callback);
|
|
51
|
+
if (callback == null || eventSubscribers.size === 0) subscribers.delete(event);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
Object.defineProperties(instance, {
|
|
55
|
+
[KEY_HERALD]: {
|
|
56
|
+
enumerable: false,
|
|
57
|
+
value: NAME_HERALD
|
|
58
|
+
},
|
|
59
|
+
events: {
|
|
60
|
+
enumerable: true,
|
|
61
|
+
value: getEvents(instance)
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
return Object.freeze(instance);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Is the value events for a herald?
|
|
68
|
+
*
|
|
69
|
+
* @param value Value to check
|
|
70
|
+
* @returns `true` if the value is events for a herald, otherwise `false`
|
|
71
|
+
*/
|
|
72
|
+
function isEvents(value) {
|
|
73
|
+
return isHeraldInstance(NAME_EVENTS, value);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Is the value a herald?
|
|
77
|
+
*
|
|
78
|
+
* @param value Value to check
|
|
79
|
+
* @returns `true` if the value is a herald, otherwise `false`
|
|
80
|
+
*/
|
|
81
|
+
function isHerald(value) {
|
|
82
|
+
return isHeraldInstance(NAME_HERALD, value);
|
|
83
|
+
}
|
|
84
|
+
function isHeraldInstance(name, value) {
|
|
85
|
+
return typeof value === "object" && value != null && KEY_HERALD in value && value[KEY_HERALD] === name;
|
|
99
86
|
}
|
|
87
|
+
const KEY_HERALD = "$herald";
|
|
100
88
|
const MESSAGE = "Herald requires an array of event names.";
|
|
89
|
+
const NAME_EVENTS = "events";
|
|
90
|
+
const NAME_HERALD = "herald";
|
|
101
91
|
//#endregion
|
|
102
|
-
export { herald };
|
|
92
|
+
export { herald, isEvents, isHerald };
|