@just-io/utils 1.0.2 → 1.2.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/cjs/debounce.js +39 -0
- package/dist/cjs/deep-map.js +44 -8
- package/dist/cjs/event-emitter.js +3 -0
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/memo.js +40 -0
- package/dist/mjs/debounce.js +35 -0
- package/dist/mjs/deep-map.js +37 -1
- package/dist/mjs/event-emitter.js +3 -0
- package/dist/mjs/index.js +2 -0
- package/dist/mjs/memo.js +36 -0
- package/dist/types/debounce.d.ts +7 -0
- package/dist/types/deep-map.d.ts +7 -1
- package/dist/types/event-emitter.d.ts +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/memo.d.ts +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.debounce = debounce;
|
|
4
|
+
exports.debounceByKey = debounceByKey;
|
|
5
|
+
function debounce(func, debounceTime) {
|
|
6
|
+
let timer;
|
|
7
|
+
const debounceFunc = (...args) => {
|
|
8
|
+
clearTimeout(timer);
|
|
9
|
+
timer = setTimeout(() => {
|
|
10
|
+
func(...args);
|
|
11
|
+
}, debounceTime);
|
|
12
|
+
};
|
|
13
|
+
debounceFunc.teardown = () => {
|
|
14
|
+
clearTimeout(timer);
|
|
15
|
+
};
|
|
16
|
+
return debounceFunc;
|
|
17
|
+
}
|
|
18
|
+
function debounceByKey(func, extractKey, debounceTime) {
|
|
19
|
+
const map = new Map();
|
|
20
|
+
const debounceFunc = (...args) => {
|
|
21
|
+
const key = extractKey(...args);
|
|
22
|
+
clearTimeout(map.get(key));
|
|
23
|
+
const timer = setTimeout(() => {
|
|
24
|
+
map.delete(key);
|
|
25
|
+
func(...args);
|
|
26
|
+
}, debounceTime);
|
|
27
|
+
map.set(key, timer);
|
|
28
|
+
};
|
|
29
|
+
debounceFunc.teardown = (...args) => {
|
|
30
|
+
const key = extractKey(...args);
|
|
31
|
+
clearTimeout(map.get(key));
|
|
32
|
+
map.delete(key);
|
|
33
|
+
};
|
|
34
|
+
debounceFunc.teardownAll = () => {
|
|
35
|
+
map.forEach((timer) => clearTimeout(timer));
|
|
36
|
+
map.clear();
|
|
37
|
+
};
|
|
38
|
+
return debounceFunc;
|
|
39
|
+
}
|
package/dist/cjs/deep-map.js
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
2
7
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
8
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
9
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
11
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
12
|
};
|
|
8
|
-
var
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
-
};
|
|
13
|
-
var _DeepMap_rootNode;
|
|
13
|
+
var _DeepMap_instances, _DeepMap_rootNode, _DeepMap_entries;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.DeepMap = void 0;
|
|
16
|
-
class DeepMap {
|
|
16
|
+
class DeepMap extends Map {
|
|
17
17
|
constructor() {
|
|
18
|
+
super(...arguments);
|
|
19
|
+
_DeepMap_instances.add(this);
|
|
18
20
|
_DeepMap_rootNode.set(this, {
|
|
19
21
|
children: new Map(),
|
|
20
22
|
});
|
|
@@ -83,6 +85,40 @@ class DeepMap {
|
|
|
83
85
|
this.delete(keys);
|
|
84
86
|
return value;
|
|
85
87
|
}
|
|
88
|
+
get size() {
|
|
89
|
+
let count = 0;
|
|
90
|
+
for (const {} of __classPrivateFieldGet(this, _DeepMap_instances, "m", _DeepMap_entries).call(this, __classPrivateFieldGet(this, _DeepMap_rootNode, "f"), [])) {
|
|
91
|
+
count++;
|
|
92
|
+
}
|
|
93
|
+
return count;
|
|
94
|
+
}
|
|
95
|
+
forEach(callbackfn) {
|
|
96
|
+
for (const entry of __classPrivateFieldGet(this, _DeepMap_instances, "m", _DeepMap_entries).call(this, __classPrivateFieldGet(this, _DeepMap_rootNode, "f"), [])) {
|
|
97
|
+
callbackfn(entry[1], entry[0], this);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
[(_DeepMap_rootNode = new WeakMap(), _DeepMap_instances = new WeakSet(), _DeepMap_entries = function* _DeepMap_entries(node, key) {
|
|
101
|
+
if (node.value !== undefined) {
|
|
102
|
+
yield [key, node.value];
|
|
103
|
+
}
|
|
104
|
+
for (const child of node.children) {
|
|
105
|
+
yield* __classPrivateFieldGet(this, _DeepMap_instances, "m", _DeepMap_entries).call(this, child[1], key.concat(child[0]));
|
|
106
|
+
}
|
|
107
|
+
}, Symbol.iterator)]() {
|
|
108
|
+
return __classPrivateFieldGet(this, _DeepMap_instances, "m", _DeepMap_entries).call(this, __classPrivateFieldGet(this, _DeepMap_rootNode, "f"), []);
|
|
109
|
+
}
|
|
110
|
+
entries() {
|
|
111
|
+
return __classPrivateFieldGet(this, _DeepMap_instances, "m", _DeepMap_entries).call(this, __classPrivateFieldGet(this, _DeepMap_rootNode, "f"), []);
|
|
112
|
+
}
|
|
113
|
+
*keys() {
|
|
114
|
+
for (const entry of __classPrivateFieldGet(this, _DeepMap_instances, "m", _DeepMap_entries).call(this, __classPrivateFieldGet(this, _DeepMap_rootNode, "f"), [])) {
|
|
115
|
+
yield entry[0];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
*values() {
|
|
119
|
+
for (const entry of __classPrivateFieldGet(this, _DeepMap_instances, "m", _DeepMap_entries).call(this, __classPrivateFieldGet(this, _DeepMap_rootNode, "f"), [])) {
|
|
120
|
+
yield entry[1];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
86
123
|
}
|
|
87
124
|
exports.DeepMap = DeepMap;
|
|
88
|
-
_DeepMap_rootNode = new WeakMap();
|
|
@@ -18,6 +18,9 @@ class Notifier {
|
|
|
18
18
|
_Notifier_subscribers.set(this, new Set());
|
|
19
19
|
_Notifier_onceSubscribers.set(this, new WeakSet());
|
|
20
20
|
}
|
|
21
|
+
getSuscribers() {
|
|
22
|
+
return __classPrivateFieldGet(this, _Notifier_subscribers, "f");
|
|
23
|
+
}
|
|
21
24
|
subscribe(subscriber, options) {
|
|
22
25
|
__classPrivateFieldGet(this, _Notifier_subscribers, "f").add(subscriber);
|
|
23
26
|
if (options === null || options === void 0 ? void 0 : options.once) {
|
package/dist/cjs/index.js
CHANGED
|
@@ -16,3 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./event-emitter"), exports);
|
|
18
18
|
__exportStar(require("./deep-map"), exports);
|
|
19
|
+
__exportStar(require("./debounce"), exports);
|
|
20
|
+
__exportStar(require("./memo"), exports);
|
package/dist/cjs/memo.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.memo = memo;
|
|
4
|
+
exports.memoByKey = memoByKey;
|
|
5
|
+
function memo(func, extractMemoValues, onlyChanges = false) {
|
|
6
|
+
let oldMemoValues;
|
|
7
|
+
return (...args) => {
|
|
8
|
+
const memoValues = extractMemoValues(...args);
|
|
9
|
+
if (!oldMemoValues) {
|
|
10
|
+
if (!onlyChanges) {
|
|
11
|
+
func(...args);
|
|
12
|
+
}
|
|
13
|
+
oldMemoValues = memoValues;
|
|
14
|
+
}
|
|
15
|
+
else if (oldMemoValues.length !== memoValues.length ||
|
|
16
|
+
oldMemoValues.some((value, i) => value !== memoValues[i])) {
|
|
17
|
+
func(...args);
|
|
18
|
+
oldMemoValues = memoValues;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function memoByKey(func, extractKey, extractMemoValues, onlyChanges = false) {
|
|
23
|
+
const map = new Map();
|
|
24
|
+
return (...args) => {
|
|
25
|
+
const key = extractKey(...args);
|
|
26
|
+
const oldMemoValues = map.get(key);
|
|
27
|
+
const memoValues = extractMemoValues(...args);
|
|
28
|
+
if (!oldMemoValues) {
|
|
29
|
+
if (!onlyChanges) {
|
|
30
|
+
func(...args);
|
|
31
|
+
}
|
|
32
|
+
map.set(key, memoValues);
|
|
33
|
+
}
|
|
34
|
+
else if (oldMemoValues.length !== memoValues.length ||
|
|
35
|
+
oldMemoValues.some((value, i) => value !== memoValues[i])) {
|
|
36
|
+
func(...args);
|
|
37
|
+
map.set(key, memoValues);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export function debounce(func, debounceTime) {
|
|
2
|
+
let timer;
|
|
3
|
+
const debounceFunc = (...args) => {
|
|
4
|
+
clearTimeout(timer);
|
|
5
|
+
timer = setTimeout(() => {
|
|
6
|
+
func(...args);
|
|
7
|
+
}, debounceTime);
|
|
8
|
+
};
|
|
9
|
+
debounceFunc.teardown = () => {
|
|
10
|
+
clearTimeout(timer);
|
|
11
|
+
};
|
|
12
|
+
return debounceFunc;
|
|
13
|
+
}
|
|
14
|
+
export function debounceByKey(func, extractKey, debounceTime) {
|
|
15
|
+
const map = new Map();
|
|
16
|
+
const debounceFunc = (...args) => {
|
|
17
|
+
const key = extractKey(...args);
|
|
18
|
+
clearTimeout(map.get(key));
|
|
19
|
+
const timer = setTimeout(() => {
|
|
20
|
+
map.delete(key);
|
|
21
|
+
func(...args);
|
|
22
|
+
}, debounceTime);
|
|
23
|
+
map.set(key, timer);
|
|
24
|
+
};
|
|
25
|
+
debounceFunc.teardown = (...args) => {
|
|
26
|
+
const key = extractKey(...args);
|
|
27
|
+
clearTimeout(map.get(key));
|
|
28
|
+
map.delete(key);
|
|
29
|
+
};
|
|
30
|
+
debounceFunc.teardownAll = () => {
|
|
31
|
+
map.forEach((timer) => clearTimeout(timer));
|
|
32
|
+
map.clear();
|
|
33
|
+
};
|
|
34
|
+
return debounceFunc;
|
|
35
|
+
}
|
package/dist/mjs/deep-map.js
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
export class DeepMap {
|
|
1
|
+
export class DeepMap extends Map {
|
|
2
2
|
#rootNode = {
|
|
3
3
|
children: new Map(),
|
|
4
4
|
};
|
|
5
|
+
*#entries(node, key) {
|
|
6
|
+
if (node.value !== undefined) {
|
|
7
|
+
yield [key, node.value];
|
|
8
|
+
}
|
|
9
|
+
for (const child of node.children) {
|
|
10
|
+
yield* this.#entries(child[1], key.concat(child[0]));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
5
13
|
clear() {
|
|
6
14
|
this.#rootNode = {
|
|
7
15
|
children: new Map(),
|
|
@@ -66,4 +74,32 @@ export class DeepMap {
|
|
|
66
74
|
this.delete(keys);
|
|
67
75
|
return value;
|
|
68
76
|
}
|
|
77
|
+
get size() {
|
|
78
|
+
let count = 0;
|
|
79
|
+
for (const {} of this.#entries(this.#rootNode, [])) {
|
|
80
|
+
count++;
|
|
81
|
+
}
|
|
82
|
+
return count;
|
|
83
|
+
}
|
|
84
|
+
forEach(callbackfn) {
|
|
85
|
+
for (const entry of this.#entries(this.#rootNode, [])) {
|
|
86
|
+
callbackfn(entry[1], entry[0], this);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
[Symbol.iterator]() {
|
|
90
|
+
return this.#entries(this.#rootNode, []);
|
|
91
|
+
}
|
|
92
|
+
entries() {
|
|
93
|
+
return this.#entries(this.#rootNode, []);
|
|
94
|
+
}
|
|
95
|
+
*keys() {
|
|
96
|
+
for (const entry of this.#entries(this.#rootNode, [])) {
|
|
97
|
+
yield entry[0];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
*values() {
|
|
101
|
+
for (const entry of this.#entries(this.#rootNode, [])) {
|
|
102
|
+
yield entry[1];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
69
105
|
}
|
package/dist/mjs/index.js
CHANGED
package/dist/mjs/memo.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export function memo(func, extractMemoValues, onlyChanges = false) {
|
|
2
|
+
let oldMemoValues;
|
|
3
|
+
return (...args) => {
|
|
4
|
+
const memoValues = extractMemoValues(...args);
|
|
5
|
+
if (!oldMemoValues) {
|
|
6
|
+
if (!onlyChanges) {
|
|
7
|
+
func(...args);
|
|
8
|
+
}
|
|
9
|
+
oldMemoValues = memoValues;
|
|
10
|
+
}
|
|
11
|
+
else if (oldMemoValues.length !== memoValues.length ||
|
|
12
|
+
oldMemoValues.some((value, i) => value !== memoValues[i])) {
|
|
13
|
+
func(...args);
|
|
14
|
+
oldMemoValues = memoValues;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export function memoByKey(func, extractKey, extractMemoValues, onlyChanges = false) {
|
|
19
|
+
const map = new Map();
|
|
20
|
+
return (...args) => {
|
|
21
|
+
const key = extractKey(...args);
|
|
22
|
+
const oldMemoValues = map.get(key);
|
|
23
|
+
const memoValues = extractMemoValues(...args);
|
|
24
|
+
if (!oldMemoValues) {
|
|
25
|
+
if (!onlyChanges) {
|
|
26
|
+
func(...args);
|
|
27
|
+
}
|
|
28
|
+
map.set(key, memoValues);
|
|
29
|
+
}
|
|
30
|
+
else if (oldMemoValues.length !== memoValues.length ||
|
|
31
|
+
oldMemoValues.some((value, i) => value !== memoValues[i])) {
|
|
32
|
+
func(...args);
|
|
33
|
+
map.set(key, memoValues);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare function debounce<A extends unknown[]>(func: (...args: A) => void, debounceTime: number): ((...args: A) => void) & {
|
|
2
|
+
teardown: () => void;
|
|
3
|
+
};
|
|
4
|
+
export declare function debounceByKey<K, A extends unknown[]>(func: (...args: A) => void, extractKey: (...args: A) => K, debounceTime: number): ((...args: A) => void) & {
|
|
5
|
+
teardown: (...args: A) => void;
|
|
6
|
+
teardownAll: () => void;
|
|
7
|
+
};
|
package/dist/types/deep-map.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare class DeepMap<K, V> {
|
|
1
|
+
export declare class DeepMap<K, V> extends Map<K[], V> {
|
|
2
2
|
#private;
|
|
3
3
|
clear(): void;
|
|
4
4
|
set(keys: K[], value: V): this;
|
|
@@ -6,4 +6,10 @@ export declare class DeepMap<K, V> {
|
|
|
6
6
|
has(keys: K[]): boolean;
|
|
7
7
|
delete(keys: K[]): boolean;
|
|
8
8
|
extract(keys: K[]): V | undefined;
|
|
9
|
+
get size(): number;
|
|
10
|
+
forEach(callbackfn: (value: V, key: K[], map: Map<K[], V>) => void): void;
|
|
11
|
+
[Symbol.iterator](): MapIterator<[K[], V]>;
|
|
12
|
+
entries(): MapIterator<[K[], V]>;
|
|
13
|
+
keys(): MapIterator<K[]>;
|
|
14
|
+
values(): MapIterator<V>;
|
|
9
15
|
}
|
|
@@ -7,6 +7,7 @@ export interface Notifiable<E extends unknown[]> {
|
|
|
7
7
|
}
|
|
8
8
|
export declare class Notifier<E extends unknown[]> implements Notifiable<E> {
|
|
9
9
|
#private;
|
|
10
|
+
getSuscribers(): Set<Subscriber<E>>;
|
|
10
11
|
subscribe(subscriber: Subscriber<E>, options?: {
|
|
11
12
|
once?: boolean;
|
|
12
13
|
}): this;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare function memo<A extends unknown[]>(func: (...args: A) => void, extractMemoValues: (...args: A) => unknown[], onlyChanges?: boolean): (...args: A) => void;
|
|
2
|
+
export declare function memoByKey<K, A extends unknown[]>(func: (...args: A) => void, extractKey: (...args: A) => K, extractMemoValues: (...args: A) => unknown[], onlyChanges?: boolean): (...args: A) => void;
|