@noeldemartin/utils 0.2.1 → 0.3.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/.nvmrc +1 -0
- package/README.md +25 -0
- package/dist/noeldemartin-utils.cjs.js +1 -1
- package/dist/noeldemartin-utils.cjs.js.map +1 -1
- package/dist/noeldemartin-utils.d.ts +173 -20
- 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 +11 -8
- package/src/fluent/FluentArray.ts +9 -2
- package/src/fluent/FluentObject.ts +2 -2
- package/src/fluent/index.ts +1 -0
- package/src/fluent/noop.ts +3 -0
- package/src/helpers/JSError.ts +25 -0
- package/src/helpers/MagicObject.ts +89 -0
- package/src/helpers/ObjectsMap.ts +18 -0
- package/src/helpers/PromisedValue.ts +51 -7
- package/src/helpers/array_helpers.ts +108 -34
- package/src/helpers/boolean_helpers.ts +11 -0
- package/src/helpers/chaos_helpers.ts +10 -0
- package/src/helpers/error_helpers.ts +16 -3
- package/src/helpers/escape_helpers.ts +7 -0
- package/src/helpers/file_helpers.ts +12 -0
- package/src/helpers/hash_helpers.ts +171 -0
- package/src/helpers/index.ts +16 -2
- package/src/helpers/location_helpers.ts +33 -0
- package/src/helpers/logical_helpers.ts +2 -3
- package/src/helpers/mixin_helpers.ts +7 -0
- package/src/helpers/network_helpers.ts +3 -0
- package/src/helpers/object_helpers.ts +18 -2
- package/src/helpers/promise_helpers.ts +1 -1
- package/src/helpers/runtime_helpers.ts +2 -1
- package/src/helpers/string_helpers.ts +34 -3
- package/src/helpers/url_helpers.ts +3 -1
- package/src/helpers/value_helpers.ts +7 -0
- package/src/testing/index.ts +5 -4
- package/src/testing/mocking.ts +26 -0
- package/src/types/arrays.ts +29 -0
- package/src/types/classes.ts +10 -0
- package/src/types/helpers.ts +14 -0
- package/src/types/index.ts +3 -14
- package/src/helpers/Error.ts +0 -17
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
export function md5(message: string): string {
|
|
2
|
+
return _md5(message);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* @see https://gist.github.com/PixnBits/8811212
|
|
7
|
+
* @see http://www.myersdaily.org/joseph/javascript/md5-text.html
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
11
|
+
|
|
12
|
+
function md5cycle(x: any, k: any) {
|
|
13
|
+
let a = x[0],
|
|
14
|
+
b = x[1],
|
|
15
|
+
c = x[2],
|
|
16
|
+
d = x[3];
|
|
17
|
+
|
|
18
|
+
a = ff(a, b, c, d, k[0], 7, -680876936);
|
|
19
|
+
d = ff(d, a, b, c, k[1], 12, -389564586);
|
|
20
|
+
c = ff(c, d, a, b, k[2], 17, 606105819);
|
|
21
|
+
b = ff(b, c, d, a, k[3], 22, -1044525330);
|
|
22
|
+
a = ff(a, b, c, d, k[4], 7, -176418897);
|
|
23
|
+
d = ff(d, a, b, c, k[5], 12, 1200080426);
|
|
24
|
+
c = ff(c, d, a, b, k[6], 17, -1473231341);
|
|
25
|
+
b = ff(b, c, d, a, k[7], 22, -45705983);
|
|
26
|
+
a = ff(a, b, c, d, k[8], 7, 1770035416);
|
|
27
|
+
d = ff(d, a, b, c, k[9], 12, -1958414417);
|
|
28
|
+
c = ff(c, d, a, b, k[10], 17, -42063);
|
|
29
|
+
b = ff(b, c, d, a, k[11], 22, -1990404162);
|
|
30
|
+
a = ff(a, b, c, d, k[12], 7, 1804603682);
|
|
31
|
+
d = ff(d, a, b, c, k[13], 12, -40341101);
|
|
32
|
+
c = ff(c, d, a, b, k[14], 17, -1502002290);
|
|
33
|
+
b = ff(b, c, d, a, k[15], 22, 1236535329);
|
|
34
|
+
|
|
35
|
+
a = gg(a, b, c, d, k[1], 5, -165796510);
|
|
36
|
+
d = gg(d, a, b, c, k[6], 9, -1069501632);
|
|
37
|
+
c = gg(c, d, a, b, k[11], 14, 643717713);
|
|
38
|
+
b = gg(b, c, d, a, k[0], 20, -373897302);
|
|
39
|
+
a = gg(a, b, c, d, k[5], 5, -701558691);
|
|
40
|
+
d = gg(d, a, b, c, k[10], 9, 38016083);
|
|
41
|
+
c = gg(c, d, a, b, k[15], 14, -660478335);
|
|
42
|
+
b = gg(b, c, d, a, k[4], 20, -405537848);
|
|
43
|
+
a = gg(a, b, c, d, k[9], 5, 568446438);
|
|
44
|
+
d = gg(d, a, b, c, k[14], 9, -1019803690);
|
|
45
|
+
c = gg(c, d, a, b, k[3], 14, -187363961);
|
|
46
|
+
b = gg(b, c, d, a, k[8], 20, 1163531501);
|
|
47
|
+
a = gg(a, b, c, d, k[13], 5, -1444681467);
|
|
48
|
+
d = gg(d, a, b, c, k[2], 9, -51403784);
|
|
49
|
+
c = gg(c, d, a, b, k[7], 14, 1735328473);
|
|
50
|
+
b = gg(b, c, d, a, k[12], 20, -1926607734);
|
|
51
|
+
|
|
52
|
+
a = hh(a, b, c, d, k[5], 4, -378558);
|
|
53
|
+
d = hh(d, a, b, c, k[8], 11, -2022574463);
|
|
54
|
+
c = hh(c, d, a, b, k[11], 16, 1839030562);
|
|
55
|
+
b = hh(b, c, d, a, k[14], 23, -35309556);
|
|
56
|
+
a = hh(a, b, c, d, k[1], 4, -1530992060);
|
|
57
|
+
d = hh(d, a, b, c, k[4], 11, 1272893353);
|
|
58
|
+
c = hh(c, d, a, b, k[7], 16, -155497632);
|
|
59
|
+
b = hh(b, c, d, a, k[10], 23, -1094730640);
|
|
60
|
+
a = hh(a, b, c, d, k[13], 4, 681279174);
|
|
61
|
+
d = hh(d, a, b, c, k[0], 11, -358537222);
|
|
62
|
+
c = hh(c, d, a, b, k[3], 16, -722521979);
|
|
63
|
+
b = hh(b, c, d, a, k[6], 23, 76029189);
|
|
64
|
+
a = hh(a, b, c, d, k[9], 4, -640364487);
|
|
65
|
+
d = hh(d, a, b, c, k[12], 11, -421815835);
|
|
66
|
+
c = hh(c, d, a, b, k[15], 16, 530742520);
|
|
67
|
+
b = hh(b, c, d, a, k[2], 23, -995338651);
|
|
68
|
+
|
|
69
|
+
a = ii(a, b, c, d, k[0], 6, -198630844);
|
|
70
|
+
d = ii(d, a, b, c, k[7], 10, 1126891415);
|
|
71
|
+
c = ii(c, d, a, b, k[14], 15, -1416354905);
|
|
72
|
+
b = ii(b, c, d, a, k[5], 21, -57434055);
|
|
73
|
+
a = ii(a, b, c, d, k[12], 6, 1700485571);
|
|
74
|
+
d = ii(d, a, b, c, k[3], 10, -1894986606);
|
|
75
|
+
c = ii(c, d, a, b, k[10], 15, -1051523);
|
|
76
|
+
b = ii(b, c, d, a, k[1], 21, -2054922799);
|
|
77
|
+
a = ii(a, b, c, d, k[8], 6, 1873313359);
|
|
78
|
+
d = ii(d, a, b, c, k[15], 10, -30611744);
|
|
79
|
+
c = ii(c, d, a, b, k[6], 15, -1560198380);
|
|
80
|
+
b = ii(b, c, d, a, k[13], 21, 1309151649);
|
|
81
|
+
a = ii(a, b, c, d, k[4], 6, -145523070);
|
|
82
|
+
d = ii(d, a, b, c, k[11], 10, -1120210379);
|
|
83
|
+
c = ii(c, d, a, b, k[2], 15, 718787259);
|
|
84
|
+
b = ii(b, c, d, a, k[9], 21, -343485551);
|
|
85
|
+
|
|
86
|
+
x[0] = add32(a, x[0]);
|
|
87
|
+
x[1] = add32(b, x[1]);
|
|
88
|
+
x[2] = add32(c, x[2]);
|
|
89
|
+
x[3] = add32(d, x[3]);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function cmn(q: any, a: any, b: any, x: any, s: any, t: any) {
|
|
93
|
+
a = add32(add32(a, q), add32(x, t));
|
|
94
|
+
|
|
95
|
+
return add32((a << s) | (a >>> (32 - s)), b);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function ff(a: any, b: any, c: any, d: any, x: any, s: any, t: any) {
|
|
99
|
+
return cmn((b & c) | ((~b) & d), a, b, x, s, t);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function gg(a: any, b: any, c: any, d: any, x: any, s: any, t: any) {
|
|
103
|
+
return cmn((b & d) | (c & (~d)), a, b, x, s, t);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function hh(a: any, b: any, c: any, d: any, x: any, s: any, t: any) {
|
|
107
|
+
return cmn(b ^ c ^ d, a, b, x, s, t);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function ii(a: any, b: any, c: any, d: any, x: any, s: any, t: any) {
|
|
111
|
+
return cmn(c ^ (b | (~d)), a, b, x, s, t);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function md51(s: any) {
|
|
115
|
+
const n = s.length;
|
|
116
|
+
const state = [1732584193, -271733879, -1732584194, 271733878];
|
|
117
|
+
|
|
118
|
+
let i;
|
|
119
|
+
|
|
120
|
+
for (i = 64; i <= s.length; i += 64) {
|
|
121
|
+
md5cycle(state, md5blk(s.substring(i - 64, i)));
|
|
122
|
+
}
|
|
123
|
+
s = s.substring(i - 64);
|
|
124
|
+
const tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
|
125
|
+
for (i = 0; i < s.length; i++)
|
|
126
|
+
tail[i >> 2] |= s.charCodeAt(i) << ((i % 4) << 3);
|
|
127
|
+
tail[i >> 2] |= 0x80 << ((i % 4) << 3);
|
|
128
|
+
if (i > 55) {
|
|
129
|
+
md5cycle(state, tail);
|
|
130
|
+
for (i = 0; i < 16; i++) tail[i] = 0;
|
|
131
|
+
}
|
|
132
|
+
tail[14] = n * 8;
|
|
133
|
+
md5cycle(state, tail);
|
|
134
|
+
return state;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function md5blk(s: any) {
|
|
138
|
+
let i;
|
|
139
|
+
const md5blks = [];
|
|
140
|
+
|
|
141
|
+
for (i = 0; i < 64; i += 4) {
|
|
142
|
+
md5blks[i >> 2] = s.charCodeAt(i) + (s.charCodeAt(i + 1) << 8) +
|
|
143
|
+
(s.charCodeAt(i + 2) << 16) + (s.charCodeAt(i + 3) << 24);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return md5blks;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const hex_chr = '0123456789abcdef'.split('');
|
|
150
|
+
|
|
151
|
+
function rhex(n: any) {
|
|
152
|
+
let s = '',
|
|
153
|
+
j = 0;
|
|
154
|
+
for (; j < 4; j++)
|
|
155
|
+
s += hex_chr[(n >> (j * 8 + 4)) & 0x0F] as string + hex_chr[(n >> (j * 8)) & 0x0F];
|
|
156
|
+
return s;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function hex(x: any) {
|
|
160
|
+
for (let i = 0; i < x.length; i++)
|
|
161
|
+
x[i] = rhex(x[i]);
|
|
162
|
+
return x.join('');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function add32(a: any, b: any) {
|
|
166
|
+
return (a + b) & 0xFFFFFFFF;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function _md5(s: any) {
|
|
170
|
+
return hex(md51(s));
|
|
171
|
+
}
|
package/src/helpers/index.ts
CHANGED
|
@@ -1,18 +1,32 @@
|
|
|
1
|
-
export { default as
|
|
1
|
+
export { default as JSError, RuntimeErrorClass, JSErrorOptions } from './JSError';
|
|
2
|
+
export { default as MagicObject, MagicObjectConstructor } from './MagicObject';
|
|
2
3
|
export { default as ObjectsMap, ObjectKeyExtractor } from './ObjectsMap';
|
|
3
|
-
export { default as PromisedValue } from './PromisedValue';
|
|
4
4
|
export { default as Semaphore } from './Semaphore';
|
|
5
5
|
export { default as Storage, StorageSingleton } from './Storage';
|
|
6
|
+
export {
|
|
7
|
+
default as PromisedValue,
|
|
8
|
+
PromisedValueResolveListener,
|
|
9
|
+
PromisedValueRejectListener,
|
|
10
|
+
PromisedValueResetListener,
|
|
11
|
+
} from './PromisedValue';
|
|
6
12
|
export * from './array_helpers';
|
|
13
|
+
export * from './boolean_helpers';
|
|
7
14
|
export * from './caching_helpers';
|
|
15
|
+
export * from './chaos_helpers';
|
|
8
16
|
export * from './date_helpers';
|
|
9
17
|
export * from './error_helpers';
|
|
18
|
+
export * from './escape_helpers';
|
|
19
|
+
export * from './file_helpers';
|
|
20
|
+
export * from './hash_helpers';
|
|
10
21
|
export * from './identity_helpers';
|
|
22
|
+
export * from './location_helpers';
|
|
11
23
|
export * from './logical_helpers';
|
|
12
24
|
export * from './mixin_helpers';
|
|
25
|
+
export * from './network_helpers';
|
|
13
26
|
export * from './object_helpers';
|
|
14
27
|
export * from './promise_helpers';
|
|
15
28
|
export * from './runtime_helpers';
|
|
16
29
|
export * from './string_helpers';
|
|
17
30
|
export * from './time_helpers';
|
|
18
31
|
export * from './url_helpers';
|
|
32
|
+
export * from './value_helpers';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export function getLocationQueryParameter(parameter: string): string | undefined {
|
|
2
|
+
return getLocationQueryParameters()[parameter];
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function getLocationQueryParameters<T extends string = string>(): Partial<{ [key in T]: string }> {
|
|
6
|
+
const url = new URL(location.href);
|
|
7
|
+
const queryParameters = {} as Record<string, string>;
|
|
8
|
+
|
|
9
|
+
url.searchParams.forEach((value, key) => (queryParameters[key] = value));
|
|
10
|
+
|
|
11
|
+
return queryParameters as Partial<{ [key in T]: string }>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function hasLocationQueryParameter(parameter: string): boolean {
|
|
15
|
+
const url = new URL(location.href);
|
|
16
|
+
|
|
17
|
+
return url.searchParams.has(parameter);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function updateLocationQueryParameters(parameters: Record<string, string | undefined>): void {
|
|
21
|
+
const url = Object.entries(parameters).reduce(
|
|
22
|
+
(url, [parameter, value]) => {
|
|
23
|
+
value
|
|
24
|
+
? url.searchParams.set(parameter, value)
|
|
25
|
+
: url.searchParams.delete(parameter);
|
|
26
|
+
|
|
27
|
+
return url;
|
|
28
|
+
},
|
|
29
|
+
new URL(location.href),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
history.pushState(null, document.title, url.href);
|
|
33
|
+
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export function compare(a:
|
|
2
|
-
|
|
3
|
-
return (a as any) > (b as any) ? 1 : ((b as any) > (a as any) ? -1 : 0);
|
|
1
|
+
export function compare<T>(a: T, b: T): 1 | -1 | 0 {
|
|
2
|
+
return a > b ? 1 : (b > a ? -1 : 0);
|
|
4
3
|
}
|
|
@@ -7,6 +7,8 @@ const OBJECT_PROTOTYPE = Object.getPrototypeOf(Object);
|
|
|
7
7
|
function createMixedClass(baseClass: Constructor, mixinClasses: Constructor[]): Constructor {
|
|
8
8
|
class MixedClass extends baseClass {
|
|
9
9
|
|
|
10
|
+
public static __mixins = mixinClasses;
|
|
11
|
+
|
|
10
12
|
constructor(...args: any[]) {
|
|
11
13
|
super(...args);
|
|
12
14
|
|
|
@@ -80,3 +82,8 @@ export function mixedWithoutTypes<Base>(baseClass: Base, mixins: Constructor[]):
|
|
|
80
82
|
export function mixedWithoutTypes(...args: any[]): unknown {
|
|
81
83
|
return (mixed as Function)(...args);
|
|
82
84
|
}
|
|
85
|
+
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
|
|
87
|
+
export function usesMixin(target: any, mixin: Constructor): boolean {
|
|
88
|
+
return (target?.__mixins ?? target?.__proto__?.constructor?.__mixins ?? []).includes(mixin);
|
|
89
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Constructor, KeyOf } from '@/types/index';
|
|
1
2
|
import type { Equals } from '@/testing/index';
|
|
2
3
|
|
|
3
4
|
export type Obj = Record<string, unknown>;
|
|
@@ -60,6 +61,10 @@ export function invert(map: Record<string, string>): Record<string, string> {
|
|
|
60
61
|
|
|
61
62
|
export const isArray = Array.isArray.bind(Array);
|
|
62
63
|
|
|
64
|
+
export function isConstructor(value: object): value is Constructor {
|
|
65
|
+
return 'prototype' in value;
|
|
66
|
+
}
|
|
67
|
+
|
|
63
68
|
export function isEmpty(value: unknown): boolean {
|
|
64
69
|
if (value === null || value === undefined)
|
|
65
70
|
return true;
|
|
@@ -108,6 +113,14 @@ export function objectHasOwnProperty(object: Obj, property: string): boolean {
|
|
|
108
113
|
return Object.prototype.hasOwnProperty.call(object, property);
|
|
109
114
|
}
|
|
110
115
|
|
|
116
|
+
export function objectMap<T>(items: T[], property: KeyOf<T, number | string>): Record<string, T> {
|
|
117
|
+
return items.reduce((map, item) => {
|
|
118
|
+
map[item[property] as unknown as number | string] = item;
|
|
119
|
+
|
|
120
|
+
return map;
|
|
121
|
+
}, {} as Record<string, T>);
|
|
122
|
+
}
|
|
123
|
+
|
|
111
124
|
export function objectOnly<T extends Obj, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> {
|
|
112
125
|
const newObject: Pick<T, K> = {} as Pick<T, K>;
|
|
113
126
|
|
|
@@ -137,16 +150,19 @@ export function objectPull<T extends Obj, K extends keyof T>(obj: T, key: K): T[
|
|
|
137
150
|
return value;
|
|
138
151
|
}
|
|
139
152
|
|
|
140
|
-
export function objectWithout<T extends Obj, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> {
|
|
153
|
+
export function objectWithout<T extends Obj, K extends keyof T>(obj: T, keys: K | K[]): Omit<T, K> {
|
|
141
154
|
const newObject: T = { ...obj };
|
|
155
|
+
const keysArray = Array.isArray(keys) ? keys : [keys];
|
|
142
156
|
|
|
143
|
-
for (const key of
|
|
157
|
+
for (const key of keysArray) {
|
|
144
158
|
delete newObject[key];
|
|
145
159
|
}
|
|
146
160
|
|
|
147
161
|
return newObject;
|
|
148
162
|
}
|
|
149
163
|
|
|
164
|
+
export function objectWithoutEmpty<T extends Obj>(obj: T): ObjectWithoutEmpty<T>;
|
|
165
|
+
export function objectWithoutEmpty<T extends object>(obj: T): ObjectWithoutEmpty<T>;
|
|
150
166
|
export function objectWithoutEmpty<T extends Obj>(obj: T): ObjectWithoutEmpty<T> {
|
|
151
167
|
const cleanObject = {} as Record<string, unknown>;
|
|
152
168
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2
|
+
export function runtimeGlobal<T = any>(): T {
|
|
2
3
|
return typeof global !== 'undefined'
|
|
3
4
|
? global
|
|
4
5
|
: typeof globalThis !== 'undefined'
|
|
@@ -1,3 +1,30 @@
|
|
|
1
|
+
import type { ArrayItem, Tuple } from '@/types/index';
|
|
2
|
+
|
|
3
|
+
export function stringCapitalize(text: string): string {
|
|
4
|
+
return text[0]?.toUpperCase() + text.slice(1);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function stringExcerpt(text: string, maxLength: number = 300, filler: string = '...'): string {
|
|
8
|
+
if (text.length < maxLength)
|
|
9
|
+
return text;
|
|
10
|
+
|
|
11
|
+
return text.substring(0, maxLength - filler.length) + filler;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function stringMatch<Length extends number, Optional extends number = number>(
|
|
15
|
+
text: string,
|
|
16
|
+
pattern: RegExp,
|
|
17
|
+
): Tuple<ArrayItem<RegExpMatchArray>, Length, Optional> | null {
|
|
18
|
+
return text.match(pattern) as Tuple<ArrayItem<RegExpMatchArray>, Length> | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function stringMatchAll<Length extends number, Optional extends number = number>(
|
|
22
|
+
text: string,
|
|
23
|
+
pattern: RegExp,
|
|
24
|
+
): IterableIterator<Tuple<ArrayItem<RegExpMatchArray>, Length, Optional>> {
|
|
25
|
+
return text.matchAll(pattern) as IterableIterator<Tuple<ArrayItem<RegExpMatchArray>, Length>>;
|
|
26
|
+
}
|
|
27
|
+
|
|
1
28
|
export function stringReverse(text: string): string {
|
|
2
29
|
return text.split('').reverse().join('');
|
|
3
30
|
}
|
|
@@ -7,10 +34,10 @@ export function stringSplitWords(text: string): string[] {
|
|
|
7
34
|
}
|
|
8
35
|
|
|
9
36
|
export function stringToCamelCase(text: string): string {
|
|
10
|
-
const
|
|
37
|
+
const [firstWord, ...otherWords] = stringSplitWords(text);
|
|
11
38
|
|
|
12
|
-
return
|
|
13
|
-
+
|
|
39
|
+
return (firstWord ?? '').toLowerCase()
|
|
40
|
+
+ otherWords.map(word => word.slice(0, 1).toUpperCase() + word.slice(1).toLowerCase()).join('');
|
|
14
41
|
}
|
|
15
42
|
|
|
16
43
|
export function stringToSlug(text: string, separator: string = '-'): string {
|
|
@@ -34,3 +61,7 @@ export function stringToStudlyCase(text: string): string {
|
|
|
34
61
|
|
|
35
62
|
return words.map(word => word.slice(0, 1).toUpperCase() + word.slice(1).toLowerCase()).join('');
|
|
36
63
|
}
|
|
64
|
+
|
|
65
|
+
export function stringTrimmed(text: string): string {
|
|
66
|
+
return text.trim();
|
|
67
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { fail } from './error_helpers';
|
|
2
|
+
|
|
1
3
|
export interface UrlParts {
|
|
2
4
|
protocol?: string;
|
|
3
5
|
domain?: string;
|
|
@@ -27,7 +29,7 @@ export function urlResolve(...parts: string[]): string {
|
|
|
27
29
|
|
|
28
30
|
if (fragment.startsWith('/')) {
|
|
29
31
|
url = urlRoot(url) + fragment;
|
|
30
|
-
} else if (fragment.
|
|
32
|
+
} else if (fragment.match(/^[\w-]+:\/\//)) {
|
|
31
33
|
url = fragment;
|
|
32
34
|
} else if (url.endsWith('/')) {
|
|
33
35
|
url += fragment;
|
package/src/testing/index.ts
CHANGED
|
@@ -6,8 +6,9 @@ export type Extends<X, Y> = Y extends X ? true : false;
|
|
|
6
6
|
export type Equals<X, Y> =
|
|
7
7
|
(<T>() => T extends X ? 1 : 2) extends
|
|
8
8
|
(<T>() => T extends Y ? 1 : 2) ? true : false;
|
|
9
|
+
export type HasKey<T, K extends string> = K extends keyof T ? true : false;
|
|
9
10
|
|
|
10
|
-
// eslint-disable-next-line
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
// eslint-disable-next-line
|
|
12
|
+
export const tt: <T>() => () => any = () => () => expect(true).toBe(true);
|
|
13
|
+
|
|
14
|
+
export * from './mocking';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export function mock<T>(instance?: Record<string, unknown>): T;
|
|
2
|
+
export function mock<T>(methods: string[], instance?: Record<string, unknown>): T;
|
|
3
|
+
export function mock<T>(
|
|
4
|
+
methodsOrInstance: string[] | Record<string, unknown> = [],
|
|
5
|
+
instance: Record<string, unknown> = {},
|
|
6
|
+
): T {
|
|
7
|
+
instance = Array.isArray(methodsOrInstance) ? instance : methodsOrInstance;
|
|
8
|
+
|
|
9
|
+
const methods = Array.isArray(methodsOrInstance) ? methodsOrInstance : [];
|
|
10
|
+
|
|
11
|
+
for (const property of Object.getOwnPropertyNames(instance)) {
|
|
12
|
+
const value = instance[property];
|
|
13
|
+
|
|
14
|
+
if (typeof value !== 'function') {
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
instance[property] = jest.fn((...args) => value.call(instance, ...args));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
for (const method of methods) {
|
|
22
|
+
instance[method] = jest.fn();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return instance as T;
|
|
26
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Internal
|
|
2
|
+
export type CollapseTupleUnion<Tuple extends unknown[]> = _CollapseTupleUnionRecursion<Tuple, []>;
|
|
3
|
+
export type _CollapseTupleUnionRecursion<Tuple extends unknown[], Rest extends unknown[]> =
|
|
4
|
+
Rest['length'] extends Tuple['length']
|
|
5
|
+
? Rest
|
|
6
|
+
: _CollapseTupleUnionRecursion<Tuple, [Tuple[Rest['length']], ...Rest]>;
|
|
7
|
+
|
|
8
|
+
export type RequiredTuple<Item, Length extends number> =
|
|
9
|
+
Length extends Length
|
|
10
|
+
? number extends Length
|
|
11
|
+
? Item[]
|
|
12
|
+
: _RequiredTupleRecursion<Item, Length, []>
|
|
13
|
+
: never;
|
|
14
|
+
export type _RequiredTupleRecursion<Item, Length extends number, Rest extends unknown[]> =
|
|
15
|
+
Rest['length'] extends Length
|
|
16
|
+
? Rest
|
|
17
|
+
: _RequiredTupleRecursion<Item, Length, [Item, ...Rest]>;
|
|
18
|
+
|
|
19
|
+
export type _TupleRecursion<Tuple extends unknown[], Optional extends number, Rest extends unknown[]> =
|
|
20
|
+
Tuple['length'] extends Rest['length']
|
|
21
|
+
? Rest
|
|
22
|
+
: Optional extends Rest['length']
|
|
23
|
+
? _TupleRecursion<Tuple, Optional, [Tuple[Rest['length']] | undefined, ...Rest]>
|
|
24
|
+
: _TupleRecursion<Tuple, Optional, [Tuple[Rest['length']], ...Rest]>;
|
|
25
|
+
|
|
26
|
+
// External
|
|
27
|
+
export type ArrayItem<Array> = Array extends (infer Item)[] ? Item : never;
|
|
28
|
+
export type Tuple<Item, Length extends number, Optional extends number = number> =
|
|
29
|
+
CollapseTupleUnion<_TupleRecursion<RequiredTuple<Item, Length>, Optional, []>>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ClosureArgs } from './helpers';
|
|
2
|
+
|
|
3
|
+
export interface Constructor<T = Object> {
|
|
4
|
+
new (...args: any[]): T;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export type ReplaceConstructorArgs<T, Args extends ClosureArgs> =
|
|
8
|
+
T extends { new(...args: ClosureArgs): infer Instance }
|
|
9
|
+
? Omit<T, 'new'> & { new (...args: Args): Instance }
|
|
10
|
+
: never;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type ClassInstance<Class> = Class extends { new(...args: any[]): infer T } ? T : never;
|
|
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
|
+
export type ClosureArgs = any[]; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
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
|
+
export type GetClosureArgs<T extends Closure> = T extends (...args: infer Args) => ClosureResult ? Args : never;
|
|
9
|
+
export type GetClosureResult<T extends Closure> = T extends (...args: any[]) => infer Result ? Result : never;
|
|
10
|
+
export type Pretty<T> = T extends infer U ? { [K in keyof U]: U[K] } : never;
|
|
11
|
+
export type KeyOf<Object, Values> = { [k in keyof Object]: Object[k] extends Values ? k : never; }[keyof Object];
|
|
12
|
+
|
|
13
|
+
// Workaround for https://github.com/typescript-eslint/typescript-eslint/issues/3573
|
|
14
|
+
export type Use<T> = {}; // eslint-disable-line @typescript-eslint/no-unused-vars
|
package/src/types/index.ts
CHANGED
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export type Falsy = null | undefined | false | 0;
|
|
5
|
-
export type GetOptionalKeys<T> = { [K in keyof T]-?: Record<string, never> extends Pick<T, K> ? K : never }[keyof T];
|
|
6
|
-
export type GetRequiredKeys<T> = { [K in keyof T]-?: T extends Record<K, T[K]> ? K : never }[keyof T];
|
|
7
|
-
export type ReturnType<T extends (...args: any[]) => unknown> = T extends (...args: any[]) => infer R ? R : never;
|
|
8
|
-
|
|
9
|
-
// Workaround for https://github.com/typescript-eslint/typescript-eslint/issues/3573
|
|
10
|
-
export type Use<T> = {}; // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
11
|
-
|
|
12
|
-
export interface Constructor<T = Object> {
|
|
13
|
-
new (...args: any[]): T;
|
|
14
|
-
}
|
|
1
|
+
export * from './arrays';
|
|
2
|
+
export * from './classes';
|
|
3
|
+
export * from './helpers';
|
package/src/helpers/Error.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { runtimeGlobal } from '@/helpers/runtime_helpers';
|
|
2
|
-
|
|
3
|
-
const BaseError = runtimeGlobal().Error;
|
|
4
|
-
|
|
5
|
-
class Error extends BaseError {
|
|
6
|
-
|
|
7
|
-
constructor(message?: string) {
|
|
8
|
-
super(message);
|
|
9
|
-
|
|
10
|
-
// Fix inheritance: https://stackoverflow.com/questions/41102060/typescript-extending-error-class
|
|
11
|
-
this.name = new.target.name;
|
|
12
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export default Error as typeof BaseError;
|