@webdeveric/utils 1.5.0 → 1.7.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 +2 -14
- package/dist/cjs/has.js +3 -15
- package/dist/cjs/hasSameOrigin.js +5 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/internal/getNode.js +31 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/predicate/factory/allOf.js +1 -1
- package/dist/cjs/predicate/factory/anyOf.js +1 -1
- package/dist/cjs/predicate/factory/index.js +2 -0
- package/dist/cjs/predicate/factory/is.js +1 -1
- package/dist/cjs/predicate/factory/oneOf.js +23 -0
- package/dist/cjs/predicate/factory/record.js +10 -0
- package/dist/cjs/predicate/isAnyObjectWith.js +8 -2
- package/dist/cjs/set.js +7 -13
- package/dist/mjs/get.js +2 -14
- package/dist/mjs/has.js +3 -15
- package/dist/mjs/hasSameOrigin.js +1 -0
- package/dist/mjs/index.js +1 -0
- package/dist/mjs/internal/getNode.js +28 -0
- package/dist/mjs/predicate/factory/allOf.js +1 -1
- package/dist/mjs/predicate/factory/anyOf.js +1 -1
- package/dist/mjs/predicate/factory/index.js +2 -0
- package/dist/mjs/predicate/factory/is.js +1 -1
- package/dist/mjs/predicate/factory/oneOf.js +19 -0
- package/dist/mjs/predicate/factory/record.js +6 -0
- package/dist/mjs/predicate/isAnyObjectWith.js +8 -2
- package/dist/mjs/set.js +7 -13
- package/dist/types/hasSameOrigin.d.ts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/internal/getNode.d.ts +15 -0
- package/dist/types/predicate/factory/index.d.ts +2 -0
- package/dist/types/predicate/factory/oneOf.d.ts +16 -0
- package/dist/types/predicate/factory/record.d.ts +2 -0
- package/dist/types/predicate/isAnyObjectWith.d.ts +7 -2
- package/dist/types/types/branded.d.ts +3 -0
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/utils.d.ts +28 -2
- package/package.json +33 -19
package/dist/cjs/get.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.get = get;
|
|
4
|
+
const getNode_js_1 = require("./internal/getNode.js");
|
|
4
5
|
const pathParts_js_1 = require("./pathParts.js");
|
|
5
6
|
/**
|
|
6
7
|
* Get the value at `path` within `input`.
|
|
@@ -11,18 +12,5 @@ const pathParts_js_1 = require("./pathParts.js");
|
|
|
11
12
|
* ```
|
|
12
13
|
*/
|
|
13
14
|
function get(input, path) {
|
|
14
|
-
|
|
15
|
-
return input;
|
|
16
|
-
}
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
-
let current = input;
|
|
19
|
-
for (const part of (0, pathParts_js_1.pathParts)(path)) {
|
|
20
|
-
if (typeof current === 'object' && current !== null && part in current) {
|
|
21
|
-
current = current[part];
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return current;
|
|
15
|
+
return path === '' ? input : (0, getNode_js_1.getNode)(input, (0, pathParts_js_1.pathParts)(path)).value;
|
|
28
16
|
}
|
package/dist/cjs/has.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.has = has;
|
|
4
|
+
const getNode_js_1 = require("./internal/getNode.js");
|
|
4
5
|
const pathParts_js_1 = require("./pathParts.js");
|
|
5
6
|
const isAnyObject_js_1 = require("./predicate/isAnyObject.js");
|
|
6
7
|
/**
|
|
@@ -12,21 +13,8 @@ const isAnyObject_js_1 = require("./predicate/isAnyObject.js");
|
|
|
12
13
|
* ```
|
|
13
14
|
*/
|
|
14
15
|
function has(input, path) {
|
|
15
|
-
if (!(0, isAnyObject_js_1.isAnyObject)(input)) {
|
|
16
|
+
if (!(0, isAnyObject_js_1.isAnyObject)(input) || path === '') {
|
|
16
17
|
return false;
|
|
17
18
|
}
|
|
18
|
-
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
-
let current = input;
|
|
23
|
-
for (const part of (0, pathParts_js_1.pathParts)(path)) {
|
|
24
|
-
if (typeof current === 'object' && current !== null && part in current) {
|
|
25
|
-
current = current[part];
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return true;
|
|
19
|
+
return (0, getNode_js_1.getNode)(input, (0, pathParts_js_1.pathParts)(path)).found;
|
|
32
20
|
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -62,6 +62,7 @@ __exportStar(require("./getType.js"), exports);
|
|
|
62
62
|
__exportStar(require("./graphemeLength.js"), exports);
|
|
63
63
|
__exportStar(require("./has.js"), exports);
|
|
64
64
|
__exportStar(require("./hasAdditionalProperties.js"), exports);
|
|
65
|
+
__exportStar(require("./hasSameOrigin.js"), exports);
|
|
65
66
|
__exportStar(require("./indent.js"), exports);
|
|
66
67
|
__exportStar(require("./inRange.js"), exports);
|
|
67
68
|
__exportStar(require("./isEmpty.js"), exports);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getNode = getNode;
|
|
4
|
+
const isAnyObjectWith_js_1 = require("../predicate/isAnyObjectWith.js");
|
|
5
|
+
/**
|
|
6
|
+
* Retrieves the value of a nested property from an object based on a sequence of keys.
|
|
7
|
+
*
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
function getNode(input, pathKeys) {
|
|
11
|
+
let current = input;
|
|
12
|
+
const visitedNodes = [];
|
|
13
|
+
for (const key of pathKeys) {
|
|
14
|
+
visitedNodes.push(key);
|
|
15
|
+
if ((0, isAnyObjectWith_js_1.isAnyObjectWith)(current, key)) {
|
|
16
|
+
current = current[key];
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return {
|
|
20
|
+
value: undefined,
|
|
21
|
+
found: false,
|
|
22
|
+
visitedNodes,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
value: current,
|
|
28
|
+
found: true,
|
|
29
|
+
visitedNodes,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -14,7 +14,7 @@ exports.allOf = void 0;
|
|
|
14
14
|
*/
|
|
15
15
|
const allOf = (...predicates) => {
|
|
16
16
|
if (!predicates.length) {
|
|
17
|
-
throw new
|
|
17
|
+
throw new TypeError('Predicate functions not provided');
|
|
18
18
|
}
|
|
19
19
|
return (input) => predicates.every((predicate) => predicate(input));
|
|
20
20
|
};
|
|
@@ -14,7 +14,7 @@ exports.anyOf = void 0;
|
|
|
14
14
|
*/
|
|
15
15
|
const anyOf = (...predicates) => {
|
|
16
16
|
if (!predicates.length) {
|
|
17
|
-
throw new
|
|
17
|
+
throw new TypeError('Predicate functions not provided');
|
|
18
18
|
}
|
|
19
19
|
return (input) => predicates.some((predicate) => predicate(input));
|
|
20
20
|
};
|
|
@@ -26,8 +26,10 @@ __exportStar(require("./maybeArray.js"), exports);
|
|
|
26
26
|
__exportStar(require("./nonNullable.js"), exports);
|
|
27
27
|
__exportStar(require("./nullable.js"), exports);
|
|
28
28
|
__exportStar(require("./nullish.js"), exports);
|
|
29
|
+
__exportStar(require("./oneOf.js"), exports);
|
|
29
30
|
__exportStar(require("./optional.js"), exports);
|
|
30
31
|
__exportStar(require("./range.js"), exports);
|
|
32
|
+
__exportStar(require("./record.js"), exports);
|
|
31
33
|
__exportStar(require("./shape.js"), exports);
|
|
32
34
|
__exportStar(require("./simple.js"), exports);
|
|
33
35
|
__exportStar(require("./stringLength.js"), exports);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.oneOf = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Create a type predicate function that passes if `input` is one of the given `values`.
|
|
6
|
+
*
|
|
7
|
+
* Values are compared using [SameValueZero](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#same-value-zero_equality)
|
|
8
|
+
* (like `Array.prototype.includes()`), so `NaN` matches `NaN` and `0` matches `-0`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const isPrimary = oneOf('red', 'green', 'blue');
|
|
13
|
+
* isPrimary('red'); // true
|
|
14
|
+
* isPrimary('yellow'); // false
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
const oneOf = (...values) => {
|
|
18
|
+
if (!values.length) {
|
|
19
|
+
throw new TypeError('Values not provided');
|
|
20
|
+
}
|
|
21
|
+
return (input) => values.includes(input);
|
|
22
|
+
};
|
|
23
|
+
exports.oneOf = oneOf;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.record = void 0;
|
|
4
|
+
const isObject_js_1 = require("../isObject.js");
|
|
5
|
+
const record = (keyPredicate, valuePredicate) => {
|
|
6
|
+
return (input) => (0, isObject_js_1.isObject)(input)
|
|
7
|
+
? Reflect.ownKeys(input).every((key) => keyPredicate(key) && valuePredicate(Reflect.get(input, key)))
|
|
8
|
+
: false;
|
|
9
|
+
};
|
|
10
|
+
exports.record = record;
|
|
@@ -3,15 +3,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.isAnyObjectWith = void 0;
|
|
4
4
|
const isAnyObject_js_1 = require("./isAnyObject.js");
|
|
5
5
|
/**
|
|
6
|
-
* Determine if `input` is an object and has the provided properties.
|
|
6
|
+
* Determine if `input` is an object and has the provided property or properties.
|
|
7
7
|
*
|
|
8
8
|
* @example
|
|
9
9
|
* ```ts
|
|
10
|
+
* isAnyObjectWith({ a: 1 }, 'a'); // true
|
|
11
|
+
* isAnyObjectWith({ a: 1 }, 'b'); // false
|
|
10
12
|
* isAnyObjectWith({ a: 1, b: 2 }, ['a', 'b']); // true
|
|
11
13
|
* isAnyObjectWith({ a: 1 }, ['a', 'b']); // false
|
|
14
|
+
* isAnyObjectWith({}, 'toString'); // true (inherited properties are included)
|
|
15
|
+
* isAnyObjectWith(['a'], 0); // true (arrays are objects)
|
|
16
|
+
* isAnyObjectWith(null, 'a'); // false
|
|
12
17
|
* ```
|
|
13
18
|
*/
|
|
14
19
|
const isAnyObjectWith = (input, properties) => {
|
|
15
|
-
return (0, isAnyObject_js_1.isAnyObject)(input) &&
|
|
20
|
+
return ((0, isAnyObject_js_1.isAnyObject)(input) &&
|
|
21
|
+
(Array.isArray(properties) ? properties.every((property) => property in input) : properties in input));
|
|
16
22
|
};
|
|
17
23
|
exports.isAnyObjectWith = isAnyObjectWith;
|
package/dist/cjs/set.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.set = set;
|
|
4
|
+
const assertIsObject_js_1 = require("./assertion/assertIsObject.js");
|
|
5
|
+
const getNode_js_1 = require("./internal/getNode.js");
|
|
4
6
|
const pathParts_js_1 = require("./pathParts.js");
|
|
5
7
|
const prototypePollutionPattern = /\b(__proto__|constructor|prototype)\b/;
|
|
6
8
|
/**
|
|
@@ -26,18 +28,10 @@ function set(input, path, value) {
|
|
|
26
28
|
if (typeof lastPart === 'undefined') {
|
|
27
29
|
throw new Error('Path must have at least one part');
|
|
28
30
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
// get a reference to the last node that will be updated
|
|
33
|
-
for (const part of parts) {
|
|
34
|
-
useParts.push(part);
|
|
35
|
-
if (typeof current === 'object' && current !== null && part in current) {
|
|
36
|
-
current = current[part];
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
throw new Error(`Path "${useParts.join('.')}" does not exist in the input object`);
|
|
40
|
-
}
|
|
31
|
+
const result = (0, getNode_js_1.getNode)(input, parts);
|
|
32
|
+
if (!result.found) {
|
|
33
|
+
throw new Error(`Path "${result.visitedNodes.join('.')}" does not exist in the input object`);
|
|
41
34
|
}
|
|
42
|
-
|
|
35
|
+
(0, assertIsObject_js_1.assertIsObject)(result.value);
|
|
36
|
+
return (result.value[lastPart] = value);
|
|
43
37
|
}
|
package/dist/mjs/get.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getNode } from './internal/getNode.js';
|
|
1
2
|
import { pathParts } from './pathParts.js';
|
|
2
3
|
/**
|
|
3
4
|
* Get the value at `path` within `input`.
|
|
@@ -8,18 +9,5 @@ import { pathParts } from './pathParts.js';
|
|
|
8
9
|
* ```
|
|
9
10
|
*/
|
|
10
11
|
export function get(input, path) {
|
|
11
|
-
|
|
12
|
-
return input;
|
|
13
|
-
}
|
|
14
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
-
let current = input;
|
|
16
|
-
for (const part of pathParts(path)) {
|
|
17
|
-
if (typeof current === 'object' && current !== null && part in current) {
|
|
18
|
-
current = current[part];
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return current;
|
|
12
|
+
return path === '' ? input : getNode(input, pathParts(path)).value;
|
|
25
13
|
}
|
package/dist/mjs/has.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getNode } from './internal/getNode.js';
|
|
1
2
|
import { pathParts } from './pathParts.js';
|
|
2
3
|
import { isAnyObject } from './predicate/isAnyObject.js';
|
|
3
4
|
/**
|
|
@@ -9,21 +10,8 @@ import { isAnyObject } from './predicate/isAnyObject.js';
|
|
|
9
10
|
* ```
|
|
10
11
|
*/
|
|
11
12
|
export function has(input, path) {
|
|
12
|
-
if (!isAnyObject(input)) {
|
|
13
|
+
if (!isAnyObject(input) || path === '') {
|
|
13
14
|
return false;
|
|
14
15
|
}
|
|
15
|
-
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
-
let current = input;
|
|
20
|
-
for (const part of pathParts(path)) {
|
|
21
|
-
if (typeof current === 'object' && current !== null && part in current) {
|
|
22
|
-
current = current[part];
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return true;
|
|
16
|
+
return getNode(input, pathParts(path)).found;
|
|
29
17
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const hasSameOrigin = (left, right) => new URL(left).origin === new URL(right).origin;
|
package/dist/mjs/index.js
CHANGED
|
@@ -46,6 +46,7 @@ export * from './getType.js';
|
|
|
46
46
|
export * from './graphemeLength.js';
|
|
47
47
|
export * from './has.js';
|
|
48
48
|
export * from './hasAdditionalProperties.js';
|
|
49
|
+
export * from './hasSameOrigin.js';
|
|
49
50
|
export * from './indent.js';
|
|
50
51
|
export * from './inRange.js';
|
|
51
52
|
export * from './isEmpty.js';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { isAnyObjectWith } from '../predicate/isAnyObjectWith.js';
|
|
2
|
+
/**
|
|
3
|
+
* Retrieves the value of a nested property from an object based on a sequence of keys.
|
|
4
|
+
*
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
export function getNode(input, pathKeys) {
|
|
8
|
+
let current = input;
|
|
9
|
+
const visitedNodes = [];
|
|
10
|
+
for (const key of pathKeys) {
|
|
11
|
+
visitedNodes.push(key);
|
|
12
|
+
if (isAnyObjectWith(current, key)) {
|
|
13
|
+
current = current[key];
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
return {
|
|
17
|
+
value: undefined,
|
|
18
|
+
found: false,
|
|
19
|
+
visitedNodes,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
value: current,
|
|
25
|
+
found: true,
|
|
26
|
+
visitedNodes,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
export const allOf = (...predicates) => {
|
|
13
13
|
if (!predicates.length) {
|
|
14
|
-
throw new
|
|
14
|
+
throw new TypeError('Predicate functions not provided');
|
|
15
15
|
}
|
|
16
16
|
return (input) => predicates.every((predicate) => predicate(input));
|
|
17
17
|
};
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
export const anyOf = (...predicates) => {
|
|
13
13
|
if (!predicates.length) {
|
|
14
|
-
throw new
|
|
14
|
+
throw new TypeError('Predicate functions not provided');
|
|
15
15
|
}
|
|
16
16
|
return (input) => predicates.some((predicate) => predicate(input));
|
|
17
17
|
};
|
|
@@ -10,8 +10,10 @@ export * from './maybeArray.js';
|
|
|
10
10
|
export * from './nonNullable.js';
|
|
11
11
|
export * from './nullable.js';
|
|
12
12
|
export * from './nullish.js';
|
|
13
|
+
export * from './oneOf.js';
|
|
13
14
|
export * from './optional.js';
|
|
14
15
|
export * from './range.js';
|
|
16
|
+
export * from './record.js';
|
|
15
17
|
export * from './shape.js';
|
|
16
18
|
export * from './simple.js';
|
|
17
19
|
export * from './stringLength.js';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a type predicate function that passes if `input` is one of the given `values`.
|
|
3
|
+
*
|
|
4
|
+
* Values are compared using [SameValueZero](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#same-value-zero_equality)
|
|
5
|
+
* (like `Array.prototype.includes()`), so `NaN` matches `NaN` and `0` matches `-0`.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const isPrimary = oneOf('red', 'green', 'blue');
|
|
10
|
+
* isPrimary('red'); // true
|
|
11
|
+
* isPrimary('yellow'); // false
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export const oneOf = (...values) => {
|
|
15
|
+
if (!values.length) {
|
|
16
|
+
throw new TypeError('Values not provided');
|
|
17
|
+
}
|
|
18
|
+
return (input) => values.includes(input);
|
|
19
|
+
};
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import { isAnyObject } from './isAnyObject.js';
|
|
2
2
|
/**
|
|
3
|
-
* Determine if `input` is an object and has the provided properties.
|
|
3
|
+
* Determine if `input` is an object and has the provided property or properties.
|
|
4
4
|
*
|
|
5
5
|
* @example
|
|
6
6
|
* ```ts
|
|
7
|
+
* isAnyObjectWith({ a: 1 }, 'a'); // true
|
|
8
|
+
* isAnyObjectWith({ a: 1 }, 'b'); // false
|
|
7
9
|
* isAnyObjectWith({ a: 1, b: 2 }, ['a', 'b']); // true
|
|
8
10
|
* isAnyObjectWith({ a: 1 }, ['a', 'b']); // false
|
|
11
|
+
* isAnyObjectWith({}, 'toString'); // true (inherited properties are included)
|
|
12
|
+
* isAnyObjectWith(['a'], 0); // true (arrays are objects)
|
|
13
|
+
* isAnyObjectWith(null, 'a'); // false
|
|
9
14
|
* ```
|
|
10
15
|
*/
|
|
11
16
|
export const isAnyObjectWith = (input, properties) => {
|
|
12
|
-
return isAnyObject(input) &&
|
|
17
|
+
return (isAnyObject(input) &&
|
|
18
|
+
(Array.isArray(properties) ? properties.every((property) => property in input) : properties in input));
|
|
13
19
|
};
|
package/dist/mjs/set.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { assertIsObject } from './assertion/assertIsObject.js';
|
|
2
|
+
import { getNode } from './internal/getNode.js';
|
|
1
3
|
import { pathParts } from './pathParts.js';
|
|
2
4
|
const prototypePollutionPattern = /\b(__proto__|constructor|prototype)\b/;
|
|
3
5
|
/**
|
|
@@ -23,18 +25,10 @@ export function set(input, path, value) {
|
|
|
23
25
|
if (typeof lastPart === 'undefined') {
|
|
24
26
|
throw new Error('Path must have at least one part');
|
|
25
27
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
// get a reference to the last node that will be updated
|
|
30
|
-
for (const part of parts) {
|
|
31
|
-
useParts.push(part);
|
|
32
|
-
if (typeof current === 'object' && current !== null && part in current) {
|
|
33
|
-
current = current[part];
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
throw new Error(`Path "${useParts.join('.')}" does not exist in the input object`);
|
|
37
|
-
}
|
|
28
|
+
const result = getNode(input, parts);
|
|
29
|
+
if (!result.found) {
|
|
30
|
+
throw new Error(`Path "${result.visitedNodes.join('.')}" does not exist in the input object`);
|
|
38
31
|
}
|
|
39
|
-
|
|
32
|
+
assertIsObject(result.value);
|
|
33
|
+
return (result.value[lastPart] = value);
|
|
40
34
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const hasSameOrigin: (left: string | URL, right: string | URL) => boolean;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -44,6 +44,7 @@ export * from './getType.js';
|
|
|
44
44
|
export * from './graphemeLength.js';
|
|
45
45
|
export * from './has.js';
|
|
46
46
|
export * from './hasAdditionalProperties.js';
|
|
47
|
+
export * from './hasSameOrigin.js';
|
|
47
48
|
export * from './indent.js';
|
|
48
49
|
export * from './inRange.js';
|
|
49
50
|
export * from './isEmpty.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type GetNodeResult = {
|
|
2
|
+
value: unknown;
|
|
3
|
+
found: true;
|
|
4
|
+
visitedNodes: PropertyKey[];
|
|
5
|
+
} | {
|
|
6
|
+
value: undefined;
|
|
7
|
+
found: false;
|
|
8
|
+
visitedNodes: PropertyKey[];
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Retrieves the value of a nested property from an object based on a sequence of keys.
|
|
12
|
+
*
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export declare function getNode<Input extends object>(input: Input, pathKeys: Iterable<PropertyKey>): GetNodeResult;
|
|
@@ -10,8 +10,10 @@ export * from './maybeArray.js';
|
|
|
10
10
|
export * from './nonNullable.js';
|
|
11
11
|
export * from './nullable.js';
|
|
12
12
|
export * from './nullish.js';
|
|
13
|
+
export * from './oneOf.js';
|
|
13
14
|
export * from './optional.js';
|
|
14
15
|
export * from './range.js';
|
|
16
|
+
export * from './record.js';
|
|
15
17
|
export * from './shape.js';
|
|
16
18
|
export * from './simple.js';
|
|
17
19
|
export * from './stringLength.js';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { NonEmptyArray } from '../../types/arrays.js';
|
|
2
|
+
import type { TypePredicateFn } from '../../types/functions.js';
|
|
3
|
+
/**
|
|
4
|
+
* Create a type predicate function that passes if `input` is one of the given `values`.
|
|
5
|
+
*
|
|
6
|
+
* Values are compared using [SameValueZero](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#same-value-zero_equality)
|
|
7
|
+
* (like `Array.prototype.includes()`), so `NaN` matches `NaN` and `0` matches `-0`.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const isPrimary = oneOf('red', 'green', 'blue');
|
|
12
|
+
* isPrimary('red'); // true
|
|
13
|
+
* isPrimary('yellow'); // false
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare const oneOf: <const Values extends NonEmptyArray<unknown>>(...values: Values) => TypePredicateFn<Values[number]>;
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Determine if `input` is an object and has the provided properties.
|
|
2
|
+
* Determine if `input` is an object and has the provided property or properties.
|
|
3
3
|
*
|
|
4
4
|
* @example
|
|
5
5
|
* ```ts
|
|
6
|
+
* isAnyObjectWith({ a: 1 }, 'a'); // true
|
|
7
|
+
* isAnyObjectWith({ a: 1 }, 'b'); // false
|
|
6
8
|
* isAnyObjectWith({ a: 1, b: 2 }, ['a', 'b']); // true
|
|
7
9
|
* isAnyObjectWith({ a: 1 }, ['a', 'b']); // false
|
|
10
|
+
* isAnyObjectWith({}, 'toString'); // true (inherited properties are included)
|
|
11
|
+
* isAnyObjectWith(['a'], 0); // true (arrays are objects)
|
|
12
|
+
* isAnyObjectWith(null, 'a'); // false
|
|
8
13
|
* ```
|
|
9
14
|
*/
|
|
10
|
-
export declare const isAnyObjectWith: <T, P extends PropertyKey>(input: T, properties: P[]) => input is T & Record<P, unknown>;
|
|
15
|
+
export declare const isAnyObjectWith: <T, P extends PropertyKey>(input: T, properties: P | P[]) => input is T & Record<P, unknown>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { NotPromise } from './utils.js';
|
|
1
|
+
import type { NotPromise, Pretty } from './utils.js';
|
|
2
2
|
export type Primitive = string | number | boolean | bigint | symbol | undefined | null;
|
|
3
3
|
/**
|
|
4
4
|
* This is any number or bigint that has been stringified.
|
|
@@ -10,7 +10,6 @@ export type JsonObject = {
|
|
|
10
10
|
};
|
|
11
11
|
export type JsonArray = JsonValue[];
|
|
12
12
|
export type JsonValue = string | number | boolean | null | object | JsonObject | JsonArray;
|
|
13
|
-
export type TypeOf = 'undefined' | 'object' | 'boolean' | 'number' | 'bigint' | 'string' | 'symbol' | 'function';
|
|
14
13
|
export type TypeOfMapping = {
|
|
15
14
|
undefined: undefined;
|
|
16
15
|
object: object;
|
|
@@ -21,6 +20,7 @@ export type TypeOfMapping = {
|
|
|
21
20
|
symbol: symbol;
|
|
22
21
|
function: Function;
|
|
23
22
|
};
|
|
23
|
+
export type TypeOf = Pretty<keyof TypeOfMapping>;
|
|
24
24
|
export type Builtin = Date | Error | Function | Primitive | RegExp;
|
|
25
25
|
export type AnyFunction = (...args: any[]) => any;
|
|
26
26
|
export type AnySyncFunction = (...args: any[]) => NotPromise<any>;
|
|
@@ -70,8 +70,34 @@ export type DeepPartial<Type> = Type extends unknown[] ? DeepPartial<Type[number
|
|
|
70
70
|
[Property in keyof Type]?: DeepPartial<Type[Property]>;
|
|
71
71
|
} : Type;
|
|
72
72
|
export type UnionToIntersection<Union> = (Union extends any ? (union: Union) => void : never) extends (union: infer Type extends Union) => void ? Type : never;
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Tail-recursive so TypeScript can evaluate long tuples (up to ~1000 elements)
|
|
75
|
+
* without hitting the instantiation depth limit.
|
|
76
|
+
*
|
|
77
|
+
* @internal
|
|
78
|
+
*/
|
|
79
|
+
export type IntersectionOfAccumulator<T extends readonly unknown[], Result> = T extends readonly [
|
|
80
|
+
infer First,
|
|
81
|
+
...infer Rest
|
|
82
|
+
] ? IntersectionOfAccumulator<Rest, Result & First> : Result;
|
|
83
|
+
/**
|
|
84
|
+
* Intersection of the element types of `T`.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* type Example = IntersectionOf<[{ a: 1 }, { b: 2 }]>; // { a: 1 } & { b: 2 }
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export type IntersectionOf<T extends readonly unknown[]> = IntersectionOfAccumulator<T, unknown>;
|
|
92
|
+
/**
|
|
93
|
+
* Union of the element types of `T`.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* type Example = UnionOf<[1, 'a', true]>; // 1 | 'a' | true
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export type UnionOf<T extends readonly unknown[]> = T[number];
|
|
75
101
|
export type Pretty<Type> = {
|
|
76
102
|
[Property in keyof Type]: Type[Property];
|
|
77
103
|
} & unknown;
|
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": "1.
|
|
4
|
+
"version": "1.7.0",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -22,6 +22,20 @@
|
|
|
22
22
|
"engines": {
|
|
23
23
|
"node": ">=18.0.0"
|
|
24
24
|
},
|
|
25
|
+
"devEngines": {
|
|
26
|
+
"packageManager": {
|
|
27
|
+
"name": "pnpm",
|
|
28
|
+
"version": "12.23.0",
|
|
29
|
+
"onFail": "warn"
|
|
30
|
+
},
|
|
31
|
+
"runtime": [
|
|
32
|
+
{
|
|
33
|
+
"name": "node",
|
|
34
|
+
"version": "^26.13.1",
|
|
35
|
+
"onFail": "download"
|
|
36
|
+
}
|
|
37
|
+
]
|
|
38
|
+
},
|
|
25
39
|
"exports": {
|
|
26
40
|
".": {
|
|
27
41
|
"types": "./dist/types/index.d.ts",
|
|
@@ -53,6 +67,7 @@
|
|
|
53
67
|
"require": "./dist/cjs/*.js",
|
|
54
68
|
"import": "./dist/mjs/*.js"
|
|
55
69
|
},
|
|
70
|
+
"./internal/*": null,
|
|
56
71
|
"./package.json": "./package.json"
|
|
57
72
|
},
|
|
58
73
|
"files": [
|
|
@@ -66,19 +81,18 @@
|
|
|
66
81
|
"url": "https://github.com/webdeveric/utils/issues"
|
|
67
82
|
},
|
|
68
83
|
"homepage": "https://github.com/webdeveric/utils/#readme",
|
|
69
|
-
"packageManager": "pnpm@11.25.0+sha512.5cde925b4f075f725eb71fbae18a42ffe784524789f19b61c731cb8721ec28aaee160e01a8d5af4fedb2a42cdbf300efe23db356b0d4a17b4d63e11f8ab7c956",
|
|
70
84
|
"scripts": {
|
|
71
|
-
"clean": "rimraf ./dist/ ./cache/ ./coverage/",
|
|
85
|
+
"clean": "rimraf ./dist/ ./docs/ ./cache/ ./coverage/",
|
|
72
86
|
"prebuild": "pnpm clean",
|
|
73
87
|
"build": "tsc --build tsconfig.cjs.json tsconfig.mjs.json --force",
|
|
74
88
|
"build:docs": "typedoc",
|
|
75
|
-
"postbuild": "echo '{\"type\":\"commonjs\"}' > ./dist/cjs/package.json
|
|
89
|
+
"postbuild": "echo '{\"type\":\"commonjs\"}' > ./dist/cjs/package.json",
|
|
76
90
|
"typecheck": "tsc --build --verbose --noEmit",
|
|
77
91
|
"lint": "eslint ./*.{js,cjs,mjs,ts,cts,mts} ./src/ ./bench/ ./test/ --ext .ts,.mjs,.cjs",
|
|
78
92
|
"bench": "vitest bench",
|
|
79
93
|
"test": "vitest --typecheck",
|
|
80
94
|
"coverage": "vitest run --coverage --typecheck",
|
|
81
|
-
"validate": "validate-package-exports --check",
|
|
95
|
+
"validate": "validate-package-exports --check --strict-module-type",
|
|
82
96
|
"spellcheck": "cspell './{.github,src,bench,test}/**/*.{ts,json}' './*.{md,mjs,mts}' './package.json' --no-progress",
|
|
83
97
|
"prepublishOnly": "pnpm typecheck && pnpm spellcheck && pnpm lint && pnpm coverage && pnpm build",
|
|
84
98
|
"format": "prettier --write ./*.{js,json,md} ./src/ ./bench/ ./test/",
|
|
@@ -86,31 +100,31 @@
|
|
|
86
100
|
},
|
|
87
101
|
"prettier": "@webdeveric/prettier-config",
|
|
88
102
|
"devDependencies": {
|
|
89
|
-
"@commitlint/config-conventional": "^21.2.
|
|
90
|
-
"@commitlint/types": "^21.2.
|
|
91
|
-
"@types/node": "^
|
|
103
|
+
"@commitlint/config-conventional": "^21.2.4",
|
|
104
|
+
"@commitlint/types": "^21.2.3",
|
|
105
|
+
"@types/node": "^26.6.11",
|
|
92
106
|
"@typescript/native": "npm:typescript@^7.0.2",
|
|
93
|
-
"@vitest/coverage-v8": "^
|
|
107
|
+
"@vitest/coverage-v8": "^5.1.2",
|
|
94
108
|
"@webdeveric/eslint-config-ts": "^0.12.1",
|
|
95
109
|
"@webdeveric/prettier-config": "^0.4.0",
|
|
96
110
|
"@webdeveric/ts-data-structures": "^1.0.1",
|
|
97
|
-
"commitlint": "^21.2.
|
|
98
|
-
"commitlint-plugin-cspell": "^0.9.
|
|
111
|
+
"commitlint": "^21.2.4",
|
|
112
|
+
"commitlint-plugin-cspell": "^0.9.5",
|
|
99
113
|
"conventional-changelog-conventionalcommits": "^9.3.1",
|
|
100
|
-
"cspell": "^10.
|
|
114
|
+
"cspell": "^10.5.2",
|
|
101
115
|
"eslint": "^8.57.1",
|
|
102
|
-
"eslint-config-prettier": "^10.1.
|
|
116
|
+
"eslint-config-prettier": "^10.1.9",
|
|
103
117
|
"eslint-import-resolver-typescript": "^4.4.5",
|
|
104
118
|
"eslint-plugin-import": "^2.32.0",
|
|
105
119
|
"husky": "^9.1.7",
|
|
106
|
-
"jsdom": "^
|
|
107
|
-
"lint-staged": "^17.
|
|
108
|
-
"prettier": "^3.
|
|
109
|
-
"rimraf": "^6.1.
|
|
120
|
+
"jsdom": "^31.0.1",
|
|
121
|
+
"lint-staged": "^17.6.2",
|
|
122
|
+
"prettier": "^3.10.1",
|
|
123
|
+
"rimraf": "^6.1.4",
|
|
110
124
|
"semantic-release": "^25.0.9",
|
|
111
125
|
"typedoc": "^0.28.20",
|
|
112
126
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
113
|
-
"validate-package-exports": "^1.
|
|
114
|
-
"vitest": "^
|
|
127
|
+
"validate-package-exports": "^1.7.0",
|
|
128
|
+
"vitest": "^5.1.2"
|
|
115
129
|
}
|
|
116
130
|
}
|