@webdeveric/utils 1.3.0 → 1.5.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/codePointLength.js +6 -1
- package/dist/cjs/combinations.js +1 -3
- package/dist/cjs/convert.js +4 -1
- package/dist/cjs/deepFreeze.js +1 -1
- package/dist/cjs/findIndentation.js +3 -3
- package/dist/cjs/getOwnPaths.js +3 -1
- package/dist/cjs/getPaths.js +3 -1
- package/dist/cjs/indent.js +2 -2
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/lazyProp.js +29 -0
- package/dist/cjs/lazyRecord.js +30 -0
- package/dist/cjs/normalize.js +4 -1
- package/dist/cjs/pathParts.js +2 -2
- package/dist/cjs/predicate/isPrototypePollutionPropertyKey.js +6 -0
- package/dist/cjs/set.js +2 -1
- package/dist/cjs/trimIndentation.js +2 -1
- package/dist/mjs/codePointLength.js +6 -1
- package/dist/mjs/combinations.js +1 -3
- package/dist/mjs/convert.js +4 -1
- package/dist/mjs/deepFreeze.js +1 -1
- package/dist/mjs/findIndentation.js +3 -3
- package/dist/mjs/getOwnPaths.js +3 -1
- package/dist/mjs/getPaths.js +3 -1
- package/dist/mjs/indent.js +2 -2
- package/dist/mjs/index.js +2 -0
- package/dist/mjs/lazyProp.js +26 -0
- package/dist/mjs/lazyRecord.js +27 -0
- package/dist/mjs/normalize.js +4 -1
- package/dist/mjs/pathParts.js +2 -2
- package/dist/mjs/predicate/isPrototypePollutionPropertyKey.js +3 -0
- package/dist/mjs/set.js +2 -1
- package/dist/mjs/trimIndentation.js +2 -1
- package/dist/types/index.d.ts +2 -0
- package/dist/types/lazyProp.d.ts +13 -0
- package/dist/types/lazyRecord.d.ts +19 -0
- package/dist/types/predicate/isPrototypePollutionPropertyKey.d.ts +2 -0
- package/package.json +10 -10
|
@@ -12,5 +12,10 @@ exports.codePointLength = codePointLength;
|
|
|
12
12
|
* ```
|
|
13
13
|
*/
|
|
14
14
|
function codePointLength(input) {
|
|
15
|
-
|
|
15
|
+
let count = 0;
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
17
|
+
for (const _ of input) {
|
|
18
|
+
count++;
|
|
19
|
+
}
|
|
20
|
+
return count;
|
|
16
21
|
}
|
package/dist/cjs/combinations.js
CHANGED
|
@@ -16,9 +16,7 @@ const isObject_js_1 = require("./predicate/isObject.js");
|
|
|
16
16
|
function* combinations(input) {
|
|
17
17
|
const propertyNames = Object.keys(input);
|
|
18
18
|
const propertyValues = Object.values(input).map((value) => (0, isIterable_js_1.isIterable)(value)
|
|
19
|
-
? Array.from(value, (item)
|
|
20
|
-
return (0, isObject_js_1.isObject)(item) ? [...combinations(item)] : (0, asArray_js_1.asArray)(item);
|
|
21
|
-
}).flat()
|
|
19
|
+
? Array.from(value).flatMap((item) => ((0, isObject_js_1.isObject)(item) ? [...combinations(item)] : (0, asArray_js_1.asArray)(item)))
|
|
22
20
|
: (0, isObject_js_1.isObject)(value)
|
|
23
21
|
? [...combinations(value)]
|
|
24
22
|
: (0, asArray_js_1.asArray)(value));
|
package/dist/cjs/convert.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.convert = void 0;
|
|
4
4
|
const getOwnProperties_js_1 = require("./getOwnProperties.js");
|
|
5
5
|
const getType_js_1 = require("./getType.js");
|
|
6
|
+
const isPrototypePollutionPropertyKey_js_1 = require("./predicate/isPrototypePollutionPropertyKey.js");
|
|
6
7
|
/**
|
|
7
8
|
* Convert `input` using `converter`, which may be a function or a record of nested converters.
|
|
8
9
|
*
|
|
@@ -16,7 +17,9 @@ const convert = (input, converter) => {
|
|
|
16
17
|
return converter(input);
|
|
17
18
|
}
|
|
18
19
|
if (typeof converter === 'object' && converter !== null) {
|
|
19
|
-
return (0, getOwnProperties_js_1.getOwnProperties)(converter)
|
|
20
|
+
return (0, getOwnProperties_js_1.getOwnProperties)(converter)
|
|
21
|
+
.filter((key) => !(0, isPrototypePollutionPropertyKey_js_1.isPrototypePollutionPropertyKey)(key))
|
|
22
|
+
.reduce((data, key) => {
|
|
20
23
|
data[key] = (0, exports.convert)(input, converter[key]);
|
|
21
24
|
return data;
|
|
22
25
|
}, {});
|
package/dist/cjs/deepFreeze.js
CHANGED
|
@@ -15,7 +15,7 @@ exports.deepFreeze = deepFreeze;
|
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
17
|
function deepFreeze(input, partialOptions = {}) {
|
|
18
|
-
const { filterProperties = new Set(['
|
|
18
|
+
const { filterProperties = new Set(['__proto__', 'constructor', 'prototype']), doNotFreeze = new WeakSet([globalThis]), frozen = new WeakSet(), } = partialOptions;
|
|
19
19
|
if (doNotFreeze.has(input)) {
|
|
20
20
|
return input;
|
|
21
21
|
}
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.findIndentation = findIndentation;
|
|
4
4
|
const constants_js_1 = require("./constants.js");
|
|
5
|
+
const blankLinePattern = /^\s*$/;
|
|
6
|
+
const leadingWhitespacePattern = new RegExp(String.raw `^(?:${constants_js_1.NEWLINE_RAW_PATTERN})?(?<whiteSpace>${constants_js_1.INDENT_CHAR_RAW_PATTERN}+)`);
|
|
5
7
|
/**
|
|
6
8
|
* Find indentation on first line that isn't whitespace only.
|
|
7
9
|
*
|
|
@@ -11,7 +13,5 @@ const constants_js_1 = require("./constants.js");
|
|
|
11
13
|
* ```
|
|
12
14
|
*/
|
|
13
15
|
function findIndentation(lines) {
|
|
14
|
-
return lines
|
|
15
|
-
.find((line) => !/^\s*$/.test(line))
|
|
16
|
-
?.match(new RegExp(String.raw `^(?:${constants_js_1.NEWLINE_RAW_PATTERN})?(?<whiteSpace>${constants_js_1.INDENT_CHAR_RAW_PATTERN}+)`))?.groups?.['whiteSpace'];
|
|
16
|
+
return lines.find((line) => !blankLinePattern.test(line))?.match(leadingWhitespacePattern)?.groups?.['whiteSpace'];
|
|
17
17
|
}
|
package/dist/cjs/getOwnPaths.js
CHANGED
|
@@ -13,7 +13,9 @@ const getOwnPaths = (input) => {
|
|
|
13
13
|
return Object.entries(input).reduce((paths, [key, value]) => {
|
|
14
14
|
paths.push(key);
|
|
15
15
|
if (value && typeof value === 'object') {
|
|
16
|
-
|
|
16
|
+
for (const childKey of (0, exports.getOwnPaths)(value)) {
|
|
17
|
+
paths.push(`${key}.${childKey}`);
|
|
18
|
+
}
|
|
17
19
|
}
|
|
18
20
|
return paths;
|
|
19
21
|
}, []);
|
package/dist/cjs/getPaths.js
CHANGED
|
@@ -15,7 +15,9 @@ const getPaths = (input) => {
|
|
|
15
15
|
paths.push(key);
|
|
16
16
|
const value = input[key];
|
|
17
17
|
if (value && typeof value === 'object') {
|
|
18
|
-
|
|
18
|
+
for (const childKey of (0, exports.getPaths)(value)) {
|
|
19
|
+
paths.push(`${key}.${childKey}`);
|
|
20
|
+
}
|
|
19
21
|
}
|
|
20
22
|
}
|
|
21
23
|
return paths;
|
package/dist/cjs/indent.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.indent = indent;
|
|
4
4
|
const constants_js_1 = require("./constants.js");
|
|
5
|
+
const newLinePattern = new RegExp(String.raw `(?<newLine>${constants_js_1.NEWLINE_RAW_PATTERN})`, 'g');
|
|
5
6
|
/**
|
|
6
7
|
* Add `indent` before each line in `input`.
|
|
7
8
|
*
|
|
@@ -11,6 +12,5 @@ const constants_js_1 = require("./constants.js");
|
|
|
11
12
|
* ```
|
|
12
13
|
*/
|
|
13
14
|
function indent(input, indentation) {
|
|
14
|
-
return
|
|
15
|
-
input.replaceAll(new RegExp(String.raw `(?<newLine>${constants_js_1.NEWLINE_RAW_PATTERN})`, 'g'), `$<newLine>${indentation}`));
|
|
15
|
+
return indentation + input.replaceAll(newLinePattern, `$<newLine>${indentation}`);
|
|
16
16
|
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -68,6 +68,8 @@ __exportStar(require("./isEmpty.js"), exports);
|
|
|
68
68
|
__exportStar(require("./iterateForever.js"), exports);
|
|
69
69
|
__exportStar(require("./joinStrings.js"), exports);
|
|
70
70
|
__exportStar(require("./jsonParse.js"), exports);
|
|
71
|
+
__exportStar(require("./lazyProp.js"), exports);
|
|
72
|
+
__exportStar(require("./lazyRecord.js"), exports);
|
|
71
73
|
__exportStar(require("./looksLikeURL.js"), exports);
|
|
72
74
|
__exportStar(require("./memo.js"), exports);
|
|
73
75
|
__exportStar(require("./normalize.js"), exports);
|
|
@@ -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
|
+
}
|
package/dist/cjs/normalize.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.normalize = normalize;
|
|
4
4
|
const getOwnProperties_js_1 = require("./getOwnProperties.js");
|
|
5
5
|
const isObject_js_1 = require("./predicate/isObject.js");
|
|
6
|
+
const isPrototypePollutionPropertyKey_js_1 = require("./predicate/isPrototypePollutionPropertyKey.js");
|
|
6
7
|
/**
|
|
7
8
|
* Recursively transform the properties of `input` using `normalizers`.
|
|
8
9
|
*
|
|
@@ -31,7 +32,9 @@ function normalize(input, normalizers, initContextData = () => ({})) {
|
|
|
31
32
|
return currentNormalizers(currentRecord, context);
|
|
32
33
|
}
|
|
33
34
|
if ((0, isObject_js_1.isObject)(currentNormalizers) && (0, isObject_js_1.isObject)(currentRecord)) {
|
|
34
|
-
return (0, getOwnProperties_js_1.getOwnProperties)(currentNormalizers)
|
|
35
|
+
return (0, getOwnProperties_js_1.getOwnProperties)(currentNormalizers)
|
|
36
|
+
.filter((key) => !(0, isPrototypePollutionPropertyKey_js_1.isPrototypePollutionPropertyKey)(key))
|
|
37
|
+
.reduce((data, key) => {
|
|
35
38
|
const innerNormalizers = currentNormalizers[key];
|
|
36
39
|
// If the value is undefined but there is a normalizer record, set value to empty object.
|
|
37
40
|
const value = typeof data[key] === 'undefined' && (0, isObject_js_1.isObject)(innerNormalizers)
|
package/dist/cjs/pathParts.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.pathParts = pathParts;
|
|
4
|
+
const wordPattern = /\w+/g;
|
|
4
5
|
/**
|
|
5
6
|
* Yield each part of `input`, splitting a string path on non-word characters.
|
|
6
7
|
*
|
|
@@ -17,8 +18,7 @@ function* pathParts(input) {
|
|
|
17
18
|
yield input;
|
|
18
19
|
return;
|
|
19
20
|
}
|
|
20
|
-
const
|
|
21
|
-
const iterator = input.matchAll(regexp);
|
|
21
|
+
const iterator = input.matchAll(wordPattern);
|
|
22
22
|
for (const part of iterator) {
|
|
23
23
|
yield part[0];
|
|
24
24
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isPrototypePollutionPropertyKey = isPrototypePollutionPropertyKey;
|
|
4
|
+
function isPrototypePollutionPropertyKey(key) {
|
|
5
|
+
return typeof key === 'string' && (key === '__proto__' || key === 'constructor' || key === 'prototype');
|
|
6
|
+
}
|
package/dist/cjs/set.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.set = set;
|
|
4
4
|
const pathParts_js_1 = require("./pathParts.js");
|
|
5
|
+
const prototypePollutionPattern = /\b(__proto__|constructor|prototype)\b/;
|
|
5
6
|
/**
|
|
6
7
|
* Set the value of a property
|
|
7
8
|
*
|
|
@@ -17,7 +18,7 @@ function set(input, path, value) {
|
|
|
17
18
|
if (path === '') {
|
|
18
19
|
throw new Error('Path cannot be an empty string');
|
|
19
20
|
}
|
|
20
|
-
if (typeof path === 'string' &&
|
|
21
|
+
if (typeof path === 'string' && prototypePollutionPattern.test(path)) {
|
|
21
22
|
throw new Error('Cannot pollute prototype');
|
|
22
23
|
}
|
|
23
24
|
const parts = Array.from((0, pathParts_js_1.pathParts)(path));
|
|
@@ -4,6 +4,7 @@ exports.trimIndentation = trimIndentation;
|
|
|
4
4
|
const constants_js_1 = require("./constants.js");
|
|
5
5
|
const findIndentation_js_1 = require("./findIndentation.js");
|
|
6
6
|
const stripIndent_js_1 = require("./stripIndent.js");
|
|
7
|
+
const newLinePattern = new RegExp(constants_js_1.NEWLINE_RAW_PATTERN);
|
|
7
8
|
/**
|
|
8
9
|
* Remove common leading indentation from every line in `input` and trim the result.
|
|
9
10
|
*
|
|
@@ -13,6 +14,6 @@ const stripIndent_js_1 = require("./stripIndent.js");
|
|
|
13
14
|
* ```
|
|
14
15
|
*/
|
|
15
16
|
function trimIndentation(input) {
|
|
16
|
-
const whiteSpace = (0, findIndentation_js_1.findIndentation)(input.split(
|
|
17
|
+
const whiteSpace = (0, findIndentation_js_1.findIndentation)(input.split(newLinePattern));
|
|
17
18
|
return (whiteSpace ? (0, stripIndent_js_1.stripIndent)(input, whiteSpace) : input).trim();
|
|
18
19
|
}
|
package/dist/mjs/combinations.js
CHANGED
|
@@ -13,9 +13,7 @@ import { isObject } from './predicate/isObject.js';
|
|
|
13
13
|
export function* combinations(input) {
|
|
14
14
|
const propertyNames = Object.keys(input);
|
|
15
15
|
const propertyValues = Object.values(input).map((value) => isIterable(value)
|
|
16
|
-
? Array.from(value
|
|
17
|
-
return isObject(item) ? [...combinations(item)] : asArray(item);
|
|
18
|
-
}).flat()
|
|
16
|
+
? Array.from(value).flatMap((item) => (isObject(item) ? [...combinations(item)] : asArray(item)))
|
|
19
17
|
: isObject(value)
|
|
20
18
|
? [...combinations(value)]
|
|
21
19
|
: asArray(value));
|
package/dist/mjs/convert.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { getOwnProperties } from './getOwnProperties.js';
|
|
2
2
|
import { getType } from './getType.js';
|
|
3
|
+
import { isPrototypePollutionPropertyKey } from './predicate/isPrototypePollutionPropertyKey.js';
|
|
3
4
|
/**
|
|
4
5
|
* Convert `input` using `converter`, which may be a function or a record of nested converters.
|
|
5
6
|
*
|
|
@@ -13,7 +14,9 @@ export const convert = (input, converter) => {
|
|
|
13
14
|
return converter(input);
|
|
14
15
|
}
|
|
15
16
|
if (typeof converter === 'object' && converter !== null) {
|
|
16
|
-
return getOwnProperties(converter)
|
|
17
|
+
return getOwnProperties(converter)
|
|
18
|
+
.filter((key) => !isPrototypePollutionPropertyKey(key))
|
|
19
|
+
.reduce((data, key) => {
|
|
17
20
|
data[key] = convert(input, converter[key]);
|
|
18
21
|
return data;
|
|
19
22
|
}, {});
|
package/dist/mjs/deepFreeze.js
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* ```
|
|
13
13
|
*/
|
|
14
14
|
export function deepFreeze(input, partialOptions = {}) {
|
|
15
|
-
const { filterProperties = new Set(['
|
|
15
|
+
const { filterProperties = new Set(['__proto__', 'constructor', 'prototype']), doNotFreeze = new WeakSet([globalThis]), frozen = new WeakSet(), } = partialOptions;
|
|
16
16
|
if (doNotFreeze.has(input)) {
|
|
17
17
|
return input;
|
|
18
18
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { INDENT_CHAR_RAW_PATTERN, NEWLINE_RAW_PATTERN } from './constants.js';
|
|
2
|
+
const blankLinePattern = /^\s*$/;
|
|
3
|
+
const leadingWhitespacePattern = new RegExp(String.raw `^(?:${NEWLINE_RAW_PATTERN})?(?<whiteSpace>${INDENT_CHAR_RAW_PATTERN}+)`);
|
|
2
4
|
/**
|
|
3
5
|
* Find indentation on first line that isn't whitespace only.
|
|
4
6
|
*
|
|
@@ -8,7 +10,5 @@ import { INDENT_CHAR_RAW_PATTERN, NEWLINE_RAW_PATTERN } from './constants.js';
|
|
|
8
10
|
* ```
|
|
9
11
|
*/
|
|
10
12
|
export function findIndentation(lines) {
|
|
11
|
-
return lines
|
|
12
|
-
.find((line) => !/^\s*$/.test(line))
|
|
13
|
-
?.match(new RegExp(String.raw `^(?:${NEWLINE_RAW_PATTERN})?(?<whiteSpace>${INDENT_CHAR_RAW_PATTERN}+)`))?.groups?.['whiteSpace'];
|
|
13
|
+
return lines.find((line) => !blankLinePattern.test(line))?.match(leadingWhitespacePattern)?.groups?.['whiteSpace'];
|
|
14
14
|
}
|
package/dist/mjs/getOwnPaths.js
CHANGED
|
@@ -10,7 +10,9 @@ export const getOwnPaths = (input) => {
|
|
|
10
10
|
return Object.entries(input).reduce((paths, [key, value]) => {
|
|
11
11
|
paths.push(key);
|
|
12
12
|
if (value && typeof value === 'object') {
|
|
13
|
-
|
|
13
|
+
for (const childKey of getOwnPaths(value)) {
|
|
14
|
+
paths.push(`${key}.${childKey}`);
|
|
15
|
+
}
|
|
14
16
|
}
|
|
15
17
|
return paths;
|
|
16
18
|
}, []);
|
package/dist/mjs/getPaths.js
CHANGED
|
@@ -12,7 +12,9 @@ export const getPaths = (input) => {
|
|
|
12
12
|
paths.push(key);
|
|
13
13
|
const value = input[key];
|
|
14
14
|
if (value && typeof value === 'object') {
|
|
15
|
-
|
|
15
|
+
for (const childKey of getPaths(value)) {
|
|
16
|
+
paths.push(`${key}.${childKey}`);
|
|
17
|
+
}
|
|
16
18
|
}
|
|
17
19
|
}
|
|
18
20
|
return paths;
|
package/dist/mjs/indent.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { NEWLINE_RAW_PATTERN } from './constants.js';
|
|
2
|
+
const newLinePattern = new RegExp(String.raw `(?<newLine>${NEWLINE_RAW_PATTERN})`, 'g');
|
|
2
3
|
/**
|
|
3
4
|
* Add `indent` before each line in `input`.
|
|
4
5
|
*
|
|
@@ -8,6 +9,5 @@ import { NEWLINE_RAW_PATTERN } from './constants.js';
|
|
|
8
9
|
* ```
|
|
9
10
|
*/
|
|
10
11
|
export function indent(input, indentation) {
|
|
11
|
-
return
|
|
12
|
-
input.replaceAll(new RegExp(String.raw `(?<newLine>${NEWLINE_RAW_PATTERN})`, 'g'), `$<newLine>${indentation}`));
|
|
12
|
+
return indentation + input.replaceAll(newLinePattern, `$<newLine>${indentation}`);
|
|
13
13
|
}
|
package/dist/mjs/index.js
CHANGED
|
@@ -52,6 +52,8 @@ export * from './isEmpty.js';
|
|
|
52
52
|
export * from './iterateForever.js';
|
|
53
53
|
export * from './joinStrings.js';
|
|
54
54
|
export * from './jsonParse.js';
|
|
55
|
+
export * from './lazyProp.js';
|
|
56
|
+
export * from './lazyRecord.js';
|
|
55
57
|
export * from './looksLikeURL.js';
|
|
56
58
|
export * from './memo.js';
|
|
57
59
|
export * from './normalize.js';
|
|
@@ -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
|
+
}
|
package/dist/mjs/normalize.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { getOwnProperties } from './getOwnProperties.js';
|
|
2
2
|
import { isObject } from './predicate/isObject.js';
|
|
3
|
+
import { isPrototypePollutionPropertyKey } from './predicate/isPrototypePollutionPropertyKey.js';
|
|
3
4
|
/**
|
|
4
5
|
* Recursively transform the properties of `input` using `normalizers`.
|
|
5
6
|
*
|
|
@@ -28,7 +29,9 @@ export function normalize(input, normalizers, initContextData = () => ({})) {
|
|
|
28
29
|
return currentNormalizers(currentRecord, context);
|
|
29
30
|
}
|
|
30
31
|
if (isObject(currentNormalizers) && isObject(currentRecord)) {
|
|
31
|
-
return getOwnProperties(currentNormalizers)
|
|
32
|
+
return getOwnProperties(currentNormalizers)
|
|
33
|
+
.filter((key) => !isPrototypePollutionPropertyKey(key))
|
|
34
|
+
.reduce((data, key) => {
|
|
32
35
|
const innerNormalizers = currentNormalizers[key];
|
|
33
36
|
// If the value is undefined but there is a normalizer record, set value to empty object.
|
|
34
37
|
const value = typeof data[key] === 'undefined' && isObject(innerNormalizers)
|
package/dist/mjs/pathParts.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const wordPattern = /\w+/g;
|
|
1
2
|
/**
|
|
2
3
|
* Yield each part of `input`, splitting a string path on non-word characters.
|
|
3
4
|
*
|
|
@@ -14,8 +15,7 @@ export function* pathParts(input) {
|
|
|
14
15
|
yield input;
|
|
15
16
|
return;
|
|
16
17
|
}
|
|
17
|
-
const
|
|
18
|
-
const iterator = input.matchAll(regexp);
|
|
18
|
+
const iterator = input.matchAll(wordPattern);
|
|
19
19
|
for (const part of iterator) {
|
|
20
20
|
yield part[0];
|
|
21
21
|
}
|
package/dist/mjs/set.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { pathParts } from './pathParts.js';
|
|
2
|
+
const prototypePollutionPattern = /\b(__proto__|constructor|prototype)\b/;
|
|
2
3
|
/**
|
|
3
4
|
* Set the value of a property
|
|
4
5
|
*
|
|
@@ -14,7 +15,7 @@ export function set(input, path, value) {
|
|
|
14
15
|
if (path === '') {
|
|
15
16
|
throw new Error('Path cannot be an empty string');
|
|
16
17
|
}
|
|
17
|
-
if (typeof path === 'string' &&
|
|
18
|
+
if (typeof path === 'string' && prototypePollutionPattern.test(path)) {
|
|
18
19
|
throw new Error('Cannot pollute prototype');
|
|
19
20
|
}
|
|
20
21
|
const parts = Array.from(pathParts(path));
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { NEWLINE_RAW_PATTERN } from './constants.js';
|
|
2
2
|
import { findIndentation } from './findIndentation.js';
|
|
3
3
|
import { stripIndent } from './stripIndent.js';
|
|
4
|
+
const newLinePattern = new RegExp(NEWLINE_RAW_PATTERN);
|
|
4
5
|
/**
|
|
5
6
|
* Remove common leading indentation from every line in `input` and trim the result.
|
|
6
7
|
*
|
|
@@ -10,6 +11,6 @@ import { stripIndent } from './stripIndent.js';
|
|
|
10
11
|
* ```
|
|
11
12
|
*/
|
|
12
13
|
export function trimIndentation(input) {
|
|
13
|
-
const whiteSpace = findIndentation(input.split(
|
|
14
|
+
const whiteSpace = findIndentation(input.split(newLinePattern));
|
|
14
15
|
return (whiteSpace ? stripIndent(input, whiteSpace) : input).trim();
|
|
15
16
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -50,6 +50,8 @@ export * from './isEmpty.js';
|
|
|
50
50
|
export * from './iterateForever.js';
|
|
51
51
|
export * from './joinStrings.js';
|
|
52
52
|
export * from './jsonParse.js';
|
|
53
|
+
export * from './lazyProp.js';
|
|
54
|
+
export * from './lazyRecord.js';
|
|
53
55
|
export * from './looksLikeURL.js';
|
|
54
56
|
export * from './memo.js';
|
|
55
57
|
export * from './normalize.js';
|
|
@@ -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;
|
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.5.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@11.
|
|
69
|
+
"packageManager": "pnpm@11.25.0+sha512.5cde925b4f075f725eb71fbae18a42ffe784524789f19b61c731cb8721ec28aaee160e01a8d5af4fedb2a42cdbf300efe23db356b0d4a17b4d63e11f8ab7c956",
|
|
70
70
|
"scripts": {
|
|
71
71
|
"clean": "rimraf ./dist/ ./cache/ ./coverage/",
|
|
72
72
|
"prebuild": "pnpm clean",
|
|
@@ -86,31 +86,31 @@
|
|
|
86
86
|
},
|
|
87
87
|
"prettier": "@webdeveric/prettier-config",
|
|
88
88
|
"devDependencies": {
|
|
89
|
-
"@commitlint/config-conventional": "^21.2.
|
|
89
|
+
"@commitlint/config-conventional": "^21.2.2",
|
|
90
90
|
"@commitlint/types": "^21.2.0",
|
|
91
91
|
"@types/node": "^24.13.3",
|
|
92
92
|
"@typescript/native": "npm:typescript@^7.0.2",
|
|
93
|
-
"@vitest/coverage-v8": "^4.1.
|
|
93
|
+
"@vitest/coverage-v8": "^4.1.11",
|
|
94
94
|
"@webdeveric/eslint-config-ts": "^0.12.1",
|
|
95
95
|
"@webdeveric/prettier-config": "^0.4.0",
|
|
96
96
|
"@webdeveric/ts-data-structures": "^1.0.1",
|
|
97
|
-
"commitlint": "^21.2.
|
|
97
|
+
"commitlint": "^21.2.2",
|
|
98
98
|
"commitlint-plugin-cspell": "^0.9.4",
|
|
99
99
|
"conventional-changelog-conventionalcommits": "^9.3.1",
|
|
100
|
-
"cspell": "^10.
|
|
100
|
+
"cspell": "^10.1.1",
|
|
101
101
|
"eslint": "^8.57.1",
|
|
102
102
|
"eslint-config-prettier": "^10.1.8",
|
|
103
103
|
"eslint-import-resolver-typescript": "^4.4.5",
|
|
104
104
|
"eslint-plugin-import": "^2.32.0",
|
|
105
105
|
"husky": "^9.1.7",
|
|
106
106
|
"jsdom": "^30.0.1",
|
|
107
|
-
"lint-staged": "^17.
|
|
107
|
+
"lint-staged": "^17.4.1",
|
|
108
108
|
"prettier": "^3.9.6",
|
|
109
109
|
"rimraf": "^6.1.3",
|
|
110
|
-
"semantic-release": "^25.0.
|
|
110
|
+
"semantic-release": "^25.0.9",
|
|
111
111
|
"typedoc": "^0.28.20",
|
|
112
112
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
113
|
-
"validate-package-exports": "^1.
|
|
114
|
-
"vitest": "^4.1.
|
|
113
|
+
"validate-package-exports": "^1.4.0",
|
|
114
|
+
"vitest": "^4.1.11"
|
|
115
115
|
}
|
|
116
116
|
}
|