@softsky/utils 2.2.0 → 2.3.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/README.md +20 -2
- package/dist/formatting.d.ts +23 -0
- package/dist/formatting.js +7 -0
- package/dist/signals.d.ts +8 -2
- package/dist/signals.js +27 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -183,6 +183,18 @@ ${\textsf{\color{CornflowerBlue}function}}$ log - Format logging
|
|
|
183
183
|
---
|
|
184
184
|
${\textsf{\color{CornflowerBlue}function}}$ capitalizeFirstLetter - Capitalize first letter
|
|
185
185
|
|
|
186
|
+
---
|
|
187
|
+
${\textsf{\color{CornflowerBlue}function}}$ pipe - pipe() can be called on one or more functions, each of which can take the return of previous value.
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
// Takes string, converts to int, calc sqrt, convert and return date
|
|
191
|
+
pipe(
|
|
192
|
+
(x: string) => Number.parseInt(x),
|
|
193
|
+
(x) => Math.sqrt(x),
|
|
194
|
+
(x) => new Date(x)
|
|
195
|
+
)('69')
|
|
196
|
+
```
|
|
197
|
+
|
|
186
198
|
---
|
|
187
199
|
|
|
188
200
|
|
|
@@ -339,9 +351,15 @@ $b(5);
|
|
|
339
351
|
---
|
|
340
352
|
${\textsf{\color{CornflowerBlue}function}}$ when - __SIGNALS SYSTEM__
|
|
341
353
|
|
|
342
|
-
Returns
|
|
354
|
+
Returns ImmediatePromise that is resolved when check function returns truthy value.
|
|
355
|
+
If you want to, you can resolve or reject promise beforehand.
|
|
356
|
+
|
|
343
357
|
```ts
|
|
344
|
-
await when(()
|
|
358
|
+
await when(() => $a()>5)
|
|
359
|
+
// With timeout
|
|
360
|
+
const promise = when(() => $a() > 5)
|
|
361
|
+
const timeout = setTimeout(() => promise.reject('Timeout')}, 5000)
|
|
362
|
+
primise.then(() => clearTimeout(timeout))
|
|
345
363
|
```
|
|
346
364
|
|
|
347
365
|
---
|
package/dist/formatting.d.ts
CHANGED
|
@@ -40,3 +40,26 @@ export declare function formatBytes(bytes: number): string;
|
|
|
40
40
|
export declare function log(...agrs: unknown[]): void;
|
|
41
41
|
/** Capitalize first letter */
|
|
42
42
|
export declare function capitalizeFirstLetter(value: string): string;
|
|
43
|
+
/**
|
|
44
|
+
* pipe() can be called on one or more functions, each of which can take the return of previous value.
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* // Takes string, converts to int, calc sqrt, convert and return date
|
|
48
|
+
* pipe(
|
|
49
|
+
* (x: string) => Number.parseInt(x),
|
|
50
|
+
* (x) => Math.sqrt(x),
|
|
51
|
+
* (x) => new Date(x)
|
|
52
|
+
* )('69')
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare function pipe(): <T>(x: T) => T;
|
|
56
|
+
export declare function pipe<T, A>(function1: (x: T) => A): (x: T) => A;
|
|
57
|
+
export declare function pipe<T, A, B>(function1: (x: T) => A, function2: (x: A) => B): (x: T) => B;
|
|
58
|
+
export declare function pipe<T, A, B, C>(function1: (x: T) => A, function2: (x: A) => B, function3: (x: B) => C): (x: T) => C;
|
|
59
|
+
export declare function pipe<T, A, B, C, D>(function1: (x: T) => A, function2: (x: A) => B, function3: (x: B) => C, function4: (x: C) => D): (x: T) => D;
|
|
60
|
+
export declare function pipe<T, A, B, C, D, E>(function1: (x: T) => A, function2: (x: A) => B, function3: (x: B) => C, function4: (x: C) => D, function5: (x: D) => E): (x: T) => E;
|
|
61
|
+
export declare function pipe<T, A, B, C, D, E, F>(function1: (x: T) => A, function2: (x: A) => B, function3: (x: B) => C, function4: (x: C) => D, function5: (x: D) => E, function6: (x: E) => F): (x: T) => F;
|
|
62
|
+
export declare function pipe<T, A, B, C, D, E, F, G>(function1: (x: T) => A, function2: (x: A) => B, function3: (x: B) => C, function4: (x: C) => D, function5: (x: D) => E, function6: (x: E) => F, function7: (x: F) => G): (x: T) => G;
|
|
63
|
+
export declare function pipe<T, A, B, C, D, E, F, G, H>(function1: (x: T) => A, function2: (x: A) => B, function3: (x: B) => C, function4: (x: C) => D, function5: (x: D) => E, function6: (x: E) => F, function7: (x: F) => G, function8: (x: G) => H): (x: T) => H;
|
|
64
|
+
export declare function pipe<T, A, B, C, D, E, F, G, H, I>(function1: (x: T) => A, function2: (x: A) => B, function3: (x: B) => C, function4: (x: C) => D, function5: (x: D) => E, function6: (x: E) => F, function7: (x: F) => G, function8: (x: G) => H, function9: (x: H) => I): (x: T) => I;
|
|
65
|
+
export declare function pipe<T, A, B, C, D, E, F, G, H>(function1: (x: T) => A, function2: (x: A) => B, function3: (x: B) => C, function4: (x: C) => D, function5: (x: D) => E, function6: (x: E) => F, function7: (x: F) => G, function8: (x: G) => H, function9: (x: H) => unknown, ...fns: ((x: unknown) => unknown)[]): (x: T) => unknown;
|
package/dist/formatting.js
CHANGED
|
@@ -117,3 +117,10 @@ export function log(...agrs) {
|
|
|
117
117
|
export function capitalizeFirstLetter(value) {
|
|
118
118
|
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
119
119
|
}
|
|
120
|
+
export function pipe(...fns) {
|
|
121
|
+
return (input) => {
|
|
122
|
+
for (let index = 0; index < fns.length; index++)
|
|
123
|
+
input = fns[index](input);
|
|
124
|
+
return input;
|
|
125
|
+
};
|
|
126
|
+
}
|
package/dist/signals.d.ts
CHANGED
|
@@ -100,9 +100,15 @@ export declare function batch(handler: AnyFunction): void;
|
|
|
100
100
|
/**
|
|
101
101
|
* __SIGNALS SYSTEM__
|
|
102
102
|
*
|
|
103
|
-
* Returns
|
|
103
|
+
* Returns ImmediatePromise that is resolved when check function returns truthy value.
|
|
104
|
+
* If you want to, you can resolve or reject promise beforehand.
|
|
105
|
+
*
|
|
104
106
|
* ```ts
|
|
105
|
-
* await when(()
|
|
107
|
+
* await when(() => $a()>5)
|
|
108
|
+
* // With timeout
|
|
109
|
+
* const promise = when(() => $a() > 5)
|
|
110
|
+
* const timeout = setTimeout(() => promise.reject('Timeout')}, 5000)
|
|
111
|
+
* primise.then(() => clearTimeout(timeout))
|
|
106
112
|
* ```
|
|
107
113
|
*/
|
|
108
114
|
export declare function when(check: () => unknown): ImmediatePromise<undefined>;
|
package/dist/signals.js
CHANGED
|
@@ -22,12 +22,24 @@ export function signal(value) {
|
|
|
22
22
|
subscriber();
|
|
23
23
|
}
|
|
24
24
|
else if (currentEffect) {
|
|
25
|
-
effectsMap.set(currentEffect, subscribers);
|
|
26
25
|
subscribers.add(currentEffect);
|
|
26
|
+
// This is for clear() of effects
|
|
27
|
+
let effectSubscribers = effectsMap.get(currentEffect);
|
|
28
|
+
if (!effectSubscribers) {
|
|
29
|
+
effectSubscribers = new Set();
|
|
30
|
+
effectsMap.set(currentEffect, effectSubscribers);
|
|
31
|
+
}
|
|
32
|
+
effectSubscribers.add(subscribers);
|
|
27
33
|
}
|
|
28
34
|
return value;
|
|
29
35
|
};
|
|
30
36
|
}
|
|
37
|
+
function clearEffect(handler) {
|
|
38
|
+
const signalSubscribers = effectsMap.get(handler);
|
|
39
|
+
if (signalSubscribers)
|
|
40
|
+
for (const subscribers of signalSubscribers)
|
|
41
|
+
subscribers.delete(handler);
|
|
42
|
+
}
|
|
31
43
|
/**
|
|
32
44
|
* __SIGNALS SYSTEM__
|
|
33
45
|
*
|
|
@@ -56,7 +68,7 @@ export function effect(handler, initialValue) {
|
|
|
56
68
|
wrappedHandler();
|
|
57
69
|
currentEffect = undefined;
|
|
58
70
|
return () => {
|
|
59
|
-
|
|
71
|
+
clearEffect(wrappedHandler);
|
|
60
72
|
};
|
|
61
73
|
}
|
|
62
74
|
/**
|
|
@@ -88,7 +100,7 @@ export function derived(handler, initialValue) {
|
|
|
88
100
|
return {
|
|
89
101
|
signal: signal$,
|
|
90
102
|
clear: () => {
|
|
91
|
-
|
|
103
|
+
clearEffect(wrappedHandler);
|
|
92
104
|
},
|
|
93
105
|
};
|
|
94
106
|
}
|
|
@@ -122,19 +134,25 @@ export function batch(handler) {
|
|
|
122
134
|
/**
|
|
123
135
|
* __SIGNALS SYSTEM__
|
|
124
136
|
*
|
|
125
|
-
* Returns
|
|
137
|
+
* Returns ImmediatePromise that is resolved when check function returns truthy value.
|
|
138
|
+
* If you want to, you can resolve or reject promise beforehand.
|
|
139
|
+
*
|
|
126
140
|
* ```ts
|
|
127
|
-
* await when(()
|
|
141
|
+
* await when(() => $a()>5)
|
|
142
|
+
* // With timeout
|
|
143
|
+
* const promise = when(() => $a() > 5)
|
|
144
|
+
* const timeout = setTimeout(() => promise.reject('Timeout')}, 5000)
|
|
145
|
+
* primise.then(() => clearTimeout(timeout))
|
|
128
146
|
* ```
|
|
129
147
|
*/
|
|
130
148
|
export function when(check) {
|
|
131
149
|
const promise = new ImmediatePromise();
|
|
132
150
|
const clear = effect(() => {
|
|
133
151
|
if (check())
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
152
|
+
promise.resolve();
|
|
153
|
+
});
|
|
154
|
+
void promise.finally(() => {
|
|
155
|
+
clear();
|
|
138
156
|
});
|
|
139
157
|
return promise;
|
|
140
158
|
}
|