@webdeveric/utils 0.19.0 → 0.21.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/cjs/type-assertion.js +57 -2
- package/dist/cjs/type-assertion.js.map +1 -1
- package/dist/cjs/type-predicate-factory.js +21 -0
- package/dist/cjs/type-predicate-factory.js.map +1 -0
- package/dist/cjs/type-predicate.js +23 -33
- package/dist/cjs/type-predicate.js.map +1 -1
- package/dist/mjs/type-assertion.js +48 -2
- package/dist/mjs/type-assertion.js.map +1 -1
- package/dist/mjs/type-predicate-factory.js +12 -0
- package/dist/mjs/type-predicate-factory.js.map +1 -0
- package/dist/mjs/type-predicate.js +21 -20
- package/dist/mjs/type-predicate.js.map +1 -1
- package/dist/types/type-assertion.d.ts +11 -0
- package/dist/types/type-assertion.d.ts.map +1 -1
- package/dist/types/type-predicate-factory.d.ts +8 -0
- package/dist/types/type-predicate-factory.d.ts.map +1 -0
- package/dist/types/type-predicate.d.ts +22 -14
- package/dist/types/type-predicate.d.ts.map +1 -1
- package/package.json +22 -21
- package/src/asArray.ts +0 -3
- package/src/capitalize.ts +0 -1
- package/src/clamp.ts +0 -7
- package/src/compose.ts +0 -13
- package/src/convert.ts +0 -30
- package/src/delay.ts +0 -23
- package/src/delayAnimationFrame.ts +0 -35
- package/src/getMaxValue.ts +0 -16
- package/src/getMinValue.ts +0 -16
- package/src/getOwnKeys.ts +0 -3
- package/src/getOwnProperties.ts +0 -3
- package/src/getType.ts +0 -3
- package/src/index.ts +0 -24
- package/src/isEmpty.ts +0 -21
- package/src/isObject.ts +0 -3
- package/src/joinStrings.ts +0 -6
- package/src/node/fcrdns.ts +0 -37
- package/src/node/index.ts +0 -1
- package/src/normalize.ts +0 -102
- package/src/secToString.ts +0 -45
- package/src/toPascalCase.ts +0 -29
- package/src/trimEnd.ts +0 -5
- package/src/trimIndentation.ts +0 -18
- package/src/trimStart.ts +0 -5
- package/src/type-assertion.ts +0 -319
- package/src/type-predicate.ts +0 -101
- package/src/types.ts +0 -48
- package/src/uniqueItems.ts +0 -3
- package/src/until.ts +0 -130
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import type { NumericValue, Primitive, StringRecord,
|
|
2
|
-
export declare const createIsEnumPredicate: <E extends Record<string, unknown>, T = E>(enumObject: E) => TypePredicateFn<T>;
|
|
3
|
-
export declare const maybeUndefined: <T>(predicate: TypePredicateFn<T>, input: unknown) => input is T | undefined;
|
|
1
|
+
import type { NumericValue, Primitive, StringRecord, UnknownRecord } from './types.js';
|
|
4
2
|
export declare const isString: (input: unknown) => input is string;
|
|
5
3
|
export declare const isNumber: (input: unknown) => input is number;
|
|
6
4
|
export declare const isBigInt: (input: unknown) => input is bigint;
|
|
@@ -9,27 +7,37 @@ export declare const isUndefined: (input: unknown) => input is undefined;
|
|
|
9
7
|
export declare const isSymbol: (input: unknown) => input is symbol;
|
|
10
8
|
export declare const isNull: (input: unknown) => input is null;
|
|
11
9
|
export declare const isPrimitive: (input: unknown) => input is Primitive;
|
|
12
|
-
export declare const isOptionalString: (
|
|
13
|
-
export declare const isOptionalNumber: (
|
|
14
|
-
export declare const isOptionalBigInt: (
|
|
15
|
-
export declare const isOptionalBoolean: (
|
|
16
|
-
export declare const isOptionalSymbol: (
|
|
17
|
-
export declare const isOptionalNull: (
|
|
10
|
+
export declare const isOptionalString: import("./types.js").TypePredicateFn<string | undefined>;
|
|
11
|
+
export declare const isOptionalNumber: import("./types.js").TypePredicateFn<number | undefined>;
|
|
12
|
+
export declare const isOptionalBigInt: import("./types.js").TypePredicateFn<bigint | undefined>;
|
|
13
|
+
export declare const isOptionalBoolean: import("./types.js").TypePredicateFn<boolean | undefined>;
|
|
14
|
+
export declare const isOptionalSymbol: import("./types.js").TypePredicateFn<symbol | undefined>;
|
|
15
|
+
export declare const isOptionalNull: import("./types.js").TypePredicateFn<null | undefined>;
|
|
16
|
+
export declare const isStringArray: import("./types.js").TypePredicateFn<string[]>;
|
|
17
|
+
export declare const isNumberArray: import("./types.js").TypePredicateFn<number[]>;
|
|
18
|
+
export declare const isBigIntArray: import("./types.js").TypePredicateFn<bigint[]>;
|
|
19
|
+
export declare const isBooleanArray: import("./types.js").TypePredicateFn<boolean[]>;
|
|
20
|
+
export declare const isUndefinedArray: import("./types.js").TypePredicateFn<undefined[]>;
|
|
21
|
+
export declare const isSymbolArray: import("./types.js").TypePredicateFn<symbol[]>;
|
|
22
|
+
export declare const isNullArray: import("./types.js").TypePredicateFn<null[]>;
|
|
18
23
|
export declare const isStringWithLength: (input: unknown) => input is string;
|
|
19
|
-
export declare const
|
|
20
|
-
export declare const isNumericString: (input: unknown) => input is `${number}`;
|
|
24
|
+
export declare const isNumericString: import("./types.js").TypePredicateFn<`${number}`>;
|
|
21
25
|
export declare const isNumericValue: (input: unknown) => input is NumericValue;
|
|
22
|
-
export declare const isNumericValueArray: (
|
|
23
|
-
export declare const isIntString: (
|
|
26
|
+
export declare const isNumericValueArray: import("./types.js").TypePredicateFn<NumericValue[]>;
|
|
27
|
+
export declare const isIntString: import("./types.js").TypePredicateFn<`${number}`>;
|
|
28
|
+
export declare const isDigitsString: import("./types.js").TypePredicateFn<`${number}`>;
|
|
24
29
|
export declare const isFiniteNumber: (input: unknown) => input is number;
|
|
25
30
|
export declare const isInteger: (input: unknown) => input is number;
|
|
26
31
|
export declare const isPositiveInteger: (input: unknown) => input is number;
|
|
27
32
|
export declare const isPositiveFiniteNumber: (input: unknown) => input is number;
|
|
28
33
|
export declare const isPropertyKey: (input: unknown) => input is PropertyKey;
|
|
29
|
-
export declare const isObject: <T extends
|
|
34
|
+
export declare const isObject: <T extends object = UnknownRecord>(input: unknown) => input is T;
|
|
30
35
|
export declare const isLengthAware: (input: unknown) => input is {
|
|
31
36
|
length: number;
|
|
32
37
|
};
|
|
38
|
+
export declare const isSizeAware: (input: unknown) => input is {
|
|
39
|
+
size: number;
|
|
40
|
+
};
|
|
33
41
|
export declare const isStringRecord: (input: unknown) => input is StringRecord;
|
|
34
42
|
export declare const isPromiseFulfilledResult: <T>(input: unknown) => input is PromiseFulfilledResult<T>;
|
|
35
43
|
export declare const isPromiseRejectedResult: (input: unknown) => input is PromiseRejectedResult;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type-predicate.d.ts","sourceRoot":"","sources":["../../src/type-predicate.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"type-predicate.d.ts","sourceRoot":"","sources":["../../src/type-predicate.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAiB,YAAY,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAItG,eAAO,MAAM,QAAQ,UAAW,OAAO,oBAA+C,CAAC;AAEvF,eAAO,MAAM,QAAQ,UAAW,OAAO,oBAAuE,CAAC;AAE/G,eAAO,MAAM,QAAQ,UAAW,OAAO,oBAA+C,CAAC;AAEvF,eAAO,MAAM,SAAS,UAAW,OAAO,qBAAiD,CAAC;AAE1F,eAAO,MAAM,WAAW,UAAW,OAAO,uBAAqD,CAAC;AAEhG,eAAO,MAAM,QAAQ,UAAW,OAAO,oBAA+C,CAAC;AAEvF,eAAO,MAAM,MAAM,UAAW,OAAO,kBAAkC,CAAC;AAExE,eAAO,MAAM,WAAW,UAAW,OAAO,uBAO3B,CAAC;AAIhB,eAAO,MAAM,gBAAgB,0DAA2B,CAAC;AAEzD,eAAO,MAAM,gBAAgB,0DAA2B,CAAC;AAEzD,eAAO,MAAM,gBAAgB,0DAA2B,CAAC;AAEzD,eAAO,MAAM,iBAAiB,2DAA4B,CAAC;AAE3D,eAAO,MAAM,gBAAgB,0DAA2B,CAAC;AAEzD,eAAO,MAAM,cAAc,wDAAyB,CAAC;AAIrD,eAAO,MAAM,aAAa,gDAAsB,CAAC;AAEjD,eAAO,MAAM,aAAa,gDAAsB,CAAC;AAEjD,eAAO,MAAM,aAAa,gDAAsB,CAAC;AAEjD,eAAO,MAAM,cAAc,iDAAuB,CAAC;AAEnD,eAAO,MAAM,gBAAgB,mDAAyB,CAAC;AAEvD,eAAO,MAAM,aAAa,gDAAsB,CAAC;AAEjD,eAAO,MAAM,WAAW,8CAAoB,CAAC;AAI7C,eAAO,MAAM,kBAAkB,UAAW,OAAO,oBAAmE,CAAC;AAErH,eAAO,MAAM,eAAe,mDAAqE,CAAC;AAElG,eAAO,MAAM,cAAc,UAAW,OAAO,0BACqC,CAAC;AAEnF,eAAO,MAAM,mBAAmB,sDAA4B,CAAC;AAE7D,eAAO,MAAM,WAAW,mDAA6D,CAAC;AAEtF,eAAO,MAAM,cAAc,mDAAwD,CAAC;AAEpF,eAAO,MAAM,cAAc,UAAW,OAAO,oBAA4C,CAAC;AAE1F,eAAO,MAAM,SAAS,UAAW,OAAO,oBAA6C,CAAC;AAEtF,eAAO,MAAM,iBAAiB,UAAW,OAAO,oBAAoD,CAAC;AAErG,eAAO,MAAM,sBAAsB,UAAW,OAAO,oBAAyD,CAAC;AAI/G,eAAO,MAAM,aAAa,UAAW,OAAO,yBACyC,CAAC;AAEtF,eAAO,MAAM,QAAQ,4CAA6C,OAAO,eACH,CAAC;AAEvE,eAAO,MAAM,aAAa,UAAW,OAAO;YAAsB,MAAM;CAC8B,CAAC;AAEvG,eAAO,MAAM,WAAW,UAAW,OAAO;UAAoB,MAAM;CAC8B,CAAC;AAEnG,eAAO,MAAM,cAAc,UAAW,OAAO,0BAE5C,CAAC;AAEF,eAAO,MAAM,wBAAwB,aAAc,OAAO,uCAEzD,CAAC;AAEF,eAAO,MAAM,uBAAuB,UAAW,OAAO,mCAErD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdeveric/utils",
|
|
3
3
|
"description": "Just another collection of utilities.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.21.0",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"keywords": [
|
|
10
10
|
"utilities",
|
|
11
11
|
"asArray",
|
|
12
|
+
"capitalize",
|
|
12
13
|
"clamp",
|
|
13
14
|
"compose",
|
|
14
15
|
"convert",
|
|
@@ -27,6 +28,7 @@
|
|
|
27
28
|
"secToString",
|
|
28
29
|
"toPascalCase",
|
|
29
30
|
"trimEnd",
|
|
31
|
+
"trimIndentation",
|
|
30
32
|
"trimStart",
|
|
31
33
|
"type-assertion",
|
|
32
34
|
"type-predicate",
|
|
@@ -58,8 +60,7 @@
|
|
|
58
60
|
"./package.json": "./package.json"
|
|
59
61
|
},
|
|
60
62
|
"files": [
|
|
61
|
-
"dist"
|
|
62
|
-
"src"
|
|
63
|
+
"dist"
|
|
63
64
|
],
|
|
64
65
|
"repository": {
|
|
65
66
|
"type": "git",
|
|
@@ -101,29 +102,29 @@
|
|
|
101
102
|
"lodash.escaperegexp": "^4.1.2"
|
|
102
103
|
},
|
|
103
104
|
"devDependencies": {
|
|
104
|
-
"@swc/core": "^1.
|
|
105
|
-
"@swc/jest": "^0.2.
|
|
106
|
-
"@types/jest": "^29.
|
|
107
|
-
"@types/lodash.clonedeep": "^4.5.
|
|
108
|
-
"@types/lodash.escaperegexp": "^4.1.
|
|
109
|
-
"@types/node": "^18.
|
|
105
|
+
"@swc/core": "^1.3.22",
|
|
106
|
+
"@swc/jest": "^0.2.23",
|
|
107
|
+
"@types/jest": "^29.2.4",
|
|
108
|
+
"@types/lodash.clonedeep": "^4.5.7",
|
|
109
|
+
"@types/lodash.escaperegexp": "^4.1.7",
|
|
110
|
+
"@types/node": "^18.11.12",
|
|
110
111
|
"@webdeveric/eslint-config-ts": "^0.5.2",
|
|
111
112
|
"@webdeveric/prettier-config": "^0.2.0",
|
|
112
|
-
"core-js": "^3.
|
|
113
|
-
"cspell": "^6.
|
|
114
|
-
"eslint": "^8.
|
|
113
|
+
"core-js": "^3.26.1",
|
|
114
|
+
"cspell": "^6.17.0",
|
|
115
|
+
"eslint": "^8.29.0",
|
|
115
116
|
"eslint-config-prettier": "^8.5.0",
|
|
116
|
-
"eslint-import-resolver-typescript": "^3.
|
|
117
|
-
"eslint-plugin-import": "^2.
|
|
118
|
-
"eslint-plugin-jest": "^27.
|
|
119
|
-
"eslint-plugin-prettier": "^4.
|
|
120
|
-
"husky": "^8.0.
|
|
121
|
-
"jest": "^29.
|
|
122
|
-
"jest-environment-jsdom": "^29.
|
|
117
|
+
"eslint-import-resolver-typescript": "^3.5.2",
|
|
118
|
+
"eslint-plugin-import": "^2.26.0",
|
|
119
|
+
"eslint-plugin-jest": "^27.1.6",
|
|
120
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
121
|
+
"husky": "^8.0.2",
|
|
122
|
+
"jest": "^29.3.1",
|
|
123
|
+
"jest-environment-jsdom": "^29.3.1",
|
|
123
124
|
"jest-ts-webcompat-resolver": "^1.0.0",
|
|
124
125
|
"lint-staged": "^13.1.0",
|
|
125
|
-
"prettier": "^2.
|
|
126
|
+
"prettier": "^2.8.1",
|
|
126
127
|
"rimraf": "^3.0.2",
|
|
127
|
-
"typescript": "^4.9.
|
|
128
|
+
"typescript": "^4.9.4"
|
|
128
129
|
}
|
|
129
130
|
}
|
package/src/asArray.ts
DELETED
package/src/capitalize.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const capitalize = (word: string): string => word.substring(0, 1).toUpperCase() + word.substring(1);
|
package/src/clamp.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { getMaxValue } from './getMaxValue.js';
|
|
2
|
-
import { getMinValue } from './getMinValue.js';
|
|
3
|
-
import type { NumericValue } from './types.js';
|
|
4
|
-
|
|
5
|
-
export function clamp(min: NumericValue, value: NumericValue, max: NumericValue): NumericValue {
|
|
6
|
-
return getMaxValue(min, getMinValue(value, max));
|
|
7
|
-
}
|
package/src/compose.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export type ComposeFn<T, A extends unknown[], R = T> = ((data: T) => R) | ((data: T, ...args: A) => R);
|
|
2
|
-
|
|
3
|
-
export function compose<T, A extends unknown[]>(...functions: ComposeFn<T, A, T | void>[]): ComposeFn<T, A> {
|
|
4
|
-
return functions.reduce<ComposeFn<T, A>>(
|
|
5
|
-
(prev, next) =>
|
|
6
|
-
(data: T, ...args: A): T => {
|
|
7
|
-
const updatedData = prev(data, ...args);
|
|
8
|
-
|
|
9
|
-
return (next(updatedData, ...args) ?? updatedData) as T;
|
|
10
|
-
},
|
|
11
|
-
(data: T) => data,
|
|
12
|
-
);
|
|
13
|
-
}
|
package/src/convert.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { getOwnProperties } from './getOwnProperties.js';
|
|
2
|
-
import { getType } from './getType.js';
|
|
3
|
-
|
|
4
|
-
import type { Builtin } from './types.js';
|
|
5
|
-
|
|
6
|
-
export type ConvertFn<SourceType, ReturnType> = (source: Readonly<SourceType>) => ReturnType;
|
|
7
|
-
|
|
8
|
-
export type ConvertRecord<Input, Output> = {
|
|
9
|
-
[Property in keyof Output]: Output[Property] extends Builtin
|
|
10
|
-
? ConvertFn<Input, Output[Property]>
|
|
11
|
-
: AnyConverter<Input, Output[Property]>;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export type AnyConverter<Input, Output> = ConvertRecord<Input, Output> | ConvertFn<Input, Output>;
|
|
15
|
-
|
|
16
|
-
export const convert = <Input, Output>(input: Readonly<Input>, converter: AnyConverter<Input, Output>): Output => {
|
|
17
|
-
if (typeof converter === 'function') {
|
|
18
|
-
return converter(input);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (typeof converter === 'object' && converter !== null) {
|
|
22
|
-
return getOwnProperties(converter).reduce<Partial<Output>>((data, key) => {
|
|
23
|
-
data[key] = convert(input, converter[key]);
|
|
24
|
-
|
|
25
|
-
return data;
|
|
26
|
-
}, {}) as Output;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
throw new TypeError(`${getType(converter)} is not a valid converter`);
|
|
30
|
-
};
|
package/src/delay.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { getType } from './getType.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Delay a specified number of milliseconds before resolving.
|
|
5
|
-
* The `ms` value can be between zero and the max delay value for `setTimeout`.
|
|
6
|
-
*
|
|
7
|
-
* {@link https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#maximum_delay_value}
|
|
8
|
-
*/
|
|
9
|
-
export function delay(milliseconds: number): Promise<undefined>;
|
|
10
|
-
|
|
11
|
-
export function delay<T>(milliseconds: number, value: T): Promise<T>;
|
|
12
|
-
|
|
13
|
-
export function delay<T>(milliseconds: number, value?: T): Promise<typeof value> {
|
|
14
|
-
return new Promise((resolve, reject) => {
|
|
15
|
-
if (typeof milliseconds !== 'number') {
|
|
16
|
-
reject(new TypeError(`milliseconds must be a number. ${getType(milliseconds)} was provided.`));
|
|
17
|
-
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
setTimeout(resolve, Math.max(0, Math.min(milliseconds, 2_147_483_647)), value);
|
|
22
|
-
});
|
|
23
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
export function delayAnimationFrame(): Promise<undefined>;
|
|
2
|
-
|
|
3
|
-
export function delayAnimationFrame<T>(value: T): Promise<T>;
|
|
4
|
-
|
|
5
|
-
export function delayAnimationFrame<T>(value?: T): Promise<typeof value> {
|
|
6
|
-
return new Promise(resolve => {
|
|
7
|
-
window.requestAnimationFrame(() => resolve(value));
|
|
8
|
-
});
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function delayAnimationFrames(frames: number): Promise<undefined>;
|
|
12
|
-
|
|
13
|
-
export function delayAnimationFrames<T>(frames: number, value: T): Promise<T>;
|
|
14
|
-
|
|
15
|
-
export function delayAnimationFrames<T>(frames: number, value?: T): Promise<typeof value> {
|
|
16
|
-
return new Promise((resolve, reject) => {
|
|
17
|
-
if (!Number.isInteger(frames) || frames < 1) {
|
|
18
|
-
reject(new TypeError('frames must be a positive integer'));
|
|
19
|
-
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const loop = (frame: number): void => {
|
|
24
|
-
if (frame === 0) {
|
|
25
|
-
resolve(value);
|
|
26
|
-
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
window.requestAnimationFrame(() => loop(frame - 1));
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
loop(frames);
|
|
34
|
-
});
|
|
35
|
-
}
|
package/src/getMaxValue.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { assertIsNumericValueArray } from './type-assertion.js';
|
|
2
|
-
import type { NumericValue } from './types.js';
|
|
3
|
-
|
|
4
|
-
export function getMaxValue(...values: [NumericValue, ...NumericValue[]]): NumericValue {
|
|
5
|
-
if (values.length === 0) {
|
|
6
|
-
throw new Error('Expected at least one numeric value');
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
assertIsNumericValueArray(values);
|
|
10
|
-
|
|
11
|
-
return values.reduce((high, current) => {
|
|
12
|
-
const value = typeof current === 'string' ? Number(current) : current;
|
|
13
|
-
|
|
14
|
-
return value > high ? current : high;
|
|
15
|
-
});
|
|
16
|
-
}
|
package/src/getMinValue.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { assertIsNumericValueArray } from './type-assertion.js';
|
|
2
|
-
import type { NumericValue } from './types.js';
|
|
3
|
-
|
|
4
|
-
export function getMinValue(...values: [NumericValue, ...NumericValue[]]): NumericValue {
|
|
5
|
-
if (values.length === 0) {
|
|
6
|
-
throw new Error('Expected at least one numeric value');
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
assertIsNumericValueArray(values);
|
|
10
|
-
|
|
11
|
-
return values.reduce((low, current) => {
|
|
12
|
-
const value = typeof current === 'string' ? Number(current) : current;
|
|
13
|
-
|
|
14
|
-
return value < low ? current : low;
|
|
15
|
-
});
|
|
16
|
-
}
|
package/src/getOwnKeys.ts
DELETED
package/src/getOwnProperties.ts
DELETED
package/src/getType.ts
DELETED
package/src/index.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export * from './asArray.js';
|
|
2
|
-
export * from './capitalize.js';
|
|
3
|
-
export * from './clamp.js';
|
|
4
|
-
export * from './compose.js';
|
|
5
|
-
export * from './convert.js';
|
|
6
|
-
export * from './delay.js';
|
|
7
|
-
export * from './delayAnimationFrame.js';
|
|
8
|
-
export * from './getMaxValue.js';
|
|
9
|
-
export * from './getMinValue.js';
|
|
10
|
-
export * from './getOwnKeys.js';
|
|
11
|
-
export * from './getOwnProperties.js';
|
|
12
|
-
export * from './getType.js';
|
|
13
|
-
export * from './isEmpty.js';
|
|
14
|
-
export * from './isObject.js';
|
|
15
|
-
export * from './joinStrings.js';
|
|
16
|
-
export * from './normalize.js';
|
|
17
|
-
export * from './secToString.js';
|
|
18
|
-
export * from './toPascalCase.js';
|
|
19
|
-
export * from './trimEnd.js';
|
|
20
|
-
export * from './trimIndentation.js';
|
|
21
|
-
export * from './trimStart.js';
|
|
22
|
-
export * from './types.js';
|
|
23
|
-
export * from './uniqueItems.js';
|
|
24
|
-
export * from './until.js';
|
package/src/isEmpty.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { isLengthAware } from './type-predicate.js';
|
|
2
|
-
|
|
3
|
-
export function isEmpty(input: unknown): boolean {
|
|
4
|
-
if (typeof input === 'undefined' || input === null || (typeof input === 'string' && !input.length)) {
|
|
5
|
-
return true;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
if (Array.isArray(input)) {
|
|
9
|
-
return input.length === 0;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if (input instanceof Set || input instanceof Map) {
|
|
13
|
-
return input.size === 0;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (typeof input === 'object') {
|
|
17
|
-
return isLengthAware(input) ? input.length === 0 : Object.keys(input).length === 0;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return false;
|
|
21
|
-
}
|
package/src/isObject.ts
DELETED
package/src/joinStrings.ts
DELETED
package/src/node/fcrdns.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { promises as dns } from 'node:dns';
|
|
2
|
-
import { isIP } from 'node:net';
|
|
3
|
-
|
|
4
|
-
export async function hostnameResolvesToIp(hostname: string, ip: string): Promise<boolean> {
|
|
5
|
-
const ipVersion = isIP(ip);
|
|
6
|
-
|
|
7
|
-
if (!ipVersion) {
|
|
8
|
-
throw new Error(`Invalid IP: ${ip}`);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const resolve = ipVersion === 4 ? dns.resolve4 : dns.resolve6;
|
|
12
|
-
|
|
13
|
-
const resolved = await resolve(hostname);
|
|
14
|
-
|
|
15
|
-
return resolved.some(addr => addr === ip);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Forward-confirmed reverse DNS
|
|
20
|
-
*
|
|
21
|
-
* @see https://en.wikipedia.org/wiki/Forward-confirmed_reverse_DNS
|
|
22
|
-
*/
|
|
23
|
-
export async function fcrdns(hostname: string, ip: string): Promise<boolean> {
|
|
24
|
-
const hostnames = await dns.reverse(ip);
|
|
25
|
-
|
|
26
|
-
if (!hostnames.length) {
|
|
27
|
-
throw new Error(`Unable to get hostnames for ${ip}`);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (!hostnames.every(name => name.endsWith(hostname))) {
|
|
31
|
-
throw new Error(`${ip} does not belong to ${hostname}`);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const results = await Promise.all(hostnames.map(name => hostnameResolvesToIp(name, ip)));
|
|
35
|
-
|
|
36
|
-
return results.some(val => val);
|
|
37
|
-
}
|
package/src/node/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './fcrdns.js';
|
package/src/normalize.ts
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import cloneDeep from 'lodash.clonedeep';
|
|
2
|
-
|
|
3
|
-
import { getOwnProperties } from './getOwnProperties.js';
|
|
4
|
-
import { isObject } from './isObject.js';
|
|
5
|
-
|
|
6
|
-
import type { AnyRecord, Builtin } from './types.js';
|
|
7
|
-
|
|
8
|
-
export type NormalizeContext<OwnerRecordType, ContextData extends AnyRecord> = {
|
|
9
|
-
original: Readonly<OwnerRecordType>;
|
|
10
|
-
current: OwnerRecordType;
|
|
11
|
-
data: ContextData;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export type NormalizerFn<PropertyType, OwnerRecordType = unknown, ContextData extends AnyRecord = AnyRecord> = (
|
|
15
|
-
input: PropertyType,
|
|
16
|
-
context: NormalizeContext<OwnerRecordType, ContextData>,
|
|
17
|
-
) => PropertyType;
|
|
18
|
-
|
|
19
|
-
export type NormalizersRecord<RecordType, OwnerRecordType, ContextData extends AnyRecord = AnyRecord> = {
|
|
20
|
-
readonly [Property in keyof RecordType]?: RecordType[Property] extends Builtin
|
|
21
|
-
? NormalizerFn<RecordType[Property], OwnerRecordType, ContextData>
|
|
22
|
-
: AnyNormalizer<RecordType[Property], OwnerRecordType, ContextData>;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export type AnyNormalizer<RecordType, OwnerRecordType, ContextData extends AnyRecord = AnyRecord> =
|
|
26
|
-
| NormalizerFn<RecordType, OwnerRecordType, ContextData>
|
|
27
|
-
| NormalizersRecord<RecordType, OwnerRecordType, ContextData>;
|
|
28
|
-
|
|
29
|
-
export type ContextInitializer<OwnerRecordType, ContextData extends AnyRecord = AnyRecord> = (
|
|
30
|
-
data: OwnerRecordType,
|
|
31
|
-
normalizers: AnyNormalizer<OwnerRecordType, OwnerRecordType, ContextData>,
|
|
32
|
-
) => ContextData;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* @example
|
|
36
|
-
*
|
|
37
|
-
* ```ts
|
|
38
|
-
* normalize({ demo: true }, { demo: () => false });
|
|
39
|
-
* ```
|
|
40
|
-
*
|
|
41
|
-
* ```ts
|
|
42
|
-
* normalize({ demo: true }, () => ({ demo: false }) });
|
|
43
|
-
* ```
|
|
44
|
-
*
|
|
45
|
-
* ```ts
|
|
46
|
-
* normalize(true, () => false );
|
|
47
|
-
* ```
|
|
48
|
-
*/
|
|
49
|
-
export function normalize<Data extends object, ContextData extends AnyRecord = AnyRecord>(
|
|
50
|
-
input: Readonly<Data>,
|
|
51
|
-
normalizers: AnyNormalizer<Data, Data, Partial<ContextData>>, // No initializer so data must be partial
|
|
52
|
-
): Data;
|
|
53
|
-
|
|
54
|
-
export function normalize<Data extends object, ContextData extends AnyRecord = AnyRecord>(
|
|
55
|
-
input: Readonly<Data>,
|
|
56
|
-
normalizers: AnyNormalizer<Data, Data, ContextData>,
|
|
57
|
-
initContextData: ContextInitializer<Data, ContextData>,
|
|
58
|
-
): Data;
|
|
59
|
-
|
|
60
|
-
export function normalize<Data extends object, ContextData extends AnyRecord = AnyRecord>(
|
|
61
|
-
input: Readonly<Data>,
|
|
62
|
-
normalizers: AnyNormalizer<Data, Data, ContextData>,
|
|
63
|
-
initContextData: ContextInitializer<Data, ContextData> = (): AnyRecord => ({}),
|
|
64
|
-
): Data {
|
|
65
|
-
const current = cloneDeep(input);
|
|
66
|
-
|
|
67
|
-
const context: NormalizeContext<Data, ContextData> = Object.seal({
|
|
68
|
-
original: input,
|
|
69
|
-
current,
|
|
70
|
-
data: initContextData(input, normalizers),
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
const walk = <CurrentRecord extends AnyRecord>(
|
|
74
|
-
currentRecord: CurrentRecord,
|
|
75
|
-
currentNormalizers?: AnyNormalizer<CurrentRecord, Data, ContextData>,
|
|
76
|
-
): CurrentRecord => {
|
|
77
|
-
if (typeof currentNormalizers === 'function') {
|
|
78
|
-
return currentNormalizers(currentRecord, context);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (typeof currentNormalizers === 'object' && currentNormalizers !== null) {
|
|
82
|
-
if (typeof currentRecord === 'object' && currentRecord !== null) {
|
|
83
|
-
return getOwnProperties(currentNormalizers).reduce((data, key) => {
|
|
84
|
-
const normalizers = currentNormalizers[key];
|
|
85
|
-
// If the value is undefined but there is a normalizer record, set value to empty object.
|
|
86
|
-
const value =
|
|
87
|
-
typeof data[key] === 'undefined' && isObject(normalizers)
|
|
88
|
-
? ({} as unknown as CurrentRecord[keyof CurrentRecord])
|
|
89
|
-
: data[key];
|
|
90
|
-
|
|
91
|
-
data[key] = walk(value, normalizers);
|
|
92
|
-
|
|
93
|
-
return data;
|
|
94
|
-
}, currentRecord);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return currentRecord;
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
return walk(current, normalizers);
|
|
102
|
-
}
|
package/src/secToString.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
export enum Seconds {
|
|
2
|
-
Second = 1,
|
|
3
|
-
Minute = 60 * Second,
|
|
4
|
-
Hour = 60 * Minute,
|
|
5
|
-
Day = 24 * Hour,
|
|
6
|
-
Week = 7 * Day,
|
|
7
|
-
Month = (365.25 / 12) * Day,
|
|
8
|
-
Year = 12 * Month,
|
|
9
|
-
Decade = 10 * Year,
|
|
10
|
-
Century = 10 * Decade,
|
|
11
|
-
Millennium = 10 * Century,
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
type SecondsTuple = [key: Lowercase<keyof typeof Seconds>, seconds: Seconds];
|
|
15
|
-
|
|
16
|
-
export function secToString(time: number, joinString = ' '): string {
|
|
17
|
-
const secTime: SecondsTuple[] = [
|
|
18
|
-
['millennium', Seconds.Millennium],
|
|
19
|
-
['century', Seconds.Century],
|
|
20
|
-
['decade', Seconds.Decade],
|
|
21
|
-
['year', Seconds.Year],
|
|
22
|
-
['month', Seconds.Month],
|
|
23
|
-
['week', Seconds.Week],
|
|
24
|
-
['day', Seconds.Day],
|
|
25
|
-
['hour', Seconds.Hour],
|
|
26
|
-
['minute', Seconds.Minute],
|
|
27
|
-
['second', Seconds.Second],
|
|
28
|
-
];
|
|
29
|
-
|
|
30
|
-
const parts = secTime.reduce<string[]>((output, [units, seconds]) => {
|
|
31
|
-
if (seconds <= time) {
|
|
32
|
-
const currentValue = Math.floor(time / seconds);
|
|
33
|
-
const suffix = currentValue === 1 ? units : `${units}s`;
|
|
34
|
-
|
|
35
|
-
time %= seconds;
|
|
36
|
-
|
|
37
|
-
output.push(`${currentValue} ${suffix}`);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return output;
|
|
41
|
-
}, []);
|
|
42
|
-
|
|
43
|
-
// cSpell:ignore centurys
|
|
44
|
-
return parts.join(joinString).replace('centurys', 'centuries');
|
|
45
|
-
}
|
package/src/toPascalCase.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { capitalize } from './capitalize.js';
|
|
2
|
-
import { getType } from './getType.js';
|
|
3
|
-
|
|
4
|
-
import type { KeyValueTuple, StringRecord } from './types.js';
|
|
5
|
-
|
|
6
|
-
export function toPascalCase(text: string, customWords?: StringRecord): string {
|
|
7
|
-
const words = String(text).match(/[A-Z][a-z']+|\d+|[a-z']+/g);
|
|
8
|
-
|
|
9
|
-
if (words === null) {
|
|
10
|
-
return '';
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
let fixedWords = words.map(word => capitalize(word.replace(/'/g, '')));
|
|
14
|
-
|
|
15
|
-
if (customWords && getType(customWords) === 'Object') {
|
|
16
|
-
const replacements = new Map(
|
|
17
|
-
Object.entries(customWords).map<KeyValueTuple<string, string>>(([key, value]) => [key.toLowerCase(), value]),
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
if (replacements.size) {
|
|
21
|
-
// This takes a single word and returns the first matching replacement, if any.
|
|
22
|
-
const replaceCustomWords = (word: string): string => replacements.get(word.toLowerCase()) ?? word;
|
|
23
|
-
|
|
24
|
-
fixedWords = fixedWords.map(replaceCustomWords);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return fixedWords.join('');
|
|
29
|
-
}
|
package/src/trimEnd.ts
DELETED
package/src/trimIndentation.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const isWhitespaceOnly = (input: string): boolean => /^\s*$/.test(input);
|
|
2
|
-
|
|
3
|
-
export function trimIndentation(input: string): string {
|
|
4
|
-
const lines = input.split('\n');
|
|
5
|
-
const firstNonWhitespaceLine = lines.find(line => !isWhitespaceOnly(line));
|
|
6
|
-
const whiteSpace = firstNonWhitespaceLine?.match(/^(?<whiteSpace>\s*)/)?.groups?.whiteSpace;
|
|
7
|
-
|
|
8
|
-
if (whiteSpace) {
|
|
9
|
-
const wsPattern = new RegExp(`^${whiteSpace}`);
|
|
10
|
-
|
|
11
|
-
return lines
|
|
12
|
-
.map(line => line.replace(wsPattern, ''))
|
|
13
|
-
.join('\n')
|
|
14
|
-
.trim();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return input;
|
|
18
|
-
}
|