@noeldemartin/utils 0.4.0 → 0.5.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/.github/workflows/ci.yml +16 -0
- package/README.md +1 -1
- package/dist/noeldemartin-utils.cjs.js +1 -1
- package/dist/noeldemartin-utils.cjs.js.map +1 -1
- package/dist/noeldemartin-utils.d.ts +65 -17
- package/dist/noeldemartin-utils.esm.js +1 -1
- package/dist/noeldemartin-utils.esm.js.map +1 -1
- package/dist/noeldemartin-utils.umd.js +1 -1
- package/dist/noeldemartin-utils.umd.js.map +1 -1
- package/package.json +2 -2
- package/src/fluent/FluentString.ts +8 -1
- package/src/fluent/facade.ts +55 -8
- package/src/fluent/tap.ts +1 -1
- package/src/helpers/MagicObject.ts +71 -48
- package/src/helpers/array_helpers.ts +26 -4
- package/src/helpers/caching_helpers.ts +15 -0
- package/src/helpers/index.ts +1 -1
- package/src/helpers/object_helpers.ts +27 -1
- package/src/helpers/promise_helpers.ts +17 -0
- package/src/helpers/string_helpers.ts +6 -0
- package/src/testing/index.ts +1 -0
- package/src/testing/mocking.ts +10 -7
- package/src/testing/namespace.ts +15 -0
- package/src/types/helpers.ts +12 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noeldemartin/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "My JavaScript utilities",
|
|
5
5
|
"main": "dist/noeldemartin-utils.cjs.js",
|
|
6
6
|
"module": "dist/noeldemartin-utils.esm.js",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"types": "dist/noeldemartin-utils.d.ts",
|
|
9
9
|
"sideEffects": false,
|
|
10
10
|
"scripts": {
|
|
11
|
-
"build": "rm
|
|
11
|
+
"build": "rm -rf dist && npm run build:js && npm run build:types",
|
|
12
12
|
"build:js": "noeldemartin-build-javascript",
|
|
13
13
|
"build:types": "noeldemartin-build-types && ./scripts/fix-error-types.sh",
|
|
14
14
|
"lint": "noeldemartin-lint src",
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
stringReverse,
|
|
3
|
+
stringToCamelCase,
|
|
4
|
+
stringToSlug,
|
|
5
|
+
stringToStudlyCase,
|
|
6
|
+
stringToTitleCase,
|
|
7
|
+
} from '@/helpers/string_helpers';
|
|
2
8
|
import type { ClassInstance } from '@/types/index';
|
|
3
9
|
|
|
4
10
|
import FluentObjectDefinition, {
|
|
@@ -12,6 +18,7 @@ export const fluentStringHelpers = {
|
|
|
12
18
|
toCamelCase: stringToCamelCase,
|
|
13
19
|
toSlug: stringToSlug,
|
|
14
20
|
toStudlyCase: stringToStudlyCase,
|
|
21
|
+
toTitleCase: stringToTitleCase,
|
|
15
22
|
};
|
|
16
23
|
|
|
17
24
|
export type FluentStringInstance<FluentClass> =
|
package/src/fluent/facade.ts
CHANGED
|
@@ -1,18 +1,53 @@
|
|
|
1
1
|
import { fail } from '@/helpers/error_helpers';
|
|
2
|
+
import { getClassMethods } from '@/helpers/object_helpers';
|
|
3
|
+
import { mock } from '@/testing/mocking';
|
|
4
|
+
import type { ClassInstance } from '@/types/helpers';
|
|
5
|
+
import type { Constructor } from '@/types/classes';
|
|
6
|
+
import type { Obj } from '@/helpers/object_helpers';
|
|
2
7
|
|
|
3
|
-
export type FacadeMethods<
|
|
4
|
-
instance:
|
|
5
|
-
|
|
6
|
-
|
|
8
|
+
export type FacadeMethods<TInstance, TMockClass extends Constructor> = {
|
|
9
|
+
instance: TInstance | null;
|
|
10
|
+
mockClass: TMockClass | null;
|
|
11
|
+
mockInstance: ClassInstance<TMockClass> | null;
|
|
12
|
+
setInstance(instance: TInstance): void;
|
|
13
|
+
setMockInstance: (mockClass: ClassInstance<TMockClass>) => void;
|
|
14
|
+
setMockClass: (mockClass: TMockClass) => void;
|
|
15
|
+
requireInstance(): TInstance;
|
|
16
|
+
mock: <T extends ClassInstance<TMockClass>>(mockInstance?: T | Obj | object) => T;
|
|
7
17
|
};
|
|
8
18
|
|
|
9
|
-
export type Facade<
|
|
19
|
+
export type Facade<TInstance, TMockClass extends Constructor> = TInstance & FacadeMethods<TInstance, TMockClass>;
|
|
10
20
|
|
|
11
|
-
export function facade<
|
|
12
|
-
|
|
21
|
+
export function facade<TInstance extends object>(
|
|
22
|
+
instance?: TInstance | null
|
|
23
|
+
): Facade<TInstance, Constructor<TInstance>>;
|
|
24
|
+
export function facade<TInstance extends object, TMockClass extends Constructor>(
|
|
25
|
+
instance: TInstance | null,
|
|
26
|
+
mockClass: TMockClass | null,
|
|
27
|
+
): Facade<TInstance, TMockClass>;
|
|
28
|
+
export function facade<TInstance extends object, TMockClass extends Constructor>(
|
|
29
|
+
instance: TInstance | null = null,
|
|
30
|
+
mockClass: TMockClass | null = null,
|
|
31
|
+
): Facade<TInstance, TMockClass> {
|
|
32
|
+
const facade: FacadeMethods<TInstance, TMockClass> = {
|
|
13
33
|
instance,
|
|
34
|
+
mockClass,
|
|
35
|
+
mockInstance: null,
|
|
14
36
|
setInstance: instance => facade.instance = instance,
|
|
37
|
+
setMockInstance: mockInstance => facade.mockInstance = mockInstance,
|
|
38
|
+
setMockClass: mockClass => facade.mockClass = mockClass,
|
|
15
39
|
requireInstance: () => facade.instance ?? fail('Facade not initialized'),
|
|
40
|
+
mock<T extends ClassInstance<TMockClass>>(mockInstance?: T | Obj | object) {
|
|
41
|
+
const instance = mock(
|
|
42
|
+
mockInstance ??
|
|
43
|
+
getFacadeInstance(facade.mockInstance) ??
|
|
44
|
+
(facade.mockClass ? new facade.mockClass() : getClassMethods(facade.instance as object)),
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
facade.instance = instance as TInstance;
|
|
48
|
+
|
|
49
|
+
return instance as T;
|
|
50
|
+
},
|
|
16
51
|
};
|
|
17
52
|
|
|
18
53
|
return new Proxy(facade, {
|
|
@@ -22,5 +57,17 @@ export function facade<T extends object>(instance: T | null = null): Facade<T> {
|
|
|
22
57
|
return Reflect.get(t, property, receiver);
|
|
23
58
|
},
|
|
24
59
|
set: (_, property, receiver) => Reflect.set(facade.requireInstance(), property, receiver),
|
|
25
|
-
}) as unknown as Facade<
|
|
60
|
+
}) as unknown as Facade<TInstance, TMockClass>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function isFacade(facade: unknown): facade is Facade<unknown, Constructor<object>> {
|
|
64
|
+
return typeof facade === 'object'
|
|
65
|
+
&& facade !== null
|
|
66
|
+
&& 'requireInstance' in facade;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function getFacadeInstance<T>(facade: Facade<T, Constructor<object>>): T;
|
|
70
|
+
export function getFacadeInstance<T>(facade: T): T;
|
|
71
|
+
export function getFacadeInstance<T>(facade: T): T {
|
|
72
|
+
return isFacade(facade) ? facade.requireInstance() as T : facade;
|
|
26
73
|
}
|
package/src/fluent/tap.ts
CHANGED
|
@@ -29,8 +29,8 @@ function proxyTap<Target extends object>(target: Target): Tapped<Target> {
|
|
|
29
29
|
|
|
30
30
|
/* eslint-disable max-len */
|
|
31
31
|
export function tap<Target extends object>(target: Target): Tapped<Target>;
|
|
32
|
-
export function tap<Target>(target: Target, callback: (target: Target) => unknown): Target;
|
|
33
32
|
export function tap<Target>(target: Target, callback: (target: Target) => Promise<unknown>): Promise<Target>;
|
|
33
|
+
export function tap<Target>(target: Target, callback: (target: Target) => unknown): Target;
|
|
34
34
|
export function tap<Target extends object>(target: Target, callback?: (target: Target) => unknown | Promise<unknown>): Target | Tapped<Target> | Promise<Target> {
|
|
35
35
|
if (!callback)
|
|
36
36
|
return proxyTap(target);
|
|
@@ -1,40 +1,61 @@
|
|
|
1
1
|
import type { Constructor } from '../types/classes';
|
|
2
|
+
import type { ClosureArgs } from '../types/helpers';
|
|
3
|
+
|
|
4
|
+
export interface MagicObjectProxy<T> {
|
|
5
|
+
target: T;
|
|
6
|
+
instance: T;
|
|
7
|
+
}
|
|
2
8
|
|
|
3
9
|
export type MagicObjectConstructor<T extends MagicObject = MagicObject> = Constructor<T> & typeof MagicObject;
|
|
4
10
|
|
|
5
11
|
export default class MagicObject {
|
|
6
12
|
|
|
7
|
-
private static
|
|
13
|
+
private static __conjuring: boolean;
|
|
8
14
|
private static __reservedProperties: WeakMap<typeof MagicObject, Set<string>> = new WeakMap;
|
|
9
15
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
16
|
+
protected static isConjuring(): boolean {
|
|
17
|
+
return this.__conjuring;
|
|
18
|
+
}
|
|
13
19
|
|
|
14
|
-
|
|
15
|
-
|
|
20
|
+
private static isMagicReady(): boolean {
|
|
21
|
+
return this.__reservedProperties.has(this);
|
|
22
|
+
}
|
|
16
23
|
|
|
17
|
-
|
|
24
|
+
private static prepareMagic(): void {
|
|
25
|
+
this.__conjuring = true;
|
|
18
26
|
this.__reservedProperties.set(this, new Set(Object.getOwnPropertyNames(new this)));
|
|
19
|
-
this.
|
|
20
|
-
|
|
21
|
-
return false;
|
|
27
|
+
this.__conjuring = false;
|
|
22
28
|
}
|
|
23
29
|
|
|
24
30
|
protected static isReservedProperty(property: string): boolean {
|
|
25
31
|
return !!this.__reservedProperties.get(this)?.has(property);
|
|
26
32
|
}
|
|
27
33
|
|
|
28
|
-
declare protected
|
|
29
|
-
declare protected _proxy: this;
|
|
34
|
+
declare protected _proxy: MagicObjectProxy<this>;
|
|
30
35
|
|
|
31
|
-
constructor() {
|
|
32
|
-
if (this.static().
|
|
36
|
+
constructor(...args: ClosureArgs) {
|
|
37
|
+
if (this.static().isConjuring()) {
|
|
33
38
|
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!this.static().isMagicReady()) {
|
|
42
|
+
this.static().prepareMagic();
|
|
43
|
+
}
|
|
34
44
|
|
|
35
|
-
this.
|
|
45
|
+
this._proxy = this.createProxy();
|
|
46
|
+
|
|
47
|
+
this.initialize(...args);
|
|
48
|
+
|
|
49
|
+
return this._proxy.instance;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
protected reserveProperty(property: string): void {
|
|
53
|
+
Object.assign(this, { [property]: null });
|
|
54
|
+
}
|
|
36
55
|
|
|
37
|
-
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
57
|
+
protected initialize(...args: ClosureArgs): void {
|
|
58
|
+
//
|
|
38
59
|
}
|
|
39
60
|
|
|
40
61
|
protected static(): MagicObjectConstructor<this> {
|
|
@@ -42,48 +63,50 @@ export default class MagicObject {
|
|
|
42
63
|
}
|
|
43
64
|
|
|
44
65
|
protected __get(property: string): unknown {
|
|
45
|
-
return Reflect.get(this.
|
|
66
|
+
return Reflect.get(this._proxy.target, property);
|
|
46
67
|
}
|
|
47
68
|
|
|
48
69
|
protected __set(property: string, value: unknown): void {
|
|
49
|
-
Reflect.set(this.
|
|
70
|
+
Reflect.set(this._proxy.target, property, value);
|
|
50
71
|
}
|
|
51
72
|
|
|
52
73
|
protected __delete(property: string): void {
|
|
53
|
-
Reflect.deleteProperty(this.
|
|
74
|
+
Reflect.deleteProperty(this._proxy.target, property);
|
|
54
75
|
}
|
|
55
76
|
|
|
56
|
-
protected
|
|
77
|
+
protected createProxy(): MagicObjectProxy<this> {
|
|
57
78
|
const Static = this.static();
|
|
58
79
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
80
|
+
return {
|
|
81
|
+
target: this,
|
|
82
|
+
instance: new Proxy(this, {
|
|
83
|
+
get(target, property, receiver) {
|
|
84
|
+
if (typeof property !== 'string' || property in target || Static.isReservedProperty(property)) {
|
|
85
|
+
return Reflect.get(target, property, receiver);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return target.__get(property);
|
|
89
|
+
},
|
|
90
|
+
set(target, property, value, receiver) {
|
|
91
|
+
if (typeof property !== 'string' || property in target || Static.isReservedProperty(property)) {
|
|
92
|
+
return Reflect.set(target, property, value, receiver);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
target.__set(property, value);
|
|
96
|
+
|
|
97
|
+
return true;
|
|
98
|
+
},
|
|
99
|
+
deleteProperty(target, property) {
|
|
100
|
+
if (typeof property !== 'string' || property in target || Static.isReservedProperty(property)) {
|
|
101
|
+
return Reflect.deleteProperty(target, property);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
target.__delete(property);
|
|
105
|
+
|
|
106
|
+
return true;
|
|
107
|
+
},
|
|
108
|
+
}),
|
|
109
|
+
};
|
|
87
110
|
}
|
|
88
111
|
|
|
89
112
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Closure, Falsy } from '@/types/
|
|
1
|
+
import type { Closure, Falsy } from '@/types/helpers';
|
|
2
2
|
|
|
3
3
|
import { compare } from './logical_helpers';
|
|
4
4
|
import { isIterable, isString } from './object_helpers';
|
|
@@ -47,7 +47,7 @@ export function arrayEquals<T>(original: T[], updated: T[]): boolean {
|
|
|
47
47
|
return !original.some((value, index) => updated[index] !== value);
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
export function arrayFilter<T>(items: T[]):
|
|
50
|
+
export function arrayFilter<T>(items: (T | Falsy)[]): T[];
|
|
51
51
|
export function arrayFilter<T>(items: T[], filter: (item: T) => boolean): T[];
|
|
52
52
|
export function arrayFilter<T>(items: T[], filter?: (item: T) => boolean): T[] {
|
|
53
53
|
return filter
|
|
@@ -154,10 +154,32 @@ export function arraySorted<T>(
|
|
|
154
154
|
? compareOrFieldOrDirection as 'asc' | 'desc'
|
|
155
155
|
: direction;
|
|
156
156
|
|
|
157
|
+
const fieldDefaults: Partial<Record<keyof T, unknown>> = {};
|
|
158
|
+
const getDefaultValue = (sample: unknown): unknown => {
|
|
159
|
+
switch (typeof sample) {
|
|
160
|
+
case 'string':
|
|
161
|
+
return '';
|
|
162
|
+
case 'number':
|
|
163
|
+
return Number.MIN_SAFE_INTEGER;
|
|
164
|
+
case 'boolean':
|
|
165
|
+
return false;
|
|
166
|
+
default:
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
const getFieldValue = (object: T, field: keyof T): unknown => {
|
|
171
|
+
if (!(field in fieldDefaults)) {
|
|
172
|
+
const sampleValue = items.find(item => item[field] !== undefined && item[field] !== null)?.[field];
|
|
173
|
+
|
|
174
|
+
fieldDefaults[field] = getDefaultValue(sampleValue);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return object[field] ?? fieldDefaults[field];
|
|
178
|
+
};
|
|
157
179
|
const getComparisonFunction = (): Closure<[T, T], number> | undefined => {
|
|
158
180
|
const compareItems = direction === 'desc'
|
|
159
|
-
? (field: keyof T) => (a: T, b: T) => compare(b
|
|
160
|
-
: (field: keyof T) => (a: T, b: T) => compare(a
|
|
181
|
+
? (field: keyof T) => (a: T, b: T) => compare(getFieldValue(b, field), getFieldValue(a, field))
|
|
182
|
+
: (field: keyof T) => (a: T, b: T) => compare(getFieldValue(a, field), getFieldValue(b, field));
|
|
161
183
|
|
|
162
184
|
switch (typeof compareOrFieldOrDirection) {
|
|
163
185
|
case 'function':
|
|
@@ -1,8 +1,23 @@
|
|
|
1
|
+
import PromisedValue from '@/helpers/PromisedValue';
|
|
1
2
|
import type { Closure, ClosureArgs } from '@/types/index';
|
|
2
3
|
|
|
4
|
+
const asyncCache: Record<string, PromisedValue> = {};
|
|
5
|
+
|
|
6
|
+
export function asyncMemo<T>(key: string, operation: () => Promise<T>): Promise<T> {
|
|
7
|
+
asyncCache[key] = asyncCache[key] ?? PromisedValue.from<unknown>(operation());
|
|
8
|
+
|
|
9
|
+
return asyncCache[key] as Promise<T>;
|
|
10
|
+
}
|
|
11
|
+
|
|
3
12
|
export function once<P extends ClosureArgs, R>(operation: Closure<P, R>): Closure<P, R> {
|
|
4
13
|
const memo: { result?: R } = {};
|
|
5
14
|
|
|
6
15
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
16
|
return (...params) => 'result' in memo ? memo.result : (memo.result = operation(...params)) as any;
|
|
8
17
|
}
|
|
18
|
+
|
|
19
|
+
export function setAsyncMemo(key: string, value: unknown): void {
|
|
20
|
+
asyncCache[key] = asyncCache[key] ?? new PromisedValue();
|
|
21
|
+
|
|
22
|
+
asyncCache[key]?.resolve(value);
|
|
23
|
+
}
|
package/src/helpers/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { default as JSError, RuntimeErrorClass, JSErrorOptions } from './JSError';
|
|
2
|
-
export { default as MagicObject, MagicObjectConstructor } from './MagicObject';
|
|
2
|
+
export { default as MagicObject, MagicObjectConstructor, MagicObjectProxy } from './MagicObject';
|
|
3
3
|
export { default as ObjectsMap, ObjectKeyExtractor } from './ObjectsMap';
|
|
4
4
|
export { default as Semaphore } from './Semaphore';
|
|
5
5
|
export { default as Storage, StorageSingleton } from './Storage';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Constructor, KeyOf } from '@/types/index';
|
|
1
|
+
import type { Closure, ClosureArgs, Constructor, GetObjectMethods, KeyOf, VoidClosure } from '@/types/index';
|
|
2
2
|
import type { Equals } from '@/testing/index';
|
|
3
3
|
|
|
4
4
|
export type Obj = Record<string, unknown>;
|
|
@@ -51,6 +51,22 @@ export function deepEquals(a: unknown, b: unknown): boolean {
|
|
|
51
51
|
return !Object.keys(a).some(key => !deepEquals(a[key], b[key]));
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
export function getClassMethods(target: object): string[] {
|
|
55
|
+
const properties = new Set<string>();
|
|
56
|
+
const classDefinition = target.constructor === Object.constructor ? target : target.constructor;
|
|
57
|
+
|
|
58
|
+
let prototype = (classDefinition as { prototype: Constructor }).prototype;
|
|
59
|
+
while (prototype.constructor !== Object) {
|
|
60
|
+
Object.getOwnPropertyNames(prototype).forEach(property => properties.add(property));
|
|
61
|
+
|
|
62
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
properties.delete('constructor');
|
|
66
|
+
|
|
67
|
+
return Array.from(properties);
|
|
68
|
+
}
|
|
69
|
+
|
|
54
70
|
export function invert(map: Record<string, string>): Record<string, string> {
|
|
55
71
|
return Object.entries(map).reduce((invertedMap, [key, value]) => {
|
|
56
72
|
invertedMap[value] = key;
|
|
@@ -98,6 +114,16 @@ export function isNullable(value: unknown): value is undefined | null {
|
|
|
98
114
|
return typeof value === 'undefined' || value === null;
|
|
99
115
|
}
|
|
100
116
|
|
|
117
|
+
export function monkeyPatch<T, K extends GetObjectMethods<T>>(object: T, method: K, callback: VoidClosure<T[K]>): void {
|
|
118
|
+
const originalMethod = object[method] as unknown as Closure;
|
|
119
|
+
|
|
120
|
+
object[method] = ((...args: ClosureArgs) => {
|
|
121
|
+
(callback as unknown as Closure)(...args);
|
|
122
|
+
|
|
123
|
+
return originalMethod.call(object, ...args);
|
|
124
|
+
}) as unknown as T[K];
|
|
125
|
+
}
|
|
126
|
+
|
|
101
127
|
export function objectDeepClone<T extends Obj>(object: T): T {
|
|
102
128
|
object = { ...object };
|
|
103
129
|
|
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
import type { Closure, ClosureArgs } from '@/types/index';
|
|
2
2
|
|
|
3
|
+
export async function asyncFirst<TValue, TResult>(
|
|
4
|
+
values: TValue[],
|
|
5
|
+
operation: (value: TValue) => Promise<TResult | null>,
|
|
6
|
+
): Promise<TResult | null> {
|
|
7
|
+
for (const value of values) {
|
|
8
|
+
const result = await operation(value);
|
|
9
|
+
|
|
10
|
+
if (!result) {
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
3
20
|
export function isPromise(p: unknown): p is Promise<unknown> {
|
|
4
21
|
return 'then' in (p as Promise<unknown>);
|
|
5
22
|
}
|
|
@@ -64,6 +64,12 @@ export function stringToStudlyCase(text: string): string {
|
|
|
64
64
|
return words.map(word => word.slice(0, 1).toUpperCase() + word.slice(1).toLowerCase()).join('');
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
export function stringToTitleCase(text: string): string {
|
|
68
|
+
const words = stringSplitWords(text);
|
|
69
|
+
|
|
70
|
+
return words.map(word => word.slice(0, 1).toUpperCase() + word.slice(1).toLowerCase()).join(' ');
|
|
71
|
+
}
|
|
72
|
+
|
|
67
73
|
export function stringTrimmed(text: string): string {
|
|
68
74
|
return text.trim();
|
|
69
75
|
}
|
package/src/testing/index.ts
CHANGED
package/src/testing/mocking.ts
CHANGED
|
@@ -1,29 +1,32 @@
|
|
|
1
|
+
import { getClassMethods } from '@/helpers/object_helpers';
|
|
2
|
+
import { testing } from '@/testing/namespace';
|
|
1
3
|
import type { Obj } from '@/helpers/object_helpers';
|
|
2
4
|
|
|
3
5
|
export type Mock<T> = T;
|
|
4
6
|
|
|
5
|
-
export function mock<T>(instance?: T | Obj | object): Mock<T>;
|
|
6
|
-
export function mock<T>(methods: string[], instance?: T | Obj | object): Mock<T>;
|
|
7
|
-
export function mock<T>(
|
|
7
|
+
export function mock<T extends object>(instance?: T | Obj | object): Mock<T>;
|
|
8
|
+
export function mock<T extends object>(methods: string[], instance?: T | Obj | object): Mock<T>;
|
|
9
|
+
export function mock<T extends object>(
|
|
8
10
|
methodsOrInstance: string[] | T | Obj | object = [],
|
|
9
11
|
defaultInstance: T | Obj | object = {},
|
|
10
12
|
): Mock<T> {
|
|
11
13
|
const instance = (Array.isArray(methodsOrInstance) ? defaultInstance : methodsOrInstance) as T;
|
|
12
14
|
const methods = (Array.isArray(methodsOrInstance) ? methodsOrInstance : []) as Array<keyof T>;
|
|
13
|
-
const properties =
|
|
15
|
+
const properties = getClassMethods(instance);
|
|
14
16
|
|
|
15
17
|
for (const property of properties) {
|
|
16
|
-
const value = instance[property];
|
|
18
|
+
const value = instance[property as keyof T];
|
|
17
19
|
|
|
18
20
|
if (typeof value !== 'function') {
|
|
19
21
|
continue;
|
|
20
22
|
}
|
|
21
23
|
|
|
22
|
-
instance[property
|
|
24
|
+
instance[property as keyof T] =
|
|
25
|
+
testing().fn((...args) => value.call(instance, ...args)) as unknown as T[keyof T];
|
|
23
26
|
}
|
|
24
27
|
|
|
25
28
|
for (const method of methods) {
|
|
26
|
-
instance[method] =
|
|
29
|
+
instance[method] = testing().fn() as unknown as T[keyof T];
|
|
27
30
|
}
|
|
28
31
|
|
|
29
32
|
return instance;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Closure } from '@/types/index';
|
|
2
|
+
|
|
3
|
+
let testingNamespace: TestingNamespace | undefined;
|
|
4
|
+
|
|
5
|
+
export interface TestingNamespace {
|
|
6
|
+
fn<T extends Closure>(method?: T): T;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function setTestingNamespace(namespace: TestingNamespace): void {
|
|
10
|
+
testingNamespace = namespace;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function testing(): TestingNamespace {
|
|
14
|
+
return testingNamespace ?? jest;
|
|
15
|
+
}
|
package/src/types/helpers.ts
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
export type ClassInstance<Class> = Class extends { new(...args: any[]): infer T } ? T : never;
|
|
2
2
|
export type Closure<Args extends ClosureArgs = ClosureArgs, Result = unknown> = (...args: Args) => Result;
|
|
3
|
-
export type ClosureResult = any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
4
3
|
export type ClosureArgs = any[]; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
4
|
+
export type ClosureResult = any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
5
5
|
export type Falsy = null | undefined | false | 0;
|
|
6
|
-
export type GetOptionalKeys<T> = { [K in keyof T]-?: Record<string, never> extends Pick<T, K> ? K : never }[keyof T];
|
|
7
|
-
export type GetRequiredKeys<T> = { [K in keyof T]-?: T extends Record<K, T[K]> ? K : never }[keyof T];
|
|
8
6
|
export type GetClosureArgs<T extends Closure> = T extends (...args: infer Args) => ClosureResult ? Args : never;
|
|
9
7
|
export type GetClosureResult<T extends Closure> = T extends (...args: any[]) => infer Result ? Result : never;
|
|
10
|
-
export type
|
|
8
|
+
export type GetObjectMethods<T> = {
|
|
9
|
+
[K in keyof T]: T[K] extends (...args: ClosureArgs) => ClosureResult ? K : never;
|
|
10
|
+
}[keyof T];
|
|
11
|
+
export type GetOptionalKeys<T> = { [K in keyof T]-?: Record<string, never> extends Pick<T, K> ? K : never }[keyof T];
|
|
12
|
+
export type GetRequiredKeys<T> = { [K in keyof T]-?: T extends Record<K, T[K]> ? K : never }[keyof T];
|
|
11
13
|
export type KeyOf<Object, Values> = { [k in keyof Object]: Object[k] extends Values ? k : never; }[keyof Object];
|
|
14
|
+
export type ObjectValues<T> = T[keyof T];
|
|
15
|
+
export type OnlyKnownProperties<TGeneric, TKnown> = {
|
|
16
|
+
[ K in keyof TGeneric]: K extends keyof TKnown ? TGeneric[K] : never
|
|
17
|
+
};
|
|
18
|
+
export type Pretty<T> = T extends infer U ? { [K in keyof U]: U[K] } : never;
|
|
19
|
+
export type VoidClosure<T> = T extends (...args: infer A) => ClosureResult ? (...args: A) => void : never;
|
|
12
20
|
|
|
13
21
|
// Workaround for https://github.com/typescript-eslint/typescript-eslint/issues/3573
|
|
14
22
|
export type Use<T> = {}; // eslint-disable-line @typescript-eslint/no-unused-vars
|