@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
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, no_symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
return target;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
export { __exportAll };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Falsy } from "../types/helpers.js";
|
|
2
|
+
import FluentObjectDefinition, { FluentInstance } from "./FluentObject.js";
|
|
3
|
+
//#region src/fluent/FluentArray.d.ts
|
|
4
|
+
export type FluentArrayHelpers<T> = {
|
|
5
|
+
clear(items: T[]): void;
|
|
6
|
+
diff(items: T[], updated: T[]): {
|
|
7
|
+
added: T[];
|
|
8
|
+
removed: T[];
|
|
9
|
+
};
|
|
10
|
+
first(items: T[], filter: (item: T) => boolean): T | null;
|
|
11
|
+
filter(items: T[], filter?: (item: T) => boolean): T[] | Exclude<T, Falsy>[];
|
|
12
|
+
flatMap<TMapped>(items: T[], map?: (item: T, index: number) => TMapped[]): TMapped[];
|
|
13
|
+
groupBy<TKey extends string>(items: T[], groupBy: (item: T) => TKey): Partial<Record<TKey, T[]>>;
|
|
14
|
+
groupBy<TKey extends keyof T>(items: T[], groupBy: TKey): Record<string, T[]>;
|
|
15
|
+
isEmpty(items: T[]): boolean;
|
|
16
|
+
project(items: T[], property: string): unknown[];
|
|
17
|
+
randomItem(items: T[]): T | null;
|
|
18
|
+
randomItems(items: T[], count: number): T[];
|
|
19
|
+
remove(items: T[], item: T): boolean;
|
|
20
|
+
removeIndex(items: T[], index: number | string): boolean;
|
|
21
|
+
replace(items: T[], original: T, replacement: T): boolean;
|
|
22
|
+
shuffled(items: T[]): T[];
|
|
23
|
+
sorted(items: T[]): T[];
|
|
24
|
+
unique(items: T[], extractKey?: (item: T) => string): T[];
|
|
25
|
+
where(items: T[], filter: string, value?: unknown): T[];
|
|
26
|
+
without(items: T[], exclude: T[]): T[];
|
|
27
|
+
withoutIndexes(items: T[], indexes: number[]): T[];
|
|
28
|
+
};
|
|
29
|
+
export type FluentArrayInstance<FluentClass, Item> = FluentInstance<FluentClass, Item[], keyof Item[], FluentArrayHelpers<Item>>;
|
|
30
|
+
export type FluentArray<T> = FluentArrayInstance<FluentArrayDefinition<T>, T>;
|
|
31
|
+
declare class FluentArrayDefinition<Item> extends FluentObjectDefinition<Item[]> {
|
|
32
|
+
static create<T>(value?: T[]): FluentArrayInstance<FluentArrayDefinition<T>, T>;
|
|
33
|
+
[Symbol.iterator](): Iterator<Item>;
|
|
34
|
+
flatMap: <T>(callback: (item: Item, index: number) => T[]) => FluentArrayInstance<FluentArrayDefinition<T>, T>;
|
|
35
|
+
map: <T>(callback: (item: Item, index: number) => T) => FluentArrayInstance<FluentArrayDefinition<T>, T>;
|
|
36
|
+
project: <K extends keyof Item>(key: K) => Item[K][];
|
|
37
|
+
where: <K extends keyof Item>(key: K, value?: Item[K]) => FluentArrayInstance<FluentArrayDefinition<Item>, Item>;
|
|
38
|
+
get(index: number): Item | undefined;
|
|
39
|
+
toArray(): Item[];
|
|
40
|
+
protected isPrimitive(value: unknown): value is Item[];
|
|
41
|
+
}
|
|
42
|
+
interface FluentArrayDefinition<Item> {
|
|
43
|
+
filter(): FluentArrayInstance<FluentArrayDefinition<Exclude<Item, Falsy>>, Exclude<Item, Falsy>>;
|
|
44
|
+
filter(filter: (item: Item) => boolean): FluentArrayInstance<FluentArrayDefinition<Item>, Item>;
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
export { FluentArrayDefinition as default };
|
|
48
|
+
//# sourceMappingURL=FluentArray.d.ts.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { arrayClear, arrayDiff, arrayFilter, arrayFirst, arrayFlatMap, arrayGroupBy, arrayIsEmpty, arrayProject, arrayRandomItem, arrayRandomItems, arrayRemove, arrayRemoveIndex, arrayReplace, arrayShuffled, arraySorted, arrayUnique, arrayWhere, arrayWithout, arrayWithoutIndexes } from "../helpers/array_helpers.js";
|
|
2
|
+
import FluentObjectDefinition, { addHelperMethodsToPrototype, addPrimitiveMethodsToPrototype } from "./FluentObject.js";
|
|
3
|
+
//#region src/fluent/FluentArray.ts
|
|
4
|
+
const fluentArrayHelpers = {
|
|
5
|
+
clear: arrayClear,
|
|
6
|
+
diff: arrayDiff,
|
|
7
|
+
filter: arrayFilter,
|
|
8
|
+
first: arrayFirst,
|
|
9
|
+
flatMap: arrayFlatMap,
|
|
10
|
+
groupBy: arrayGroupBy,
|
|
11
|
+
isEmpty: arrayIsEmpty,
|
|
12
|
+
project: arrayProject,
|
|
13
|
+
randomItem: arrayRandomItem,
|
|
14
|
+
randomItems: arrayRandomItems,
|
|
15
|
+
remove: arrayRemove,
|
|
16
|
+
removeIndex: arrayRemoveIndex,
|
|
17
|
+
replace: arrayReplace,
|
|
18
|
+
shuffled: arrayShuffled,
|
|
19
|
+
sorted: arraySorted,
|
|
20
|
+
unique: arrayUnique,
|
|
21
|
+
where: arrayWhere,
|
|
22
|
+
without: arrayWithout,
|
|
23
|
+
withoutIndexes: arrayWithoutIndexes
|
|
24
|
+
};
|
|
25
|
+
var FluentArrayDefinition = class extends FluentObjectDefinition {
|
|
26
|
+
static create(value = []) {
|
|
27
|
+
const { prototype } = this;
|
|
28
|
+
return prototype.create(value);
|
|
29
|
+
}
|
|
30
|
+
*[Symbol.iterator]() {
|
|
31
|
+
yield* this.value;
|
|
32
|
+
}
|
|
33
|
+
get(index) {
|
|
34
|
+
return this.value[index];
|
|
35
|
+
}
|
|
36
|
+
toArray() {
|
|
37
|
+
return this.value.slice(0);
|
|
38
|
+
}
|
|
39
|
+
isPrimitive(value) {
|
|
40
|
+
return Array.isArray(value);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
addHelperMethodsToPrototype(FluentArrayDefinition, fluentArrayHelpers);
|
|
44
|
+
addPrimitiveMethodsToPrototype(FluentArrayDefinition, Array);
|
|
45
|
+
//#endregion
|
|
46
|
+
export { FluentArrayDefinition as default };
|
|
47
|
+
|
|
48
|
+
//# sourceMappingURL=FluentArray.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FluentArray.js","names":[],"sources":["../../src/fluent/FluentArray.ts"],"sourcesContent":["import {\n arrayClear,\n arrayDiff,\n arrayFilter,\n arrayFirst,\n arrayFlatMap,\n arrayGroupBy,\n arrayIsEmpty,\n arrayProject,\n arrayRandomItem,\n arrayRandomItems,\n arrayRemove,\n arrayRemoveIndex,\n arrayReplace,\n arrayShuffled,\n arraySorted,\n arrayUnique,\n arrayWhere,\n arrayWithout,\n arrayWithoutIndexes,\n} from '@noeldemartin/utils/helpers/array_helpers';\nimport type { Falsy } from '@noeldemartin/utils/types/helpers';\n\nimport FluentObjectDefinition, { addHelperMethodsToPrototype, addPrimitiveMethodsToPrototype } from './FluentObject';\nimport type { FluentInstance } from './FluentObject';\n\nconst fluentArrayHelpers: FluentArrayHelpers<unknown> = {\n clear: arrayClear,\n diff: arrayDiff,\n filter: arrayFilter,\n first: arrayFirst,\n flatMap: arrayFlatMap,\n groupBy: arrayGroupBy,\n isEmpty: arrayIsEmpty,\n project: arrayProject,\n randomItem: arrayRandomItem,\n randomItems: arrayRandomItems,\n remove: arrayRemove,\n removeIndex: arrayRemoveIndex,\n replace: arrayReplace,\n shuffled: arrayShuffled,\n sorted: arraySorted,\n unique: arrayUnique,\n where: arrayWhere,\n without: arrayWithout,\n withoutIndexes: arrayWithoutIndexes,\n};\n\nexport type FluentArrayHelpers<T> = {\n clear(items: T[]): void;\n diff(items: T[], updated: T[]): { added: T[]; removed: T[] };\n first(items: T[], filter: (item: T) => boolean): T | null;\n filter(items: T[], filter?: (item: T) => boolean): T[] | Exclude<T, Falsy>[];\n flatMap<TMapped>(items: T[], map?: (item: T, index: number) => TMapped[]): TMapped[];\n groupBy<TKey extends string>(items: T[], groupBy: (item: T) => TKey): Partial<Record<TKey, T[]>>;\n groupBy<TKey extends keyof T>(items: T[], groupBy: TKey): Record<string, T[]>;\n isEmpty(items: T[]): boolean;\n project(items: T[], property: string): unknown[];\n randomItem(items: T[]): T | null;\n randomItems(items: T[], count: number): T[];\n remove(items: T[], item: T): boolean;\n removeIndex(items: T[], index: number | string): boolean;\n replace(items: T[], original: T, replacement: T): boolean;\n shuffled(items: T[]): T[];\n sorted(items: T[]): T[];\n unique(items: T[], extractKey?: (item: T) => string): T[];\n where(items: T[], filter: string, value?: unknown): T[];\n without(items: T[], exclude: T[]): T[];\n withoutIndexes(items: T[], indexes: number[]): T[];\n};\n\nexport type FluentArrayInstance<FluentClass, Item> = FluentInstance<\n FluentClass,\n Item[],\n keyof Item[],\n FluentArrayHelpers<Item>\n>;\n\nexport type FluentArray<T> = FluentArrayInstance<FluentArrayDefinition<T>, T>;\n\nclass FluentArrayDefinition<Item> extends FluentObjectDefinition<Item[]> {\n public static create<T>(value: T[] = []): FluentArrayInstance<FluentArrayDefinition<T>, T> {\n const { prototype } = this as unknown as {\n prototype: { create(value: T[]): FluentArrayInstance<FluentArrayDefinition<T>, T> };\n };\n\n return prototype.create(value);\n }\n\n public *[Symbol.iterator](): Iterator<Item> {\n yield* this.value;\n }\n\n declare public flatMap: <T>(\n callback: (item: Item, index: number) => T[],\n ) => FluentArrayInstance<FluentArrayDefinition<T>, T>;\n\n declare public map: <T>(\n callback: (item: Item, index: number) => T,\n ) => FluentArrayInstance<FluentArrayDefinition<T>, T>;\n\n declare public project: <K extends keyof Item>(key: K) => Item[K][];\n\n declare public where: <K extends keyof Item>(\n key: K,\n value?: Item[K],\n ) => FluentArrayInstance<FluentArrayDefinition<Item>, Item>;\n\n public get(index: number): Item | undefined {\n return this.value[index];\n }\n\n public toArray(): Item[] {\n return this.value.slice(0);\n }\n\n protected isPrimitive(value: unknown): value is Item[] {\n return Array.isArray(value);\n }\n}\n\ninterface FluentArrayDefinition<Item> {\n filter(): FluentArrayInstance<FluentArrayDefinition<Exclude<Item, Falsy>>, Exclude<Item, Falsy>>;\n filter(filter: (item: Item) => boolean): FluentArrayInstance<FluentArrayDefinition<Item>, Item>;\n}\n\naddHelperMethodsToPrototype(FluentArrayDefinition, fluentArrayHelpers);\naddPrimitiveMethodsToPrototype(FluentArrayDefinition, Array);\n\nexport default FluentArrayDefinition;\n"],"mappings":";;;AA0BA,MAAM,qBAAkD;CACpD,OAAO;CACP,MAAM;CACN,QAAQ;CACR,OAAO;CACP,SAAS;CACT,SAAS;CACT,SAAS;CACT,SAAS;CACT,YAAY;CACZ,aAAa;CACb,QAAQ;CACR,aAAa;CACb,SAAS;CACT,UAAU;CACV,QAAQ;CACR,QAAQ;CACR,OAAO;CACP,SAAS;CACT,gBAAgB;AACpB;AAkCA,IAAM,wBAAN,cAA0C,uBAA+B;CACrE,OAAc,OAAU,QAAa,CAAC,GAAqD;EACvF,MAAM,EAAE,cAAc;EAItB,OAAO,UAAU,OAAO,KAAK;CACjC;CAEA,EAAS,OAAO,YAA4B;EACxC,OAAO,KAAK;CAChB;CAiBA,IAAW,OAAiC;EACxC,OAAO,KAAK,MAAM;CACtB;CAEA,UAAyB;EACrB,OAAO,KAAK,MAAM,MAAM,CAAC;CAC7B;CAEA,YAAsB,OAAiC;EACnD,OAAO,MAAM,QAAQ,KAAK;CAC9B;AACJ;AAOA,4BAA4B,uBAAuB,kBAAkB;AACrE,+BAA+B,uBAAuB,KAAK"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/fluent/FluentObject.d.ts
|
|
2
|
+
export type Helper<Primitive = unknown> = (value: Primitive, ...args: any[]) => unknown;
|
|
3
|
+
export type HelperParams<TPrimitive, THelper> = THelper extends ((value: TPrimitive, ...args: infer P) => unknown) ? P : never;
|
|
4
|
+
export type FluentPrimitiveMethods<FluentClass, Primitive extends Record<PrimitiveKey, unknown>, PrimitiveKey extends string | number | symbol, Helpers extends Record<string, Helper<Primitive>>> = { [K in PrimitiveKey]: Primitive extends Record<K, (...args: infer P) => Primitive> ? (...args: P) => FluentInstance<FluentClass, Primitive, PrimitiveKey, Helpers> : Primitive[K]; };
|
|
5
|
+
export type FluentHelperMethods<FluentClass, Primitive extends Record<PrimitiveKey, unknown>, PrimitiveKey extends string | number | symbol, Helpers extends Record<string, Helper<Primitive>>> = { [K in keyof Helpers]: Helpers extends Record<K, (value: Primitive, ...args: any[]) => Primitive> ? (...args: HelperParams<Primitive, Helpers[K]>) => FluentInstance<FluentClass, Primitive, PrimitiveKey, Helpers> : (...args: HelperParams<Primitive, Helpers[K]>) => ReturnType<Helpers[K]>; };
|
|
6
|
+
export type FluentInstance<FluentClass, Primitive extends Record<PrimitiveKey, unknown>, PrimitiveKey extends string | number | symbol, Helpers extends Record<string, Helper<Primitive>>> = FluentClass & FluentHelperMethods<FluentClass, Primitive, PrimitiveKey, Helpers> & FluentPrimitiveMethods<FluentClass, Primitive, PrimitiveKey, Helpers>;
|
|
7
|
+
export default abstract class FluentObjectDefinition<Primitive> {
|
|
8
|
+
protected value: Primitive;
|
|
9
|
+
static isPrimitive(value: unknown): boolean;
|
|
10
|
+
constructor(value: Primitive);
|
|
11
|
+
protected abstract isPrimitive(value: unknown): value is Primitive;
|
|
12
|
+
protected create(value: Primitive): this;
|
|
13
|
+
protected chain(value: unknown): unknown;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
//# sourceMappingURL=FluentObject.d.ts.map
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//#region src/fluent/FluentObject.ts
|
|
2
|
+
function addHelperMethodsToPrototype(fluentClass, helpers) {
|
|
3
|
+
const prototype = fluentClass.prototype;
|
|
4
|
+
for (const [name, method] of Object.entries(helpers)) prototype[name] = function(...args) {
|
|
5
|
+
const value = method(this.value, ...args);
|
|
6
|
+
return this.chain?.(value);
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
function addPrimitiveMethodsToPrototype(fluentClass, primitiveClass) {
|
|
10
|
+
const prototype = fluentClass.prototype;
|
|
11
|
+
const descriptors = Object.getOwnPropertyDescriptors(primitiveClass.prototype);
|
|
12
|
+
for (const [name, descriptor] of Object.entries(descriptors)) {
|
|
13
|
+
if (name in prototype) continue;
|
|
14
|
+
if (typeof descriptor.value !== "function") {
|
|
15
|
+
Object.defineProperty(prototype, name, { get() {
|
|
16
|
+
return this.chain(this.value[name]);
|
|
17
|
+
} });
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
prototype[name] = function(...args) {
|
|
21
|
+
const value = descriptor.value.call(this.value, ...args);
|
|
22
|
+
return this.chain?.(value);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
var FluentObjectDefinition = class {
|
|
27
|
+
value;
|
|
28
|
+
static isPrimitive(value) {
|
|
29
|
+
return this.prototype.isPrimitive(value);
|
|
30
|
+
}
|
|
31
|
+
constructor(value) {
|
|
32
|
+
this.value = value;
|
|
33
|
+
}
|
|
34
|
+
create(value) {
|
|
35
|
+
const constructor = this.constructor;
|
|
36
|
+
return new constructor(value);
|
|
37
|
+
}
|
|
38
|
+
chain(value) {
|
|
39
|
+
return this.isPrimitive(value) ? this.create(value) : value;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
//#endregion
|
|
43
|
+
export { addHelperMethodsToPrototype, addPrimitiveMethodsToPrototype, FluentObjectDefinition as default };
|
|
44
|
+
|
|
45
|
+
//# sourceMappingURL=FluentObject.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FluentObject.js","names":[],"sources":["../../src/fluent/FluentObject.ts"],"sourcesContent":["import type { Closure } from '@noeldemartin/utils/types/helpers';\n\nexport type Helper<Primitive = unknown> = (value: Primitive, ...args: any[]) => unknown;\nexport type HelperParams<TPrimitive, THelper> = THelper extends (value: TPrimitive, ...args: infer P) => unknown\n ? P\n : never;\n\nexport type FluentPrimitiveMethods<\n FluentClass,\n Primitive extends Record<PrimitiveKey, unknown>,\n PrimitiveKey extends string | number | symbol,\n Helpers extends Record<string, Helper<Primitive>>,\n> = {\n [K in PrimitiveKey]: Primitive extends Record<K, (...args: infer P) => Primitive>\n ? (...args: P) => FluentInstance<FluentClass, Primitive, PrimitiveKey, Helpers>\n : Primitive[K];\n};\n\nexport type FluentHelperMethods<\n FluentClass,\n Primitive extends Record<PrimitiveKey, unknown>,\n PrimitiveKey extends string | number | symbol,\n Helpers extends Record<string, Helper<Primitive>>,\n> = {\n [K in keyof Helpers]: Helpers extends Record<K, (value: Primitive, ...args: any[]) => Primitive>\n ? (\n ...args: HelperParams<Primitive, Helpers[K]>\n ) => FluentInstance<FluentClass, Primitive, PrimitiveKey, Helpers>\n : (...args: HelperParams<Primitive, Helpers[K]>) => ReturnType<Helpers[K]>;\n};\n\nexport type FluentInstance<\n FluentClass,\n Primitive extends Record<PrimitiveKey, unknown>,\n PrimitiveKey extends string | number | symbol,\n Helpers extends Record<string, Helper<Primitive>>,\n> = FluentClass &\n FluentHelperMethods<FluentClass, Primitive, PrimitiveKey, Helpers> &\n FluentPrimitiveMethods<FluentClass, Primitive, PrimitiveKey, Helpers>;\n\nexport function addHelperMethodsToPrototype(\n fluentClass: { prototype: unknown },\n helpers: Record<string, Closure>,\n): void {\n const prototype = fluentClass.prototype as Record<string, Closure>;\n\n for (const [name, method] of Object.entries(helpers)) {\n prototype[name] = function (...args: any[]) {\n const value = method(this.value, ...args);\n\n return this.chain?.(value);\n };\n }\n}\n\nexport function addPrimitiveMethodsToPrototype(\n fluentClass: { prototype: unknown },\n primitiveClass: { prototype: unknown },\n): void {\n const prototype = fluentClass.prototype as Record<string, Closure>;\n const descriptors = Object.getOwnPropertyDescriptors(primitiveClass.prototype);\n\n for (const [name, descriptor] of Object.entries(descriptors)) {\n if (name in prototype) continue;\n\n if (typeof descriptor.value !== 'function') {\n Object.defineProperty(prototype, name, {\n get() {\n return this.chain(this.value[name]);\n },\n });\n\n continue;\n }\n\n prototype[name] = function (...args: any[]) {\n const value = descriptor.value.call(this.value, ...args);\n\n return this.chain?.(value);\n };\n }\n}\n\nexport default abstract class FluentObjectDefinition<Primitive> {\n public static isPrimitive(value: unknown): boolean {\n return this.prototype.isPrimitive(value);\n }\n\n constructor(protected value: Primitive) {}\n\n protected abstract isPrimitive(value: unknown): value is Primitive;\n\n protected create(value: Primitive): this {\n const constructor = this.constructor as unknown as { new (v: Primitive): unknown };\n\n return new constructor(value) as this;\n }\n\n protected chain(value: unknown): unknown {\n return this.isPrimitive(value) ? this.create(value) : value;\n }\n}\n"],"mappings":";AAwCA,SAAgB,4BACZ,aACA,SACI;CACJ,MAAM,YAAY,YAAY;CAE9B,KAAK,MAAM,CAAC,MAAM,WAAW,OAAO,QAAQ,OAAO,GAC/C,UAAU,QAAQ,SAAU,GAAG,MAAa;EACxC,MAAM,QAAQ,OAAO,KAAK,OAAO,GAAG,IAAI;EAExC,OAAO,KAAK,QAAQ,KAAK;CAC7B;AAER;AAEA,SAAgB,+BACZ,aACA,gBACI;CACJ,MAAM,YAAY,YAAY;CAC9B,MAAM,cAAc,OAAO,0BAA0B,eAAe,SAAS;CAE7E,KAAK,MAAM,CAAC,MAAM,eAAe,OAAO,QAAQ,WAAW,GAAG;EAC1D,IAAI,QAAQ,WAAW;EAEvB,IAAI,OAAO,WAAW,UAAU,YAAY;GACxC,OAAO,eAAe,WAAW,MAAM,EACnC,MAAM;IACF,OAAO,KAAK,MAAM,KAAK,MAAM,KAAK;GACtC,EACJ,CAAC;GAED;EACJ;EAEA,UAAU,QAAQ,SAAU,GAAG,MAAa;GACxC,MAAM,QAAQ,WAAW,MAAM,KAAK,KAAK,OAAO,GAAG,IAAI;GAEvD,OAAO,KAAK,QAAQ,KAAK;EAC7B;CACJ;AACJ;AAEA,IAA8B,yBAA9B,MAAgE;CAKtC;CAJtB,OAAc,YAAY,OAAyB;EAC/C,OAAO,KAAK,UAAU,YAAY,KAAK;CAC3C;CAEA,YAAY,OAA4B;EAAlB,KAAA,QAAA;CAAmB;CAIzC,OAAiB,OAAwB;EACrC,MAAM,cAAc,KAAK;EAEzB,OAAO,IAAI,YAAY,KAAK;CAChC;CAEA,MAAgB,OAAyB;EACrC,OAAO,KAAK,YAAY,KAAK,IAAI,KAAK,OAAO,KAAK,IAAI;CAC1D;AACJ"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ClassInstance } from "../types/helpers.js";
|
|
2
|
+
import { stringReverse, stringToCamelCase, stringToSlug, stringToStudlyCase, stringToTitleCase } from "../helpers/string_helpers.js";
|
|
3
|
+
import FluentObjectDefinition, { FluentInstance } from "./FluentObject.js";
|
|
4
|
+
//#region src/fluent/FluentString.d.ts
|
|
5
|
+
export declare const fluentStringHelpers: {
|
|
6
|
+
reverse: typeof stringReverse;
|
|
7
|
+
toCamelCase: typeof stringToCamelCase;
|
|
8
|
+
toSlug: typeof stringToSlug;
|
|
9
|
+
toStudlyCase: typeof stringToStudlyCase;
|
|
10
|
+
toTitleCase: typeof stringToTitleCase;
|
|
11
|
+
};
|
|
12
|
+
export type FluentStringInstance<FluentClass> = FluentInstance<FluentClass, string, keyof string, typeof fluentStringHelpers>;
|
|
13
|
+
export type FluentString = FluentStringInstance<FluentStringDefinition>;
|
|
14
|
+
declare class FluentStringDefinition extends FluentObjectDefinition<string> {
|
|
15
|
+
static create<T>(this: T, value?: string): FluentStringInstance<ClassInstance<T>>;
|
|
16
|
+
toString(): string;
|
|
17
|
+
protected isPrimitive(value: unknown): value is string;
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
export { FluentStringDefinition as default };
|
|
21
|
+
//# sourceMappingURL=FluentString.d.ts.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { stringReverse, stringToCamelCase, stringToSlug, stringToStudlyCase, stringToTitleCase } from "../helpers/string_helpers.js";
|
|
2
|
+
import FluentObjectDefinition, { addHelperMethodsToPrototype, addPrimitiveMethodsToPrototype } from "./FluentObject.js";
|
|
3
|
+
//#region src/fluent/FluentString.ts
|
|
4
|
+
const fluentStringHelpers = {
|
|
5
|
+
reverse: stringReverse,
|
|
6
|
+
toCamelCase: stringToCamelCase,
|
|
7
|
+
toSlug: stringToSlug,
|
|
8
|
+
toStudlyCase: stringToStudlyCase,
|
|
9
|
+
toTitleCase: stringToTitleCase
|
|
10
|
+
};
|
|
11
|
+
var FluentStringDefinition = class extends FluentObjectDefinition {
|
|
12
|
+
static create(value = "") {
|
|
13
|
+
const { prototype } = this;
|
|
14
|
+
return prototype.create(value);
|
|
15
|
+
}
|
|
16
|
+
toString() {
|
|
17
|
+
return this.value;
|
|
18
|
+
}
|
|
19
|
+
isPrimitive(value) {
|
|
20
|
+
return typeof value === "string";
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
addHelperMethodsToPrototype(FluentStringDefinition, fluentStringHelpers);
|
|
24
|
+
addPrimitiveMethodsToPrototype(FluentStringDefinition, String);
|
|
25
|
+
//#endregion
|
|
26
|
+
export { FluentStringDefinition as default, fluentStringHelpers };
|
|
27
|
+
|
|
28
|
+
//# sourceMappingURL=FluentString.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FluentString.js","names":[],"sources":["../../src/fluent/FluentString.ts"],"sourcesContent":["import {\n stringReverse,\n stringToCamelCase,\n stringToSlug,\n stringToStudlyCase,\n stringToTitleCase,\n} from '@noeldemartin/utils/helpers/string_helpers';\nimport type { ClassInstance } from '@noeldemartin/utils/types/helpers';\n\nimport FluentObjectDefinition, { addHelperMethodsToPrototype, addPrimitiveMethodsToPrototype } from './FluentObject';\nimport type { FluentInstance } from './FluentObject';\n\nexport const fluentStringHelpers = {\n reverse: stringReverse,\n toCamelCase: stringToCamelCase,\n toSlug: stringToSlug,\n toStudlyCase: stringToStudlyCase,\n toTitleCase: stringToTitleCase,\n};\n\nexport type FluentStringInstance<FluentClass> = FluentInstance<\n FluentClass,\n string,\n keyof string,\n typeof fluentStringHelpers\n>;\n\nexport type FluentString = FluentStringInstance<FluentStringDefinition>;\n\nclass FluentStringDefinition extends FluentObjectDefinition<string> {\n public static create<T>(this: T, value: string = ''): FluentStringInstance<ClassInstance<T>> {\n const { prototype } = this as unknown as {\n prototype: { create(value: string): FluentStringInstance<ClassInstance<T>> };\n };\n\n return prototype.create(value);\n }\n\n public toString(): string {\n return this.value;\n }\n\n protected isPrimitive(value: unknown): value is string {\n return typeof value === 'string';\n }\n}\n\naddHelperMethodsToPrototype(FluentStringDefinition, fluentStringHelpers);\naddPrimitiveMethodsToPrototype(FluentStringDefinition, String);\n\nexport default FluentStringDefinition;\n"],"mappings":";;;AAYA,MAAa,sBAAsB;CAC/B,SAAS;CACT,aAAa;CACb,QAAQ;CACR,cAAc;CACd,aAAa;AACjB;AAWA,IAAM,yBAAN,cAAqC,uBAA+B;CAChE,OAAc,OAAmB,QAAgB,IAA4C;EACzF,MAAM,EAAE,cAAc;EAItB,OAAO,UAAU,OAAO,KAAK;CACjC;CAEA,WAA0B;EACtB,OAAO,KAAK;CAChB;CAEA,YAAsB,OAAiC;EACnD,OAAO,OAAO,UAAU;CAC5B;AACJ;AAEA,4BAA4B,wBAAwB,mBAAmB;AACvE,+BAA+B,wBAAwB,MAAM"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Constructor } from "../types/classes.js";
|
|
2
|
+
//#region src/fluent/facade.d.ts
|
|
3
|
+
export type FacadeExtension = (facadeInstance: FacadeMethods) => void;
|
|
4
|
+
export interface FacadeMethods<TInstance extends object = object> {
|
|
5
|
+
newInstance(): TInstance;
|
|
6
|
+
requireInstance(): TInstance;
|
|
7
|
+
setInstance(instance: TInstance): TInstance;
|
|
8
|
+
reset(): TInstance;
|
|
9
|
+
}
|
|
10
|
+
export type Facade<TInstance extends object = object> = TInstance & FacadeMethods<TInstance>;
|
|
11
|
+
export declare function facade<TInstance extends object>(defaultClassOrFactory: (() => TInstance) | Constructor<TInstance>): Facade<TInstance>;
|
|
12
|
+
export declare function extendFacadeMethods(extension: FacadeExtension): void;
|
|
13
|
+
export declare function isFacade(value: unknown): value is Facade;
|
|
14
|
+
export declare function getFacadeInstance<T extends object>(value: Facade<T>): T;
|
|
15
|
+
export declare function getFacadeInstance<T>(value: T): T;
|
|
16
|
+
//#endregion
|
|
17
|
+
//# sourceMappingURL=facade.d.ts.map
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//#region src/fluent/facade.ts
|
|
2
|
+
const extensions = [];
|
|
3
|
+
const facadeMarker = Symbol("facade");
|
|
4
|
+
function facade(defaultClassOrFactory) {
|
|
5
|
+
let instance = null;
|
|
6
|
+
function newInstance() {
|
|
7
|
+
try {
|
|
8
|
+
return defaultClassOrFactory();
|
|
9
|
+
} catch {
|
|
10
|
+
return new defaultClassOrFactory();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function setInstance(_instance) {
|
|
14
|
+
instance = _instance;
|
|
15
|
+
return _instance;
|
|
16
|
+
}
|
|
17
|
+
const facadeInstance = {
|
|
18
|
+
[facadeMarker]: true,
|
|
19
|
+
setInstance,
|
|
20
|
+
newInstance,
|
|
21
|
+
requireInstance: () => instance ?? setInstance(newInstance()),
|
|
22
|
+
reset: () => setInstance(newInstance())
|
|
23
|
+
};
|
|
24
|
+
extensions.forEach((extension) => extension(facadeInstance));
|
|
25
|
+
return new Proxy(facadeInstance, {
|
|
26
|
+
get: (_, property, receiver) => {
|
|
27
|
+
const t = property !== "constructor" && property in facadeInstance ? facadeInstance : facadeInstance.requireInstance();
|
|
28
|
+
return Reflect.get(t, property, receiver);
|
|
29
|
+
},
|
|
30
|
+
set: (_, property, value) => Reflect.set(facadeInstance.requireInstance(), property, value)
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function extendFacadeMethods(extension) {
|
|
34
|
+
extensions.push(extension);
|
|
35
|
+
}
|
|
36
|
+
function isFacade(value) {
|
|
37
|
+
return typeof value === "object" && value !== null && facadeMarker in value;
|
|
38
|
+
}
|
|
39
|
+
function getFacadeInstance(value) {
|
|
40
|
+
return isFacade(value) ? value.requireInstance() : value;
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
export { extendFacadeMethods, facade, getFacadeInstance, isFacade };
|
|
44
|
+
|
|
45
|
+
//# sourceMappingURL=facade.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"facade.js","names":[],"sources":["../../src/fluent/facade.ts"],"sourcesContent":["import type { Constructor } from '@noeldemartin/utils/types/classes';\n\nconst extensions: FacadeExtension[] = [];\nconst facadeMarker = Symbol('facade');\n\nexport type FacadeExtension = (facadeInstance: FacadeMethods) => void;\n\nexport interface FacadeMethods<TInstance extends object = object> {\n newInstance(): TInstance;\n requireInstance(): TInstance;\n setInstance(instance: TInstance): TInstance;\n reset(): TInstance;\n}\n\nexport type Facade<TInstance extends object = object> = TInstance & FacadeMethods<TInstance>;\n\nexport function facade<TInstance extends object>(\n defaultClassOrFactory: (() => TInstance) | Constructor<TInstance>,\n): Facade<TInstance> {\n let instance: TInstance | null = null;\n\n function newInstance(): TInstance {\n try {\n return (defaultClassOrFactory as () => TInstance)();\n } catch {\n return new (defaultClassOrFactory as Constructor<TInstance>)();\n }\n }\n\n function setInstance(_instance: TInstance): TInstance {\n instance = _instance;\n\n return _instance;\n }\n\n const facadeInstance: FacadeMethods<TInstance> & { [facadeMarker]: true } = {\n [facadeMarker]: true,\n setInstance,\n newInstance,\n requireInstance: () => instance ?? setInstance(newInstance()),\n reset: () => setInstance(newInstance()),\n };\n\n extensions.forEach((extension) => extension(facadeInstance));\n\n return new Proxy(facadeInstance, {\n get: (_, property, receiver) => {\n const t =\n property !== 'constructor' && property in facadeInstance\n ? facadeInstance\n : facadeInstance.requireInstance();\n\n return Reflect.get(t, property, receiver);\n },\n set: (_, property, value) => Reflect.set(facadeInstance.requireInstance(), property, value),\n }) as unknown as Facade<TInstance>;\n}\n\nexport function extendFacadeMethods(extension: FacadeExtension): void {\n extensions.push(extension);\n}\n\nexport function isFacade(value: unknown): value is Facade {\n return typeof value === 'object' && value !== null && facadeMarker in value;\n}\n\nexport function getFacadeInstance<T extends object>(value: Facade<T>): T;\nexport function getFacadeInstance<T>(value: T): T;\nexport function getFacadeInstance<T>(value: T): T {\n return isFacade(value) ? (value.requireInstance() as T) : value;\n}\n"],"mappings":";AAEA,MAAM,aAAgC,CAAC;AACvC,MAAM,eAAe,OAAO,QAAQ;AAapC,SAAgB,OACZ,uBACiB;CACjB,IAAI,WAA6B;CAEjC,SAAS,cAAyB;EAC9B,IAAI;GACA,OAAQ,sBAA0C;EACtD,QAAQ;GACJ,OAAO,IAAK,sBAAiD;EACjE;CACJ;CAEA,SAAS,YAAY,WAAiC;EAClD,WAAW;EAEX,OAAO;CACX;CAEA,MAAM,iBAAsE;GACvE,eAAe;EAChB;EACA;EACA,uBAAuB,YAAY,YAAY,YAAY,CAAC;EAC5D,aAAa,YAAY,YAAY,CAAC;CAC1C;CAEA,WAAW,SAAS,cAAc,UAAU,cAAc,CAAC;CAE3D,OAAO,IAAI,MAAM,gBAAgB;EAC7B,MAAM,GAAG,UAAU,aAAa;GAC5B,MAAM,IACF,aAAa,iBAAiB,YAAY,iBACpC,iBACA,eAAe,gBAAgB;GAEzC,OAAO,QAAQ,IAAI,GAAG,UAAU,QAAQ;EAC5C;EACA,MAAM,GAAG,UAAU,UAAU,QAAQ,IAAI,eAAe,gBAAgB,GAAG,UAAU,KAAK;CAC9F,CAAC;AACL;AAEA,SAAgB,oBAAoB,WAAkC;CAClE,WAAW,KAAK,SAAS;AAC7B;AAEA,SAAgB,SAAS,OAAiC;CACtD,OAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,gBAAgB;AAC1E;AAIA,SAAgB,kBAAqB,OAAa;CAC9C,OAAO,SAAS,KAAK,IAAK,MAAM,gBAAgB,IAAU;AAC9D"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Facade, FacadeExtension, FacadeMethods, extendFacadeMethods, facade, getFacadeInstance, isFacade } from "./facade.js";
|
|
2
|
+
import ObjectsMap from "../helpers/ObjectsMap.js";
|
|
3
|
+
import { Obj } from "../helpers/object_helpers.js";
|
|
4
|
+
import FluentObjectDefinition, { FluentHelperMethods, FluentInstance, FluentPrimitiveMethods, Helper, HelperParams } from "./FluentObject.js";
|
|
5
|
+
import FluentArrayDefinition, { FluentArray, FluentArrayHelpers, FluentArrayInstance } from "./FluentArray.js";
|
|
6
|
+
import FluentStringDefinition, { FluentString, FluentStringInstance, fluentStringHelpers } from "./FluentString.js";
|
|
7
|
+
import { LazyFacade, LazyFacadeInstanceMethods, LazyFacadeMethods, isLazyFacade, lazyFacade } from "./lazyFacade.js";
|
|
8
|
+
import { Tapped, tap } from "./tap.js";
|
|
9
|
+
import { noop } from "./noop.js";
|
|
10
|
+
import { required } from "./required.js";
|
|
11
|
+
import { value } from "./value.js";
|
|
12
|
+
import { when } from "./when.js";
|
|
13
|
+
import { Invaded, invade } from "./invade.js";
|
|
14
|
+
//#region src/fluent/index.d.ts
|
|
15
|
+
export declare function str(value?: string): FluentStringInstance<FluentStringDefinition>;
|
|
16
|
+
export declare function arr<T>(value: Iterable<T>): FluentArrayInstance<FluentArrayDefinition<T>, T>;
|
|
17
|
+
export declare function arr<T>(value: T): FluentArrayInstance<FluentArrayDefinition<T>, T>;
|
|
18
|
+
export declare function arr<T>(): FluentArrayInstance<FluentArrayDefinition<T>, T>;
|
|
19
|
+
export declare function map<T>(items: string[], getKey?: (item: string) => T): Record<string, T>;
|
|
20
|
+
export declare function map<T extends object>(items: []): ObjectsMap<T>;
|
|
21
|
+
export declare function map<T extends object>(items: Iterable<T>, getKey?: (item: T) => string): ObjectsMap<T>;
|
|
22
|
+
export declare function map<T extends object, S extends keyof T>(items: Iterable<T>, key: S): ObjectsMap<T>;
|
|
23
|
+
export declare function pull<T>(items: T[], index: number): T;
|
|
24
|
+
export declare function pull<T extends Obj, K extends keyof T>(obj: T, key: K): T[K];
|
|
25
|
+
export declare function fluent(value: string): FluentStringInstance<FluentStringDefinition>;
|
|
26
|
+
export declare function fluent<T>(value: T[]): FluentArrayInstance<FluentArrayDefinition<T>, T>;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { Facade, FacadeExtension, FacadeMethods, type FluentArray, FluentArrayDefinition, type FluentArrayHelpers, type FluentArrayInstance, type FluentHelperMethods, type FluentInstance, FluentObjectDefinition, type FluentPrimitiveMethods, type FluentString, FluentStringDefinition, type FluentStringInstance, type Helper, type HelperParams, Invaded, LazyFacade, LazyFacadeInstanceMethods, LazyFacadeMethods, type Tapped, extendFacadeMethods, facade, fluentStringHelpers, getFacadeInstance, invade, isFacade, isLazyFacade, lazyFacade, noop, required, tap, value, when };
|
|
29
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { extendFacadeMethods, facade, getFacadeInstance, isFacade } from "./facade.js";
|
|
2
|
+
import { objectPull } from "../helpers/object_helpers.js";
|
|
3
|
+
import { arrayFrom, arrayPull } from "../helpers/array_helpers.js";
|
|
4
|
+
import ObjectsMap from "../helpers/ObjectsMap.js";
|
|
5
|
+
import FluentObjectDefinition from "./FluentObject.js";
|
|
6
|
+
import FluentArrayDefinition from "./FluentArray.js";
|
|
7
|
+
import FluentStringDefinition, { fluentStringHelpers } from "./FluentString.js";
|
|
8
|
+
import { isLazyFacade, lazyFacade } from "./lazyFacade.js";
|
|
9
|
+
import { tap } from "./tap.js";
|
|
10
|
+
import { noop } from "./noop.js";
|
|
11
|
+
import { required } from "./required.js";
|
|
12
|
+
import { value } from "./value.js";
|
|
13
|
+
import { when } from "./when.js";
|
|
14
|
+
import { invade } from "./invade.js";
|
|
15
|
+
//#region src/fluent/index.ts
|
|
16
|
+
const fluentClasses = [FluentArrayDefinition, FluentStringDefinition];
|
|
17
|
+
function str(value = "") {
|
|
18
|
+
return FluentStringDefinition.create(value);
|
|
19
|
+
}
|
|
20
|
+
function arr(value = []) {
|
|
21
|
+
return FluentArrayDefinition.create(arrayFrom(value));
|
|
22
|
+
}
|
|
23
|
+
function map(value = [], key) {
|
|
24
|
+
if (!Array.isArray(value) || typeof value[0] !== "string") return ObjectsMap.createFromArray(value, key);
|
|
25
|
+
const mapItem = key;
|
|
26
|
+
return value.reduce((recordMap, item) => {
|
|
27
|
+
recordMap[item] = mapItem(item);
|
|
28
|
+
return recordMap;
|
|
29
|
+
}, {});
|
|
30
|
+
}
|
|
31
|
+
function pull(obj, key) {
|
|
32
|
+
return Array.isArray(obj) ? arrayPull(obj, key) : objectPull(obj, key);
|
|
33
|
+
}
|
|
34
|
+
function fluent(value) {
|
|
35
|
+
const fluentClass = arr(fluentClasses).first((c) => c.isPrimitive(value));
|
|
36
|
+
return fluentClass ? fluentClass.create(value) : null;
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { FluentArrayDefinition, FluentObjectDefinition, FluentStringDefinition, arr, extendFacadeMethods, facade, fluent, fluentStringHelpers, getFacadeInstance, invade, isFacade, isLazyFacade, lazyFacade, map, noop, pull, required, str, tap, value, when };
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["FluentArray","FluentString"],"sources":["../../src/fluent/index.ts"],"sourcesContent":["import { ObjectsMap, arrayFrom, arrayPull, objectPull } from '@noeldemartin/utils/helpers/index';\nimport type { Obj } from '@noeldemartin/utils/helpers/index';\n\nimport FluentArray from './FluentArray';\nimport type { FluentArrayInstance } from './FluentArray';\nimport FluentString from './FluentString';\nimport type { FluentStringInstance } from './FluentString';\n\nconst fluentClasses = [FluentArray, FluentString];\n\nexport function str(value: string = ''): FluentStringInstance<FluentString> {\n return FluentString.create(value);\n}\n\nexport function arr<T>(value: Iterable<T>): FluentArrayInstance<FluentArray<T>, T>;\nexport function arr<T>(value: T): FluentArrayInstance<FluentArray<T>, T>;\nexport function arr<T>(): FluentArrayInstance<FluentArray<T>, T>;\nexport function arr<T>(value: unknown = []): FluentArrayInstance<FluentArray<T>, T> {\n return FluentArray.create(arrayFrom(value) as T[]);\n}\n\nexport function map<T>(items: string[], getKey?: (item: string) => T): Record<string, T>;\nexport function map<T extends object>(items: []): ObjectsMap<T>;\nexport function map<T extends object>(items: Iterable<T>, getKey?: (item: T) => string): ObjectsMap<T>;\nexport function map<T extends object, S extends keyof T>(items: Iterable<T>, key: S): ObjectsMap<T>;\nexport function map<T extends object, S extends keyof T>(\n value: Iterable<T> = [],\n key?: S | ((item: T) => string),\n): ObjectsMap<T> {\n if (!Array.isArray(value) || typeof value[0] !== 'string') return ObjectsMap.createFromArray(value, key as S);\n\n const mapItem = key as unknown as (item: string) => T;\n\n return value.reduce(\n (recordMap, item) => {\n recordMap[item] = mapItem(item);\n\n return recordMap;\n },\n {} as Record<string, unknown>,\n );\n}\n\nexport function pull<T>(items: T[], index: number): T;\nexport function pull<T extends Obj, K extends keyof T>(obj: T, key: K): T[K];\nexport function pull(obj: unknown, key: unknown): unknown {\n return Array.isArray(obj) ? arrayPull(obj, key as number) : objectPull(obj as Obj, key as keyof Obj);\n}\n\nexport function fluent(value: string): FluentStringInstance<FluentString>;\nexport function fluent<T>(value: T[]): FluentArrayInstance<FluentArray<T>, T>;\nexport function fluent(value: unknown): unknown {\n const fluentClass = arr(fluentClasses).first((c) => c.isPrimitive(value));\n\n return fluentClass ? (fluentClass as { create(v: unknown): unknown }).create(value) : null;\n}\n\nexport * from './facade';\nexport * from './lazyFacade';\nexport type { Facade, FacadeMethods } from './facade';\nexport type { LazyFacade, LazyFacadeMethods } from './lazyFacade';\n\nexport { tap } from './tap';\nexport type { Tapped } from './tap';\n\nexport { noop } from './noop';\nexport { required } from './required';\nexport { value } from './value';\nexport { when } from './when';\n\nexport { default as FluentObjectDefinition } from './FluentObject';\nexport type { FluentHelperMethods, FluentInstance, FluentPrimitiveMethods, Helper, HelperParams } from './FluentObject';\n\nexport { default as FluentArrayDefinition } from './FluentArray';\nexport type { FluentArray, FluentArrayHelpers, FluentArrayInstance } from './FluentArray';\n\nexport { default as FluentStringDefinition, fluentStringHelpers } from './FluentString';\nexport type { FluentString, FluentStringInstance } from './FluentString';\n\nexport * from './invade';\n"],"mappings":";;;;;;;;;;;;;;;AAQA,MAAM,gBAAgB,CAACA,uBAAaC,sBAAY;AAEhD,SAAgB,IAAI,QAAgB,IAAwC;CACxE,OAAOA,uBAAa,OAAO,KAAK;AACpC;AAKA,SAAgB,IAAO,QAAiB,CAAC,GAA2C;CAChF,OAAOD,sBAAY,OAAO,UAAU,KAAK,CAAQ;AACrD;AAMA,SAAgB,IACZ,QAAqB,CAAC,GACtB,KACa;CACb,IAAI,CAAC,MAAM,QAAQ,KAAK,KAAK,OAAO,MAAM,OAAO,UAAU,OAAO,WAAW,gBAAgB,OAAO,GAAQ;CAE5G,MAAM,UAAU;CAEhB,OAAO,MAAM,QACR,WAAW,SAAS;EACjB,UAAU,QAAQ,QAAQ,IAAI;EAE9B,OAAO;CACX,GACA,CAAC,CACL;AACJ;AAIA,SAAgB,KAAK,KAAc,KAAuB;CACtD,OAAO,MAAM,QAAQ,GAAG,IAAI,UAAU,KAAK,GAAa,IAAI,WAAW,KAAY,GAAgB;AACvG;AAIA,SAAgB,OAAO,OAAyB;CAC5C,MAAM,cAAc,IAAI,aAAa,CAAC,CAAC,OAAO,MAAM,EAAE,YAAY,KAAK,CAAC;CAExE,OAAO,cAAe,YAAgD,OAAO,KAAK,IAAI;AAC1F"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Override } from "../types/helpers.js";
|
|
2
|
+
//#region src/fluent/invade.d.ts
|
|
3
|
+
export type Invaded<TObject, TMethods extends string> = { [K in TMethods]: TObject[K]; };
|
|
4
|
+
export declare function invade<T, TMethods extends string>(object: T, methods: TMethods[]): Override<T, Invaded<T, TMethods>>;
|
|
5
|
+
//#endregion
|
|
6
|
+
//# sourceMappingURL=invade.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invade.js","names":[],"sources":["../../src/fluent/invade.ts"],"sourcesContent":["import type { Override } from '@noeldemartin/utils/types';\n\n// @ts-expect-error Allow invades\nexport type Invaded<TObject, TMethods extends string> = { [K in TMethods]: TObject[K] };\n\n// oxlint-disable-next-line typescript/no-unused-vars\nexport function invade<T, TMethods extends string>(object: T, methods: TMethods[]): Override<T, Invaded<T, TMethods>> {\n return object as Override<T, Invaded<T, TMethods>>;\n}\n"],"mappings":";AAMA,SAAgB,OAAmC,QAAW,SAAwD;CAClH,OAAO;AACX"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { GetObjectMethods, Override } from "../types/helpers.js";
|
|
2
|
+
import { Constructor } from "../types/classes.js";
|
|
3
|
+
//#region src/fluent/lazyFacade.d.ts
|
|
4
|
+
export interface LazyFacadeMethods<TInstance extends object = object> {
|
|
5
|
+
getInstance(): Promise<TInstance>;
|
|
6
|
+
getProperty<TProperty extends keyof TInstance>(property: TProperty): Promise<TInstance[TProperty]>;
|
|
7
|
+
setInstance(instance: TInstance): TInstance;
|
|
8
|
+
reset(): void;
|
|
9
|
+
}
|
|
10
|
+
export type LazyFacadeInstanceMethods<TInstance extends object> = { [K in GetObjectMethods<TInstance>]: TInstance[K] extends ((...args: infer TArgs) => infer TResult) ? (...args: TArgs) => Promise<Awaited<TResult>> : never; };
|
|
11
|
+
export type LazyFacade<TInstance extends object = object> = Override<LazyFacadeInstanceMethods<TInstance>, LazyFacadeMethods<TInstance>>;
|
|
12
|
+
export declare function lazyFacade<TInstance extends object>(factory: () => Promise<TInstance>): LazyFacade<TInstance>;
|
|
13
|
+
export declare function lazyFacade<TInstance extends object>(defaultClassOrFactory: (() => TInstance) | Constructor<TInstance>): LazyFacade<TInstance>;
|
|
14
|
+
export declare function isLazyFacade(value: unknown): value is LazyFacade;
|
|
15
|
+
//#endregion
|
|
16
|
+
//# sourceMappingURL=lazyFacade.d.ts.map
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
//#region src/fluent/lazyFacade.ts
|
|
2
|
+
const lazyFacadeMarker = Symbol("lazyFacade");
|
|
3
|
+
function lazyFacade(defaultClassOrFactory) {
|
|
4
|
+
let instance = null;
|
|
5
|
+
let loadingInstance = null;
|
|
6
|
+
function newInstance() {
|
|
7
|
+
try {
|
|
8
|
+
return defaultClassOrFactory();
|
|
9
|
+
} catch (error) {
|
|
10
|
+
try {
|
|
11
|
+
return new defaultClassOrFactory();
|
|
12
|
+
} catch {
|
|
13
|
+
throw error;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
async function getInstance() {
|
|
18
|
+
while (!instance) {
|
|
19
|
+
const loading = loadingInstance ??= Promise.resolve().then(newInstance);
|
|
20
|
+
try {
|
|
21
|
+
const loadedInstance = await loading;
|
|
22
|
+
if (loadingInstance === loading) instance = loadedInstance;
|
|
23
|
+
} catch (error) {
|
|
24
|
+
if (loadingInstance !== loading) continue;
|
|
25
|
+
loadingInstance = null;
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return instance;
|
|
30
|
+
}
|
|
31
|
+
const lazyFacadeInstance = {
|
|
32
|
+
[lazyFacadeMarker]: true,
|
|
33
|
+
getInstance,
|
|
34
|
+
getProperty: async (property) => (await getInstance())[property],
|
|
35
|
+
setInstance: (_instance) => {
|
|
36
|
+
instance = _instance;
|
|
37
|
+
loadingInstance = null;
|
|
38
|
+
return _instance;
|
|
39
|
+
},
|
|
40
|
+
reset: () => {
|
|
41
|
+
instance = null;
|
|
42
|
+
loadingInstance = null;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
return new Proxy(lazyFacadeInstance, {
|
|
46
|
+
get: (_, property) => {
|
|
47
|
+
if (Object.hasOwn(lazyFacadeInstance, property)) return Reflect.get(lazyFacadeInstance, property);
|
|
48
|
+
if (property === "then" || typeof property === "symbol") return;
|
|
49
|
+
return async (...args) => {
|
|
50
|
+
const target = await getInstance();
|
|
51
|
+
const method = Reflect.get(target, property);
|
|
52
|
+
if (typeof method !== "function") throw new Error(`'${String(property)}' is not a function, use getProperty() to read properties`);
|
|
53
|
+
return method.apply(target, args);
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
set: (_, property) => {
|
|
57
|
+
throw new Error(`Can't set '${String(property)}' on a lazy facade, use getInstance() instead`);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function isLazyFacade(value) {
|
|
62
|
+
return typeof value === "object" && value !== null && lazyFacadeMarker in value;
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
export { isLazyFacade, lazyFacade };
|
|
66
|
+
|
|
67
|
+
//# sourceMappingURL=lazyFacade.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lazyFacade.js","names":[],"sources":["../../src/fluent/lazyFacade.ts"],"sourcesContent":["import type { Constructor } from '@noeldemartin/utils/types/classes';\nimport type { GetObjectMethods, Override } from '@noeldemartin/utils/types/helpers';\n\nconst lazyFacadeMarker = Symbol('lazyFacade');\n\nexport interface LazyFacadeMethods<TInstance extends object = object> {\n getInstance(): Promise<TInstance>;\n getProperty<TProperty extends keyof TInstance>(property: TProperty): Promise<TInstance[TProperty]>;\n setInstance(instance: TInstance): TInstance;\n reset(): void;\n}\n\nexport type LazyFacadeInstanceMethods<TInstance extends object> = {\n [K in GetObjectMethods<TInstance>]: TInstance[K] extends (...args: infer TArgs) => infer TResult\n ? (...args: TArgs) => Promise<Awaited<TResult>>\n : never;\n};\n\nexport type LazyFacade<TInstance extends object = object> = Override<\n LazyFacadeInstanceMethods<TInstance>,\n LazyFacadeMethods<TInstance>\n>;\n\nexport function lazyFacade<TInstance extends object>(factory: () => Promise<TInstance>): LazyFacade<TInstance>;\nexport function lazyFacade<TInstance extends object>(\n defaultClassOrFactory: (() => TInstance) | Constructor<TInstance>,\n): LazyFacade<TInstance>;\nexport function lazyFacade<TInstance extends object>(\n defaultClassOrFactory: (() => TInstance | Promise<TInstance>) | Constructor<TInstance>,\n): LazyFacade<TInstance> {\n let instance: TInstance | null = null;\n let loadingInstance: Promise<TInstance> | null = null;\n\n function newInstance(): TInstance | Promise<TInstance> {\n try {\n return (defaultClassOrFactory as () => TInstance | Promise<TInstance>)();\n } catch (error) {\n try {\n return new (defaultClassOrFactory as Constructor<TInstance>)();\n } catch {\n throw error;\n }\n }\n }\n\n async function getInstance(): Promise<TInstance> {\n while (!instance) {\n const loading = (loadingInstance ??= Promise.resolve().then(newInstance));\n\n try {\n const loadedInstance = await loading;\n\n if (loadingInstance === loading) {\n instance = loadedInstance;\n }\n } catch (error) {\n if (loadingInstance !== loading) {\n continue;\n }\n\n loadingInstance = null;\n\n throw error;\n }\n }\n\n return instance;\n }\n\n const lazyFacadeInstance: LazyFacadeMethods<TInstance> & { [lazyFacadeMarker]: true } = {\n [lazyFacadeMarker]: true,\n getInstance,\n getProperty: async (property) => (await getInstance())[property],\n setInstance: (_instance) => {\n instance = _instance;\n loadingInstance = null;\n\n return _instance;\n },\n reset: () => {\n instance = null;\n loadingInstance = null;\n },\n };\n\n return new Proxy(lazyFacadeInstance, {\n get: (_, property) => {\n if (Object.hasOwn(lazyFacadeInstance, property)) {\n return Reflect.get(lazyFacadeInstance, property);\n }\n\n if (property === 'then' || typeof property === 'symbol') {\n return undefined;\n }\n\n return async (...args: unknown[]) => {\n const target = await getInstance();\n const method = Reflect.get(target, property);\n\n if (typeof method !== 'function') {\n throw new Error(`'${String(property)}' is not a function, use getProperty() to read properties`);\n }\n\n return method.apply(target, args);\n };\n },\n set: (_, property) => {\n throw new Error(`Can't set '${String(property)}' on a lazy facade, use getInstance() instead`);\n },\n }) as unknown as LazyFacade<TInstance>;\n}\n\nexport function isLazyFacade(value: unknown): value is LazyFacade {\n return typeof value === 'object' && value !== null && lazyFacadeMarker in value;\n}\n"],"mappings":";AAGA,MAAM,mBAAmB,OAAO,YAAY;AAwB5C,SAAgB,WACZ,uBACqB;CACrB,IAAI,WAA6B;CACjC,IAAI,kBAA6C;CAEjD,SAAS,cAA8C;EACnD,IAAI;GACA,OAAQ,sBAA+D;EAC3E,SAAS,OAAO;GACZ,IAAI;IACA,OAAO,IAAK,sBAAiD;GACjE,QAAQ;IACJ,MAAM;GACV;EACJ;CACJ;CAEA,eAAe,cAAkC;EAC7C,OAAO,CAAC,UAAU;GACd,MAAM,UAAW,oBAAoB,QAAQ,QAAQ,CAAC,CAAC,KAAK,WAAW;GAEvE,IAAI;IACA,MAAM,iBAAiB,MAAM;IAE7B,IAAI,oBAAoB,SACpB,WAAW;GAEnB,SAAS,OAAO;IACZ,IAAI,oBAAoB,SACpB;IAGJ,kBAAkB;IAElB,MAAM;GACV;EACJ;EAEA,OAAO;CACX;CAEA,MAAM,qBAAkF;GACnF,mBAAmB;EACpB;EACA,aAAa,OAAO,cAAc,MAAM,YAAY,EAAA,CAAG;EACvD,cAAc,cAAc;GACxB,WAAW;GACX,kBAAkB;GAElB,OAAO;EACX;EACA,aAAa;GACT,WAAW;GACX,kBAAkB;EACtB;CACJ;CAEA,OAAO,IAAI,MAAM,oBAAoB;EACjC,MAAM,GAAG,aAAa;GAClB,IAAI,OAAO,OAAO,oBAAoB,QAAQ,GAC1C,OAAO,QAAQ,IAAI,oBAAoB,QAAQ;GAGnD,IAAI,aAAa,UAAU,OAAO,aAAa,UAC3C;GAGJ,OAAO,OAAO,GAAG,SAAoB;IACjC,MAAM,SAAS,MAAM,YAAY;IACjC,MAAM,SAAS,QAAQ,IAAI,QAAQ,QAAQ;IAE3C,IAAI,OAAO,WAAW,YAClB,MAAM,IAAI,MAAM,IAAI,OAAO,QAAQ,EAAE,0DAA0D;IAGnG,OAAO,OAAO,MAAM,QAAQ,IAAI;GACpC;EACJ;EACA,MAAM,GAAG,aAAa;GAClB,MAAM,IAAI,MAAM,cAAc,OAAO,QAAQ,EAAE,8CAA8C;EACjG;CACJ,CAAC;AACL;AAEA,SAAgB,aAAa,OAAqC;CAC9D,OAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,oBAAoB;AAC9E"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"noop.js","names":[],"sources":["../../src/fluent/noop.ts"],"sourcesContent":["export function noop(): void {\n // Nothing to do here.\n}\n"],"mappings":";AAAA,SAAgB,OAAa,CAE7B"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
//#region src/fluent/required.d.ts
|
|
2
|
+
export declare function required<T extends object | null | undefined>(getValue: () => T, errorMessage?: string): NonNullable<T>;
|
|
3
|
+
export declare function required<T>(value: T, errorMessage?: string): NonNullable<T>;
|
|
4
|
+
//#endregion
|
|
5
|
+
//# sourceMappingURL=required.d.ts.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { fail } from "../helpers/error_helpers.js";
|
|
2
|
+
//#region src/fluent/required.ts
|
|
3
|
+
var RequiredHandler = class {
|
|
4
|
+
errorMessage;
|
|
5
|
+
getValue;
|
|
6
|
+
constructor(errorMessage, getValue) {
|
|
7
|
+
this.errorMessage = errorMessage;
|
|
8
|
+
this.getValue = getValue;
|
|
9
|
+
}
|
|
10
|
+
get(_, property, receiver) {
|
|
11
|
+
const obj = this.getValue() ?? fail(this.errorMessage);
|
|
12
|
+
const value = Reflect.get(obj, property, receiver);
|
|
13
|
+
return typeof value === "function" ? value.bind(obj) : value;
|
|
14
|
+
}
|
|
15
|
+
set(_, property, value, receiver) {
|
|
16
|
+
const obj = this.getValue() ?? fail(this.errorMessage);
|
|
17
|
+
return Reflect.set(obj, property, value, receiver);
|
|
18
|
+
}
|
|
19
|
+
getPrototypeOf() {
|
|
20
|
+
const obj = this.getValue() ?? fail(this.errorMessage);
|
|
21
|
+
return Object.getPrototypeOf(obj);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
function required(value, errorMessage) {
|
|
25
|
+
errorMessage ??= "Required value is missing";
|
|
26
|
+
if (typeof value === "function") return new Proxy({}, new RequiredHandler(errorMessage, value));
|
|
27
|
+
return value ?? fail(errorMessage);
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
export { required };
|
|
31
|
+
|
|
32
|
+
//# sourceMappingURL=required.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"required.js","names":[],"sources":["../../src/fluent/required.ts"],"sourcesContent":["import { fail } from '@noeldemartin/utils/helpers/error_helpers';\n\nclass RequiredHandler<Target extends object> implements ProxyHandler<Target> {\n constructor(\n private errorMessage: string,\n private getValue: () => Target,\n ) {}\n\n // oxlint-disable-next-line typescript/no-explicit-any\n public get(_: Target, property: string | symbol, receiver: any): any {\n const obj = this.getValue() ?? fail<Target>(this.errorMessage);\n const value = Reflect.get(obj, property, receiver);\n\n return typeof value === 'function' ? value.bind(obj) : value;\n }\n\n // oxlint-disable-next-line typescript/no-explicit-any\n public set?(_: Target, property: string | symbol, value: any, receiver: any): boolean {\n const obj = this.getValue() ?? fail<Target>(this.errorMessage);\n\n return Reflect.set(obj, property, value, receiver);\n }\n\n public getPrototypeOf(): object | null {\n const obj = this.getValue() ?? fail<Target>(this.errorMessage);\n\n return Object.getPrototypeOf(obj);\n }\n}\n\nexport function required<T extends object | null | undefined>(getValue: () => T, errorMessage?: string): NonNullable<T>;\nexport function required<T>(value: T, errorMessage?: string): NonNullable<T>;\nexport function required<T>(value: (() => T) | T, errorMessage?: string): NonNullable<T> {\n errorMessage ??= 'Required value is missing';\n\n if (typeof value === 'function') {\n return new Proxy({}, new RequiredHandler(errorMessage, value as unknown as () => object)) as NonNullable<T>;\n }\n\n return (value ?? fail(errorMessage)) as NonNullable<T>;\n}\n"],"mappings":";;AAEA,IAAM,kBAAN,MAA6E;CAE7D;CACA;CAFZ,YACI,cACA,UACF;EAFU,KAAA,eAAA;EACA,KAAA,WAAA;CACT;CAGH,IAAW,GAAW,UAA2B,UAAoB;EACjE,MAAM,MAAM,KAAK,SAAS,KAAK,KAAa,KAAK,YAAY;EAC7D,MAAM,QAAQ,QAAQ,IAAI,KAAK,UAAU,QAAQ;EAEjD,OAAO,OAAO,UAAU,aAAa,MAAM,KAAK,GAAG,IAAI;CAC3D;CAGA,IAAY,GAAW,UAA2B,OAAY,UAAwB;EAClF,MAAM,MAAM,KAAK,SAAS,KAAK,KAAa,KAAK,YAAY;EAE7D,OAAO,QAAQ,IAAI,KAAK,UAAU,OAAO,QAAQ;CACrD;CAEA,iBAAuC;EACnC,MAAM,MAAM,KAAK,SAAS,KAAK,KAAa,KAAK,YAAY;EAE7D,OAAO,OAAO,eAAe,GAAG;CACpC;AACJ;AAIA,SAAgB,SAAY,OAAsB,cAAuC;CACrF,iBAAiB;CAEjB,IAAI,OAAO,UAAU,YACjB,OAAO,IAAI,MAAM,CAAC,GAAG,IAAI,gBAAgB,cAAc,KAAgC,CAAC;CAG5F,OAAQ,SAAS,KAAK,YAAY;AACtC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region src/fluent/tap.d.ts
|
|
2
|
+
export type Tapped<Target extends object> = { [prop in keyof Target]: Target[prop] extends ((...params: infer Params) => unknown) ? (...params: Params) => Tapped<Target> : Target[prop]; };
|
|
3
|
+
export declare function tap<Target extends object>(target: Target): Tapped<Target>;
|
|
4
|
+
export declare function tap<Target>(target: Target, callback: (target: Target) => Promise<unknown>): Promise<Target>;
|
|
5
|
+
export declare function tap<Target>(target: Target, callback: (target: Target) => unknown): Target;
|
|
6
|
+
//#endregion
|
|
7
|
+
//# sourceMappingURL=tap.d.ts.map
|