@webdeveric/utils 0.68.0 → 0.70.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.
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertPathExists = assertPathExists;
4
+ const get_js_1 = require("../get.js");
5
+ const has_js_1 = require("../has.js");
6
+ function assertPathExists(input, path, predicate) {
7
+ if (!(0, has_js_1.has)(input, path)) {
8
+ throw new Error(`object path (${path}) does not exist on input`);
9
+ }
10
+ if (predicate && !predicate((0, get_js_1.get)(input, path))) {
11
+ throw new Error(`object path (${path}) failed predicate check`);
12
+ }
13
+ }
@@ -59,4 +59,5 @@ __exportStar(require("./assertIsSymbol.js"), exports);
59
59
  __exportStar(require("./assertIsSymbolArray.js"), exports);
60
60
  __exportStar(require("./assertIsUndefined.js"), exports);
61
61
  __exportStar(require("./assertIsUndefinedArray.js"), exports);
62
+ __exportStar(require("./assertPathExists.js"), exports);
62
63
  __exportStar(require("./assertPredicate.js"), exports);
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.has = has;
4
+ const pathParts_js_1 = require("./pathParts.js");
5
+ const isAnyObject_js_1 = require("./predicate/isAnyObject.js");
6
+ function has(input, path) {
7
+ if (!(0, isAnyObject_js_1.isAnyObject)(input)) {
8
+ return false;
9
+ }
10
+ if (path === '') {
11
+ return false;
12
+ }
13
+ let current = input;
14
+ for (const part of (0, pathParts_js_1.pathParts)(path)) {
15
+ if (typeof current === 'object' && current !== null && part in current) {
16
+ current = current[part];
17
+ }
18
+ else {
19
+ return false;
20
+ }
21
+ }
22
+ return true;
23
+ }
package/dist/cjs/index.js CHANGED
@@ -49,6 +49,7 @@ __exportStar(require("./getPackageName.js"), exports);
49
49
  __exportStar(require("./getPaths.js"), exports);
50
50
  __exportStar(require("./getRandomItem.js"), exports);
51
51
  __exportStar(require("./getType.js"), exports);
52
+ __exportStar(require("./has.js"), exports);
52
53
  __exportStar(require("./hasAdditionalProperties.js"), exports);
53
54
  __exportStar(require("./inRange.js"), exports);
54
55
  __exportStar(require("./isEmpty.js"), exports);
@@ -64,6 +65,7 @@ __exportStar(require("./randomInt.js"), exports);
64
65
  __exportStar(require("./redactCredentialsInURL.js"), exports);
65
66
  __exportStar(require("./resultify.js"), exports);
66
67
  __exportStar(require("./secToString.js"), exports);
68
+ __exportStar(require("./set.js"), exports);
67
69
  __exportStar(require("./sort-factory.js"), exports);
68
70
  __exportStar(require("./sort.js"), exports);
69
71
  __exportStar(require("./stripWhitespace.js"), exports);
@@ -26,6 +26,7 @@ __exportStar(require("./isBigIntArray.js"), exports);
26
26
  __exportStar(require("./isBinaryNumberString.js"), exports);
27
27
  __exportStar(require("./isBoolean.js"), exports);
28
28
  __exportStar(require("./isBooleanArray.js"), exports);
29
+ __exportStar(require("./isDateInputString.js"), exports);
29
30
  __exportStar(require("./isDateString.js"), exports);
30
31
  __exportStar(require("./isDigitsString.js"), exports);
31
32
  __exportStar(require("./isFiniteNumber.js"), exports);
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isDateInputString = void 0;
4
+ const isDateInputString = (input) => typeof input === 'string' && !Number.isNaN(Date.parse(input));
5
+ exports.isDateInputString = isDateInputString;
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isDateString = void 0;
4
- const isDateString = (input) => typeof input === 'string' && !Number.isNaN(Date.parse(input));
5
- exports.isDateString = isDateString;
4
+ const isDateInputString_js_1 = require("./isDateInputString.js");
5
+ exports.isDateString = isDateInputString_js_1.isDateInputString;
@@ -2,5 +2,5 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isISODateString = exports.ISO_DATE_PATTERN = void 0;
4
4
  const matching_js_1 = require("./factory/matching.js");
