@webdeveric/utils 1.2.0 → 1.4.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/defineProperties.js +21 -0
- package/dist/cjs/defineProperty.js +20 -0
- package/dist/cjs/findIndentation.js +3 -3
- package/dist/cjs/getOwnPaths.js +3 -1
- package/dist/cjs/getPaths.js +3 -1
- package/dist/cjs/graphemeLength.js +1 -1
- package/dist/cjs/indent.js +2 -2
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/normalize.js +4 -1
- package/dist/cjs/pathParts.js +2 -2
- package/dist/cjs/predicate/assume.js +1 -1
- 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/defineProperties.js +18 -0
- package/dist/mjs/defineProperty.js +17 -0
- package/dist/mjs/findIndentation.js +3 -3
- package/dist/mjs/getOwnPaths.js +3 -1
- package/dist/mjs/getPaths.js +3 -1
- package/dist/mjs/graphemeLength.js +1 -1
- package/dist/mjs/indent.js +2 -2
- package/dist/mjs/index.js +2 -0
- package/dist/mjs/normalize.js +4 -1
- package/dist/mjs/pathParts.js +2 -2
- package/dist/mjs/predicate/assume.js +1 -1
- 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/defineProperties.d.ts +21 -0
- package/dist/types/defineProperty.d.ts +18 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/predicate/isPrototypePollutionPropertyKey.d.ts +2 -0
- package/dist/types/types/objects.d.ts +33 -1
- package/dist/types/types/utils.d.ts +8 -0
- package/package.json +16 -15
|
@@ -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
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defineProperties = defineProperties;
|
|
4
|
+
/**
|
|
5
|
+
* Type-safe wrapper around `Object.defineProperties()` that reflects the new properties in the returned type.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const user = defineProperties({ name: 'Test' }, {
|
|
10
|
+
* age: { value: 42 },
|
|
11
|
+
* isAdmin: { get: () => false },
|
|
12
|
+
* title: {
|
|
13
|
+
* writable: true,
|
|
14
|
+
* },
|
|
15
|
+
* });
|
|
16
|
+
* // user: { name: string, readonly age: number, readonly isAdmin: boolean, title: unknown }
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
function defineProperties(input, attributeMap) {
|
|
20
|
+
return Object.defineProperties(input, attributeMap);
|
|
21
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defineProperty = defineProperty;
|
|
4
|
+
/**
|
|
5
|
+
* Type-safe wrapper around `Object.defineProperty` that reflects the new property in the returned type.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const user = defineProperty({ name: 'Test' }, 'age', { value: 42 });
|
|
10
|
+
* // user: { name: string, readonly age: number }
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const user = defineProperty({ name: 'Test' }, 'title', { writable: true });
|
|
15
|
+
* // user: { name: string, title: unknown }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function defineProperty(input, property, attributes) {
|
|
19
|
+
return Object.defineProperty(input, property, attributes);
|
|
20
|
+
}
|
|
@@ -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;
|
|
@@ -14,7 +14,7 @@ const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
|
|
|
14
14
|
*/
|
|
15
15
|
function graphemeLength(input) {
|
|
16
16
|
let count = 0;
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
18
18
|
for (const _ of segmenter.segment(input)) {
|
|
19
19
|
count++;
|
|
20
20
|
}
|
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
|
@@ -37,6 +37,8 @@ __exportStar(require("./counter.js"), exports);
|
|
|
37
37
|
__exportStar(require("./dedent.js"), exports);
|
|
38
38
|
__exportStar(require("./deepDecodeURI.js"), exports);
|
|
39
39
|
__exportStar(require("./deepFreeze.js"), exports);
|
|
40
|
+
__exportStar(require("./defineProperties.js"), exports);
|
|
41
|
+
__exportStar(require("./defineProperty.js"), exports);
|
|
40
42
|
__exportStar(require("./delay.js"), exports);
|
|
41
43
|
__exportStar(require("./delayAnimationFrame.js"), exports);
|
|
42
44
|
__exportStar(require("./delayAnimationFrames.js"), exports);
|
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
|
}
|
|
@@ -14,6 +14,6 @@ exports.assume = void 0;
|
|
|
14
14
|
* assume<string>(42); // true (no runtime check is performed)
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/no-
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
18
|
const assume = (_input, ..._args) => true;
|
|
19
19
|
exports.assume = assume;
|
|
@@ -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
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-safe wrapper around `Object.defineProperties()` that reflects the new properties in the returned type.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* const user = defineProperties({ name: 'Test' }, {
|
|
7
|
+
* age: { value: 42 },
|
|
8
|
+
* isAdmin: { get: () => false },
|
|
9
|
+
* title: {
|
|
10
|
+
* writable: true,
|
|
11
|
+
* },
|
|
12
|
+
* });
|
|
13
|
+
* // user: { name: string, readonly age: number, readonly isAdmin: boolean, title: unknown }
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export function defineProperties(input, attributeMap) {
|
|
17
|
+
return Object.defineProperties(input, attributeMap);
|
|
18
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-safe wrapper around `Object.defineProperty` that reflects the new property in the returned type.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* const user = defineProperty({ name: 'Test' }, 'age', { value: 42 });
|
|
7
|
+
* // user: { name: string, readonly age: number }
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const user = defineProperty({ name: 'Test' }, 'title', { writable: true });
|
|
12
|
+
* // user: { name: string, title: unknown }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function defineProperty(input, property, attributes) {
|
|
16
|
+
return Object.defineProperty(input, property, attributes);
|
|
17
|
+
}
|
|
@@ -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;
|
|
@@ -11,7 +11,7 @@ const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
|
|
|
11
11
|
*/
|
|
12
12
|
export function graphemeLength(input) {
|
|
13
13
|
let count = 0;
|
|
14
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
15
15
|
for (const _ of segmenter.segment(input)) {
|
|
16
16
|
count++;
|
|
17
17
|
}
|
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
|
@@ -21,6 +21,8 @@ export * from './counter.js';
|
|
|
21
21
|
export * from './dedent.js';
|
|
22
22
|
export * from './deepDecodeURI.js';
|
|
23
23
|
export * from './deepFreeze.js';
|
|
24
|
+
export * from './defineProperties.js';
|
|
25
|
+
export * from './defineProperty.js';
|
|
24
26
|
export * from './delay.js';
|
|
25
27
|
export * from './delayAnimationFrame.js';
|
|
26
28
|
export * from './delayAnimationFrames.js';
|
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
|
}
|
|
@@ -11,5 +11,5 @@
|
|
|
11
11
|
* assume<string>(42); // true (no runtime check is performed)
|
|
12
12
|
* ```
|
|
13
13
|
*/
|
|
14
|
-
// eslint-disable-next-line @typescript-eslint/no-
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
15
|
export const assume = (_input, ..._args) => true;
|
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
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ApplyPropertyAttribute, GetPropertyRecordFromAttribute, PropertyAttributeMap, RetainMutability } from './types/objects.js';
|
|
2
|
+
import type { Pretty, UnionToIntersection } from './types/utils.js';
|
|
3
|
+
export type WithPropertyMap<Type, AttributeMap extends PropertyAttributeMap> = Omit<Type, keyof AttributeMap> & UnionToIntersection<{
|
|
4
|
+
[Property in keyof AttributeMap]: ApplyPropertyAttribute<GetPropertyRecordFromAttribute<Type, Property, AttributeMap[Property]>, AttributeMap[Property], RetainMutability<Type, Property>>;
|
|
5
|
+
}[keyof AttributeMap]>;
|
|
6
|
+
/**
|
|
7
|
+
* Type-safe wrapper around `Object.defineProperties()` that reflects the new properties in the returned type.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const user = defineProperties({ name: 'Test' }, {
|
|
12
|
+
* age: { value: 42 },
|
|
13
|
+
* isAdmin: { get: () => false },
|
|
14
|
+
* title: {
|
|
15
|
+
* writable: true,
|
|
16
|
+
* },
|
|
17
|
+
* });
|
|
18
|
+
* // user: { name: string, readonly age: number, readonly isAdmin: boolean, title: unknown }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function defineProperties<Type, AttributeMap extends PropertyAttributeMap>(input: Type, attributeMap: AttributeMap): Pretty<WithPropertyMap<Type, AttributeMap>>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ApplyPropertyAttribute, GetPropertyRecordFromAttribute, PropertyAttribute, RetainMutability } from './types/objects.js';
|
|
2
|
+
import type { Pretty } from './types/utils.js';
|
|
3
|
+
export type WithProperty<Type, Property extends PropertyKey, Attribute extends PropertyAttribute> = Omit<Type, Property> & ApplyPropertyAttribute<GetPropertyRecordFromAttribute<Type, Property, Attribute>, Attribute, RetainMutability<Type, Property>>;
|
|
4
|
+
/**
|
|
5
|
+
* Type-safe wrapper around `Object.defineProperty` that reflects the new property in the returned type.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const user = defineProperty({ name: 'Test' }, 'age', { value: 42 });
|
|
10
|
+
* // user: { name: string, readonly age: number }
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const user = defineProperty({ name: 'Test' }, 'title', { writable: true });
|
|
15
|
+
* // user: { name: string, title: unknown }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function defineProperty<Type, Property extends PropertyKey, Attributes extends PropertyAttribute>(input: Type, property: Property, attributes: Attributes): Pretty<WithProperty<Type, Property, Attributes>>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -19,6 +19,8 @@ export * from './counter.js';
|
|
|
19
19
|
export * from './dedent.js';
|
|
20
20
|
export * from './deepDecodeURI.js';
|
|
21
21
|
export * from './deepFreeze.js';
|
|
22
|
+
export * from './defineProperties.js';
|
|
23
|
+
export * from './defineProperty.js';
|
|
22
24
|
export * from './delay.js';
|
|
23
25
|
export * from './delayAnimationFrame.js';
|
|
24
26
|
export * from './delayAnimationFrames.js';
|
|
@@ -3,7 +3,7 @@ import type { Primitive } from './common.js';
|
|
|
3
3
|
import type { UnknownRecord } from './records.js';
|
|
4
4
|
import type { AutoCompletableString, CamelCase } from './strings.js';
|
|
5
5
|
import type { KeyValueTuple } from './tuples.js';
|
|
6
|
-
import type { CanBeUndefined, IfNever, Pretty } from './utils.js';
|
|
6
|
+
import type { CanBeUndefined, IfNever, IsReadonlyKey, Pretty } from './utils.js';
|
|
7
7
|
export type Assign<Target, Source> = IfNever<Target, Source, Omit<Target, keyof (Target | Source)> & Source>;
|
|
8
8
|
export type Entries<T extends object> = {
|
|
9
9
|
[P in keyof T]: KeyValueTuple<P, T[P]>;
|
|
@@ -112,3 +112,35 @@ export type MergeObjects<Left, Right> = {
|
|
|
112
112
|
export type MergeArrayWithObject<Left extends unknown[], Right extends UnknownRecord> = Left & {
|
|
113
113
|
[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];
|
|
114
114
|
};
|
|
115
|
+
export type GetPropertyType<Type, Property extends PropertyKey, Default = unknown> = Property extends keyof Type ? Type[Property] : Default;
|
|
116
|
+
/**
|
|
117
|
+
* Whether redefining `Property` on `Type` should keep the property mutable when
|
|
118
|
+
* the new attribute doesn't say either way. This mirrors `Object.defineProperty()`,
|
|
119
|
+
* which keeps a property's existing `writable` flag when it isn't part of the new
|
|
120
|
+
* descriptor and the property already exists.
|
|
121
|
+
*/
|
|
122
|
+
export type RetainMutability<Type, Property extends PropertyKey> = Property extends keyof Type ? IsReadonlyKey<Type, Property> extends true ? false : true : false;
|
|
123
|
+
export type AnyThisType = ThisType<any>;
|
|
124
|
+
export type PropertyAttribute = PropertyDescriptor & AnyThisType;
|
|
125
|
+
export type PropertyAttributeMap = PropertyDescriptorMap & AnyThisType;
|
|
126
|
+
export type ApplyPropertyAttribute<Type, Attribute extends PropertyAttribute, Retain extends boolean = false> = Attribute extends {
|
|
127
|
+
writable: true;
|
|
128
|
+
} | {
|
|
129
|
+
set(value: never): void;
|
|
130
|
+
} ? Type : Attribute extends {
|
|
131
|
+
writable: false;
|
|
132
|
+
} ? Readonly<Type> : Retain extends true ? Type : Readonly<Type>;
|
|
133
|
+
export type GetTypeFromAttributes<Attribute extends PropertyAttribute, DefaultType = unknown> = Attribute extends {
|
|
134
|
+
value: infer Type;
|
|
135
|
+
} | {
|
|
136
|
+
get(): infer Type;
|
|
137
|
+
} | {
|
|
138
|
+
set(value: infer Type): void;
|
|
139
|
+
} ? Type : DefaultType;
|
|
140
|
+
export type GetPropertyRecordFromAttribute<Type, Property extends PropertyKey, Attribute extends PropertyAttribute, DefaultType = GetPropertyType<Type, Property>> = Attribute extends {
|
|
141
|
+
value: infer PropertyType;
|
|
142
|
+
} | {
|
|
143
|
+
get(): infer PropertyType;
|
|
144
|
+
} | {
|
|
145
|
+
set(value: infer PropertyType): void;
|
|
146
|
+
} ? Record<Property, PropertyType> : Record<Property, DefaultType>;
|
|
@@ -4,6 +4,11 @@ export type IfDefined<Type, T, F> = undefined extends Type ? F : T;
|
|
|
4
4
|
export type IfUnknown<Type, T, F> = unknown extends Type ? T : F;
|
|
5
5
|
export type IfPromise<Type, T, F> = Type extends Promise<any> ? T : F;
|
|
6
6
|
export type IfSame<Left, Right, T, F> = [Left] extends [Right] ? ([Right] extends [Left] ? T : F) : F;
|
|
7
|
+
/**
|
|
8
|
+
* Strict type equality check. Unlike `IfSame`, this distinguishes `readonly` modifiers,
|
|
9
|
+
* since ordinary mutual `extends` checks ignore them.
|
|
10
|
+
*/
|
|
11
|
+
export type Equals<Left, Right> = (<Type>() => Type extends Left ? 1 : 2) extends <Type>() => (Type extends Right ? 1 : 2) ? true : false;
|
|
7
12
|
export type NotPromise<Type> = Type extends Promise<any> ? never : Type;
|
|
8
13
|
export type CanBeUndefined<Type, T, F> = Type | undefined extends Type ? T : F;
|
|
9
14
|
export type ReturnTypeDefault<Func, Default = undefined> = Func extends (...args: any[]) => infer R ? R : Default;
|
|
@@ -14,6 +19,9 @@ export type Writable<Type> = {
|
|
|
14
19
|
-readonly [Key in keyof Type]: Type[Key];
|
|
15
20
|
};
|
|
16
21
|
export type Unwritable<Type> = Type extends Writable<infer Inner> ? Readonly<Inner> : Readonly<Type>;
|
|
22
|
+
export type IsReadonlyKey<Type, Keys extends keyof Type> = Equals<Pick<Type, Keys>, {
|
|
23
|
+
-readonly [Property in Keys]: Type[Property];
|
|
24
|
+
}> extends true ? false : true;
|
|
17
25
|
export type GetIndex<Type> = {
|
|
18
26
|
[Key in keyof Type as symbol extends Key ? Key : string extends Key ? Key : number extends Key ? Key : never]: Type[Key];
|
|
19
27
|
};
|
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.4.0",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -66,14 +66,14 @@
|
|
|
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.22.0+sha512.1ff870c4c6133dfd88fb2afc46dd13d47f09c9794b438c6fdb47ca98caf3bc16381ee0be93a091b8e3824cf01f889f46d7d9e20910fb0be1ab0fb5baa80dd621",
|
|
70
70
|
"scripts": {
|
|
71
71
|
"clean": "rimraf ./dist/ ./cache/ ./coverage/",
|
|
72
72
|
"prebuild": "pnpm clean",
|
|
73
|
-
"build": "
|
|
73
|
+
"build": "tsc --build tsconfig.cjs.json tsconfig.mjs.json --force",
|
|
74
74
|
"build:docs": "typedoc",
|
|
75
75
|
"postbuild": "echo '{\"type\":\"commonjs\"}' > ./dist/cjs/package.json && echo '{\"type\":\"module\"}' > ./dist/mjs/package.json && pnpm validate",
|
|
76
|
-
"typecheck": "
|
|
76
|
+
"typecheck": "tsc --build --verbose --noEmit",
|
|
77
77
|
"lint": "eslint ./*.{js,cjs,mjs,ts,cts,mts} ./src/ ./bench/ ./test/ --ext .ts,.mjs,.cjs",
|
|
78
78
|
"bench": "vitest bench",
|
|
79
79
|
"test": "vitest --typecheck",
|
|
@@ -86,16 +86,16 @@
|
|
|
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
|
-
"@typescript/native
|
|
93
|
-
"@vitest/coverage-v8": "^4.1.
|
|
92
|
+
"@typescript/native": "npm:typescript@^7.0.2",
|
|
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.
|
|
98
|
-
"commitlint-plugin-cspell": "^0.9.
|
|
97
|
+
"commitlint": "^21.2.2",
|
|
98
|
+
"commitlint-plugin-cspell": "^0.9.4",
|
|
99
99
|
"conventional-changelog-conventionalcommits": "^9.3.1",
|
|
100
100
|
"cspell": "^10.0.1",
|
|
101
101
|
"eslint": "^8.57.1",
|
|
@@ -103,13 +103,14 @@
|
|
|
103
103
|
"eslint-import-resolver-typescript": "^4.4.5",
|
|
104
104
|
"eslint-plugin-import": "^2.32.0",
|
|
105
105
|
"husky": "^9.1.7",
|
|
106
|
-
"jsdom": "^
|
|
107
|
-
"lint-staged": "^17.0
|
|
108
|
-
"prettier": "^3.9.
|
|
106
|
+
"jsdom": "^30.0.1",
|
|
107
|
+
"lint-staged": "^17.3.0",
|
|
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
|
-
"
|
|
113
|
-
"
|
|
112
|
+
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
113
|
+
"validate-package-exports": "^1.3.3",
|
|
114
|
+
"vitest": "^4.1.11"
|
|
114
115
|
}
|
|
115
116
|
}
|