@augment-vir/common 21.6.1 → 22.0.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/augments/json.js +3 -3
- package/dist/cjs/augments/object/merge-deep.js +17 -16
- package/dist/cjs/augments/object/nested-keys.js +3 -3
- package/dist/cjs/augments/string/search-params.js +2 -2
- package/dist/cjs/index.js +0 -1
- package/dist/esm/augments/json.js +3 -3
- package/dist/esm/augments/object/merge-deep.js +17 -16
- package/dist/esm/augments/object/nested-keys.js +3 -3
- package/dist/esm/augments/string/search-params.js +2 -2
- package/dist/esm/index.js +0 -1
- package/dist/types/augments/object/merge-deep.d.ts +2 -1
- package/dist/types/index.d.ts +0 -1
- package/package.json +3 -2
- package/dist/cjs/augments/runtime-type-of.js +0 -23
- package/dist/esm/augments/runtime-type-of.js +0 -17
- package/dist/types/augments/runtime-type-of.d.ts +0 -19
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.areJsonEqual = exports.stringifyJson = exports.parseJson = void 0;
|
|
4
|
+
const run_time_assertions_1 = require("run-time-assertions");
|
|
4
5
|
const error_1 = require("./error");
|
|
5
6
|
const matches_object_shape_1 = require("./object/matches-object-shape");
|
|
6
7
|
const object_1 = require("./object/object");
|
|
7
|
-
const runtime_type_of_1 = require("./runtime-type-of");
|
|
8
8
|
function parseJson({ jsonString, errorHandler, shapeMatcher, }) {
|
|
9
9
|
try {
|
|
10
10
|
const parsedJson = JSON.parse(jsonString);
|
|
11
11
|
if (shapeMatcher != undefined) {
|
|
12
|
-
if ((0,
|
|
12
|
+
if ((0, run_time_assertions_1.isRunTimeType)(shapeMatcher, 'object')) {
|
|
13
13
|
(0, matches_object_shape_1.assertMatchesObjectShape)(parsedJson, shapeMatcher);
|
|
14
14
|
}
|
|
15
15
|
else {
|
|
16
|
-
(0,
|
|
16
|
+
(0, run_time_assertions_1.assertRunTimeType)(parsedJson, (0, run_time_assertions_1.getRunTimeType)(shapeMatcher), 'parsedJson');
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
return parsedJson;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.mergeDeep = void 0;
|
|
4
|
-
const
|
|
4
|
+
const run_time_assertions_1 = require("run-time-assertions");
|
|
5
5
|
const tuple_1 = require("../tuple");
|
|
6
6
|
const object_1 = require("./object");
|
|
7
7
|
/**
|
|
8
|
-
* Accepts multiple objects and merges their key-value pairs recursively.
|
|
8
|
+
* Accepts multiple objects and merges their key-value pairs recursively. Any values set to
|
|
9
|
+
* undefined will be removed.
|
|
9
10
|
*
|
|
10
11
|
* Note that order matters! Each input object will overwrite the properties of the previous objects.
|
|
11
12
|
*/
|
|
@@ -28,22 +29,13 @@ function mergeDeep(...inputs) {
|
|
|
28
29
|
return;
|
|
29
30
|
}
|
|
30
31
|
Object.entries(individualInput).forEach(([key, value,]) => {
|
|
31
|
-
const mergePropsArray = mergeProps[key] || [];
|
|
32
32
|
if (!mergeProps[key]) {
|
|
33
|
-
mergeProps[key] =
|
|
33
|
+
mergeProps[key] = [];
|
|
34
34
|
}
|
|
35
|
-
|
|
35
|
+
mergeProps[key].push(value);
|
|
36
36
|
});
|
|
37
|
-
if (result) {
|
|
38
|
-
if ((0,
|
|
39
|
-
result = {
|
|
40
|
-
...result,
|
|
41
|
-
...individualInput,
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
if ((0, runtime_type_of_1.isRuntimeTypeOf)(individualInput, 'array')) {
|
|
37
|
+
if (!result) {
|
|
38
|
+
if ((0, run_time_assertions_1.isRunTimeType)(individualInput, 'array')) {
|
|
47
39
|
result = [...individualInput];
|
|
48
40
|
}
|
|
49
41
|
else {
|
|
@@ -56,8 +48,17 @@ function mergeDeep(...inputs) {
|
|
|
56
48
|
}
|
|
57
49
|
});
|
|
58
50
|
Object.entries(mergeProps).forEach(([key, mergeValues,]) => {
|
|
59
|
-
|
|
51
|
+
const newValue = mergeDeep(...mergeValues);
|
|
52
|
+
if (newValue === undefined && key in result) {
|
|
53
|
+
delete result[key];
|
|
54
|
+
}
|
|
55
|
+
else if (newValue !== undefined) {
|
|
56
|
+
result[key] = newValue;
|
|
57
|
+
}
|
|
60
58
|
});
|
|
59
|
+
if ((0, run_time_assertions_1.isRunTimeType)(result, 'array')) {
|
|
60
|
+
result = result.filter((entry) => entry !== undefined);
|
|
61
|
+
}
|
|
61
62
|
return result;
|
|
62
63
|
}
|
|
63
64
|
exports.mergeDeep = mergeDeep;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getValueFromNestedKeys = exports.setValueWithNestedKeys = void 0;
|
|
4
|
-
const
|
|
4
|
+
const run_time_assertions_1 = require("run-time-assertions");
|
|
5
5
|
const object_1 = require("./object");
|
|
6
6
|
const typed_has_property_1 = require("./typed-has-property");
|
|
7
7
|
function setValueWithNestedKeys(originalObject, nestedKeys, value) {
|
|
@@ -12,7 +12,7 @@ function setValueWithNestedKeys(originalObject, nestedKeys, value) {
|
|
|
12
12
|
*/
|
|
13
13
|
const nestedKeysInput = nestedKeys;
|
|
14
14
|
const inputObject = originalObject;
|
|
15
|
-
if ((0,
|
|
15
|
+
if ((0, run_time_assertions_1.isRunTimeType)(inputObject, 'array')) {
|
|
16
16
|
inputObject.forEach((entry) => {
|
|
17
17
|
if ((0, object_1.isObject)(entry)) {
|
|
18
18
|
setValueWithNestedKeys(entry, nestedKeysInput, value);
|
|
@@ -44,7 +44,7 @@ function getValueFromNestedKeys(originalObject, nestedKeys) {
|
|
|
44
44
|
*/
|
|
45
45
|
const nestedKeysInput = nestedKeys;
|
|
46
46
|
const inputObject = originalObject;
|
|
47
|
-
if ((0,
|
|
47
|
+
if ((0, run_time_assertions_1.isRunTimeType)(inputObject, 'array')) {
|
|
48
48
|
return inputObject.map((entry) => getValueFromNestedKeys(entry, nestedKeys));
|
|
49
49
|
}
|
|
50
50
|
const keyToAccess = nestedKeysInput[0];
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.urlToSearchParamsObject = exports.objectToSearchParamsString = void 0;
|
|
4
|
+
const run_time_assertions_1 = require("run-time-assertions");
|
|
4
5
|
const boolean_1 = require("../boolean");
|
|
5
6
|
const common_string_1 = require("../common-string");
|
|
6
7
|
const matches_object_shape_1 = require("../object/matches-object-shape");
|
|
7
|
-
const runtime_type_of_1 = require("../runtime-type-of");
|
|
8
8
|
const prefixes_1 = require("./prefixes");
|
|
9
9
|
function objectToSearchParamsString(inputObject) {
|
|
10
10
|
const valueStrings = Object.entries(inputObject)
|
|
@@ -41,7 +41,7 @@ function splitSearchString(searchString) {
|
|
|
41
41
|
return paramEntries;
|
|
42
42
|
}
|
|
43
43
|
function urlToSearchParamsObject(inputUrl, verifyShape) {
|
|
44
|
-
const ensuredUrl = (0,
|
|
44
|
+
const ensuredUrl = (0, run_time_assertions_1.isRunTimeType)(inputUrl, 'string') ? new URL(inputUrl) : inputUrl;
|
|
45
45
|
const searchEntries = splitSearchString(ensuredUrl.search);
|
|
46
46
|
const paramsObject = Object.fromEntries(searchEntries);
|
|
47
47
|
if (verifyShape) {
|
package/dist/cjs/index.js
CHANGED
|
@@ -44,7 +44,6 @@ __exportStar(require("./augments/promise/promise"), exports);
|
|
|
44
44
|
__exportStar(require("./augments/promise/wait"), exports);
|
|
45
45
|
__exportStar(require("./augments/random"), exports);
|
|
46
46
|
__exportStar(require("./augments/regexp"), exports);
|
|
47
|
-
__exportStar(require("./augments/runtime-type-of"), exports);
|
|
48
47
|
__exportStar(require("./augments/string/prefixes"), exports);
|
|
49
48
|
__exportStar(require("./augments/string/search-params"), exports);
|
|
50
49
|
__exportStar(require("./augments/string/suffixes"), exports);
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
+
import { assertRunTimeType, getRunTimeType, isRunTimeType } from 'run-time-assertions';
|
|
1
2
|
import { ensureError } from './error';
|
|
2
3
|
import { assertMatchesObjectShape } from './object/matches-object-shape';
|
|
3
4
|
import { isObject } from './object/object';
|
|
4
|
-
import { assertRuntimeTypeOf, getRuntimeTypeOf, isRuntimeTypeOf } from './runtime-type-of';
|
|
5
5
|
export function parseJson({ jsonString, errorHandler, shapeMatcher, }) {
|
|
6
6
|
try {
|
|
7
7
|
const parsedJson = JSON.parse(jsonString);
|
|
8
8
|
if (shapeMatcher != undefined) {
|
|
9
|
-
if (
|
|
9
|
+
if (isRunTimeType(shapeMatcher, 'object')) {
|
|
10
10
|
assertMatchesObjectShape(parsedJson, shapeMatcher);
|
|
11
11
|
}
|
|
12
12
|
else {
|
|
13
|
-
|
|
13
|
+
assertRunTimeType(parsedJson, getRunTimeType(shapeMatcher), 'parsedJson');
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
return parsedJson;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isRunTimeType } from 'run-time-assertions';
|
|
2
2
|
import { isLengthAtLeast } from '../tuple';
|
|
3
3
|
import { isObject } from './object';
|
|
4
4
|
/**
|
|
5
|
-
* Accepts multiple objects and merges their key-value pairs recursively.
|
|
5
|
+
* Accepts multiple objects and merges their key-value pairs recursively. Any values set to
|
|
6
|
+
* undefined will be removed.
|
|
6
7
|
*
|
|
7
8
|
* Note that order matters! Each input object will overwrite the properties of the previous objects.
|
|
8
9
|
*/
|
|
@@ -25,22 +26,13 @@ export function mergeDeep(...inputs) {
|
|
|
25
26
|
return;
|
|
26
27
|
}
|
|
27
28
|
Object.entries(individualInput).forEach(([key, value,]) => {
|
|
28
|
-
const mergePropsArray = mergeProps[key] || [];
|
|
29
29
|
if (!mergeProps[key]) {
|
|
30
|
-
mergeProps[key] =
|
|
30
|
+
mergeProps[key] = [];
|
|
31
31
|
}
|
|
32
|
-
|
|
32
|
+
mergeProps[key].push(value);
|
|
33
33
|
});
|
|
34
|
-
if (result) {
|
|
35
|
-
if (
|
|
36
|
-
result = {
|
|
37
|
-
...result,
|
|
38
|
-
...individualInput,
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
if (isRuntimeTypeOf(individualInput, 'array')) {
|
|
34
|
+
if (!result) {
|
|
35
|
+
if (isRunTimeType(individualInput, 'array')) {
|
|
44
36
|
result = [...individualInput];
|
|
45
37
|
}
|
|
46
38
|
else {
|
|
@@ -53,7 +45,16 @@ export function mergeDeep(...inputs) {
|
|
|
53
45
|
}
|
|
54
46
|
});
|
|
55
47
|
Object.entries(mergeProps).forEach(([key, mergeValues,]) => {
|
|
56
|
-
|
|
48
|
+
const newValue = mergeDeep(...mergeValues);
|
|
49
|
+
if (newValue === undefined && key in result) {
|
|
50
|
+
delete result[key];
|
|
51
|
+
}
|
|
52
|
+
else if (newValue !== undefined) {
|
|
53
|
+
result[key] = newValue;
|
|
54
|
+
}
|
|
57
55
|
});
|
|
56
|
+
if (isRunTimeType(result, 'array')) {
|
|
57
|
+
result = result.filter((entry) => entry !== undefined);
|
|
58
|
+
}
|
|
58
59
|
return result;
|
|
59
60
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isRunTimeType } from 'run-time-assertions';
|
|
2
2
|
import { isObject } from './object';
|
|
3
3
|
import { typedHasProperty } from './typed-has-property';
|
|
4
4
|
export function setValueWithNestedKeys(originalObject, nestedKeys, value) {
|
|
@@ -9,7 +9,7 @@ export function setValueWithNestedKeys(originalObject, nestedKeys, value) {
|
|
|
9
9
|
*/
|
|
10
10
|
const nestedKeysInput = nestedKeys;
|
|
11
11
|
const inputObject = originalObject;
|
|
12
|
-
if (
|
|
12
|
+
if (isRunTimeType(inputObject, 'array')) {
|
|
13
13
|
inputObject.forEach((entry) => {
|
|
14
14
|
if (isObject(entry)) {
|
|
15
15
|
setValueWithNestedKeys(entry, nestedKeysInput, value);
|
|
@@ -40,7 +40,7 @@ export function getValueFromNestedKeys(originalObject, nestedKeys) {
|
|
|
40
40
|
*/
|
|
41
41
|
const nestedKeysInput = nestedKeys;
|
|
42
42
|
const inputObject = originalObject;
|
|
43
|
-
if (
|
|
43
|
+
if (isRunTimeType(inputObject, 'array')) {
|
|
44
44
|
return inputObject.map((entry) => getValueFromNestedKeys(entry, nestedKeys));
|
|
45
45
|
}
|
|
46
46
|
const keyToAccess = nestedKeysInput[0];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { isRunTimeType } from 'run-time-assertions';
|
|
1
2
|
import { isTruthy } from '../boolean';
|
|
2
3
|
import { typedSplit } from '../common-string';
|
|
3
4
|
import { assertMatchesObjectShape } from '../object/matches-object-shape';
|
|
4
|
-
import { isRuntimeTypeOf } from '../runtime-type-of';
|
|
5
5
|
import { removePrefix } from './prefixes';
|
|
6
6
|
export function objectToSearchParamsString(inputObject) {
|
|
7
7
|
const valueStrings = Object.entries(inputObject)
|
|
@@ -37,7 +37,7 @@ function splitSearchString(searchString) {
|
|
|
37
37
|
return paramEntries;
|
|
38
38
|
}
|
|
39
39
|
export function urlToSearchParamsObject(inputUrl, verifyShape) {
|
|
40
|
-
const ensuredUrl =
|
|
40
|
+
const ensuredUrl = isRunTimeType(inputUrl, 'string') ? new URL(inputUrl) : inputUrl;
|
|
41
41
|
const searchEntries = splitSearchString(ensuredUrl.search);
|
|
42
42
|
const paramsObject = Object.fromEntries(searchEntries);
|
|
43
43
|
if (verifyShape) {
|
package/dist/esm/index.js
CHANGED
|
@@ -28,7 +28,6 @@ export * from './augments/promise/promise';
|
|
|
28
28
|
export * from './augments/promise/wait';
|
|
29
29
|
export * from './augments/random';
|
|
30
30
|
export * from './augments/regexp';
|
|
31
|
-
export * from './augments/runtime-type-of';
|
|
32
31
|
export * from './augments/string/prefixes';
|
|
33
32
|
export * from './augments/string/search-params';
|
|
34
33
|
export * from './augments/string/suffixes';
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PartialDeep } from 'type-fest';
|
|
2
2
|
/**
|
|
3
|
-
* Accepts multiple objects and merges their key-value pairs recursively.
|
|
3
|
+
* Accepts multiple objects and merges their key-value pairs recursively. Any values set to
|
|
4
|
+
* undefined will be removed.
|
|
4
5
|
*
|
|
5
6
|
* Note that order matters! Each input object will overwrite the properties of the previous objects.
|
|
6
7
|
*/
|
package/dist/types/index.d.ts
CHANGED
|
@@ -28,7 +28,6 @@ export * from './augments/promise/promise';
|
|
|
28
28
|
export * from './augments/promise/wait';
|
|
29
29
|
export * from './augments/random';
|
|
30
30
|
export * from './augments/regexp';
|
|
31
|
-
export * from './augments/runtime-type-of';
|
|
32
31
|
export * from './augments/string/prefixes';
|
|
33
32
|
export * from './augments/string/search-params';
|
|
34
33
|
export * from './augments/string/suffixes';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@augment-vir/common",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "22.0.0",
|
|
4
4
|
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/electrovir/augment-vir/issues"
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"browser-or-node": "^2.1.1",
|
|
28
|
-
"
|
|
28
|
+
"run-time-assertions": "^0.2.0",
|
|
29
|
+
"type-fest": "^4.8.1"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"typescript": "^5.2.2"
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.assertRuntimeTypeOf = exports.isRuntimeTypeOf = exports.getRuntimeTypeOf = void 0;
|
|
4
|
-
// this function is not used at run time, it's only here for types
|
|
5
|
-
// istanbul ignore next
|
|
6
|
-
function rawGetTypeOf(x) {
|
|
7
|
-
return typeof x;
|
|
8
|
-
}
|
|
9
|
-
function getRuntimeTypeOf(input) {
|
|
10
|
-
return Array.isArray(input) ? 'array' : typeof input;
|
|
11
|
-
}
|
|
12
|
-
exports.getRuntimeTypeOf = getRuntimeTypeOf;
|
|
13
|
-
function isRuntimeTypeOf(input, testType) {
|
|
14
|
-
const inputType = getRuntimeTypeOf(input);
|
|
15
|
-
return inputType === testType;
|
|
16
|
-
}
|
|
17
|
-
exports.isRuntimeTypeOf = isRuntimeTypeOf;
|
|
18
|
-
function assertRuntimeTypeOf(input, testType, inputName) {
|
|
19
|
-
if (!isRuntimeTypeOf(input, testType)) {
|
|
20
|
-
throw new TypeError(`'${inputName}' is of type '${getRuntimeTypeOf(input)}' but type '${testType}' was expected.`);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
exports.assertRuntimeTypeOf = assertRuntimeTypeOf;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
// this function is not used at run time, it's only here for types
|
|
2
|
-
// istanbul ignore next
|
|
3
|
-
function rawGetTypeOf(x) {
|
|
4
|
-
return typeof x;
|
|
5
|
-
}
|
|
6
|
-
export function getRuntimeTypeOf(input) {
|
|
7
|
-
return Array.isArray(input) ? 'array' : typeof input;
|
|
8
|
-
}
|
|
9
|
-
export function isRuntimeTypeOf(input, testType) {
|
|
10
|
-
const inputType = getRuntimeTypeOf(input);
|
|
11
|
-
return inputType === testType;
|
|
12
|
-
}
|
|
13
|
-
export function assertRuntimeTypeOf(input, testType, inputName) {
|
|
14
|
-
if (!isRuntimeTypeOf(input, testType)) {
|
|
15
|
-
throw new TypeError(`'${inputName}' is of type '${getRuntimeTypeOf(input)}' but type '${testType}' was expected.`);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { AnyFunction } from './function';
|
|
2
|
-
declare function rawGetTypeOf(x: any): "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
|
|
3
|
-
type RawTypeOf = ReturnType<typeof rawGetTypeOf>;
|
|
4
|
-
export type RuntimeTypeOf = RawTypeOf | 'array';
|
|
5
|
-
export type RuntimeTypeOfMapping = {
|
|
6
|
-
array: any[] | ReadonlyArray<any>;
|
|
7
|
-
bigint: bigint;
|
|
8
|
-
boolean: boolean;
|
|
9
|
-
function: AnyFunction | Readonly<AnyFunction>;
|
|
10
|
-
number: number;
|
|
11
|
-
object: Record<PropertyKey, unknown> | Readonly<Record<PropertyKey, unknown>>;
|
|
12
|
-
string: string;
|
|
13
|
-
symbol: symbol;
|
|
14
|
-
undefined: undefined;
|
|
15
|
-
};
|
|
16
|
-
export declare function getRuntimeTypeOf(input: unknown): RuntimeTypeOf;
|
|
17
|
-
export declare function isRuntimeTypeOf<T extends RuntimeTypeOf>(input: unknown, testType: T): input is RuntimeTypeOfMapping[T];
|
|
18
|
-
export declare function assertRuntimeTypeOf<T extends RuntimeTypeOf>(input: unknown, testType: T, inputName: string): asserts input is RuntimeTypeOfMapping[T];
|
|
19
|
-
export {};
|