@noeldemartin/utils 0.7.3 → 0.8.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/_virtual/_rolldown/runtime.js +13 -0
- package/dist/fluent/FluentArray.d.ts +48 -0
- package/dist/fluent/FluentArray.js +48 -0
- package/dist/fluent/FluentArray.js.map +1 -0
- package/dist/fluent/FluentObject.d.ts +16 -0
- package/dist/fluent/FluentObject.js +45 -0
- package/dist/fluent/FluentObject.js.map +1 -0
- package/dist/fluent/FluentString.d.ts +21 -0
- package/dist/fluent/FluentString.js +28 -0
- package/dist/fluent/FluentString.js.map +1 -0
- package/dist/fluent/facade.d.ts +17 -0
- package/dist/fluent/facade.js +45 -0
- package/dist/fluent/facade.js.map +1 -0
- package/dist/fluent/index.d.ts +29 -0
- package/dist/fluent/index.js +41 -0
- package/dist/fluent/index.js.map +1 -0
- package/dist/fluent/invade.d.ts +6 -0
- package/dist/fluent/invade.js +8 -0
- package/dist/fluent/invade.js.map +1 -0
- package/dist/fluent/lazyFacade.d.ts +16 -0
- package/dist/fluent/lazyFacade.js +67 -0
- package/dist/fluent/lazyFacade.js.map +1 -0
- package/dist/fluent/noop.d.ts +4 -0
- package/dist/fluent/noop.js +6 -0
- package/dist/fluent/noop.js.map +1 -0
- package/dist/fluent/required.d.ts +5 -0
- package/dist/fluent/required.js +32 -0
- package/dist/fluent/required.js.map +1 -0
- package/dist/fluent/tap.d.ts +7 -0
- package/dist/fluent/tap.js +22 -0
- package/dist/fluent/tap.js.map +1 -0
- package/dist/fluent/value.d.ts +5 -0
- package/dist/fluent/value.js +8 -0
- package/dist/fluent/value.js.map +1 -0
- package/dist/fluent/when.d.ts +6 -0
- package/dist/fluent/when.js +11 -0
- package/dist/fluent/when.js.map +1 -0
- package/dist/helpers/InMemoryStorageService.d.ts +8 -0
- package/dist/helpers/InMemoryStorageService.js +16 -0
- package/dist/helpers/InMemoryStorageService.js.map +1 -0
- package/dist/helpers/IndexedDBMap.d.ts +17 -0
- package/dist/helpers/IndexedDBMap.js +52 -0
- package/dist/helpers/IndexedDBMap.js.map +1 -0
- package/dist/helpers/JSError.d.ts +14 -0
- package/dist/helpers/JSError.js +14 -0
- package/dist/helpers/JSError.js.map +1 -0
- package/dist/helpers/ListenersManager.d.ts +15 -0
- package/dist/helpers/ListenersManager.js +23 -0
- package/dist/helpers/ListenersManager.js.map +1 -0
- package/dist/helpers/MagicObject.d.ts +29 -0
- package/dist/helpers/MagicObject.js +77 -0
- package/dist/helpers/MagicObject.js.map +1 -0
- package/dist/helpers/ObjectsMap.d.ts +28 -0
- package/dist/helpers/ObjectsMap.js +86 -0
- package/dist/helpers/ObjectsMap.js.map +1 -0
- package/dist/helpers/PromisedValue.d.ts +32 -0
- package/dist/helpers/PromisedValue.js +68 -0
- package/dist/helpers/PromisedValue.js.map +1 -0
- package/dist/helpers/Semaphore.d.ts +14 -0
- package/dist/helpers/Semaphore.js +45 -0
- package/dist/helpers/Semaphore.js.map +1 -0
- package/dist/helpers/Storage.d.ts +17 -0
- package/dist/helpers/Storage.js +39 -0
- package/dist/helpers/Storage.js.map +1 -0
- package/dist/helpers/array_helpers.d.ts +57 -0
- package/dist/helpers/array_helpers.js +208 -0
- package/dist/helpers/array_helpers.js.map +1 -0
- package/dist/helpers/boolean_helpers.d.ts +4 -0
- package/dist/helpers/boolean_helpers.js +10 -0
- package/dist/helpers/boolean_helpers.js.map +1 -0
- package/dist/helpers/caching_helpers.d.ts +17 -0
- package/dist/helpers/caching_helpers.js +55 -0
- package/dist/helpers/caching_helpers.js.map +1 -0
- package/dist/helpers/chaos_helpers.d.ts +6 -0
- package/dist/helpers/chaos_helpers.js +11 -0
- package/dist/helpers/chaos_helpers.js.map +1 -0
- package/dist/helpers/date_helpers.d.ts +4 -0
- package/dist/helpers/date_helpers.js +15 -0
- package/dist/helpers/date_helpers.js.map +1 -0
- package/dist/helpers/deprecated.d.ts +24 -0
- package/dist/helpers/env.d.ts +9 -0
- package/dist/helpers/env.js +42 -0
- package/dist/helpers/env.js.map +1 -0
- package/dist/helpers/error_helpers.d.ts +12 -0
- package/dist/helpers/error_helpers.js +20 -0
- package/dist/helpers/error_helpers.js.map +1 -0
- package/dist/helpers/escape_helpers.d.ts +5 -0
- package/dist/helpers/escape_helpers.js +11 -0
- package/dist/helpers/escape_helpers.js.map +1 -0
- package/dist/helpers/file_helpers.d.ts +4 -0
- package/dist/helpers/file_helpers.js +16 -0
- package/dist/helpers/file_helpers.js.map +1 -0
- package/dist/helpers/format_helpers.d.ts +7 -0
- package/dist/helpers/format_helpers.js +28 -0
- package/dist/helpers/format_helpers.js.map +1 -0
- package/dist/helpers/hash_helpers.d.ts +4 -0
- package/dist/helpers/hash_helpers.js +156 -0
- package/dist/helpers/hash_helpers.js.map +1 -0
- package/dist/helpers/identity_helpers.d.ts +5 -0
- package/dist/helpers/identity_helpers.js +15 -0
- package/dist/helpers/identity_helpers.js.map +1 -0
- package/dist/helpers/location_helpers.d.ts +8 -0
- package/dist/helpers/location_helpers.js +37 -0
- package/dist/helpers/location_helpers.js.map +1 -0
- package/dist/helpers/logical_helpers.d.ts +4 -0
- package/dist/helpers/logical_helpers.js +13 -0
- package/dist/helpers/logical_helpers.js.map +1 -0
- package/dist/helpers/math_helpers.d.ts +5 -0
- package/dist/helpers/math_helpers.js +12 -0
- package/dist/helpers/math_helpers.js.map +1 -0
- package/dist/helpers/mixin_helpers.d.ts +20 -0
- package/dist/helpers/mixin_helpers.js +43 -0
- package/dist/helpers/mixin_helpers.js.map +1 -0
- package/dist/helpers/network_helpers.d.ts +4 -0
- package/dist/helpers/network_helpers.js +8 -0
- package/dist/helpers/network_helpers.js.map +1 -0
- package/dist/helpers/object_helpers.d.ts +58 -0
- package/dist/helpers/object_helpers.js +179 -0
- package/dist/helpers/object_helpers.js.map +1 -0
- package/dist/helpers/promise_helpers.d.ts +10 -0
- package/dist/helpers/promise_helpers.js +25 -0
- package/dist/helpers/promise_helpers.js.map +1 -0
- package/dist/helpers/replacements-scope.js +21 -0
- package/dist/helpers/replacements-scope.js.map +1 -0
- package/dist/helpers/replacements.d.ts +5 -0
- package/dist/helpers/replacements.js +34 -0
- package/dist/helpers/replacements.js.map +1 -0
- package/dist/helpers/runtime_helpers.d.ts +4 -0
- package/dist/helpers/runtime_helpers.js +8 -0
- package/dist/helpers/runtime_helpers.js.map +1 -0
- package/dist/helpers/string_helpers.d.ts +17 -0
- package/dist/helpers/string_helpers.js +42 -0
- package/dist/helpers/string_helpers.js.map +1 -0
- package/dist/helpers/time_constants.d.ts +15 -0
- package/dist/helpers/time_constants.js +17 -0
- package/dist/helpers/time_constants.js.map +1 -0
- package/dist/helpers/time_helpers.d.ts +24 -0
- package/dist/helpers/time_helpers.js +57 -0
- package/dist/helpers/time_helpers.js.map +1 -0
- package/dist/helpers/url_helpers.d.ts +24 -0
- package/dist/helpers/url_helpers.js +90 -0
- package/dist/helpers/url_helpers.js.map +1 -0
- package/dist/helpers/value_helpers.d.ts +7 -0
- package/dist/helpers/value_helpers.js +11 -0
- package/dist/helpers/value_helpers.js.map +1 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.js +49 -0
- package/dist/types/arrays.d.ts +12 -0
- package/dist/types/classes.d.ts +12 -0
- package/dist/types/helpers.d.ts +36 -0
- package/dist/types/helpers.js +6 -0
- package/dist/types/helpers.js.map +1 -0
- package/dist/types/objects.d.ts +8 -0
- package/package.json +38 -52
- package/src/fluent/FluentArray.test.ts +2 -9
- package/src/fluent/FluentArray.ts +6 -5
- package/src/fluent/FluentObject.ts +2 -4
- package/src/fluent/FluentString.test.ts +1 -8
- package/src/fluent/FluentString.ts +0 -2
- package/src/fluent/facade.test.ts +16 -4
- package/src/fluent/facade.ts +6 -4
- package/src/fluent/index.test.ts +3 -8
- package/src/fluent/index.ts +3 -3
- package/src/fluent/invade.test.ts +1 -5
- package/src/fluent/invade.ts +1 -1
- package/src/fluent/lazyFacade.test.ts +191 -0
- package/src/fluent/lazyFacade.ts +115 -0
- package/src/fluent/required.test.ts +1 -5
- package/src/fluent/required.ts +2 -4
- package/src/fluent/tap.test.ts +2 -9
- package/src/fluent/tap.ts +4 -3
- package/src/fluent/value.test.ts +1 -4
- package/src/fluent/when.test.ts +1 -6
- package/src/fluent/when.ts +2 -2
- package/src/helpers/InMemoryStorageService.ts +13 -0
- package/src/helpers/IndexedDBMap.ts +0 -2
- package/src/helpers/JSError.test.ts +1 -6
- package/src/helpers/JSError.ts +0 -2
- package/src/helpers/ListenersManager.test.ts +3 -7
- package/src/helpers/ListenersManager.ts +2 -4
- package/src/helpers/MagicObject.test.ts +14 -8
- package/src/helpers/MagicObject.ts +12 -3
- package/src/helpers/ObjectsMap.test.ts +1 -3
- package/src/helpers/ObjectsMap.ts +1 -5
- package/src/helpers/PromisedValue.test.ts +1 -3
- package/src/helpers/PromisedValue.ts +5 -6
- package/src/helpers/Semaphore.test.ts +6 -10
- package/src/helpers/Semaphore.ts +0 -2
- package/src/helpers/Storage.test.ts +47 -0
- package/src/helpers/Storage.ts +11 -19
- package/src/helpers/array_helpers.test.ts +127 -38
- package/src/helpers/array_helpers.ts +137 -78
- package/src/helpers/caching_helpers.test.ts +1 -3
- package/src/helpers/caching_helpers.ts +2 -2
- package/src/helpers/date_helpers.test.ts +1 -4
- package/src/helpers/date_helpers.ts +1 -1
- package/src/helpers/env.ts +5 -1
- package/src/helpers/error_helpers.test.ts +9 -8
- package/src/helpers/error_helpers.ts +11 -11
- package/src/helpers/format_helpers.test.ts +2 -4
- package/src/helpers/hash_helpers.test.ts +1 -4
- package/src/helpers/hash_helpers.ts +3 -3
- package/src/helpers/identity_helpers.test.ts +1 -3
- package/src/helpers/index.ts +3 -0
- package/src/helpers/logical_helpers.test.ts +1 -3
- package/src/helpers/logical_helpers.ts +1 -1
- package/src/helpers/math_helpers.test.ts +1 -3
- package/src/helpers/mixin_helpers.stubs.ts +1 -13
- package/src/helpers/mixin_helpers.test.ts +2 -7
- package/src/helpers/mixin_helpers.ts +6 -9
- package/src/helpers/object_helpers.test.ts +32 -19
- package/src/helpers/object_helpers.ts +43 -21
- package/src/helpers/promise_helpers.test.ts +1 -4
- package/src/helpers/promise_helpers.ts +9 -9
- package/src/helpers/replacements-scope.ts +15 -0
- package/src/helpers/replacements.test.ts +23 -0
- package/src/helpers/replacements.ts +52 -0
- package/src/helpers/runtime_helpers.ts +3 -3
- package/src/helpers/string_helpers.test.ts +3 -5
- package/src/helpers/time_constants.ts +13 -0
- package/src/helpers/time_helpers.ts +35 -0
- package/src/helpers/url_helpers.test.ts +12 -4
- package/src/helpers/url_helpers.ts +9 -15
- package/src/helpers/value_helpers.test.ts +12 -0
- package/src/helpers/value_helpers.ts +6 -0
- package/src/types/arrays.test.ts +1 -4
- package/src/types/classes.ts +1 -0
- package/src/types/helpers.test.ts +1 -6
- package/src/types/helpers.ts +6 -5
- package/src/types/objects.test.ts +1 -4
- package/src/types/objects.ts +3 -3
- package/dist/noeldemartin-utils.d.ts +0 -901
- package/dist/noeldemartin-utils.js +0 -1473
- package/dist/noeldemartin-utils.js.map +0 -1
package/src/fluent/tap.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isPromiseLike } from '@noeldemartin/utils/helpers/object_helpers';
|
|
2
|
+
|
|
1
3
|
// Typescript equivalent of Laravel's tap helper method
|
|
2
4
|
// @see https://medium.com/@taylorotwell/tap-tap-tap-1fc6fc1f93a6
|
|
3
5
|
// @see https://github.com/laravel/framework/blob/v7.8.0/src/Illuminate/Support/helpers.php#L422..L431
|
|
@@ -26,18 +28,17 @@ function proxyTap<Target extends object>(target: Target): Tapped<Target> {
|
|
|
26
28
|
return proxy;
|
|
27
29
|
}
|
|
28
30
|
|
|
29
|
-
/* eslint-disable max-len */
|
|
30
31
|
export function tap<Target extends object>(target: Target): Tapped<Target>;
|
|
31
32
|
export function tap<Target>(target: Target, callback: (target: Target) => Promise<unknown>): Promise<Target>;
|
|
32
33
|
export function tap<Target>(target: Target, callback: (target: Target) => unknown): Target;
|
|
33
34
|
export function tap<Target extends object>(
|
|
34
35
|
target: Target,
|
|
36
|
+
// oxlint-disable-next-line typescript/no-redundant-type-constituents
|
|
35
37
|
callback?: (target: Target) => unknown | Promise<unknown>,
|
|
36
38
|
): Target | Tapped<Target> | Promise<Target> {
|
|
37
39
|
if (!callback) return proxyTap(target);
|
|
38
40
|
|
|
39
41
|
const result = callback(target);
|
|
40
42
|
|
|
41
|
-
return result
|
|
43
|
+
return isPromiseLike(result) ? (result.then(() => target) as unknown as Promise<Target>) : target;
|
|
42
44
|
}
|
|
43
|
-
/* eslint-enable max-len */
|
package/src/fluent/value.test.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
|
|
3
1
|
import { value } from '@noeldemartin/utils/fluent/value';
|
|
2
|
+
import { describe, expect, it } from 'vite-plus/test';
|
|
4
3
|
|
|
5
4
|
describe('Value helper', () => {
|
|
6
|
-
|
|
7
5
|
it('works', () => {
|
|
8
6
|
expect(value(true)).toEqual(true);
|
|
9
7
|
expect(value(() => true)).toEqual(true);
|
|
10
8
|
});
|
|
11
|
-
|
|
12
9
|
});
|
package/src/fluent/when.test.ts
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
|
|
3
1
|
import { when } from '@noeldemartin/utils/fluent/when';
|
|
2
|
+
import { describe, expect, it } from 'vite-plus/test';
|
|
4
3
|
|
|
5
4
|
class NumberStore {
|
|
6
|
-
|
|
7
5
|
public value: null | number = null;
|
|
8
6
|
|
|
9
7
|
public setValue(value: number) {
|
|
10
8
|
this.value = value;
|
|
11
9
|
}
|
|
12
|
-
|
|
13
10
|
}
|
|
14
11
|
|
|
15
12
|
function isNumberStore(store: unknown): store is NumberStore {
|
|
@@ -17,7 +14,6 @@ function isNumberStore(store: unknown): store is NumberStore {
|
|
|
17
14
|
}
|
|
18
15
|
|
|
19
16
|
describe('When helper', () => {
|
|
20
|
-
|
|
21
17
|
it('works with truthy boolean', () => {
|
|
22
18
|
const store = new NumberStore();
|
|
23
19
|
|
|
@@ -60,5 +56,4 @@ describe('When helper', () => {
|
|
|
60
56
|
|
|
61
57
|
expect(store).toEqual({});
|
|
62
58
|
});
|
|
63
|
-
|
|
64
59
|
});
|
package/src/fluent/when.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
export function when<T extends object>(target: T, condition: () => boolean): T;
|
|
2
2
|
export function when<T extends object>(target: T, condition: boolean): T;
|
|
3
|
-
//
|
|
3
|
+
// oxlint-disable-next-line typescript/no-explicit-any, typescript/explicit-module-boundary-types
|
|
4
4
|
export function when<T extends object>(target: any, condition: (t: any) => t is T): T;
|
|
5
5
|
export function when<T extends object>(target: T, condition: unknown): T {
|
|
6
6
|
const success = typeof condition === 'function' ? condition(target) : !!condition;
|
|
7
7
|
|
|
8
8
|
return new Proxy(target, {
|
|
9
9
|
get(_target, property, receiver) {
|
|
10
|
-
//
|
|
10
|
+
// oxlint-disable-next-line typescript/no-empty-function
|
|
11
11
|
return success ? Reflect.get(_target, property, receiver) : () => {};
|
|
12
12
|
},
|
|
13
13
|
}) as unknown as T;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { StorageService } from '@noeldemartin/utils/helpers/Storage';
|
|
2
|
+
|
|
3
|
+
export default class InMemoryStorageService extends StorageService {
|
|
4
|
+
public readonly items = new Map<string, string>();
|
|
5
|
+
|
|
6
|
+
protected override get storage(): Pick<Storage, 'getItem' | 'setItem' | 'removeItem'> {
|
|
7
|
+
return {
|
|
8
|
+
getItem: (key) => this.items.get(key) ?? null,
|
|
9
|
+
setItem: (key, value) => void this.items.set(key, value),
|
|
10
|
+
removeItem: (key) => void this.items.delete(key),
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -12,7 +12,6 @@ interface DatabaseSchema<T> extends DBSchema {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
export default class IndexedDBMap<TItem> {
|
|
15
|
-
|
|
16
15
|
private db: IDBPDatabase<DatabaseSchema<TItem>> | null = null;
|
|
17
16
|
private namespace: string;
|
|
18
17
|
|
|
@@ -88,5 +87,4 @@ export default class IndexedDBMap<TItem> {
|
|
|
88
87
|
|
|
89
88
|
return db;
|
|
90
89
|
}
|
|
91
|
-
|
|
92
90
|
}
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
|
|
3
1
|
import { catchError, fail } from '@noeldemartin/utils/helpers/error_helpers';
|
|
2
|
+
import { describe, expect, it } from 'vite-plus/test';
|
|
4
3
|
|
|
5
4
|
import JSError from './JSError';
|
|
6
5
|
|
|
7
6
|
describe('JSError', () => {
|
|
8
|
-
|
|
9
7
|
it('behaves like an error', () => {
|
|
10
8
|
// Act
|
|
11
9
|
const error = catchError(() => fail(JSError, 'foobar'));
|
|
@@ -38,13 +36,10 @@ describe('JSError', () => {
|
|
|
38
36
|
expect(stubError.stack).not.toBeNull();
|
|
39
37
|
expect(stubError.stack).toContain('Error.test');
|
|
40
38
|
});
|
|
41
|
-
|
|
42
39
|
});
|
|
43
40
|
|
|
44
41
|
class StubError extends JSError {
|
|
45
|
-
|
|
46
42
|
constructor(m: string) {
|
|
47
43
|
super(`Custom message: ${m}`);
|
|
48
44
|
}
|
|
49
|
-
|
|
50
45
|
}
|
package/src/helpers/JSError.ts
CHANGED
|
@@ -8,7 +8,6 @@ export interface JSErrorOptions {
|
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export default class JSError extends RuntimeErrorClass {
|
|
11
|
-
|
|
12
11
|
declare public name: string;
|
|
13
12
|
declare public message: string;
|
|
14
13
|
declare public stack?: string;
|
|
@@ -21,5 +20,4 @@ export default class JSError extends RuntimeErrorClass {
|
|
|
21
20
|
this.name = new.target.name === 'JSError' ? 'Error' : new.target.name;
|
|
22
21
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
23
22
|
}
|
|
24
|
-
|
|
25
23
|
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { describe, it } from 'vitest';
|
|
2
1
|
import { tt } from '@noeldemartin/testing';
|
|
3
2
|
import type { Expect } from '@noeldemartin/testing';
|
|
4
|
-
|
|
5
3
|
import type { Equals } from '@noeldemartin/utils/types';
|
|
4
|
+
import { describe, it } from 'vite-plus/test';
|
|
6
5
|
|
|
7
6
|
import ListenersManager from './ListenersManager';
|
|
8
7
|
import type { ListenerEvents, ListenerPayload } from './ListenersManager';
|
|
@@ -15,8 +14,6 @@ interface StubListener {
|
|
|
15
14
|
const manager = new ListenersManager<StubListener>();
|
|
16
15
|
|
|
17
16
|
describe('ListenersManager types', () => {
|
|
18
|
-
/* eslint-disable max-len */
|
|
19
|
-
|
|
20
17
|
it(
|
|
21
18
|
'has correct types',
|
|
22
19
|
tt<
|
|
@@ -26,7 +23,8 @@ describe('ListenersManager types', () => {
|
|
|
26
23
|
| Expect<
|
|
27
24
|
Equals<
|
|
28
25
|
typeof manager.emit,
|
|
29
|
-
<E extends 'onStart' | 'onUpdate'>(
|
|
26
|
+
<E extends 'onStart' | 'onUpdate'>(
|
|
27
|
+
event: E,
|
|
30
28
|
...payload: ListenerPayload<StubListener, E>
|
|
31
29
|
) => Promise<void>
|
|
32
30
|
>
|
|
@@ -34,6 +32,4 @@ describe('ListenersManager types', () => {
|
|
|
34
32
|
| true
|
|
35
33
|
>(),
|
|
36
34
|
);
|
|
37
|
-
|
|
38
|
-
/* eslint-enable max-len */
|
|
39
35
|
});
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { arrayRemove } from './array_helpers';
|
|
2
2
|
|
|
3
3
|
export type ListenerEvents<TListener extends object> = Required<{
|
|
4
|
-
//
|
|
4
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
5
5
|
[K in keyof TListener]: NonNullable<TListener[K]> extends (...args: any[]) => any ? K : never;
|
|
6
6
|
}>[keyof TListener];
|
|
7
7
|
|
|
8
8
|
export type ListenerPayload<TListener extends object, TEvent extends keyof TListener> = Required<{
|
|
9
|
-
//
|
|
9
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
10
10
|
[K in keyof TListener]: NonNullable<TListener[K]> extends (...args: infer TPayload) => any ? TPayload : never;
|
|
11
11
|
}>[TEvent];
|
|
12
12
|
|
|
@@ -16,7 +16,6 @@ export interface Listeners<Listener extends object> {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export default class ListenersManager<Listener extends object> implements Listeners<Listener> {
|
|
19
|
-
|
|
20
19
|
private listeners: Listener[] = [];
|
|
21
20
|
|
|
22
21
|
public add(listener: Listener): () => void {
|
|
@@ -43,5 +42,4 @@ export default class ListenersManager<Listener extends object> implements Listen
|
|
|
43
42
|
await callback?.call(listener, ...payload);
|
|
44
43
|
}
|
|
45
44
|
}
|
|
46
|
-
|
|
47
45
|
}
|
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
1
|
import { tt } from '@noeldemartin/testing';
|
|
3
2
|
import type { Expect } from '@noeldemartin/testing';
|
|
4
|
-
|
|
5
3
|
import { stringToCamelCase } from '@noeldemartin/utils/helpers/string_helpers';
|
|
6
4
|
import type { Equals } from '@noeldemartin/utils/types';
|
|
5
|
+
import { describe, expect, it } from 'vite-plus/test';
|
|
7
6
|
|
|
8
7
|
import MagicObject from './MagicObject';
|
|
9
8
|
|
|
10
9
|
type Attributes = Record<string, unknown>;
|
|
11
10
|
|
|
12
11
|
class Stub extends MagicObject {
|
|
13
|
-
|
|
14
12
|
public static globalName: string;
|
|
15
13
|
|
|
16
|
-
//
|
|
14
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
17
15
|
[property: string]: any;
|
|
18
16
|
|
|
19
17
|
public constructor(attributes: Attributes = {}) {
|
|
@@ -58,10 +56,12 @@ class Stub extends MagicObject {
|
|
|
58
56
|
delete this.attributes[property];
|
|
59
57
|
}
|
|
60
58
|
|
|
59
|
+
protected __has(property: string): boolean {
|
|
60
|
+
return property in this.attributes;
|
|
61
|
+
}
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
describe('MagicObject', () => {
|
|
64
|
-
|
|
65
65
|
it('Intercepts getters and setters', () => {
|
|
66
66
|
// Arrange
|
|
67
67
|
const object = new Stub({ lorem: 'ipsum' });
|
|
@@ -90,6 +90,15 @@ describe('MagicObject', () => {
|
|
|
90
90
|
expect(object.getAttributes()).toEqual({});
|
|
91
91
|
});
|
|
92
92
|
|
|
93
|
+
it('Intercepts has', () => {
|
|
94
|
+
// Arrange
|
|
95
|
+
const object = new Stub({ lorem: 'ipsum' });
|
|
96
|
+
|
|
97
|
+
// Assert
|
|
98
|
+
expect('lorem' in object).toBe(true);
|
|
99
|
+
expect('foo' in object).toBe(false);
|
|
100
|
+
});
|
|
101
|
+
|
|
93
102
|
it('Runs methods in constructor', () => {
|
|
94
103
|
// Act
|
|
95
104
|
const object = new Stub({ 'foo-bar': true });
|
|
@@ -98,7 +107,6 @@ describe('MagicObject', () => {
|
|
|
98
107
|
expect(object['foo-bar']).toBeUndefined();
|
|
99
108
|
expect(object.fooBar).toBe(true);
|
|
100
109
|
});
|
|
101
|
-
|
|
102
110
|
});
|
|
103
111
|
|
|
104
112
|
const instance = new Stub();
|
|
@@ -106,10 +114,8 @@ const globalName = instance.static().globalName;
|
|
|
106
114
|
const globalNameShortcut = instance.static('globalName');
|
|
107
115
|
|
|
108
116
|
describe('MagicObject types', () => {
|
|
109
|
-
|
|
110
117
|
it(
|
|
111
118
|
'has correct types',
|
|
112
119
|
tt<Expect<Equals<typeof globalName, string>> | Expect<Equals<typeof globalNameShortcut, string>> | true>(),
|
|
113
120
|
);
|
|
114
|
-
|
|
115
121
|
});
|
|
@@ -9,7 +9,6 @@ export interface MagicObjectProxy<T> {
|
|
|
9
9
|
export type MagicObjectConstructor<T extends MagicObject = MagicObject> = Constructor<T> & typeof MagicObject;
|
|
10
10
|
|
|
11
11
|
export default class MagicObject {
|
|
12
|
-
|
|
13
12
|
private static __conjuring: boolean;
|
|
14
13
|
private static __reservedProperties: WeakMap<typeof MagicObject, Set<string>> = new WeakMap();
|
|
15
14
|
|
|
@@ -65,7 +64,7 @@ export default class MagicObject {
|
|
|
65
64
|
Object.assign(this, { [property]: null });
|
|
66
65
|
}
|
|
67
66
|
|
|
68
|
-
//
|
|
67
|
+
// oxlint-disable-next-line typescript/no-unused-vars
|
|
69
68
|
protected initialize(...args: ClosureArgs): void {
|
|
70
69
|
//
|
|
71
70
|
}
|
|
@@ -82,6 +81,10 @@ export default class MagicObject {
|
|
|
82
81
|
Reflect.deleteProperty(this._proxy.target, property);
|
|
83
82
|
}
|
|
84
83
|
|
|
84
|
+
protected __has(property: string): boolean {
|
|
85
|
+
return Reflect.has(this._proxy.target, property);
|
|
86
|
+
}
|
|
87
|
+
|
|
85
88
|
protected createProxy(): MagicObjectProxy<this> {
|
|
86
89
|
const Static = this.static();
|
|
87
90
|
|
|
@@ -104,6 +107,13 @@ export default class MagicObject {
|
|
|
104
107
|
|
|
105
108
|
return true;
|
|
106
109
|
},
|
|
110
|
+
has(target, property) {
|
|
111
|
+
if (typeof property !== 'string' || property in target || Static.isReservedProperty(property)) {
|
|
112
|
+
return Reflect.has(target, property);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return target.__has(property);
|
|
116
|
+
},
|
|
107
117
|
deleteProperty(target, property) {
|
|
108
118
|
if (typeof property !== 'string' || property in target || Static.isReservedProperty(property)) {
|
|
109
119
|
return Reflect.deleteProperty(target, property);
|
|
@@ -116,5 +126,4 @@ export default class MagicObject {
|
|
|
116
126
|
}),
|
|
117
127
|
};
|
|
118
128
|
}
|
|
119
|
-
|
|
120
129
|
}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { describe, expect, it } from '
|
|
1
|
+
import { describe, expect, it } from 'vite-plus/test';
|
|
2
2
|
|
|
3
3
|
import ObjectsMap from './ObjectsMap';
|
|
4
4
|
|
|
5
5
|
describe('ObjectsMap', () => {
|
|
6
|
-
|
|
7
6
|
it('gets items', () => {
|
|
8
7
|
const map = ObjectsMap.createFromArray([{ name: 'John' }, { name: 'Amy' }], 'name');
|
|
9
8
|
|
|
@@ -73,7 +72,6 @@ describe('ObjectsMap', () => {
|
|
|
73
72
|
expect(original.hasItem(clone.require('Alice'))).toBe(true);
|
|
74
73
|
expect(original.hasItem(clone.require('Bob'))).toBe(false);
|
|
75
74
|
});
|
|
76
|
-
|
|
77
75
|
});
|
|
78
76
|
|
|
79
77
|
function expectUsersFilter<T extends { name: string }>(
|
|
@@ -5,15 +5,12 @@ import { toString } from './object_helpers';
|
|
|
5
5
|
export type ObjectKeyExtractor<T> = (item: T) => string;
|
|
6
6
|
|
|
7
7
|
export default class ObjectsMap<Item extends object> {
|
|
8
|
-
|
|
9
|
-
/* eslint-disable max-len */
|
|
10
8
|
public static createFromArray<T extends object>(
|
|
11
9
|
items: Iterable<T>,
|
|
12
|
-
keyExtractor?: ObjectKeyExtractor<T
|
|
10
|
+
keyExtractor?: ObjectKeyExtractor<T>,
|
|
13
11
|
): ObjectsMap<T>;
|
|
14
12
|
|
|
15
13
|
public static createFromArray<T extends object, S extends keyof T>(items: Iterable<T>, key: S): ObjectsMap<T>;
|
|
16
|
-
/* eslint-enable max-len */
|
|
17
14
|
|
|
18
15
|
public static createFromArray<T extends object, S extends keyof T>(
|
|
19
16
|
items: Iterable<T>,
|
|
@@ -132,5 +129,4 @@ export default class ObjectsMap<Item extends object> {
|
|
|
132
129
|
|
|
133
130
|
return copy;
|
|
134
131
|
}
|
|
135
|
-
|
|
136
132
|
}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { describe, expect, it } from '
|
|
1
|
+
import { describe, expect, it } from 'vite-plus/test';
|
|
2
2
|
|
|
3
3
|
import PromisedValue from './PromisedValue';
|
|
4
4
|
|
|
5
5
|
describe('PromisedValue', () => {
|
|
6
|
-
|
|
7
6
|
it('works like a promise', async () => {
|
|
8
7
|
const promisedString = new PromisedValue<string>();
|
|
9
8
|
expect(promisedString.value).toBe(null);
|
|
@@ -28,5 +27,4 @@ describe('PromisedValue', () => {
|
|
|
28
27
|
const resolvedValue = await promisedString;
|
|
29
28
|
expect(resolvedValue).toBe('bar');
|
|
30
29
|
});
|
|
31
|
-
|
|
32
30
|
});
|
|
@@ -5,7 +5,6 @@ export type PromisedValueRejectListener = (reason?: Error) => unknown;
|
|
|
5
5
|
export type PromisedValueResetListener = () => unknown;
|
|
6
6
|
|
|
7
7
|
export default class PromisedValue<T = unknown> implements Promise<T> {
|
|
8
|
-
|
|
9
8
|
public static from<T>(promise: Promise<T>): PromisedValue<T> {
|
|
10
9
|
const promisedValue = new PromisedValue<T>();
|
|
11
10
|
|
|
@@ -37,15 +36,16 @@ export default class PromisedValue<T = unknown> implements Promise<T> {
|
|
|
37
36
|
return '_value' in this;
|
|
38
37
|
}
|
|
39
38
|
|
|
39
|
+
// oxlint-disable-next-line unicorn/no-thenable
|
|
40
40
|
public then<TResult1 = T, TResult2 = never>(
|
|
41
|
-
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) |
|
|
42
|
-
onRejected?: ((reason: Error) => TResult2 | PromiseLike<TResult2>) |
|
|
41
|
+
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
|
|
42
|
+
onRejected?: ((reason: Error) => TResult2 | PromiseLike<TResult2>) | null,
|
|
43
43
|
): Promise<TResult1 | TResult2> {
|
|
44
44
|
return this.promise.then(onFulfilled, onRejected);
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
public catch<TResult = never>(
|
|
48
|
-
onRejected?: ((reason: Error) => TResult | PromiseLike<TResult>) |
|
|
48
|
+
onRejected?: ((reason: Error) => TResult | PromiseLike<TResult>) | null,
|
|
49
49
|
): Promise<T | TResult> {
|
|
50
50
|
return this.promise.catch(onRejected);
|
|
51
51
|
}
|
|
@@ -97,7 +97,7 @@ export default class PromisedValue<T = unknown> implements Promise<T> {
|
|
|
97
97
|
public reset(): void {
|
|
98
98
|
this.setPromiseValues();
|
|
99
99
|
|
|
100
|
-
this.
|
|
100
|
+
this.resetListeners.forEach((listener) => listener());
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
private setPromiseValues(): void {
|
|
@@ -106,5 +106,4 @@ export default class PromisedValue<T = unknown> implements Promise<T> {
|
|
|
106
106
|
this._reject = reject;
|
|
107
107
|
});
|
|
108
108
|
}
|
|
109
|
-
|
|
110
109
|
}
|
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { range } from '@noeldemartin/utils/helpers/array_helpers';
|
|
2
|
+
import { fail } from '@noeldemartin/utils/helpers/error_helpers';
|
|
3
3
|
import Semaphore from '@noeldemartin/utils/helpers/Semaphore';
|
|
4
4
|
import { after } from '@noeldemartin/utils/helpers/time_helpers';
|
|
5
|
-
import {
|
|
6
|
-
import { range } from '@noeldemartin/utils/helpers/array_helpers';
|
|
5
|
+
import { describe, expect, it } from 'vite-plus/test';
|
|
7
6
|
|
|
8
7
|
describe('Semaphore', () => {
|
|
9
|
-
|
|
10
8
|
it('Locks concurrent threads', async () => {
|
|
11
9
|
// Arrange
|
|
12
10
|
class Writer {
|
|
13
|
-
|
|
14
11
|
private pen?: Promise<void>;
|
|
15
12
|
|
|
16
13
|
public async write(item: string): Promise<void> {
|
|
@@ -28,7 +25,6 @@ describe('Semaphore', () => {
|
|
|
28
25
|
|
|
29
26
|
delete this.pen;
|
|
30
27
|
}
|
|
31
|
-
|
|
32
28
|
}
|
|
33
29
|
|
|
34
30
|
const items: string[] = [];
|
|
@@ -62,7 +58,8 @@ describe('Semaphore', () => {
|
|
|
62
58
|
await after({ ms: Math.floor(Math.random() * 10) });
|
|
63
59
|
|
|
64
60
|
items.push(`Item #${i}`);
|
|
65
|
-
})
|
|
61
|
+
}),
|
|
62
|
+
),
|
|
66
63
|
);
|
|
67
64
|
|
|
68
65
|
// Assert
|
|
@@ -77,7 +74,7 @@ describe('Semaphore', () => {
|
|
|
77
74
|
// Act
|
|
78
75
|
try {
|
|
79
76
|
await lock.run(() => fail());
|
|
80
|
-
} catch
|
|
77
|
+
} catch {
|
|
81
78
|
// Silence error.
|
|
82
79
|
failed = true;
|
|
83
80
|
}
|
|
@@ -86,5 +83,4 @@ describe('Semaphore', () => {
|
|
|
86
83
|
expect(lock.isAvailable()).toBe(true);
|
|
87
84
|
expect(failed).toBe(true);
|
|
88
85
|
});
|
|
89
|
-
|
|
90
86
|
});
|
package/src/helpers/Semaphore.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
type Listener = () => void;
|
|
2
2
|
|
|
3
3
|
export default class Semaphore {
|
|
4
|
-
|
|
5
4
|
private count: number;
|
|
6
5
|
private listeners: Set<Listener>;
|
|
7
6
|
|
|
@@ -59,5 +58,4 @@ export default class Semaphore {
|
|
|
59
58
|
private notifyAvailable(): void {
|
|
60
59
|
this.listeners.forEach((listener) => listener());
|
|
61
60
|
}
|
|
62
|
-
|
|
63
61
|
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it } from 'vite-plus/test';
|
|
2
|
+
|
|
3
|
+
import InMemoryStorageService from './InMemoryStorageService';
|
|
4
|
+
import Storage from './Storage';
|
|
5
|
+
|
|
6
|
+
describe('Storage', () => {
|
|
7
|
+
let storage: InMemoryStorageService;
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
storage = new InMemoryStorageService();
|
|
11
|
+
Storage.setInstance(storage);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('uses custom storage backends', () => {
|
|
15
|
+
// Act
|
|
16
|
+
Storage.set('foo', { bar: 'baz' });
|
|
17
|
+
|
|
18
|
+
// Assert
|
|
19
|
+
expect(storage.items.get('foo')).toEqual('{"bar":"baz"}');
|
|
20
|
+
expect(Storage.get('foo')).toEqual({ bar: 'baz' });
|
|
21
|
+
expect(Storage.has('foo')).toBe(true);
|
|
22
|
+
expect(Storage.get('missing', 'default')).toEqual('default');
|
|
23
|
+
expect(() => Storage.require('missing')).toThrow("Failed getting required key from local storage: 'missing'");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('stores undefined values', () => {
|
|
27
|
+
// Act
|
|
28
|
+
Storage.set('foo', undefined);
|
|
29
|
+
|
|
30
|
+
// Assert
|
|
31
|
+
expect(Storage.has('foo')).toBe(true);
|
|
32
|
+
expect(Storage.get('foo', 'default')).toBeUndefined();
|
|
33
|
+
expect(Storage.require('foo')).toBeUndefined();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('pulls values', () => {
|
|
37
|
+
// Arrange
|
|
38
|
+
Storage.set('foo', 'bar');
|
|
39
|
+
|
|
40
|
+
// Act
|
|
41
|
+
const value = Storage.pull('foo');
|
|
42
|
+
|
|
43
|
+
// Assert
|
|
44
|
+
expect(value).toEqual('bar');
|
|
45
|
+
expect(Storage.has('foo')).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
});
|
package/src/helpers/Storage.ts
CHANGED
|
@@ -1,27 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export class StorageSingleton {
|
|
4
|
-
|
|
5
|
-
private static _instance: StorageSingleton;
|
|
6
|
-
|
|
7
|
-
public static get instance(): StorageSingleton {
|
|
8
|
-
return (this._instance = this._instance ?? new StorageSingleton());
|
|
9
|
-
}
|
|
1
|
+
import { facade } from '@noeldemartin/utils/fluent/facade';
|
|
10
2
|
|
|
11
|
-
|
|
12
|
-
// Hide instantiation
|
|
13
|
-
}
|
|
3
|
+
const DEFAULT_INSTANCE = {};
|
|
14
4
|
|
|
5
|
+
export class StorageService {
|
|
15
6
|
public set<T = unknown>(key: string, value: T): void {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
localStorage.setItem(key, JSON.stringify(value));
|
|
7
|
+
this.storage.setItem(key, typeof value === 'undefined' ? '__undefined__' : JSON.stringify(value));
|
|
19
8
|
}
|
|
20
9
|
|
|
21
10
|
public get<T>(key: string): T | null;
|
|
22
11
|
public get<T>(key: string, defaultValue: T): T;
|
|
23
12
|
public get<T>(key: string, defaultValue: T | null = null): T | null {
|
|
24
|
-
const value =
|
|
13
|
+
const value = this.storage.getItem(key);
|
|
25
14
|
|
|
26
15
|
if (value === null) return defaultValue;
|
|
27
16
|
|
|
@@ -39,11 +28,11 @@ export class StorageSingleton {
|
|
|
39
28
|
}
|
|
40
29
|
|
|
41
30
|
public has(key: string): boolean {
|
|
42
|
-
return
|
|
31
|
+
return this.storage.getItem(key) !== null;
|
|
43
32
|
}
|
|
44
33
|
|
|
45
34
|
public remove(key: string): void {
|
|
46
|
-
|
|
35
|
+
this.storage.removeItem(key);
|
|
47
36
|
}
|
|
48
37
|
|
|
49
38
|
public pull<T>(key: string): T | null;
|
|
@@ -58,6 +47,9 @@ export class StorageSingleton {
|
|
|
58
47
|
return value;
|
|
59
48
|
}
|
|
60
49
|
|
|
50
|
+
protected get storage(): Pick<Storage, 'getItem' | 'setItem' | 'removeItem'> {
|
|
51
|
+
return localStorage;
|
|
52
|
+
}
|
|
61
53
|
}
|
|
62
54
|
|
|
63
|
-
export default
|
|
55
|
+
export default facade(StorageService);
|