@webdeveric/utils 1.4.0 → 1.6.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 +3 -0
- package/dist/cjs/internal/getNode.js +31 -0
- package/dist/cjs/lazyProp.js +29 -0
- package/dist/cjs/lazyRecord.js +30 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/predicate/factory/index.js +1 -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 +3 -0
- package/dist/mjs/internal/getNode.js +28 -0
- package/dist/mjs/lazyProp.js +26 -0
- package/dist/mjs/lazyRecord.js +27 -0
- package/dist/mjs/package.json +1 -0
- package/dist/mjs/predicate/factory/index.js +1 -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 +3 -0
- package/dist/types/internal/getNode.d.ts +15 -0
- package/dist/types/lazyProp.d.ts +13 -0
- package/dist/types/lazyRecord.d.ts +19 -0
- package/dist/types/predicate/factory/index.d.ts +1 -0
- package/dist/types/predicate/factory/record.d.ts +2 -0
- package/dist/types/predicate/isAnyObjectWith.d.ts +7 -2
- package/package.json +26 -13
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,12 +62,15 @@ __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);
|
|
68
69
|
__exportStar(require("./iterateForever.js"), exports);
|
|
69
70
|
__exportStar(require("./joinStrings.js"), exports);
|
|
70
71
|
__exportStar(require("./jsonParse.js"), exports);
|
|
72
|
+
__exportStar(require("./lazyProp.js"), exports);
|
|
73
|
+
__exportStar(require("./lazyRecord.js"), exports);
|
|
71
74
|
__exportStar(require("./looksLikeURL.js"), exports);
|
|
72
75
|
__exportStar(require("./memo.js"), exports);
|
|
73
76
|
__exportStar(require("./normalize.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,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.lazyProp = lazyProp;
|
|
4
|
+
/**
|
|
5
|
+
* Create a property descriptor that lazily computes its value.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const data = {};
|
|
10
|
+
*
|
|
11
|
+
* Object.defineProperty(data, 'value', lazyProp('value', () => {
|
|
12
|
+
* return Math.random();
|
|
13
|
+
* }));
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
function lazyProp(key, getter) {
|
|
17
|
+
return {
|
|
18
|
+
configurable: true,
|
|
19
|
+
get() {
|
|
20
|
+
const value = getter();
|
|
21
|
+
Object.defineProperty(this, key, {
|
|
22
|
+
value,
|
|
23
|
+
writable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
});
|
|
26
|
+
return value;
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.lazyRecord = lazyRecord;
|
|
4
|
+
const lazyProp_js_1 = require("./lazyProp.js");
|
|
5
|
+
/**
|
|
6
|
+
* Create an object whose properties are computed lazily.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const data = lazyRecord({
|
|
11
|
+
* name: 'Name',
|
|
12
|
+
* now() {
|
|
13
|
+
* return Date.now();
|
|
14
|
+
* },
|
|
15
|
+
* });
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function lazyRecord(properties) {
|
|
19
|
+
const record = Object.create(null);
|
|
20
|
+
for (const [key, getter] of Object.entries(properties)) {
|
|
21
|
+
Object.defineProperty(record, key, typeof getter === 'function'
|
|
22
|
+
? (0, lazyProp_js_1.lazyProp)(key, getter)
|
|
23
|
+
: {
|
|
24
|
+
value: getter,
|
|
25
|
+
writable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return record;
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -28,6 +28,7 @@ __exportStar(require("./nullable.js"), exports);
|
|
|
28
28
|
__exportStar(require("./nullish.js"), exports);
|
|
29
29
|
__exportStar(require("./optional.js"), exports);
|
|
30
30
|
__exportStar(require("./range.js"), exports);
|
|
31
|
+
__exportStar(require("./record.js"), exports);
|
|
31
32
|
__exportStar(require("./shape.js"), exports);
|
|
32
33
|
__exportStar(require("./simple.js"), exports);
|
|
33
34
|
__exportStar(require("./stringLength.js"), exports);
|
|
@@ -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,12 +46,15 @@ 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';
|
|
52
53
|
export * from './iterateForever.js';
|
|
53
54
|
export * from './joinStrings.js';
|
|
54
55
|
export * from './jsonParse.js';
|
|
56
|
+
export * from './lazyProp.js';
|
|
57
|
+
export * from './lazyRecord.js';
|
|
55
58
|
export * from './looksLikeURL.js';
|
|
56
59
|
export * from './memo.js';
|
|
57
60
|
export * from './normalize.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
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a property descriptor that lazily computes its value.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* const data = {};
|
|
7
|
+
*
|
|
8
|
+
* Object.defineProperty(data, 'value', lazyProp('value', () => {
|
|
9
|
+
* return Math.random();
|
|
10
|
+
* }));
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export function lazyProp(key, getter) {
|
|
14
|
+
return {
|
|
15
|
+
configurable: true,
|
|
16
|
+
get() {
|
|
17
|
+
const value = getter();
|
|
18
|
+
Object.defineProperty(this, key, {
|
|
19
|
+
value,
|
|
20
|
+
writable: true,
|
|
21
|
+
configurable: true,
|
|
22
|
+
});
|
|
23
|
+
return value;
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { lazyProp } from './lazyProp.js';
|
|
2
|
+
/**
|
|
3
|
+
* Create an object whose properties are computed lazily.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* const data = lazyRecord({
|
|
8
|
+
* name: 'Name',
|
|
9
|
+
* now() {
|
|
10
|
+
* return Date.now();
|
|
11
|
+
* },
|
|
12
|
+
* });
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function lazyRecord(properties) {
|
|
16
|
+
const record = Object.create(null);
|
|
17
|
+
for (const [key, getter] of Object.entries(properties)) {
|
|
18
|
+
Object.defineProperty(record, key, typeof getter === 'function'
|
|
19
|
+
? lazyProp(key, getter)
|
|
20
|
+
: {
|
|
21
|
+
value: getter,
|
|
22
|
+
writable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return record;
|
|
27
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -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,12 +44,15 @@ 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';
|
|
50
51
|
export * from './iterateForever.js';
|
|
51
52
|
export * from './joinStrings.js';
|
|
52
53
|
export * from './jsonParse.js';
|
|
54
|
+
export * from './lazyProp.js';
|
|
55
|
+
export * from './lazyRecord.js';
|
|
53
56
|
export * from './looksLikeURL.js';
|
|
54
57
|
export * from './memo.js';
|
|
55
58
|
export * from './normalize.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;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a property descriptor that lazily computes its value.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* const data = {};
|
|
7
|
+
*
|
|
8
|
+
* Object.defineProperty(data, 'value', lazyProp('value', () => {
|
|
9
|
+
* return Math.random();
|
|
10
|
+
* }));
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare function lazyProp<Key extends PropertyKey, Value>(key: Key, getter: () => Value): TypedPropertyDescriptor<Value>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AnyFunction } from './types/common.js';
|
|
2
|
+
import type { UnknownRecord } from './types/records.js';
|
|
3
|
+
export type LazyRecordProperties<Type extends UnknownRecord> = {
|
|
4
|
+
[Key in keyof Type]: Exclude<Type[Key], AnyFunction> | (() => Type[Key]);
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Create an object whose properties are computed lazily.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const data = lazyRecord({
|
|
12
|
+
* name: 'Name',
|
|
13
|
+
* now() {
|
|
14
|
+
* return Date.now();
|
|
15
|
+
* },
|
|
16
|
+
* });
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function lazyRecord<Type extends UnknownRecord>(properties: LazyRecordProperties<Type>): Type;
|
|
@@ -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>;
|
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.6.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.0.0 <13.0.0",
|
|
29
|
+
"onFail": "warn"
|
|
30
|
+
},
|
|
31
|
+
"runtime": [
|
|
32
|
+
{
|
|
33
|
+
"name": "node",
|
|
34
|
+
"version": "^26.10.0",
|
|
35
|
+
"onFail": "download"
|
|
36
|
+
}
|
|
37
|
+
]
|
|
38
|
+
},
|
|
25
39
|
"exports": {
|
|
26
40
|
".": {
|
|
27
41
|
"types": "./dist/types/index.d.ts",
|
|
@@ -66,7 +80,6 @@
|
|
|
66
80
|
"url": "https://github.com/webdeveric/utils/issues"
|
|
67
81
|
},
|
|
68
82
|
"homepage": "https://github.com/webdeveric/utils/#readme",
|
|
69
|
-
"packageManager": "pnpm@11.22.0+sha512.1ff870c4c6133dfd88fb2afc46dd13d47f09c9794b438c6fdb47ca98caf3bc16381ee0be93a091b8e3824cf01f889f46d7d9e20910fb0be1ab0fb5baa80dd621",
|
|
70
83
|
"scripts": {
|
|
71
84
|
"clean": "rimraf ./dist/ ./cache/ ./coverage/",
|
|
72
85
|
"prebuild": "pnpm clean",
|
|
@@ -78,7 +91,7 @@
|
|
|
78
91
|
"bench": "vitest bench",
|
|
79
92
|
"test": "vitest --typecheck",
|
|
80
93
|
"coverage": "vitest run --coverage --typecheck",
|
|
81
|
-
"validate": "validate-package-exports --check",
|
|
94
|
+
"validate": "validate-package-exports --check --strict-module-type",
|
|
82
95
|
"spellcheck": "cspell './{.github,src,bench,test}/**/*.{ts,json}' './*.{md,mjs,mts}' './package.json' --no-progress",
|
|
83
96
|
"prepublishOnly": "pnpm typecheck && pnpm spellcheck && pnpm lint && pnpm coverage && pnpm build",
|
|
84
97
|
"format": "prettier --write ./*.{js,json,md} ./src/ ./bench/ ./test/",
|
|
@@ -86,31 +99,31 @@
|
|
|
86
99
|
},
|
|
87
100
|
"prettier": "@webdeveric/prettier-config",
|
|
88
101
|
"devDependencies": {
|
|
89
|
-
"@commitlint/config-conventional": "^21.2.
|
|
90
|
-
"@commitlint/types": "^21.2.
|
|
91
|
-
"@types/node": "^24.
|
|
102
|
+
"@commitlint/config-conventional": "^21.2.3",
|
|
103
|
+
"@commitlint/types": "^21.2.3",
|
|
104
|
+
"@types/node": "^24.19.0",
|
|
92
105
|
"@typescript/native": "npm:typescript@^7.0.2",
|
|
93
106
|
"@vitest/coverage-v8": "^4.1.11",
|
|
94
107
|
"@webdeveric/eslint-config-ts": "^0.12.1",
|
|
95
108
|
"@webdeveric/prettier-config": "^0.4.0",
|
|
96
109
|
"@webdeveric/ts-data-structures": "^1.0.1",
|
|
97
|
-
"commitlint": "^21.2.
|
|
98
|
-
"commitlint-plugin-cspell": "^0.9.
|
|
110
|
+
"commitlint": "^21.2.3",
|
|
111
|
+
"commitlint-plugin-cspell": "^0.9.5",
|
|
99
112
|
"conventional-changelog-conventionalcommits": "^9.3.1",
|
|
100
|
-
"cspell": "^10.
|
|
113
|
+
"cspell": "^10.3.5",
|
|
101
114
|
"eslint": "^8.57.1",
|
|
102
115
|
"eslint-config-prettier": "^10.1.8",
|
|
103
116
|
"eslint-import-resolver-typescript": "^4.4.5",
|
|
104
117
|
"eslint-plugin-import": "^2.32.0",
|
|
105
118
|
"husky": "^9.1.7",
|
|
106
|
-
"jsdom": "^30.
|
|
107
|
-
"lint-staged": "^17.
|
|
108
|
-
"prettier": "^3.9.
|
|
119
|
+
"jsdom": "^30.1.1",
|
|
120
|
+
"lint-staged": "^17.6.0",
|
|
121
|
+
"prettier": "^3.9.9",
|
|
109
122
|
"rimraf": "^6.1.3",
|
|
110
123
|
"semantic-release": "^25.0.9",
|
|
111
124
|
"typedoc": "^0.28.20",
|
|
112
125
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
113
|
-
"validate-package-exports": "^1.
|
|
126
|
+
"validate-package-exports": "^1.5.0",
|
|
114
127
|
"vitest": "^4.1.11"
|
|
115
128
|
}
|
|
116
129
|
}
|