5
- exports.ISO_DATE_PATTERN = /^(?<year>\d{4,})-(?<month>[01]\d)-(?<day>0[1-9]|[12]\d|3[01])T(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})\.(?<timezone>\d{3}Z)$/;
5
+ exports.ISO_DATE_PATTERN = /^(?<year>\d{4,})-(?<month>[01]\d)-(?<day>0[1-9]|[12]\d|3[01])T(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})\.(?<timezone>\d+Z)$/;
6
6
  exports.isISODateString = (0, matching_js_1.matching)(exports.ISO_DATE_PATTERN);
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.set = set;
4
+ const pathParts_js_1 = require("./pathParts.js");
5
+ function set(input, path, value) {
6
+ if (path === '') {
7
+ throw new Error('Path cannot be an empty string');
8
+ }
9
+ if (typeof path === 'string' && /\b(__proto__|constructor|prototype)\b/.test(path)) {
10
+ throw new Error('Cannot pollute prototype');
11
+ }
12
+ const parts = Array.from((0, pathParts_js_1.pathParts)(path));
13
+ const lastPart = parts.pop();
14
+ if (typeof lastPart === 'undefined') {
15
+ throw new Error('Path must have at least one part');
16
+ }
17
+ let current = input;
18
+ const useParts = [];
19
+ for (const part of parts) {
20
+ useParts.push(part);
21
+ if (typeof current === 'object' && current !== null && part in current) {
22
+ current = current[part];
23
+ }
24
+ else {
25
+ throw new Error(`Path "${useParts.join('.')}" does not exist in the input object`);
26
+ }
27
+ }
28
+ return (current[lastPart] = value);
29
+ }
@@ -0,0 +1,10 @@
1
+ import { get } from '../get.js';
2
+ import { has } from '../has.js';
3
+ export function assertPathExists(input, path, predicate) {
4
+ if (!has(input, path)) {
5
+ throw new Error(`object path (${path}) does not exist on input`);
6
+ }
7
+ if (predicate && !predicate(get(input, path))) {
8
+ throw new Error(`object path (${path}) failed predicate check`);
9
+ }
10
+ }
@@ -43,4 +43,5 @@ export * from './assertIsSymbol.js';
43
43
  export * from './assertIsSymbolArray.js';
44
44
  export * from './assertIsUndefined.js';
45
45
  export * from './assertIsUndefinedArray.js';
46
+ export * from './assertPathExists.js';
46
47
  export * from './assertPredicate.js';
@@ -0,0 +1,20 @@
1
+ import { pathParts } from './pathParts.js';
2
+ import { isAnyObject } from './predicate/isAnyObject.js';
3
+ export function has(input, path) {
4
+ if (!isAnyObject(input)) {
5
+ return false;
6
+ }
7
+ if (path === '') {
8
+ return false;
9
+ }
10
+ let current = input;
11
+ for (const part of pathParts(path)) {
12
+ if (typeof current === 'object' && current !== null && part in current) {
13
+ current = current[part];
14
+ }
15
+ else {
16
+ return false;
17
+ }
18
+ }
19
+ return true;
20
+ }
package/dist/mjs/index.js CHANGED
@@ -33,6 +33,7 @@ export * from './getPackageName.js';
33
33
  export * from './getPaths.js';
34
34
  export * from './getRandomItem.js';
35
35
  export * from './getType.js';
36
+ export * from './has.js';
36
37
  export * from './hasAdditionalProperties.js';
37
38
  export * from './inRange.js';
38
39
  export * from './isEmpty.js';
@@ -48,6 +49,7 @@ export * from './randomInt.js';
48
49
  export * from './redactCredentialsInURL.js';
49
50
  export * from './resultify.js';
50
51
  export * from './secToString.js';
52
+ export * from './set.js';
51
53
  export * from './sort-factory.js';
52
54
  export * from './sort.js';
53
55
  export * from './stripWhitespace.js';
@@ -10,6 +10,7 @@ export * from './isBigIntArray.js';
10
10
  export * from './isBinaryNumberString.js';
