@noeldemartin/utils 0.5.1 → 0.6.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 +2 -2
- package/CHANGELOG.md +50 -0
- package/dist/noeldemartin-utils.cjs.js +1 -1
- package/dist/noeldemartin-utils.d.ts +156 -20
- package/dist/noeldemartin-utils.esm.js +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/FluentArray.ts +5 -1
- package/src/fluent/facade.ts +62 -46
- package/src/fluent/index.ts +1 -0
- package/src/fluent/required.ts +46 -0
- package/src/helpers/ObjectsMap.ts +17 -1
- package/src/helpers/Semaphore.ts +17 -6
- package/src/helpers/array_helpers.ts +41 -7
- package/src/helpers/caching_helpers.ts +24 -0
- package/src/helpers/deprecated.ts +46 -0
- package/src/helpers/escape_helpers.ts +1 -1
- package/src/helpers/index.ts +2 -0
- package/src/helpers/location_helpers.ts +26 -1
- package/src/helpers/logical_helpers.ts +15 -1
- package/src/helpers/math_helpers.ts +3 -0
- package/src/helpers/object_helpers.ts +70 -32
- package/src/testing/FakeResponse.ts +62 -0
- package/src/testing/FakeServer.ts +126 -0
- package/src/testing/index.ts +4 -0
- package/src/testing/mocking.ts +2 -2
- package/src/types/helpers.ts +3 -0
|
@@ -1,39 +1,57 @@
|
|
|
1
|
-
import type { Closure, ClosureArgs, Constructor, GetObjectMethods, KeyOf, VoidClosure } from '@/types/index';
|
|
2
1
|
import type { Equals } from '@/testing/index';
|
|
2
|
+
import type {
|
|
3
|
+
Closure,
|
|
4
|
+
ClosureArgs,
|
|
5
|
+
Constructor,
|
|
6
|
+
GetObjectMethods,
|
|
7
|
+
KeyOf,
|
|
8
|
+
Pretty,
|
|
9
|
+
TypeGuard,
|
|
10
|
+
VoidClosure,
|
|
11
|
+
} from '@/types/index';
|
|
3
12
|
|
|
4
13
|
export type Obj = Record<string, unknown>;
|
|
5
14
|
export type ObjectEntry<T extends Obj, K extends keyof T> = [K, T[K]];
|
|
6
15
|
|
|
7
|
-
export type
|
|
8
|
-
export type
|
|
9
|
-
export type
|
|
10
|
-
|
|
11
|
-
|
|
16
|
+
export type ValueWithout<TValue, TExclude> = TValue extends TExclude ? never : TValue;
|
|
17
|
+
export type ReplaceValues<TObj, TExclude> = { [K in keyof TObj]: ValueWithout<TObj[K], TExclude> };
|
|
18
|
+
export type GetRequiredKeysWithout<
|
|
19
|
+
TObj,
|
|
20
|
+
TExclude,
|
|
21
|
+
U extends Record<keyof TObj, unknown> = ReplaceValues<TObj, TExclude>
|
|
22
|
+
> = {
|
|
23
|
+
[K in keyof TObj]:
|
|
24
|
+
Record<string, never> extends Pick<TObj, K>
|
|
12
25
|
? never
|
|
13
26
|
: (
|
|
14
27
|
U[K] extends never
|
|
15
28
|
? never
|
|
16
|
-
: (Equals<
|
|
29
|
+
: (Equals<TObj[K], U[K]> extends true ? K : never)
|
|
17
30
|
)
|
|
18
|
-
}[keyof
|
|
19
|
-
export type
|
|
20
|
-
|
|
21
|
-
|
|
31
|
+
}[keyof TObj];
|
|
32
|
+
export type GetOptionalKeysWithout<
|
|
33
|
+
TObj,
|
|
34
|
+
TExclude,
|
|
35
|
+
U extends Record<keyof TObj, unknown> = ReplaceValues<TObj, TExclude>
|
|
36
|
+
> = {
|
|
37
|
+
[K in keyof TObj]:
|
|
38
|
+
Record<string, never> extends Pick<TObj, K>
|
|
22
39
|
? K
|
|
23
40
|
: (
|
|
24
41
|
U[K] extends never
|
|
25
42
|
? never
|
|
26
|
-
: (Equals<
|
|
43
|
+
: (Equals<TObj[K], U[K]> extends true ? never : K)
|
|
27
44
|
)
|
|
28
|
-
}[keyof
|
|
45
|
+
}[keyof TObj];
|
|
29
46
|
|
|
30
47
|
// Given an existing bug in TypeScript, it's not possible to define optional keys using type generics without having
|
|
31
48
|
// them defined as `| undefined` as well. Until that is fixed, this may cause some problems for keys that can have
|
|
32
49
|
// empty values but not always do.
|
|
33
50
|
// See https://github.com/microsoft/TypeScript/issues/13195
|
|
34
|
-
export type
|
|
35
|
-
{ [K in
|
|
36
|
-
{ [K in
|
|
51
|
+
export type ObjectWithout<TObj, TExclude> = Pretty<
|
|
52
|
+
{ [K in GetRequiredKeysWithout<TObj, TExclude>]: ValueWithout<TObj[K], TExclude> } &
|
|
53
|
+
{ [K in GetOptionalKeysWithout<TObj, TExclude>]?: ValueWithout<TObj[K], TExclude> }
|
|
54
|
+
>;
|
|
37
55
|
|
|
38
56
|
export function deepEquals(a: unknown, b: unknown): boolean {
|
|
39
57
|
if (a === b)
|
|
@@ -106,6 +124,10 @@ export function isObject(value: unknown): value is Obj {
|
|
|
106
124
|
return typeof value === 'object' && value !== null;
|
|
107
125
|
}
|
|
108
126
|
|
|
127
|
+
export function isPlainObject(value: object): boolean {
|
|
128
|
+
return Object.getPrototypeOf(value) === Object.prototype;
|
|
129
|
+
}
|
|
130
|
+
|
|
109
131
|
export function isString(value: unknown): value is string | String {
|
|
110
132
|
return typeof value === 'string' || value instanceof String;
|
|
111
133
|
}
|
|
@@ -181,9 +203,34 @@ export function objectPull<T extends Obj, K extends keyof T>(obj: T, key: K): T[
|
|
|
181
203
|
return value;
|
|
182
204
|
}
|
|
183
205
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
206
|
+
|
|
207
|
+
/* eslint-disable max-len */
|
|
208
|
+
export function objectWithout<TObj extends Obj, TKey extends keyof TObj>(obj: TObj, keys: TKey): Omit<TObj, TKey>;
|
|
209
|
+
export function objectWithout<TObj extends object, TKey extends keyof TObj>(obj: TObj, keys: TKey): Omit<TObj, TKey>;
|
|
210
|
+
export function objectWithout<TObj extends Obj, TKey extends keyof TObj>(obj: TObj, keys: TKey[]): Omit<TObj, TKey>;
|
|
211
|
+
export function objectWithout<TObj extends object, TKey extends keyof TObj>(obj: TObj, keys: TKey[]): Omit<TObj, TKey>;
|
|
212
|
+
export function objectWithout<TObj extends Obj, TExclude>(obj: TObj, exclude: TypeGuard<TExclude>): ObjectWithout<TObj, TExclude>;
|
|
213
|
+
export function objectWithout<TObj extends object, TExclude>(obj: TObj, exclude: TypeGuard<TExclude>): ObjectWithout<TObj, TExclude>;
|
|
214
|
+
/* eslint-enable max-len */
|
|
215
|
+
|
|
216
|
+
export function objectWithout(obj: Obj, keysOrExclude: string | string[] | TypeGuard): unknown {
|
|
217
|
+
if (typeof keysOrExclude === 'function') {
|
|
218
|
+
const exclude = keysOrExclude;
|
|
219
|
+
const cleanObject = {} as Record<string, unknown>;
|
|
220
|
+
|
|
221
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
222
|
+
if (exclude(value)) {
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
cleanObject[key] = value;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return cleanObject;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const newObject = { ...obj };
|
|
233
|
+
const keysArray = Array.isArray(keysOrExclude) ? keysOrExclude : [keysOrExclude];
|
|
187
234
|
|
|
188
235
|
for (const key of keysArray) {
|
|
189
236
|
delete newObject[key];
|
|
@@ -192,19 +239,10 @@ export function objectWithout<T extends Obj, K extends keyof T>(obj: T, keys: K
|
|
|
192
239
|
return newObject;
|
|
193
240
|
}
|
|
194
241
|
|
|
195
|
-
export function objectWithoutEmpty<T extends Obj>(obj: T):
|
|
196
|
-
export function objectWithoutEmpty<T extends object>(obj: T):
|
|
197
|
-
export function objectWithoutEmpty<T extends Obj>(obj: T):
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
201
|
-
if (value === null || value === undefined)
|
|
202
|
-
continue;
|
|
203
|
-
|
|
204
|
-
cleanObject[key] = value;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
return cleanObject as ObjectWithoutEmpty<T>;
|
|
242
|
+
export function objectWithoutEmpty<T extends Obj>(obj: T): ObjectWithout<T, null | undefined>;
|
|
243
|
+
export function objectWithoutEmpty<T extends object>(obj: T): ObjectWithout<T, null | undefined>;
|
|
244
|
+
export function objectWithoutEmpty<T extends Obj>(obj: T): ObjectWithout<T, null | undefined> {
|
|
245
|
+
return objectWithout(obj, (value): value is null | undefined => value === null || value === undefined);
|
|
208
246
|
}
|
|
209
247
|
|
|
210
248
|
export function toString(value: unknown): string {
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export default class FakeResponse implements Response {
|
|
2
|
+
|
|
3
|
+
public static created(body: string = '', headers: Record<string, string> = {}): FakeResponse {
|
|
4
|
+
return new FakeResponse(body, headers, 201);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
public static success(body: string = '', headers: Record<string, string> = {}): FakeResponse {
|
|
8
|
+
return new FakeResponse(body, headers);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
public static notFound(body: string = '', headers: Record<string, string> = {}): FakeResponse {
|
|
12
|
+
return new FakeResponse(body, headers, 404);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public readonly body!: ReadableStream<Uint8Array> | null;
|
|
16
|
+
public readonly bodyUsed!: boolean;
|
|
17
|
+
public readonly headers: Headers;
|
|
18
|
+
public readonly ok!: boolean;
|
|
19
|
+
public readonly redirected!: boolean;
|
|
20
|
+
public readonly status: number;
|
|
21
|
+
public readonly statusText!: string;
|
|
22
|
+
public readonly trailer!: Promise<Headers>;
|
|
23
|
+
public readonly type!: ResponseType;
|
|
24
|
+
public readonly url!: string;
|
|
25
|
+
|
|
26
|
+
private rawBody: string;
|
|
27
|
+
|
|
28
|
+
public constructor(rawBody: string = '', headers: Record<string, string> = {}, status: number = 200) {
|
|
29
|
+
this.rawBody = rawBody;
|
|
30
|
+
this.headers = new Headers(headers);
|
|
31
|
+
this.status = status;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public async arrayBuffer(): Promise<ArrayBuffer> {
|
|
35
|
+
throw new Error('FakeResponse.arrayBuffer() is not implemented');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public async blob(): Promise<Blob> {
|
|
39
|
+
throw new Error('FakeResponse.blob() is not implemented');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public async bytes(): Promise<Uint8Array> {
|
|
43
|
+
throw new Error('FakeResponse.bytes() is not implemented');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public async formData(): Promise<FormData> {
|
|
47
|
+
throw new Error('FakeResponse.formData() is not implemented');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public async json(): Promise<unknown> {
|
|
51
|
+
return JSON.parse(this.rawBody);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public async text(): Promise<string> {
|
|
55
|
+
return this.rawBody;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public clone(): Response {
|
|
59
|
+
return { ...this };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import FakeResponse from '@/testing/FakeResponse';
|
|
2
|
+
import { arrayRemove } from '@/helpers/array_helpers';
|
|
3
|
+
import { toString } from '@/helpers/object_helpers';
|
|
4
|
+
import type { GetClosureArgs, GetClosureResult } from '@/types/helpers';
|
|
5
|
+
|
|
6
|
+
export type FetchSpy = (request: FakeServerRequest) => unknown;
|
|
7
|
+
|
|
8
|
+
export interface FakeServerRequest {
|
|
9
|
+
url: string;
|
|
10
|
+
method: string;
|
|
11
|
+
body: string;
|
|
12
|
+
headers: Record<string, string>;
|
|
13
|
+
response?: Response;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface FakeServerResponse {
|
|
17
|
+
response: Response;
|
|
18
|
+
method?: string;
|
|
19
|
+
times?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default class FakeServer {
|
|
23
|
+
|
|
24
|
+
public readonly fetch: typeof fetch;
|
|
25
|
+
protected fetchSpy: FetchSpy | null = null;
|
|
26
|
+
protected requests: FakeServerRequest[] = [];
|
|
27
|
+
protected responses: Record<string, FakeServerResponse[]> = {};
|
|
28
|
+
|
|
29
|
+
constructor() {
|
|
30
|
+
const getRequest = (...[input, options]: GetClosureArgs<typeof fetch>): FakeServerRequest => {
|
|
31
|
+
if (typeof input === 'string') {
|
|
32
|
+
return {
|
|
33
|
+
url: input,
|
|
34
|
+
method: options?.method ?? 'GET',
|
|
35
|
+
headers: options?.headers as Record<string, string> ?? {},
|
|
36
|
+
body: toString(options?.body ?? ''),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if ('href' in input) {
|
|
41
|
+
return {
|
|
42
|
+
url: toString(input),
|
|
43
|
+
method: options?.method ?? 'GET',
|
|
44
|
+
headers: options?.headers as Record<string, string> ?? {},
|
|
45
|
+
body: toString(options?.body ?? ''),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
url: input.url,
|
|
51
|
+
method: input.method,
|
|
52
|
+
headers: input?.headers as unknown as Record<string, string> ?? {},
|
|
53
|
+
body: toString(input.body),
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
this.fetch = async (input, options) => {
|
|
58
|
+
const request = getRequest(input, options);
|
|
59
|
+
const response = await this.matchResponse(request);
|
|
60
|
+
|
|
61
|
+
request.response = response;
|
|
62
|
+
this.requests.push(request);
|
|
63
|
+
|
|
64
|
+
this.fetchSpy?.(request);
|
|
65
|
+
|
|
66
|
+
return response;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public spy(fetchSpy: FetchSpy): void {
|
|
71
|
+
this.fetchSpy = fetchSpy;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
public getRequest(url: string): FakeServerRequest | null;
|
|
75
|
+
public getRequest(method: string, url: string): FakeServerRequest | null;
|
|
76
|
+
public getRequest(methodOrUrl: string, url?: string): FakeServerRequest | null {
|
|
77
|
+
const method = typeof url === 'string' ? methodOrUrl : false;
|
|
78
|
+
url = url ?? methodOrUrl;
|
|
79
|
+
|
|
80
|
+
return this.requests.find(request => request.url === url && (!method || request.method === method)) ?? null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public getRequests(url?: string): FakeServerRequest[];
|
|
84
|
+
public getRequests(method: string, url: string): FakeServerRequest[];
|
|
85
|
+
public getRequests(methodOrUrl?: string, url?: string): FakeServerRequest[] {
|
|
86
|
+
if (!methodOrUrl) {
|
|
87
|
+
return this.requests;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const method = typeof url === 'string' ? methodOrUrl : false;
|
|
91
|
+
url = url ?? methodOrUrl;
|
|
92
|
+
|
|
93
|
+
return this.requests.filter(request => request.url === url && (!method || request.method === method));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
public respond(url: string, response: Response | FakeServerResponse): void {
|
|
97
|
+
this.responses[url] ??= [];
|
|
98
|
+
this.responses[url]?.push('response' in response ? response : { response });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public respondOnce(url: string, response: Response): void {
|
|
102
|
+
this.respond(url, { response, times: 1 });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
public reset(): void {
|
|
106
|
+
this.requests = [];
|
|
107
|
+
this.responses = {};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
protected async matchResponse(request: FakeServerRequest): GetClosureResult<typeof fetch> {
|
|
111
|
+
const responses = this.responses[request.url];
|
|
112
|
+
const response = responses?.find(response => !response.method || response.method === request.method);
|
|
113
|
+
|
|
114
|
+
if (!responses || !response) {
|
|
115
|
+
return FakeResponse.notFound();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (response.times) {
|
|
119
|
+
response.times--;
|
|
120
|
+
response.times === 0 && arrayRemove(responses, response);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return response.response;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
}
|
package/src/testing/index.ts
CHANGED
|
@@ -11,5 +11,9 @@ export type HasKey<T, K extends string> = K extends keyof T ? true : false;
|
|
|
11
11
|
// eslint-disable-next-line
|
|
12
12
|
export const tt: <T>() => () => any = () => () => expect(true).toBe(true);
|
|
13
13
|
|
|
14
|
+
export * from './FakeResponse';
|
|
15
|
+
export * from './FakeServer';
|
|
14
16
|
export * from './mocking';
|
|
15
17
|
export * from './namespace';
|
|
18
|
+
export { default as FakeResponse } from './FakeResponse';
|
|
19
|
+
export { default as FakeServer } from './FakeServer';
|
package/src/testing/mocking.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getClassMethods } from '@/helpers/object_helpers';
|
|
1
|
+
import { getClassMethods, isPlainObject } from '@/helpers/object_helpers';
|
|
2
2
|
import { testing } from '@/testing/namespace';
|
|
3
3
|
import type { Obj } from '@/helpers/object_helpers';
|
|
4
4
|
|
|
@@ -12,7 +12,7 @@ export function mock<T extends object>(
|
|
|
12
12
|
): Mock<T> {
|
|
13
13
|
const instance = (Array.isArray(methodsOrInstance) ? defaultInstance : methodsOrInstance) as T;
|
|
14
14
|
const methods = (Array.isArray(methodsOrInstance) ? methodsOrInstance : []) as Array<keyof T>;
|
|
15
|
-
const properties = getClassMethods(instance);
|
|
15
|
+
const properties = isPlainObject(instance) ? Object.getOwnPropertyNames(instance) : getClassMethods(instance);
|
|
16
16
|
|
|
17
17
|
for (const property of properties) {
|
|
18
18
|
const value = instance[property as keyof T];
|
package/src/types/helpers.ts
CHANGED
|
@@ -16,7 +16,10 @@ export type OnlyKnownProperties<TGeneric, TKnown> = {
|
|
|
16
16
|
[ K in keyof TGeneric]: K extends keyof TKnown ? TGeneric[K] : never
|
|
17
17
|
};
|
|
18
18
|
export type Pretty<T> = T extends infer U ? { [K in keyof U]: U[K] } : never;
|
|
19
|
+
export type SubPartial<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
20
|
+
export type TypeGuard<T=unknown> = (value: unknown) => value is T;
|
|
19
21
|
export type VoidClosure<T> = T extends (...args: infer A) => ClosureResult ? (...args: A) => void : never;
|
|
22
|
+
export type Writable<T> = { -readonly [K in keyof T]: T[K]; };
|
|
20
23
|
|
|
21
24
|
// Workaround for https://github.com/typescript-eslint/typescript-eslint/issues/3573
|
|
22
25
|
export type Use<T> = {}; // eslint-disable-line @typescript-eslint/no-unused-vars
|