@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
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
|
|
3
1
|
import { range } from '@noeldemartin/utils/helpers/array_helpers';
|
|
2
|
+
import { describe, expect, it } from 'vite-plus/test';
|
|
4
3
|
|
|
5
4
|
import { asyncFirst } from './promise_helpers';
|
|
6
5
|
|
|
7
6
|
describe('Promise helpers', () => {
|
|
8
|
-
|
|
9
7
|
it('gets first async result', async () => {
|
|
10
8
|
// Arrange
|
|
11
9
|
let calls = 0;
|
|
@@ -18,5 +16,4 @@ describe('Promise helpers', () => {
|
|
|
18
16
|
expect(calls).toEqual(4);
|
|
19
17
|
expect(result).toEqual(3);
|
|
20
18
|
});
|
|
21
|
-
|
|
22
19
|
});
|
|
@@ -22,16 +22,16 @@ export function isPromise(p: unknown): p is Promise<unknown> {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
export function silenced<Args extends ClosureArgs, Result>(
|
|
25
|
-
operation: Closure<Args, Promise<Result
|
|
25
|
+
operation: Closure<Args, Promise<Result>>,
|
|
26
26
|
): Closure<Args, Promise<Result | null>>;
|
|
27
27
|
export function silenced<Args extends ClosureArgs, Result, Default>(
|
|
28
28
|
operation: Closure<Args, Promise<Result>>,
|
|
29
|
-
defaultValue: Default
|
|
29
|
+
defaultValue: Default,
|
|
30
30
|
): Closure<Args, Promise<Result | Default>>;
|
|
31
31
|
export function silenced<Result>(promise: Promise<Result>): Promise<Result | null>;
|
|
32
32
|
export function silenced<Args extends ClosureArgs, Result, Default>(
|
|
33
33
|
operation: Closure<Args, Promise<Result>>,
|
|
34
|
-
defaultValue: Default
|
|
34
|
+
defaultValue: Default,
|
|
35
35
|
): Closure<Args, Promise<Result | Default>>;
|
|
36
36
|
export function silenced<Args extends ClosureArgs, Result, Default>(
|
|
37
37
|
operationOrPromise: Closure<Args, Promise<Result>> | Promise<Result>,
|
|
@@ -40,10 +40,10 @@ export function silenced<Args extends ClosureArgs, Result, Default>(
|
|
|
40
40
|
return typeof operationOrPromise !== 'function'
|
|
41
41
|
? operationOrPromise.catch(() => defaultValue)
|
|
42
42
|
: async (...args: Args) => {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
43
|
+
try {
|
|
44
|
+
return await operationOrPromise(...args);
|
|
45
|
+
} catch {
|
|
46
|
+
return defaultValue ?? null;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
49
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { uuid as generateUUID } from '@noeldemartin/utils/helpers/identity_helpers';
|
|
2
|
+
|
|
3
|
+
export interface ReplacementsScope {}
|
|
4
|
+
|
|
5
|
+
export function date(description: string): string {
|
|
6
|
+
return new Date(Date.parse(description)).toISOString();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function now(): string {
|
|
10
|
+
return new Date().toISOString();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function uuid(this: ReplacementsScope): string {
|
|
14
|
+
return generateUUID();
|
|
15
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vite-plus/test';
|
|
2
|
+
|
|
3
|
+
import { applyReplacements } from './replacements';
|
|
4
|
+
|
|
5
|
+
describe('Replacements', () => {
|
|
6
|
+
it('Applies replacements', () => {
|
|
7
|
+
expect(applyReplacements('Foo {{bar}}', { bar: 'Bar' })).toEqual('Foo Bar');
|
|
8
|
+
expect(applyReplacements('Foo {{ bar }}', { bar: 'Bar' })).toEqual('Foo Bar');
|
|
9
|
+
expect(applyReplacements('{{ one }}{{ o }}{{ one }}', { one: '1', o: '0' })).toEqual('101');
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('Evaluates replacements JavaScript', () => {
|
|
13
|
+
expect(applyReplacements("Foo {{ bar || 'Fallback' }}", { bar: 'Bar' })).toEqual('Foo Bar');
|
|
14
|
+
expect(applyReplacements("Foo {{ bar || 'Bar' }}")).toEqual('Foo Bar');
|
|
15
|
+
expect(applyReplacements('Foo {{ bar || uuid() }}')).toMatch(
|
|
16
|
+
/Foo [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/,
|
|
17
|
+
);
|
|
18
|
+
expect(applyReplacements('Now: {{ now() }}').replace(/\.\d{3}Z/, '.000Z')).toEqual(
|
|
19
|
+
`Now: ${new Date().toISOString().replace(/\.\d{3}Z/, '.000Z')}`,
|
|
20
|
+
);
|
|
21
|
+
expect(applyReplacements("Date: {{ date('2024-03-03') }}")).toEqual('Date: 2024-03-03T00:00:00.000Z');
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { toString } from '@noeldemartin/utils/helpers/object_helpers';
|
|
2
|
+
import { stringMatch, stringMatchAll } from '@noeldemartin/utils/helpers/string_helpers';
|
|
3
|
+
|
|
4
|
+
import * as replacementsScope from './replacements-scope';
|
|
5
|
+
import type { ReplacementsScope } from './replacements-scope';
|
|
6
|
+
|
|
7
|
+
function renderReplacement(script: string, scope: ReplacementsScope): string {
|
|
8
|
+
try {
|
|
9
|
+
// oxlint-disable-next-line typescript/no-implied-eval
|
|
10
|
+
return Function(`with (this) { return (${script}).toString() }`).bind({
|
|
11
|
+
...replacementsScope,
|
|
12
|
+
...scope,
|
|
13
|
+
})();
|
|
14
|
+
} catch (error) {
|
|
15
|
+
const undefinedVariable = stringMatch<2>(toString(error), /([^\s]+) is not defined/)?.[1];
|
|
16
|
+
|
|
17
|
+
if (!undefinedVariable) {
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return renderReplacement(script, {
|
|
22
|
+
...scope,
|
|
23
|
+
[undefinedVariable]: undefined,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const REPLACEMENTS_REGEX = /{{([^}]+)}}/gm;
|
|
29
|
+
|
|
30
|
+
function reduce<TItem, TResult>(
|
|
31
|
+
items: IterableIterator<TItem>,
|
|
32
|
+
initial: TResult,
|
|
33
|
+
callback: (result: TResult, item: TItem) => TResult,
|
|
34
|
+
) {
|
|
35
|
+
let result = initial;
|
|
36
|
+
|
|
37
|
+
for (const item of items) {
|
|
38
|
+
result = callback(result, item);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type Replacements = Record<string, unknown>;
|
|
45
|
+
|
|
46
|
+
export function applyReplacements(text: string, replacements: Replacements = {}): string {
|
|
47
|
+
const matches = stringMatchAll<2>(text, REPLACEMENTS_REGEX);
|
|
48
|
+
|
|
49
|
+
return reduce(matches, text, (renderedText, match) => {
|
|
50
|
+
return renderedText.replace(match[0], renderReplacement(match[1], replacements));
|
|
51
|
+
});
|
|
52
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
//
|
|
1
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
2
2
|
export function runtimeGlobal<T = any>(): T {
|
|
3
3
|
return typeof global !== 'undefined'
|
|
4
4
|
? (global as T)
|
|
5
5
|
: typeof globalThis !== 'undefined'
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
? (globalThis as T)
|
|
7
|
+
: (window as T);
|
|
8
8
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { describe, expect, it } from '
|
|
1
|
+
import { describe, expect, it } from 'vite-plus/test';
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
stringExcerpt,
|
|
@@ -10,7 +10,6 @@ import {
|
|
|
10
10
|
} from './string_helpers';
|
|
11
11
|
|
|
12
12
|
describe('String helpers', () => {
|
|
13
|
-
|
|
14
13
|
it('reverses string characters', () => {
|
|
15
14
|
expect(stringReverse('foobar')).toEqual('raboof');
|
|
16
15
|
});
|
|
@@ -24,8 +23,8 @@ describe('String helpers', () => {
|
|
|
24
23
|
it('converts strings to slug', () => {
|
|
25
24
|
expect(stringToSlug('Sin perdón')).toEqual('sin-perdon');
|
|
26
25
|
expect(stringToSlug('Mañana')).toEqual('manana');
|
|
27
|
-
expect(stringToSlug(
|
|
28
|
-
expect(stringToSlug(
|
|
26
|
+
expect(stringToSlug("It's always sunny in philadelphia")).toEqual('its-always-sunny-in-philadelphia');
|
|
27
|
+
expect(stringToSlug("It's always sunny in philadelphia ")).toEqual('its-always-sunny-in-philadelphia');
|
|
29
28
|
expect(stringToSlug('already-a-slug')).toEqual('already-a-slug');
|
|
30
29
|
expect(stringToSlug('Not clean: [/\\?!] end')).toEqual('not-clean-end');
|
|
31
30
|
expect(stringToSlug('土豆')).toEqual('土豆');
|
|
@@ -55,5 +54,4 @@ describe('String helpers', () => {
|
|
|
55
54
|
'Your external self will seldom surpa [...]',
|
|
56
55
|
);
|
|
57
56
|
});
|
|
58
|
-
|
|
59
57
|
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const MINUTE_MILLISECONDS = 60 * 1000;
|
|
2
|
+
export const HOUR_MILLISECONDS = 60 * MINUTE_MILLISECONDS;
|
|
3
|
+
export const DAY_MILLISECONDS = 24 * HOUR_MILLISECONDS;
|
|
4
|
+
export const WEEK_MILLISECONDS = 7 * DAY_MILLISECONDS;
|
|
5
|
+
export const MONTH_MILLISECONDS = 30 * DAY_MILLISECONDS;
|
|
6
|
+
export const YEAR_MILLISECONDS = 365 * DAY_MILLISECONDS;
|
|
7
|
+
|
|
8
|
+
export const MINUTE_SECONDS = 60;
|
|
9
|
+
export const HOUR_SECONDS = 60 * MINUTE_SECONDS;
|
|
10
|
+
export const DAY_SECONDS = 24 * HOUR_SECONDS;
|
|
11
|
+
export const WEEK_SECONDS = 7 * DAY_SECONDS;
|
|
12
|
+
export const MONTH_SECONDS = 30 * DAY_SECONDS;
|
|
13
|
+
export const YEAR_SECONDS = 365 * DAY_SECONDS;
|
|
@@ -23,6 +23,12 @@ export function after(msOrOptions: number | AfterOptions = {}): Promise<void> {
|
|
|
23
23
|
return new Promise((resolve) => setTimeout(resolve, time));
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
export function sleep(ms: number): Promise<void>;
|
|
27
|
+
export function sleep(options?: AfterOptions): Promise<void>;
|
|
28
|
+
export function sleep(msOrOptions: number | AfterOptions = {}): Promise<void> {
|
|
29
|
+
return after(msOrOptions as number);
|
|
30
|
+
}
|
|
31
|
+
|
|
26
32
|
export function afterAnimationFrame(): Promise<void> {
|
|
27
33
|
return new Promise((resolve) => requestAnimationFrame(() => resolve()));
|
|
28
34
|
}
|
|
@@ -43,6 +49,35 @@ export function debounce<Args extends ClosureArgs>(
|
|
|
43
49
|
return debouncedCallback;
|
|
44
50
|
}
|
|
45
51
|
|
|
52
|
+
export function throttle<Args extends ClosureArgs>(
|
|
53
|
+
callback: (...args: Args) => unknown,
|
|
54
|
+
delay: number = 100,
|
|
55
|
+
): (...args: Args) => void {
|
|
56
|
+
let trailingArgs: Args | null = null;
|
|
57
|
+
let timeout: ReturnType<typeof setTimeout> | null = null;
|
|
58
|
+
|
|
59
|
+
const throttledCallback = (...args: Args) => {
|
|
60
|
+
if (timeout) {
|
|
61
|
+
trailingArgs = args;
|
|
62
|
+
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
callback(...args);
|
|
67
|
+
|
|
68
|
+
timeout = setTimeout(() => {
|
|
69
|
+
const nextArgs = trailingArgs;
|
|
70
|
+
|
|
71
|
+
trailingArgs = null;
|
|
72
|
+
timeout = null;
|
|
73
|
+
|
|
74
|
+
nextArgs && throttledCallback(...nextArgs);
|
|
75
|
+
}, delay);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
return throttledCallback;
|
|
79
|
+
}
|
|
80
|
+
|
|
46
81
|
export async function forever(): Promise<void> {
|
|
47
82
|
await new Promise(() => {
|
|
48
83
|
// Nothing to do here.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
|
|
3
1
|
import { arrayChunk } from '@noeldemartin/utils/helpers/array_helpers';
|
|
2
|
+
import { describe, expect, it } from 'vite-plus/test';
|
|
3
|
+
|
|
4
4
|
import {
|
|
5
5
|
requireUrlDirectoryName,
|
|
6
6
|
urlClean,
|
|
@@ -14,7 +14,6 @@ import {
|
|
|
14
14
|
} from './url_helpers';
|
|
15
15
|
|
|
16
16
|
describe('Url helper', () => {
|
|
17
|
-
|
|
18
17
|
it('uses root when resolving absolute paths', () => {
|
|
19
18
|
expect(urlResolve('http://example.com/something/else', '/foobar')).toEqual('http://example.com/foobar');
|
|
20
19
|
});
|
|
@@ -25,6 +24,16 @@ describe('Url helper', () => {
|
|
|
25
24
|
);
|
|
26
25
|
});
|
|
27
26
|
|
|
27
|
+
it('resolves relative paths', () => {
|
|
28
|
+
expect(urlResolve('http://example.com/something/else', 'foobar')).toEqual(
|
|
29
|
+
'http://example.com/something/foobar',
|
|
30
|
+
);
|
|
31
|
+
expect(urlResolve('http://example.com/something/else', './foobar')).toEqual(
|
|
32
|
+
'http://example.com/something/foobar',
|
|
33
|
+
);
|
|
34
|
+
expect(urlResolve('http://example.com/something/else', '../foobar')).toEqual('http://example.com/foobar');
|
|
35
|
+
});
|
|
36
|
+
|
|
28
37
|
it('splits arrays into chunks', () => {
|
|
29
38
|
expect(arrayChunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]);
|
|
30
39
|
expect(arrayChunk([1, 2, 3, 4, 5, 6], 2)).toEqual([
|
|
@@ -115,5 +124,4 @@ describe('Url helper', () => {
|
|
|
115
124
|
expect(urlFileName('https://example.com/foobar#it')).toEqual('foobar');
|
|
116
125
|
expect(urlFileName('https://example.com/foobar?ignore')).toEqual('foobar');
|
|
117
126
|
});
|
|
118
|
-
|
|
119
127
|
});
|
|
@@ -22,20 +22,14 @@ export function requireUrlParse(url: string): UrlParts {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
export function urlResolve(...parts: string[]): string {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
url = fragment;
|
|
34
|
-
} else if (url.endsWith('/')) {
|
|
35
|
-
url += fragment;
|
|
36
|
-
} else {
|
|
37
|
-
url += '/' + fragment;
|
|
38
|
-
}
|
|
25
|
+
if (parts.length === 0) {
|
|
26
|
+
return '';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let url = parts[0];
|
|
30
|
+
|
|
31
|
+
for (const part of parts.slice(1)) {
|
|
32
|
+
url = new URL(part, url).toString();
|
|
39
33
|
}
|
|
40
34
|
|
|
41
35
|
return url;
|
|
@@ -132,7 +126,7 @@ export function urlRoute(url: string): string {
|
|
|
132
126
|
export function validUrl(url: string): string | null {
|
|
133
127
|
try {
|
|
134
128
|
return new URL(url.trim()).toString();
|
|
135
|
-
} catch
|
|
129
|
+
} catch {
|
|
136
130
|
return null;
|
|
137
131
|
}
|
|
138
132
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { isTruthy } from '@noeldemartin/utils/helpers/value_helpers';
|
|
2
|
+
import { describe, expect, expectTypeOf, it } from 'vite-plus/test';
|
|
3
|
+
|
|
4
|
+
describe('Value helpers', () => {
|
|
5
|
+
it('filters truthy values', () => {
|
|
6
|
+
const items = [1, 2, 3, null, 0, '', 'foo'];
|
|
7
|
+
const filteredItems = items.filter(isTruthy);
|
|
8
|
+
|
|
9
|
+
expect(filteredItems).toEqual([1, 2, 3, 'foo']);
|
|
10
|
+
expectTypeOf(filteredItems).toEqualTypeOf<(number | string)[]>();
|
|
11
|
+
});
|
|
12
|
+
});
|
|
@@ -1,5 +1,11 @@
|
|
|
1
|
+
import type { Truthy } from '@noeldemartin/utils/types';
|
|
2
|
+
|
|
1
3
|
export type EvaluatesTo<T> = T | (() => T);
|
|
2
4
|
|
|
3
5
|
export function evaluate<T>(value: EvaluatesTo<T>): T {
|
|
4
6
|
return typeof value === 'function' ? (value as () => T)() : value;
|
|
5
7
|
}
|
|
8
|
+
|
|
9
|
+
export function isTruthy<T>(value: T): value is Truthy<T> {
|
|
10
|
+
return Boolean(value);
|
|
11
|
+
}
|
package/src/types/arrays.test.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
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 type { ArrayItem, Tuple } from './arrays';
|
|
8
7
|
|
|
9
8
|
describe('Array types', () => {
|
|
10
|
-
|
|
11
9
|
it(
|
|
12
10
|
'Has correct types',
|
|
13
11
|
tt<
|
|
@@ -17,5 +15,4 @@ describe('Array types', () => {
|
|
|
17
15
|
| true
|
|
18
16
|
>(),
|
|
19
17
|
);
|
|
20
|
-
|
|
21
18
|
});
|
package/src/types/classes.ts
CHANGED
|
@@ -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 type { KeyOf, NullableOptional, NullablePartial } from './helpers';
|
|
8
7
|
|
|
@@ -13,14 +12,11 @@ interface User {
|
|
|
13
12
|
}
|
|
14
13
|
|
|
15
14
|
describe('Type helpers', () => {
|
|
16
|
-
|
|
17
15
|
it(
|
|
18
16
|
'has correct types',
|
|
19
17
|
tt<
|
|
20
18
|
| Expect<Equals<KeyOf<User, string | number>, 'id' | 'name'>>
|
|
21
19
|
| Expect<Equals<NullablePartial<{ foo: string }>, { foo?: string | null | undefined }>>
|
|
22
|
-
|
|
23
|
-
// eslint-disable-next-line max-len
|
|
24
20
|
| Expect<
|
|
25
21
|
Equals<
|
|
26
22
|
NullableOptional<{ foo: string; bar?: string }>,
|
|
@@ -30,5 +26,4 @@ describe('Type helpers', () => {
|
|
|
30
26
|
| true
|
|
31
27
|
>(),
|
|
32
28
|
);
|
|
33
|
-
|
|
34
29
|
});
|
package/src/types/helpers.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
export const __brand = Symbol('__brand');
|
|
2
2
|
export type ClassInstance<Class> = Class extends { new (...args: any[]): infer T } ? T : never;
|
|
3
3
|
export type Closure<Args extends ClosureArgs = ClosureArgs, Result = unknown> = (...args: Args) => Result;
|
|
4
|
-
export type ClosureArgs = any[]; //
|
|
5
|
-
export type ClosureResult = any; //
|
|
4
|
+
export type ClosureArgs = any[]; // oxlint-disable-line typescript/no-explicit-any
|
|
5
|
+
export type ClosureResult = any; // oxlint-disable-line typescript/no-explicit-any
|
|
6
6
|
export type Branded<T, Brand> = T & { [__brand]: Brand };
|
|
7
7
|
export type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
|
|
8
|
-
export type Falsy = null | undefined | false | 0;
|
|
8
|
+
export type Falsy = '' | null | undefined | false | 0;
|
|
9
|
+
export type Truthy<T> = Exclude<T, Falsy>;
|
|
9
10
|
export type GetObjectMethods<T> = {
|
|
10
11
|
[K in keyof T]: T[K] extends (...args: ClosureArgs) => ClosureResult ? K : never;
|
|
11
12
|
}[keyof T];
|
|
@@ -31,7 +32,7 @@ export type IsAny<T> = { __: 'never' } extends T ? true : false;
|
|
|
31
32
|
export type IsPlainObject<T> = T extends object
|
|
32
33
|
? T extends Function
|
|
33
34
|
? false
|
|
34
|
-
: T extends any[] //
|
|
35
|
+
: T extends any[] // oxlint-disable-line typescript/no-explicit-any
|
|
35
36
|
? false
|
|
36
37
|
: T extends Date
|
|
37
38
|
? false
|
|
@@ -41,4 +42,4 @@ export type IsPlainObject<T> = T extends object
|
|
|
41
42
|
: false;
|
|
42
43
|
|
|
43
44
|
// Workaround for https://github.com/typescript-eslint/typescript-eslint/issues/3573
|
|
44
|
-
export type Use<T> = {}; //
|
|
45
|
+
export type Use<T> = {}; // oxlint-disable-line typescript/no-unused-vars
|
|
@@ -1,9 +1,8 @@
|
|
|
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 { arraySorted } from '@noeldemartin/utils/helpers';
|
|
6
4
|
import type { Equals } from '@noeldemartin/utils/types';
|
|
5
|
+
import { describe, it } from 'vite-plus/test';
|
|
7
6
|
|
|
8
7
|
import type { DeepKeyOf, DeepValue } from './objects';
|
|
9
8
|
|
|
@@ -33,7 +32,6 @@ const key = 'author' as DeepKeyOf<Post>;
|
|
|
33
32
|
const sortedFields = arraySorted(posts, key);
|
|
34
33
|
|
|
35
34
|
describe('Object types', () => {
|
|
36
|
-
|
|
37
35
|
it(
|
|
38
36
|
'Has correct types',
|
|
39
37
|
tt<
|
|
@@ -77,5 +75,4 @@ describe('Object types', () => {
|
|
|
77
75
|
| true
|
|
78
76
|
>(),
|
|
79
77
|
);
|
|
80
|
-
|
|
81
78
|
});
|
package/src/types/objects.ts
CHANGED
|
@@ -12,7 +12,6 @@ export type DeepRequired<T> = T extends object
|
|
|
12
12
|
}
|
|
13
13
|
: T;
|
|
14
14
|
|
|
15
|
-
/* eslint-disable max-len */
|
|
16
15
|
export type DeepKeyOf<T, TDepth extends number = 5, TRequired extends DeepRequired<T> = DeepRequired<T>> = NonNullable<
|
|
17
16
|
TRequired extends object
|
|
18
17
|
? {
|
|
@@ -35,7 +34,9 @@ export type DeepKeyOf<T, TDepth extends number = 5, TRequired extends DeepRequir
|
|
|
35
34
|
: TRequired[K][KK] extends object
|
|
36
35
|
?
|
|
37
36
|
| {
|
|
38
|
-
[
|
|
37
|
+
[
|
|
38
|
+
KKK in keyof TRequired[K][KK]
|
|
39
|
+
]: TRequired[K][KK][KKK] extends Function
|
|
39
40
|
? never
|
|
40
41
|
: `${Exclude<K, symbol>}.${Exclude<KK, symbol>}.${Exclude<KKK, symbol>}`;
|
|
41
42
|
}[keyof TRequired[K][KK]]
|
|
@@ -47,7 +48,6 @@ export type DeepKeyOf<T, TDepth extends number = 5, TRequired extends DeepRequir
|
|
|
47
48
|
}[keyof TRequired]
|
|
48
49
|
: never
|
|
49
50
|
>;
|
|
50
|
-
/* eslint-enable max-len */
|
|
51
51
|
|
|
52
52
|
export type DeepValue<T extends object, K> = K extends `${infer Key}.${infer Rest}`
|
|
53
53
|
? Key extends keyof T
|