11
11
  export * from './isBoolean.js';
12
12
  export * from './isBooleanArray.js';
13
+ export * from './isDateInputString.js';
13
14
  export * from './isDateString.js';
14
15
  export * from './isDigitsString.js';
15
16
  export * from './isFiniteNumber.js';
@@ -0,0 +1 @@
1
+ export const isDateInputString = (input) => typeof input === 'string' && !Number.isNaN(Date.parse(input));
@@ -1 +1,2 @@
1
- export const isDateString = (input) => typeof input === 'string' && !Number.isNaN(Date.parse(input));
1
+ import { isDateInputString } from './isDateInputString.js';
2
+ export const isDateString = isDateInputString;
@@ -1,3 +1,3 @@
1
1
  import { matching } from './factory/matching.js';
2
- export const ISO_DATE_PATTERN = /^(?<year>\d{4,})-(?<month>[01]\d)-(?<day>0[1-9]|[12]\d|3[01])T(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})\.(?<timezone>\d{3}Z)$/;
2
+ export const ISO_DATE_PATTERN = /^(?<year>\d{4,})-(?<month>[01]\d)-(?<day>0[1-9]|[12]\d|3[01])T(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})\.(?<timezone>\d+Z)$/;
3
3
  export const isISODateString = matching(ISO_DATE_PATTERN);
@@ -0,0 +1,26 @@
1
+ import { pathParts } from './pathParts.js';
2
+ export function set(input, path, value) {
3
+ if (path === '') {
4
+ throw new Error('Path cannot be an empty string');
5
+ }
6
+ if (typeof path === 'string' && /\b(__proto__|constructor|prototype)\b/.test(path)) {
7
+ throw new Error('Cannot pollute prototype');
8
+ }
9
+ const parts = Array.from(pathParts(path));
10
+ const lastPart = parts.pop();
11
+ if (typeof lastPart === 'undefined') {
12
+ throw new Error('Path must have at least one part');
13
+ }
14
+ let current = input;
15
+ const useParts = [];
16
+ for (const part of parts) {
17
+ useParts.push(part);
18
+ if (typeof current === 'object' && current !== null && part in current) {
19
+ current = current[part];
20
+ }
21
+ else {
22
+ throw new Error(`Path "${useParts.join('.')}" does not exist in the input object`);
23
+ }
24
+ }
25
+ return (current[lastPart] = value);
26
+ }
@@ -0,0 +1,4 @@
1
+ import type { TypePredicateFn } from '../types/functions.js';
2
+ import type { WithPath } from '../types/objects.js';
3
+ export declare function assertPathExists<Input extends object, InputPath extends string | number>(input: Input, path: InputPath): asserts input is Input & WithPath<Input, InputPath>;
4
+ export declare function assertPathExists<Input extends object, InputPath extends string | number, Value>(input: Input, path: InputPath, predicate: TypePredicateFn<Value>): asserts input is Input & WithPath<Input, InputPath, Value>;
@@ -43,4 +43,5 @@ export * from './assertIsSymbol.js';
43
43
  export * from './assertIsSymbolArray.js';
44
44
  export * from './assertIsUndefined.js';
45
45
  export * from './assertIsUndefinedArray.js';
46
+ export * from './assertPathExists.js';
46
47
  export * from './assertPredicate.js';
@@ -1,4 +1,4 @@
1
1
  import type { Path, PathValue } from './types/objects.js';
2
2
  export declare function get<Input extends object>(input: Input, path: ''): Input;
3
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;
4
+ export declare function get<Input extends object, InputPath extends PropertyKey>(input: Input, path: InputPath): unknown;
@@ -0,0 +1,5 @@
1
+ import type { FromPath, Merge, Path } from './types/objects.js';
2
+ import type { Pretty } from './types/utils.js';
3
+ export declare function has<Input extends object, InputPath extends Path<Input>>(input: Input, path: InputPath): input is Input;
4
+ export declare function has<Input extends object, InputPath extends string | number>(input: Input, path: InputPath): input is Pretty<Input & Merge<Input, FromPath<InputPath, unknown>>>;
5
+ export declare function has<InputPath extends string | number>(input: unknown, path: InputPath): input is FromPath<InputPath, unknown>;
@@ -33,6 +33,7 @@ export * from './getPackageName.js';
33
33
  export * from './getPaths.js';
