@webdeveric/utils 0.67.0 → 0.68.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/get.js +19 -0
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/pathParts.js +17 -0
- package/dist/mjs/get.js +16 -0
- package/dist/mjs/index.js +2 -0
- package/dist/mjs/pathParts.js +14 -0
- package/dist/types/get.d.ts +4 -0
- package/dist/types/getRandomItem.d.ts +3 -2
- package/dist/types/index.d.ts +2 -0
- package/dist/types/pathParts.d.ts +1 -0
- package/dist/types/types/arrays.d.ts +4 -1
- package/dist/types/types/objects.d.ts +8 -5
- package/dist/types/types/tuples.d.ts +2 -0
- package/dist/types/types/utils.d.ts +3 -0
- package/package.json +8 -8
package/dist/cjs/get.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.get = get;
|
|
4
|
+
const pathParts_js_1 = require("./pathParts.js");
|
|
5
|
+
function get(input, path) {
|
|
6
|
+
if (path === '') {
|
|
7
|
+
return input;
|
|
8
|
+
}
|
|
9
|
+
let current = input;
|
|
10
|
+
for (const part of (0, pathParts_js_1.pathParts)(path)) {
|
|
11
|
+
if (typeof current === 'object' && current !== null && part in current) {
|
|
12
|
+
current = current[part];
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return current;
|
|
19
|
+
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -36,6 +36,7 @@ __exportStar(require("./delayAnimationFrame.js"), exports);
|
|
|
36
36
|
__exportStar(require("./delayAnimationFrames.js"), exports);
|
|
37
37
|
__exportStar(require("./describeInput.js"), exports);
|
|
38
38
|
__exportStar(require("./escapeRegExp.js"), exports);
|
|
39
|
+
__exportStar(require("./get.js"), exports);
|
|
39
40
|
__exportStar(require("./getDateString.js"), exports);
|
|
40
41
|
__exportStar(require("./getISODateString.js"), exports);
|
|
41
42
|
__exportStar(require("./getMaxValue.js"), exports);
|
|
@@ -57,6 +58,7 @@ __exportStar(require("./jsonParse.js"), exports);
|
|
|
57
58
|
__exportStar(require("./looksLikeURL.js"), exports);
|
|
58
59
|
__exportStar(require("./normalize.js"), exports);
|
|
59
60
|
__exportStar(require("./parseNumber.js"), exports);
|
|
61
|
+
__exportStar(require("./pathParts.js"), exports);
|
|
60
62
|
__exportStar(require("./prefix.js"), exports);
|
|
61
63
|
__exportStar(require("./randomInt.js"), exports);
|
|
62
64
|
__exportStar(require("./redactCredentialsInURL.js"), exports);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pathParts = pathParts;
|
|
4
|
+
function* pathParts(input) {
|
|
5
|
+
if (input === '') {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
if (typeof input !== 'string') {
|
|
9
|
+
yield input;
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const regexp = /\w+/g;
|
|
13
|
+
const iterator = input.matchAll(regexp);
|
|
14
|
+
for (const part of iterator) {
|
|
15
|
+
yield part[0];
|
|
16
|
+
}
|
|
17
|
+
}
|
package/dist/mjs/get.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { pathParts } from './pathParts.js';
|
|
2
|
+
export function get(input, path) {
|
|
3
|
+
if (path === '') {
|
|
4
|
+
return input;
|
|
5
|
+
}
|
|
6
|
+
let current = input;
|
|
7
|
+
for (const part of pathParts(path)) {
|
|
8
|
+
if (typeof current === 'object' && current !== null && part in current) {
|
|
9
|
+
current = current[part];
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return current;
|
|
16
|
+
}
|
package/dist/mjs/index.js
CHANGED
|
@@ -20,6 +20,7 @@ export * from './delayAnimationFrame.js';
|
|
|
20
20
|
export * from './delayAnimationFrames.js';
|
|
21
21
|
export * from './describeInput.js';
|
|
22
22
|
export * from './escapeRegExp.js';
|
|
23
|
+
export * from './get.js';
|
|
23
24
|
export * from './getDateString.js';
|
|
24
25
|
export * from './getISODateString.js';
|
|
25
26
|
export * from './getMaxValue.js';
|
|
@@ -41,6 +42,7 @@ export * from './jsonParse.js';
|
|
|
41
42
|
export * from './looksLikeURL.js';
|
|
42
43
|
export * from './normalize.js';
|
|
43
44
|
export * from './parseNumber.js';
|
|
45
|
+
export * from './pathParts.js';
|
|
44
46
|
export * from './prefix.js';
|
|
45
47
|
export * from './randomInt.js';
|
|
46
48
|
export * from './redactCredentialsInURL.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function* pathParts(input) {
|
|
2
|
+
if (input === '') {
|
|
3
|
+
return;
|
|
4
|
+
}
|
|
5
|
+
if (typeof input !== 'string') {
|
|
6
|
+
yield input;
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
const regexp = /\w+/g;
|
|
10
|
+
const iterator = input.matchAll(regexp);
|
|
11
|
+
for (const part of iterator) {
|
|
12
|
+
yield part[0];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Path, PathValue } from './types/objects.js';
|
|
2
|
+
export declare function get<Input extends object>(input: Input, path: ''): Input;
|
|
3
|
+
export declare function get<Input extends object, InputPath extends Path<Input>>(input: Input, path: InputPath): PathValue<Input, InputPath>;
|
|
4
|
+
export declare function get<Input extends object, InputPath extends string>(input: Input, path: InputPath): unknown;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function getRandomItem<
|
|
1
|
+
import type { NonEmptyArray } from './types/arrays.js';
|
|
2
|
+
export declare function getRandomItem<Type>(input: NonEmptyArray<Type>): Type;
|
|
3
|
+
export declare function getRandomItem<Type>(input: Type[]): Type | undefined;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ export * from './delayAnimationFrame.js';
|
|
|
20
20
|
export * from './delayAnimationFrames.js';
|
|
21
21
|
export * from './describeInput.js';
|
|
22
22
|
export * from './escapeRegExp.js';
|
|
23
|
+
export * from './get.js';
|
|
23
24
|
export * from './getDateString.js';
|
|
24
25
|
export * from './getISODateString.js';
|
|
25
26
|
export * from './getMaxValue.js';
|
|
@@ -41,6 +42,7 @@ export * from './jsonParse.js';
|
|
|
41
42
|
export * from './looksLikeURL.js';
|
|
42
43
|
export * from './normalize.js';
|
|
43
44
|
export * from './parseNumber.js';
|
|
45
|
+
export * from './pathParts.js';
|
|
44
46
|
export * from './prefix.js';
|
|
45
47
|
export * from './randomInt.js';
|
|
46
48
|
export * from './redactCredentialsInURL.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function pathParts(input: PropertyKey): Generator<PropertyKey>;
|
|
@@ -7,5 +7,8 @@ export type GetLength<A extends any[]> = A extends {
|
|
|
7
7
|
} ? L : never;
|
|
8
8
|
export type IfLength<A extends any[], L extends number, T, F> = GetLength<A> extends L ? T : F;
|
|
9
9
|
export type IfEmpty<A extends any[], T, F> = GetLength<A> extends 0 ? T : F;
|
|
10
|
-
export type IfArray<Type, T, F> = Type extends unknown[] ? T : F;
|
|
10
|
+
export type IfArray<Type, T, F> = Type extends readonly unknown[] ? (number extends Type['length'] ? T : F) : F;
|
|
11
|
+
export type IsArray<Type> = IfArray<Type, true, false>;
|
|
11
12
|
export type NonEmptyArray<Type> = [Type, ...Type[]];
|
|
13
|
+
export type IfArrayLike<Type, T, F> = Type extends readonly unknown[] ? T : F;
|
|
14
|
+
export type IsArrayLike<Type> = IfArrayLike<Type, true, false>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { IfArray, IfArrayLike } from './arrays.js';
|
|
1
2
|
import type { Primitive } from './common.js';
|
|
2
3
|
import type { CamelCase } from './strings.js';
|
|
3
4
|
import type { KeyValueTuple } from './tuples.js';
|
|
@@ -13,10 +14,12 @@ export type NonNullableProperties<Type> = {
|
|
|
13
14
|
[Key in keyof Type]: NonNullable<Type[Key]>;
|
|
14
15
|
};
|
|
15
16
|
export type MethodNames<Type, Key extends keyof Type = keyof Type> = Key extends PropertyKey ? Type[Key] extends Function ? Key : never : never;
|
|
16
|
-
export type ValidKeys<Type> =
|
|
17
|
-
export type PathDotNotation<Type, Key extends keyof Type = keyof Type> = Key extends ValidKeys<Type> ? Type[Key] extends Primitive ? Key : Key | `${Key}.${PathDotNotation<NonNullable<Type[Key]>>}` : never;
|
|
18
|
-
export type Path<Type> = PathDotNotation<NonNullableProperties<Type>, keyof Type> | ValidKeys<Type>;
|
|
19
|
-
export type
|
|
20
|
-
export type
|
|
17
|
+
export type ValidKeys<Type extends object> = object extends Type ? never : Exclude<keyof Type, IfArrayLike<Type, symbol | MethodNames<Type>, symbol>>;
|
|
18
|
+
export type PathDotNotation<Type extends object, Key extends keyof Type = keyof Type> = Key extends ValidKeys<Type> ? Type[Key] extends Primitive ? Key : Key | `${Key}.${PathDotNotation<NonNullable<Type[Key]>>}` : never;
|
|
19
|
+
export type Path<Type extends object> = PathDotNotation<NonNullableProperties<Type>, keyof Type> | ValidKeys<Type>;
|
|
20
|
+
export type MaybeOptional<Type, Optional extends boolean> = Optional extends true ? Type | undefined : Type;
|
|
21
|
+
export type GetValueForKey<Type extends object, Key extends PropertyKey, Optional extends boolean = CanBeUndefined<Type, true, false>> = Key extends keyof Type ? MaybeOptional<Type[Key], IfArray<Type, true, Optional>> : Key extends `${infer Index extends number}` ? Index extends keyof Type ? MaybeOptional<Type[Index], IfArray<Type, true, Optional>> : never : never;
|
|
22
|
+
export type PathValue<Type extends object, TargetPath extends Path<Type>, Optional extends boolean = CanBeUndefined<Type, true, false>> = TargetPath extends `${infer Key}.${infer Rest}` ? GetValueForKey<Type, Key, Optional> extends infer Value ? Rest extends Path<NonNullable<Value>> ? PathValue<NonNullable<Value>, Rest, CanBeUndefined<Value, true, false>> : never : never : GetValueForKey<Type, TargetPath, Optional>;
|
|
23
|
+
export type PathValues<Type extends object, TargetPaths extends Path<Type>> = {
|
|
21
24
|
[TargetPath in TargetPaths as CamelCase<`${TargetPath}`, '.'>]: PathValue<Type, TargetPath>;
|
|
22
25
|
};
|
|
@@ -4,3 +4,5 @@ export type RangeTupleDistributive<T> = T extends any ? [min: T, max: T] : never
|
|
|
4
4
|
export type NumberRangeTuple = RangeTuple<number>;
|
|
5
5
|
export type KeyValueTuple<K extends PropertyKey = PropertyKey, V = unknown> = [key: K, value: V];
|
|
6
6
|
export type TupleToArray<T extends any[]> = T[number][];
|
|
7
|
+
export type IfTuple<Type, T, F> = Type extends readonly [...infer _] ? (number extends Type['length'] ? F : T) : F;
|
|
8
|
+
export type IsTuple<Type> = IfTuple<Type, true, false>;
|
|
@@ -3,6 +3,7 @@ export type IfCommonKeys<Target, Source, T, F> = IfNever<keyof (Target | Source)
|
|
|
3
3
|
export type IfDefined<Type, T, F> = undefined extends Type ? F : T;
|
|
4
4
|
export type IfUnknown<Type, T, F> = unknown extends Type ? T : F;
|
|
5
5
|
export type IfPromise<Type, T, F> = Type extends Promise<any> ? T : F;
|
|
6
|
+
export type IfSame<Left, Right, T, F> = [Left] extends [Right] ? ([Right] extends [Left] ? T : F) : F;
|
|
6
7
|
export type NotPromise<Type> = Type extends Promise<any> ? never : Type;
|
|
7
8
|
export type CanBeUndefined<Type, T, F> = Type | undefined extends Type ? T : F;
|
|
8
9
|
export type ReturnTypeDefault<Func, Default = undefined> = Func extends (...args: any[]) => infer R ? R : Default;
|
|
@@ -48,3 +49,5 @@ export type Pretty<Type> = {
|
|
|
48
49
|
export type Simplify<Type> = Type extends string ? string : Type extends number ? number : Type extends boolean ? boolean : Type extends bigint ? bigint : Type extends symbol ? symbol : Type extends undefined ? undefined : Type extends null ? null : Type extends Function ? Type : Type extends object ? {
|
|
49
50
|
[Key in keyof Type]: Simplify<Type[Key]>;
|
|
50
51
|
} : Type;
|
|
52
|
+
export type IfHasOnlyNumericKeys<Type, T, F> = `${Exclude<keyof Type, symbol>}` extends `${number}` ? T : F;
|
|
53
|
+
export type HasOnlyNumericKeys<Type> = IfHasOnlyNumericKeys<Type, true, false>;
|
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.68.0",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"url": "https://github.com/webdeveric/utils/issues"
|
|
67
67
|
},
|
|
68
68
|
"homepage": "https://github.com/webdeveric/utils/#readme",
|
|
69
|
-
"packageManager": "pnpm@10.
|
|
69
|
+
"packageManager": "pnpm@10.13.1+sha512.37ebf1a5c7a30d5fabe0c5df44ee8da4c965ca0c5af3dbab28c3a1681b70a256218d05c81c9c0dcf767ef6b8551eb5b960042b9ed4300c59242336377e01cfad",
|
|
70
70
|
"scripts": {
|
|
71
71
|
"clean": "rimraf ./dist/",
|
|
72
72
|
"prebuild": "pnpm clean",
|
|
@@ -87,14 +87,14 @@
|
|
|
87
87
|
"devDependencies": {
|
|
88
88
|
"@commitlint/config-conventional": "^19.8.1",
|
|
89
89
|
"@commitlint/types": "^19.8.1",
|
|
90
|
-
"@types/node": "^22.16.
|
|
90
|
+
"@types/node": "^22.16.3",
|
|
91
91
|
"@vitest/coverage-v8": "^3.2.4",
|
|
92
92
|
"@webdeveric/eslint-config-ts": "^0.11.0",
|
|
93
93
|
"@webdeveric/prettier-config": "^0.3.0",
|
|
94
94
|
"commitlint": "^19.8.1",
|
|
95
|
-
"commitlint-plugin-cspell": "^0.
|
|
96
|
-
"conventional-changelog-conventionalcommits": "^9.
|
|
97
|
-
"cspell": "^9.1.
|
|
95
|
+
"commitlint-plugin-cspell": "^0.3.0",
|
|
96
|
+
"conventional-changelog-conventionalcommits": "^9.1.0",
|
|
97
|
+
"cspell": "^9.1.5",
|
|
98
98
|
"eslint": "^8.57.1",
|
|
99
99
|
"eslint-config-prettier": "^10.1.5",
|
|
100
100
|
"eslint-import-resolver-typescript": "^4.4.4",
|
|
@@ -104,9 +104,9 @@
|
|
|
104
104
|
"lint-staged": "^16.1.2",
|
|
105
105
|
"prettier": "^3.6.2",
|
|
106
106
|
"rimraf": "^6.0.1",
|
|
107
|
-
"semantic-release": "^24.2.
|
|
107
|
+
"semantic-release": "^24.2.7",
|
|
108
108
|
"typescript": "^5.8.3",
|
|
109
|
-
"validate-package-exports": "^0.
|
|
109
|
+
"validate-package-exports": "^0.12.0",
|
|
110
110
|
"vitest": "^3.2.4"
|
|
111
111
|
},
|
|
112
112
|
"pnpm": {
|