@noeldemartin/utils 0.3.0 → 0.4.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/README.md +37 -1
- package/dist/noeldemartin-utils.cjs.js +1 -1
- package/dist/noeldemartin-utils.cjs.js.map +1 -1
- package/dist/noeldemartin-utils.d.ts +19 -3
- 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 -1
- package/src/fluent/facade.ts +26 -0
- package/src/fluent/index.ts +3 -0
- package/src/helpers/array_helpers.ts +3 -3
- package/src/helpers/identity_helpers.ts +9 -7
- package/src/helpers/object_helpers.ts +4 -0
- package/src/helpers/string_helpers.ts +7 -5
- package/src/testing/mocking.ts +16 -12
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noeldemartin/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "My JavaScript utilities",
|
|
5
5
|
"main": "dist/noeldemartin-utils.cjs.js",
|
|
6
6
|
"module": "dist/noeldemartin-utils.esm.js",
|
|
7
7
|
"browser": "dist/noeldemartin-utils.umd.js",
|
|
8
8
|
"types": "dist/noeldemartin-utils.d.ts",
|
|
9
|
+
"sideEffects": false,
|
|
9
10
|
"scripts": {
|
|
10
11
|
"build": "rm dist -rf && npm run build:js && npm run build:types",
|
|
11
12
|
"build:js": "noeldemartin-build-javascript",
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { fail } from '@/helpers/error_helpers';
|
|
2
|
+
|
|
3
|
+
export type FacadeMethods<T> = {
|
|
4
|
+
instance: T | null;
|
|
5
|
+
setInstance(instance: T): void;
|
|
6
|
+
requireInstance(): T;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type Facade<T> = T & FacadeMethods<T>;
|
|
10
|
+
|
|
11
|
+
export function facade<T extends object>(instance: T | null = null): Facade<T> {
|
|
12
|
+
const facade: FacadeMethods<T> = {
|
|
13
|
+
instance,
|
|
14
|
+
setInstance: instance => facade.instance = instance,
|
|
15
|
+
requireInstance: () => facade.instance ?? fail('Facade not initialized'),
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
return new Proxy(facade, {
|
|
19
|
+
get: (_, property, receiver) => {
|
|
20
|
+
const t = property !== 'constructor' && property in facade ? facade : facade.requireInstance();
|
|
21
|
+
|
|
22
|
+
return Reflect.get(t, property, receiver);
|
|
23
|
+
},
|
|
24
|
+
set: (_, property, receiver) => Reflect.set(facade.requireInstance(), property, receiver),
|
|
25
|
+
}) as unknown as Facade<T>;
|
|
26
|
+
}
|
package/src/fluent/index.ts
CHANGED
|
@@ -54,6 +54,9 @@ export function fluent(value: unknown): unknown {
|
|
|
54
54
|
return fluentClass ? (fluentClass as { create(v: unknown): unknown }).create(value) : null;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
export { facade } from './facade';
|
|
58
|
+
export type { Facade, FacadeMethods } from './facade';
|
|
59
|
+
|
|
57
60
|
export { tap } from './tap';
|
|
58
61
|
export type { Tapped } from './tap';
|
|
59
62
|
|
|
@@ -245,11 +245,11 @@ export function arrayZip<T>(...arrays: T[][]): T[][] {
|
|
|
245
245
|
}
|
|
246
246
|
|
|
247
247
|
export function arrayFrom<T>(value: Iterable<T>): T[];
|
|
248
|
-
export function arrayFrom<T>(value: T): T[];
|
|
249
|
-
export function arrayFrom(value: unknown): unknown[] {
|
|
248
|
+
export function arrayFrom<T>(value: T, ignoreEmptyValues?: boolean): T[];
|
|
249
|
+
export function arrayFrom(value: unknown, ignoreEmptyValues: boolean = false): unknown[] {
|
|
250
250
|
return Array.isArray(value) || (isIterable(value) && !isString(value))
|
|
251
251
|
? Array.from(value)
|
|
252
|
-
: [value];
|
|
252
|
+
: ignoreEmptyValues && (value === null || value === undefined) ? [] : [value];
|
|
253
253
|
}
|
|
254
254
|
|
|
255
255
|
export function range(length: number): number[] {
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
// Obtained from https://gist.github.com/jed/982883
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
function randomHexadecimalDigit(seed: string): string {
|
|
3
|
+
const s = seed as unknown as number;
|
|
4
|
+
|
|
5
|
+
return (s ^ Math.random() * 16 >> s / 4).toString(16);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function shortId(length: number = 9): string {
|
|
9
|
+
return (10 ** (length - 1)).toString().replace(/[01]/g, randomHexadecimalDigit);
|
|
8
10
|
}
|
|
9
11
|
|
|
10
12
|
export function uuid(): string {
|
|
11
|
-
return
|
|
13
|
+
return ('' + 1e7 + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, randomHexadecimalDigit);
|
|
12
14
|
}
|
|
@@ -78,6 +78,10 @@ export function isEmpty(value: unknown): boolean {
|
|
|
78
78
|
return false;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
+
export function isInstanceOf<T>(value: unknown, objectClass: Constructor<T>): value is T {
|
|
82
|
+
return value instanceof objectClass;
|
|
83
|
+
}
|
|
84
|
+
|
|
81
85
|
export function isIterable(value: unknown): value is Iterable<unknown> {
|
|
82
86
|
return typeof (value as { [Symbol.iterator]?: unknown })?.[Symbol.iterator] === 'function';
|
|
83
87
|
}
|
|
@@ -42,16 +42,18 @@ export function stringToCamelCase(text: string): string {
|
|
|
42
42
|
|
|
43
43
|
export function stringToSlug(text: string, separator: string = '-'): string {
|
|
44
44
|
text = text
|
|
45
|
-
.trim()
|
|
46
45
|
.normalize('NFD')
|
|
47
|
-
.replace(/
|
|
48
|
-
.replace(/[\u0300-\u036f]/g, '')
|
|
49
|
-
.replace(/[
|
|
46
|
+
.replace(/[֊־᐀‐‑‒–—―⸗⸚⸺⸻~〜〰゠︱︲﹘﹣-(){}><[\]]/g, ' ')
|
|
47
|
+
.replace(/[\u0300-\u036f]/g, '') // Remove diacritical marks
|
|
48
|
+
.replace(/[\u3000-\u303f]/g, '') // Remove punctuation
|
|
49
|
+
.replace(/['".,#!$%/\\^&*;:=`@+?]/g, '') // Remove special characters
|
|
50
50
|
.toLowerCase()
|
|
51
|
+
.trim()
|
|
51
52
|
.replace(/\s+/g, separator);
|
|
52
53
|
|
|
53
|
-
if (separator.length > 0)
|
|
54
|
+
if (separator.length > 0) {
|
|
54
55
|
text = text.replace(new RegExp(`${separator}+`, 'g'), separator);
|
|
56
|
+
}
|
|
55
57
|
|
|
56
58
|
return text;
|
|
57
59
|
}
|
package/src/testing/mocking.ts
CHANGED
|
@@ -1,26 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
methodsOrInstance: string[] | Record<string, unknown> = [],
|
|
5
|
-
instance: Record<string, unknown> = {},
|
|
6
|
-
): T {
|
|
7
|
-
instance = Array.isArray(methodsOrInstance) ? instance : methodsOrInstance;
|
|
1
|
+
import type { Obj } from '@/helpers/object_helpers';
|
|
2
|
+
|
|
3
|
+
export type Mock<T> = T;
|
|
8
4
|
|
|
9
|
-
|
|
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>(
|
|
8
|
+
methodsOrInstance: string[] | T | Obj | object = [],
|
|
9
|
+
defaultInstance: T | Obj | object = {},
|
|
10
|
+
): Mock<T> {
|
|
11
|
+
const instance = (Array.isArray(methodsOrInstance) ? defaultInstance : methodsOrInstance) as T;
|
|
12
|
+
const methods = (Array.isArray(methodsOrInstance) ? methodsOrInstance : []) as Array<keyof T>;
|
|
13
|
+
const properties = Object.getOwnPropertyNames(instance) as Array<keyof T>;
|
|
10
14
|
|
|
11
|
-
for (const property of
|
|
15
|
+
for (const property of properties) {
|
|
12
16
|
const value = instance[property];
|
|
13
17
|
|
|
14
18
|
if (typeof value !== 'function') {
|
|
15
19
|
continue;
|
|
16
20
|
}
|
|
17
21
|
|
|
18
|
-
instance[property] = jest.fn((...args) => value.call(instance, ...args));
|
|
22
|
+
instance[property] = jest.fn((...args) => value.call(instance, ...args)) as unknown as T[keyof T];
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
for (const method of methods) {
|
|
22
|
-
instance[method] = jest.fn();
|
|
26
|
+
instance[method] = jest.fn() as unknown as T[keyof T];
|
|
23
27
|
}
|
|
24
28
|
|
|
25
|
-
return instance
|
|
29
|
+
return instance;
|
|
26
30
|
}
|