34
34
  export * from './getRandomItem.js';
35
35
  export * from './getType.js';
36
+ export * from './has.js';
36
37
  export * from './hasAdditionalProperties.js';
37
38
  export * from './inRange.js';
38
39
  export * from './isEmpty.js';
@@ -48,6 +49,7 @@ export * from './randomInt.js';
48
49
  export * from './redactCredentialsInURL.js';
49
50
  export * from './resultify.js';
50
51
  export * from './secToString.js';
52
+ export * from './set.js';
51
53
  export * from './sort-factory.js';
52
54
  export * from './sort.js';
53
55
  export * from './stripWhitespace.js';
@@ -10,6 +10,7 @@ export * from './isBigIntArray.js';
10
10
  export * from './isBinaryNumberString.js';
11
11
  export * from './isBoolean.js';
12
12
  export * from './isBooleanArray.js';
13
+ export * from './isDateInputString.js';
13
14
  export * from './isDateString.js';
14
15
  export * from './isDigitsString.js';
15
16
  export * from './isFiniteNumber.js';
@@ -0,0 +1,3 @@
1
+ import type { DateInputString } from '../types/date-time.js';
2
+ import type { TypePredicateFn } from '../types/functions.js';
3
+ export declare const isDateInputString: TypePredicateFn<DateInputString>;
@@ -0,0 +1,3 @@
1
+ import type { Path, PathValue } from './types/objects.js';
2
+ export declare function set<Input extends object, InputPath extends Path<Input>, Value extends PathValue<Input, InputPath>>(input: Input, path: InputPath, value: Value): Value;
3
+ export declare function set<Input extends object, InputPath extends string, Value>(input: Input, path: InputPath, value: Value): Value;
@@ -1,6 +1,7 @@
1
1
  import type { Branded } from './branded.js';
2
2
  import type { NumericString } from './common.js';
3
3
  export type DateInput = ConstructorParameters<typeof Date>[0];
4
+ export type DateInputString = Branded<string, 'DateInputString'>;
4
5
  export type YearString = Branded<NumericString<number>, 'YearString'>;
5
6
  export type MonthString = Branded<NumericString<number>, 'MonthString'>;
6
7
  export type DayString = Branded<NumericString<number>, 'DayString'>;
@@ -1,8 +1,9 @@
1
1
  import type { IfArray, IfArrayLike } from './arrays.js';
2
2
  import type { Primitive } from './common.js';
3
- import type { CamelCase } from './strings.js';
3
+ import type { UnknownRecord } from './records.js';
4
+ import type { AutoCompletableString, CamelCase } from './strings.js';
4
5
  import type { KeyValueTuple } from './tuples.js';
5
- import type { CanBeUndefined, IfNever } from './utils.js';
6
+ import type { CanBeUndefined, IfNever, Pretty } from './utils.js';
6
7
  export type Assign<Target, Source> = IfNever<Target, Source, Omit<Target, keyof (Target | Source)> & Source>;
7
8
  export type Entries<T extends object> = {
8
9
  [P in keyof T]: KeyValueTuple<P, T[P]>;
@@ -19,7 +20,21 @@ export type PathDotNotation<Type extends object, Key extends keyof Type = keyof
19
20
  export type Path<Type extends object> = PathDotNotation<NonNullableProperties<Type>, keyof Type> | ValidKeys<Type>;
20
21
  export type MaybeOptional<Type, Optional extends boolean> = Optional extends true ? Type | undefined : Type;
21
22
  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 PathValue<Type extends object, TargetPath extends Path<Type> | AutoCompletableString | number, 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
24
  export type PathValues<Type extends object, TargetPaths extends Path<Type>> = {
24
25
  [TargetPath in TargetPaths as CamelCase<`${TargetPath}`, '.'>]: PathValue<Type, TargetPath>;
25
26
  };
27
+ export type FromPath<TargetPath extends PropertyKey, Value> = TargetPath extends `${infer Key}.${infer Rest}` ? {
28
+ [K in Key]: FromPath<Rest, Value>;
29
+ } : {
30
+ [K in TargetPath]: Value;
31
+ };
32
+ export type AllPaths<TargetPath extends PropertyKey> = TargetPath extends `${infer Head}.${infer Tail}` ? Head | `${Head}.${AllPaths<Tail>}` : TargetPath;
33
+ export type WithPath<Input extends object, InputPath extends PropertyKey, Value = unknown> = Pretty<Input & Merge<Input, FromPath<InputPath, Value>>>;
34
+ export type Merge<Left, Right> = Left extends unknown[] ? Right extends Record<number, unknown> ? MergeArrayWithObject<Left, Right> : Right : Left extends object ? Right extends object ? MergeObjects<Left, Right> : Right : Right;
35
+ export type MergeObjects<Left, Right> = {
36
+ [Key in keyof Left | keyof Right]: Key extends keyof Right ? Key extends keyof Left ? Merge<Left[Key], Right[Key]> : Right[Key] : Key extends keyof Left ? Left[Key] : never;
37
+ };
38
+ export type MergeArrayWithObject<Left extends unknown[], Right extends UnknownRecord> = Left & {
39
+ [Key in keyof Right]: Key extends keyof Left ? Merge<Left[Key & keyof Left], Right[Key]> : Key extends `${infer Index extends number}` ? Index extends keyof Left ? Merge<Left[Index], Right[Key]> : Right[Key] : Right[Key];
40
+ };
@@ -51,3 +51,4 @@ export type Simplify<Type> = Type extends string ? string : Type extends number
51
51
  } : Type;
52
52
  export type IfHasOnlyNumericKeys<Type, T, F> = `${Exclude<keyof Type, symbol>}` extends `${number}` ? T : F;
53
53
  export type HasOnlyNumericKeys<Type> = IfHasOnlyNumericKeys<Type, true, false>;
54
+ export type ParseNumber<T extends string> = T extends `${infer N extends number}` ? N : never;
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.68.0",
4
+ "version": "0.70.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.13.1+sha512.37ebf1a5c7a30d5fabe0c5df44ee8da4c965ca0c5af3dbab28c3a1681b70a256218d05c81c9c0dcf767ef6b8551eb5b960042b9ed4300c59242336377e01cfad",
69
+ "packageManager": "pnpm@10.14.0+sha512.ad27a79641b49c3e481a16a805baa71817a04bbe06a38d17e60e2eaee83f6a146c6a688125f5792e48dd5ba30e7da52a5cda4c3992b9ccf333f9ce223af84748",
70
70
  "scripts": {
71
71
  "clean": "rimraf ./dist/",
72
72
  "prebuild": "pnpm clean",
@@ -87,26 +87,26 @@
87
87
  "devDependencies": {
88
88
  "@commitlint/config-conventional": "^19.8.1",
89
89
  "@commitlint/types": "^19.8.1",
90
- "@types/node": "^22.16.3",
90
+ "@types/node": "^22.17.2",
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
95
  "commitlint-plugin-cspell": "^0.3.0",
96
96
  "conventional-changelog-conventionalcommits": "^9.1.0",
97
- "cspell": "^9.1.5",
97
+ "cspell": "^9.2.0",
98
98
  "eslint": "^8.57.1",
99
- "eslint-config-prettier": "^10.1.5",
99
+ "eslint-config-prettier": "^10.1.8",
100
100
  "eslint-import-resolver-typescript": "^4.4.4",
101
101
  "eslint-plugin-import": "^2.32.0",
102
102
  "husky": "^9.1.7",
103
103
  "jsdom": "^26.1.0",
104
- "lint-staged": "^16.1.2",
104
+ "lint-staged": "^16.1.5",
105
105
  "prettier": "^3.6.2",
106
106
  "rimraf": "^6.0.1",
107
107
  "semantic-release": "^24.2.7",
108
- "typescript": "^5.8.3",
109
- "validate-package-exports": "^0.12.0",
108
+ "typescript": "^5.9.2",
109
+ "validate-package-exports": "^0.13.0",
110
110
  "vitest": "^3.2.4"
111
111
  },
112
112
  "pnpm": {