@oscarpalmer/atoms 0.154.0 → 0.155.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/atoms.full.js +95 -28
- package/dist/function/assert.js +46 -6
- package/dist/index.js +4 -3
- package/dist/internal/is.js +10 -1
- package/dist/internal/value/get.js +2 -10
- package/dist/internal/value/has.js +9 -0
- package/dist/internal/value/misc.js +30 -2
- package/dist/internal/value/set.js +1 -1
- package/dist/is.js +1 -10
- package/dist/math.js +1 -1
- package/dist/value/{get-set.js → handle.js} +2 -1
- package/package.json +4 -4
- package/src/function/assert.ts +3 -3
- package/src/index.ts +1 -0
- package/src/internal/is.ts +13 -0
- package/src/internal/value/get.ts +2 -27
- package/src/internal/value/has.ts +75 -0
- package/src/internal/value/misc.ts +65 -5
- package/src/internal/value/set.ts +6 -6
- package/src/is.ts +3 -15
- package/src/value/{get-set.ts → handle.ts} +1 -0
- package/types/function/assert.d.ts +41 -3
- package/types/index.d.ts +1 -0
- package/types/internal/is.d.ts +7 -0
- package/types/internal/value/has.d.ts +40 -0
- package/types/internal/value/misc.d.ts +10 -2
- package/types/is.d.ts +2 -9
- package/types/value/{get-set.d.ts → handle.d.ts} +1 -0
package/dist/atoms.full.js
CHANGED
|
@@ -86,6 +86,15 @@ function isConstructor(value) {
|
|
|
86
86
|
return typeof value === "function" && value.prototype?.constructor === value;
|
|
87
87
|
}
|
|
88
88
|
/**
|
|
89
|
+
* Is the value an instance of the constructor?
|
|
90
|
+
* @param constructor Class constructor
|
|
91
|
+
* @param value Value to check
|
|
92
|
+
* @returns `true` if the value is an instance of the constructor, otherwise `false`
|
|
93
|
+
*/
|
|
94
|
+
function isInstanceOf(constructor, value) {
|
|
95
|
+
return isConstructor(constructor) && value instanceof constructor;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
89
98
|
* Is the value a key?
|
|
90
99
|
* @param value Value to check
|
|
91
100
|
* @returns `true` if the value is a `Key` _(`number` or `string`)_, otherwise `false`
|
|
@@ -1036,24 +1045,64 @@ function isOk(value) {
|
|
|
1036
1045
|
function isResult(value) {
|
|
1037
1046
|
return _isResult(value, true) || _isResult(value, false);
|
|
1038
1047
|
}
|
|
1048
|
+
/**
|
|
1049
|
+
* Asserts that a condition is true, throwing an error if it is not
|
|
1050
|
+
* @param condition Condition to assert
|
|
1051
|
+
* @param message Error message
|
|
1052
|
+
* @param error Error constructor
|
|
1053
|
+
*/
|
|
1039
1054
|
function assert(condition, message, error) {
|
|
1040
1055
|
if (!condition()) throw new (error ?? Error)(message);
|
|
1041
1056
|
}
|
|
1042
|
-
assert.condition =
|
|
1057
|
+
assert.condition = assertCondition;
|
|
1058
|
+
assert.defined = assertDefined;
|
|
1059
|
+
assert.instanceOf = assertInstanceOf;
|
|
1060
|
+
assert.is = assertIs;
|
|
1061
|
+
/**
|
|
1062
|
+
* Creates an asserter that asserts a condition is true, throwing an error if it is not
|
|
1063
|
+
* @param condition Condition to assert
|
|
1064
|
+
* @param message Error message
|
|
1065
|
+
* @param error Error constructor
|
|
1066
|
+
* @returns Asserter
|
|
1067
|
+
*/
|
|
1068
|
+
function assertCondition(condition, message, error) {
|
|
1043
1069
|
return (value) => {
|
|
1044
1070
|
assert(() => condition(value), message, error);
|
|
1045
1071
|
};
|
|
1046
|
-
}
|
|
1047
|
-
|
|
1072
|
+
}
|
|
1073
|
+
/**
|
|
1074
|
+
* Asserts that a value is defined throwing an error if it is not
|
|
1075
|
+
* @param value Value to assert
|
|
1076
|
+
* @param message Error message
|
|
1077
|
+
*/
|
|
1078
|
+
function assertDefined(value, message) {
|
|
1079
|
+
assert(() => value != null, message ?? MESSAGE_VALUE_DEFINED);
|
|
1080
|
+
}
|
|
1081
|
+
/**
|
|
1082
|
+
* Creates an asserter that asserts a value is an instance of a constructor, throwing an error if it is not
|
|
1083
|
+
* @param constructor Constructor to check against
|
|
1084
|
+
* @param message Error message
|
|
1085
|
+
* @param error Error constructor
|
|
1086
|
+
* @returns Asserter
|
|
1087
|
+
*/
|
|
1088
|
+
function assertInstanceOf(constructor, message, error) {
|
|
1048
1089
|
return (value) => {
|
|
1049
1090
|
assert(() => value instanceof constructor, message, error);
|
|
1050
1091
|
};
|
|
1051
|
-
}
|
|
1052
|
-
|
|
1092
|
+
}
|
|
1093
|
+
/**
|
|
1094
|
+
* Creates an asserter that asserts a value is of a specific type, throwing an error if it is not
|
|
1095
|
+
* @param condition Type guard function to check the value
|
|
1096
|
+
* @param message Error message
|
|
1097
|
+
* @param error Error constructor
|
|
1098
|
+
* @returns Asserter
|
|
1099
|
+
*/
|
|
1100
|
+
function assertIs(condition, message, error) {
|
|
1053
1101
|
return (value) => {
|
|
1054
1102
|
assert(() => condition(value), message, error);
|
|
1055
1103
|
};
|
|
1056
|
-
}
|
|
1104
|
+
}
|
|
1105
|
+
const MESSAGE_VALUE_DEFINED = "Expected value to be defined";
|
|
1057
1106
|
function asyncFlow(...fns) {
|
|
1058
1107
|
assertFlowFunctions(fns);
|
|
1059
1108
|
return (...args) => asyncWork(args.map((value) => {
|
|
@@ -1284,6 +1333,27 @@ function findKey(needle, haystack) {
|
|
|
1284
1333
|
const index = keys.map((key) => key.toLowerCase()).indexOf(needle.toLowerCase());
|
|
1285
1334
|
return index > -1 ? keys[index] : needle;
|
|
1286
1335
|
}
|
|
1336
|
+
function getNestedValue(data, path, ignoreCase) {
|
|
1337
|
+
if (typeof data !== "object" || data === null || typeof path !== "string" || path.trim().length === 0) return {
|
|
1338
|
+
exists: false,
|
|
1339
|
+
value: void 0
|
|
1340
|
+
};
|
|
1341
|
+
const shouldIgnoreCase = ignoreCase === true;
|
|
1342
|
+
const paths = getPaths(path, shouldIgnoreCase);
|
|
1343
|
+
if (typeof paths === "string") return handleValue(data, paths, null, true, shouldIgnoreCase);
|
|
1344
|
+
const { length } = paths;
|
|
1345
|
+
let current = data;
|
|
1346
|
+
for (let index = 0; index < length; index += 1) {
|
|
1347
|
+
const part = paths[index];
|
|
1348
|
+
const handled = handleValue(current, part, null, true, shouldIgnoreCase);
|
|
1349
|
+
if (!handled.exists) return handled;
|
|
1350
|
+
current = handled.value;
|
|
1351
|
+
}
|
|
1352
|
+
return {
|
|
1353
|
+
exists: true,
|
|
1354
|
+
value: current
|
|
1355
|
+
};
|
|
1356
|
+
}
|
|
1287
1357
|
function getPaths(path, lowercase) {
|
|
1288
1358
|
const normalized = lowercase ? path.toLowerCase() : path;
|
|
1289
1359
|
if (!EXPRESSION_NESTED.test(normalized)) return normalized;
|
|
@@ -1292,23 +1362,29 @@ function getPaths(path, lowercase) {
|
|
|
1292
1362
|
function handleValue(data, path, value, get, ignoreCase) {
|
|
1293
1363
|
if (typeof data === "object" && data !== null && !ignoreKey(path)) {
|
|
1294
1364
|
const key = ignoreCase ? findKey(path, data) : path;
|
|
1295
|
-
if (get) return
|
|
1365
|
+
if (get) return {
|
|
1366
|
+
exists: key in data,
|
|
1367
|
+
value: data[key]
|
|
1368
|
+
};
|
|
1296
1369
|
data[key] = typeof value === "function" ? value(data[key]) : value;
|
|
1297
1370
|
}
|
|
1371
|
+
if (get) return {
|
|
1372
|
+
exists: false,
|
|
1373
|
+
value: void 0
|
|
1374
|
+
};
|
|
1298
1375
|
}
|
|
1299
1376
|
const EXPRESSION_BRACKET = /\[(\w+)\]/g;
|
|
1300
1377
|
const EXPRESSION_DOTS = /^\.|\.$/g;
|
|
1301
1378
|
const EXPRESSION_NESTED = /\.|\[\w+\]/;
|
|
1302
1379
|
function getValue(data, path, ignoreCase) {
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
return value;
|
|
1380
|
+
return getNestedValue(data, path, ignoreCase === true).value;
|
|
1381
|
+
}
|
|
1382
|
+
function hasValue(data, path, ignoreCase) {
|
|
1383
|
+
return getNestedValue(data, path, ignoreCase === true).exists;
|
|
1384
|
+
}
|
|
1385
|
+
hasValue.get = getWithHasValue;
|
|
1386
|
+
function getWithHasValue(data, path, ignoreCase) {
|
|
1387
|
+
return getNestedValue(data, path, ignoreCase === true);
|
|
1312
1388
|
}
|
|
1313
1389
|
function setValue(data, path, value, ignoreCase) {
|
|
1314
1390
|
if (typeof data !== "object" || data === null || typeof path !== "string" || path.trim().length === 0) return data;
|
|
@@ -1327,7 +1403,7 @@ function setValue(data, path, value, ignoreCase) {
|
|
|
1327
1403
|
handleValue(target, currentPath, value, false, shouldIgnoreCase);
|
|
1328
1404
|
break;
|
|
1329
1405
|
}
|
|
1330
|
-
let next = handleValue(target, currentPath, null, true, shouldIgnoreCase);
|
|
1406
|
+
let next = handleValue(target, currentPath, null, true, shouldIgnoreCase).value;
|
|
1331
1407
|
if (typeof next !== "object" || next === null) {
|
|
1332
1408
|
const nextPath = paths[index + 1];
|
|
1333
1409
|
if (EXPRESSION_INDEX.test(nextPath)) next = Array.from({ length: Number.parseInt(nextPath, 10) + 1 }, () => void 0);
|
|
@@ -2796,15 +2872,6 @@ function isEmpty(value) {
|
|
|
2796
2872
|
return true;
|
|
2797
2873
|
}
|
|
2798
2874
|
/**
|
|
2799
|
-
* Is the value an instance of the constructor?
|
|
2800
|
-
* @param constructor Class constructor
|
|
2801
|
-
* @param value Value to check
|
|
2802
|
-
* @returns `true` if the value is an instance of the constructor, otherwise `false`
|
|
2803
|
-
*/
|
|
2804
|
-
function isInstanceOf(constructor, value) {
|
|
2805
|
-
return isConstructor(constructor) && value instanceof constructor;
|
|
2806
|
-
}
|
|
2807
|
-
/**
|
|
2808
2875
|
* Is the value not `undefined` or `null`?
|
|
2809
2876
|
* @param value Value to check
|
|
2810
2877
|
* @returns `true` if the value is not `undefined` or `null`, otherwise `false`
|
|
@@ -3010,12 +3077,12 @@ function floor(value, decimals) {
|
|
|
3010
3077
|
function median(array, key) {
|
|
3011
3078
|
let length = Array.isArray(array) ? array.length : 0;
|
|
3012
3079
|
if (!Array.isArray(array) || length === 0) return NaN;
|
|
3013
|
-
if (length === 1) return isNumber(array[0]) ? array[0] : NaN;
|
|
3014
3080
|
let values = array;
|
|
3015
3081
|
const callback = getAggregateCallback(key);
|
|
3016
3082
|
if (callback != null) values = array.map((item, index) => callback(item, index, array));
|
|
3017
3083
|
const numbers = values.filter(isNumber).sort((first, second) => first - second);
|
|
3018
3084
|
length = numbers.length;
|
|
3085
|
+
if (length === 1) return numbers[0];
|
|
3019
3086
|
if (length % 2 === 0) {
|
|
3020
3087
|
const first = length / 2 - 1;
|
|
3021
3088
|
const second = length / 2;
|
|
@@ -3772,4 +3839,4 @@ var SizedSet = class extends Set {
|
|
|
3772
3839
|
}
|
|
3773
3840
|
}
|
|
3774
3841
|
};
|
|
3775
|
-
export { CancelablePromise, PromiseTimeoutError, QueueError, RetryError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, floor, flow, toResult as fromPromise, toResult, fromQuery, toPromise as fromResult, toPromise, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, retry, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, times, titleCase, toMap, toQuery, toRecord, toSet, toggle, trim, truncate, tryDecode, tryEncode, unique, unsmush, unwrap, update, upperCase, words };
|
|
3842
|
+
export { CancelablePromise, PromiseTimeoutError, QueueError, RetryError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, floor, flow, toResult as fromPromise, toResult, fromQuery, toPromise as fromResult, toPromise, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hasValue, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, retry, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, times, titleCase, toMap, toQuery, toRecord, toSet, toggle, trim, truncate, tryDecode, tryEncode, unique, unsmush, unwrap, update, upperCase, words };
|
package/dist/function/assert.js
CHANGED
|
@@ -1,19 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Asserts that a condition is true, throwing an error if it is not
|
|
3
|
+
* @param condition Condition to assert
|
|
4
|
+
* @param message Error message
|
|
5
|
+
* @param error Error constructor
|
|
6
|
+
*/
|
|
1
7
|
function assert(condition, message, error) {
|
|
2
8
|
if (!condition()) throw new (error ?? Error)(message);
|
|
3
9
|
}
|
|
4
|
-
assert.condition =
|
|
10
|
+
assert.condition = assertCondition;
|
|
11
|
+
assert.defined = assertDefined;
|
|
12
|
+
assert.instanceOf = assertInstanceOf;
|
|
13
|
+
assert.is = assertIs;
|
|
14
|
+
/**
|
|
15
|
+
* Creates an asserter that asserts a condition is true, throwing an error if it is not
|
|
16
|
+
* @param condition Condition to assert
|
|
17
|
+
* @param message Error message
|
|
18
|
+
* @param error Error constructor
|
|
19
|
+
* @returns Asserter
|
|
20
|
+
*/
|
|
21
|
+
function assertCondition(condition, message, error) {
|
|
5
22
|
return (value) => {
|
|
6
23
|
assert(() => condition(value), message, error);
|
|
7
24
|
};
|
|
8
|
-
}
|
|
9
|
-
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Asserts that a value is defined throwing an error if it is not
|
|
28
|
+
* @param value Value to assert
|
|
29
|
+
* @param message Error message
|
|
30
|
+
*/
|
|
31
|
+
function assertDefined(value, message) {
|
|
32
|
+
assert(() => value != null, message ?? MESSAGE_VALUE_DEFINED);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Creates an asserter that asserts a value is an instance of a constructor, throwing an error if it is not
|
|
36
|
+
* @param constructor Constructor to check against
|
|
37
|
+
* @param message Error message
|
|
38
|
+
* @param error Error constructor
|
|
39
|
+
* @returns Asserter
|
|
40
|
+
*/
|
|
41
|
+
function assertInstanceOf(constructor, message, error) {
|
|
10
42
|
return (value) => {
|
|
11
43
|
assert(() => value instanceof constructor, message, error);
|
|
12
44
|
};
|
|
13
|
-
}
|
|
14
|
-
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Creates an asserter that asserts a value is of a specific type, throwing an error if it is not
|
|
48
|
+
* @param condition Type guard function to check the value
|
|
49
|
+
* @param message Error message
|
|
50
|
+
* @param error Error constructor
|
|
51
|
+
* @returns Asserter
|
|
52
|
+
*/
|
|
53
|
+
function assertIs(condition, message, error) {
|
|
15
54
|
return (value) => {
|
|
16
55
|
assert(() => condition(value), message, error);
|
|
17
56
|
};
|
|
18
|
-
}
|
|
57
|
+
}
|
|
58
|
+
var MESSAGE_VALUE_DEFINED = "Expected value to be defined";
|
|
19
59
|
export { assert };
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { filter } from "./array/filter.js";
|
|
|
3
3
|
import { find } from "./array/find.js";
|
|
4
4
|
import { flatten } from "./array/flatten.js";
|
|
5
5
|
import { range, times } from "./array/from.js";
|
|
6
|
-
import { isArrayOrPlainObject, isConstructor, isKey, isNumber, isPlainObject, isTypedArray } from "./internal/is.js";
|
|
6
|
+
import { isArrayOrPlainObject, isConstructor, isInstanceOf, isKey, isNumber, isPlainObject, isTypedArray } from "./internal/is.js";
|
|
7
7
|
import { getArray } from "./array/get.js";
|
|
8
8
|
import { groupBy } from "./array/group-by.js";
|
|
9
9
|
import { indexOf } from "./array/index-of.js";
|
|
@@ -44,6 +44,7 @@ import { isError, isOk, isResult } from "./internal/result.js";
|
|
|
44
44
|
import { flow, pipe } from "./function/work.js";
|
|
45
45
|
import { equal } from "./internal/value/equal.js";
|
|
46
46
|
import { getValue } from "./internal/value/get.js";
|
|
47
|
+
import { hasValue } from "./internal/value/has.js";
|
|
47
48
|
import { setValue } from "./internal/value/set.js";
|
|
48
49
|
import { camelCase, capitalize, kebabCase, lowerCase, pascalCase, snakeCase, titleCase, upperCase } from "./string/case.js";
|
|
49
50
|
import { endsWith, includes, startsWith } from "./string/match.js";
|
|
@@ -56,7 +57,7 @@ import { omit } from "./value/omit.js";
|
|
|
56
57
|
import { pick } from "./value/pick.js";
|
|
57
58
|
import { smush } from "./value/smush.js";
|
|
58
59
|
import { unsmush } from "./value/unsmush.js";
|
|
59
|
-
import { isEmpty,
|
|
60
|
+
import { isEmpty, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumerical, isObject, isPrimitive } from "./is.js";
|
|
60
61
|
import { logger } from "./logger.js";
|
|
61
62
|
import { average, ceil, count, floor, median, min, round, sum } from "./math.js";
|
|
62
63
|
import { CancelablePromise, PromiseTimeoutError } from "./promise/models.js";
|
|
@@ -71,4 +72,4 @@ import { QueueError, queue } from "./queue.js";
|
|
|
71
72
|
import { getRandomBoolean, getRandomCharacters, getRandomColor, getRandomHex, getRandomItem, getRandomItems } from "./random.js";
|
|
72
73
|
import { attempt } from "./result/index.js";
|
|
73
74
|
import { SizedSet } from "./sized/set.js";
|
|
74
|
-
export { CancelablePromise, PromiseTimeoutError, QueueError, RetryError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, floor, flow, toResult as fromPromise, fromQuery, toPromise as fromResult, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, retry, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, times, titleCase, toMap, toPromise, toQuery, toRecord, toResult, toSet, toggle, trim, truncate, tryDecode, tryEncode, unique, unsmush, unwrap, update, upperCase, words };
|
|
75
|
+
export { CancelablePromise, PromiseTimeoutError, QueueError, RetryError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, floor, flow, toResult as fromPromise, fromQuery, toPromise as fromResult, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hasValue, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, retry, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, times, titleCase, toMap, toPromise, toQuery, toRecord, toResult, toSet, toggle, trim, truncate, tryDecode, tryEncode, unique, unsmush, unwrap, update, upperCase, words };
|
package/dist/internal/is.js
CHANGED
|
@@ -15,6 +15,15 @@ function isConstructor(value) {
|
|
|
15
15
|
return typeof value === "function" && value.prototype?.constructor === value;
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
|
+
* Is the value an instance of the constructor?
|
|
19
|
+
* @param constructor Class constructor
|
|
20
|
+
* @param value Value to check
|
|
21
|
+
* @returns `true` if the value is an instance of the constructor, otherwise `false`
|
|
22
|
+
*/
|
|
23
|
+
function isInstanceOf(constructor, value) {
|
|
24
|
+
return isConstructor(constructor) && value instanceof constructor;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
18
27
|
* Is the value a key?
|
|
19
28
|
* @param value Value to check
|
|
20
29
|
* @returns `true` if the value is a `Key` _(`number` or `string`)_, otherwise `false`
|
|
@@ -63,4 +72,4 @@ function isTypedArray(value) {
|
|
|
63
72
|
return TYPED_ARRAYS.has(value?.constructor);
|
|
64
73
|
}
|
|
65
74
|
var TYPED_ARRAYS;
|
|
66
|
-
export { isArrayOrPlainObject, isConstructor, isKey, isNumber, isPlainObject, isTypedArray };
|
|
75
|
+
export { isArrayOrPlainObject, isConstructor, isInstanceOf, isKey, isNumber, isPlainObject, isTypedArray };
|
|
@@ -1,13 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getNestedValue } from "./misc.js";
|
|
2
2
|
function getValue(data, path, ignoreCase) {
|
|
3
|
-
|
|
4
|
-
const shouldIgnoreCase = ignoreCase === true;
|
|
5
|
-
const paths = getPaths(path, shouldIgnoreCase);
|
|
6
|
-
if (typeof paths === "string") return handleValue(data, paths, null, true, shouldIgnoreCase);
|
|
7
|
-
const { length } = paths;
|
|
8
|
-
let index = 0;
|
|
9
|
-
let value = data;
|
|
10
|
-
while (index < length && value != null) value = handleValue(value, paths[index++], null, true, shouldIgnoreCase);
|
|
11
|
-
return value;
|
|
3
|
+
return getNestedValue(data, path, ignoreCase === true).value;
|
|
12
4
|
}
|
|
13
5
|
export { getValue };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { getNestedValue } from "./misc.js";
|
|
2
|
+
function hasValue(data, path, ignoreCase) {
|
|
3
|
+
return getNestedValue(data, path, ignoreCase === true).exists;
|
|
4
|
+
}
|
|
5
|
+
hasValue.get = getWithHasValue;
|
|
6
|
+
function getWithHasValue(data, path, ignoreCase) {
|
|
7
|
+
return getNestedValue(data, path, ignoreCase === true);
|
|
8
|
+
}
|
|
9
|
+
export { hasValue };
|
|
@@ -4,6 +4,27 @@ function findKey(needle, haystack) {
|
|
|
4
4
|
const index = keys.map((key) => key.toLowerCase()).indexOf(needle.toLowerCase());
|
|
5
5
|
return index > -1 ? keys[index] : needle;
|
|
6
6
|
}
|
|
7
|
+
function getNestedValue(data, path, ignoreCase) {
|
|
8
|
+
if (typeof data !== "object" || data === null || typeof path !== "string" || path.trim().length === 0) return {
|
|
9
|
+
exists: false,
|
|
10
|
+
value: void 0
|
|
11
|
+
};
|
|
12
|
+
const shouldIgnoreCase = ignoreCase === true;
|
|
13
|
+
const paths = getPaths(path, shouldIgnoreCase);
|
|
14
|
+
if (typeof paths === "string") return handleValue(data, paths, null, true, shouldIgnoreCase);
|
|
15
|
+
const { length } = paths;
|
|
16
|
+
let current = data;
|
|
17
|
+
for (let index = 0; index < length; index += 1) {
|
|
18
|
+
const part = paths[index];
|
|
19
|
+
const handled = handleValue(current, part, null, true, shouldIgnoreCase);
|
|
20
|
+
if (!handled.exists) return handled;
|
|
21
|
+
current = handled.value;
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
exists: true,
|
|
25
|
+
value: current
|
|
26
|
+
};
|
|
27
|
+
}
|
|
7
28
|
function getPaths(path, lowercase) {
|
|
8
29
|
const normalized = lowercase ? path.toLowerCase() : path;
|
|
9
30
|
if (!EXPRESSION_NESTED.test(normalized)) return normalized;
|
|
@@ -12,11 +33,18 @@ function getPaths(path, lowercase) {
|
|
|
12
33
|
function handleValue(data, path, value, get, ignoreCase) {
|
|
13
34
|
if (typeof data === "object" && data !== null && !ignoreKey(path)) {
|
|
14
35
|
const key = ignoreCase ? findKey(path, data) : path;
|
|
15
|
-
if (get) return
|
|
36
|
+
if (get) return {
|
|
37
|
+
exists: key in data,
|
|
38
|
+
value: data[key]
|
|
39
|
+
};
|
|
16
40
|
data[key] = typeof value === "function" ? value(data[key]) : value;
|
|
17
41
|
}
|
|
42
|
+
if (get) return {
|
|
43
|
+
exists: false,
|
|
44
|
+
value: void 0
|
|
45
|
+
};
|
|
18
46
|
}
|
|
19
47
|
var EXPRESSION_BRACKET = /\[(\w+)\]/g;
|
|
20
48
|
var EXPRESSION_DOTS = /^\.|\.$/g;
|
|
21
49
|
var EXPRESSION_NESTED = /\.|\[\w+\]/;
|
|
22
|
-
export { getPaths, handleValue };
|
|
50
|
+
export { findKey, getNestedValue, getPaths, handleValue };
|
|
@@ -16,7 +16,7 @@ function setValue(data, path, value, ignoreCase) {
|
|
|
16
16
|
handleValue(target, currentPath, value, false, shouldIgnoreCase);
|
|
17
17
|
break;
|
|
18
18
|
}
|
|
19
|
-
let next = handleValue(target, currentPath, null, true, shouldIgnoreCase);
|
|
19
|
+
let next = handleValue(target, currentPath, null, true, shouldIgnoreCase).value;
|
|
20
20
|
if (typeof next !== "object" || next === null) {
|
|
21
21
|
const nextPath = paths[index + 1];
|
|
22
22
|
if (EXPRESSION_INDEX.test(nextPath)) next = Array.from({ length: Number.parseInt(nextPath, 10) + 1 }, () => void 0);
|
package/dist/is.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isArrayOrPlainObject, isConstructor, isKey, isNumber, isPlainObject, isTypedArray } from "./internal/is.js";
|
|
1
|
+
import { isArrayOrPlainObject, isConstructor, isInstanceOf, isKey, isNumber, isPlainObject, isTypedArray } from "./internal/is.js";
|
|
2
2
|
import { getArray } from "./array/get.js";
|
|
3
3
|
import { getString } from "./internal/string.js";
|
|
4
4
|
/**
|
|
@@ -15,15 +15,6 @@ function isEmpty(value) {
|
|
|
15
15
|
return true;
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
|
-
* Is the value an instance of the constructor?
|
|
19
|
-
* @param constructor Class constructor
|
|
20
|
-
* @param value Value to check
|
|
21
|
-
* @returns `true` if the value is an instance of the constructor, otherwise `false`
|
|
22
|
-
*/
|
|
23
|
-
function isInstanceOf(constructor, value) {
|
|
24
|
-
return isConstructor(constructor) && value instanceof constructor;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
18
|
* Is the value not `undefined` or `null`?
|
|
28
19
|
* @param value Value to check
|
|
29
20
|
* @returns `true` if the value is not `undefined` or `null`, otherwise `false`
|
package/dist/math.js
CHANGED
|
@@ -37,12 +37,12 @@ function floor(value, decimals) {
|
|
|
37
37
|
function median(array, key) {
|
|
38
38
|
let length = Array.isArray(array) ? array.length : 0;
|
|
39
39
|
if (!Array.isArray(array) || length === 0) return NaN;
|
|
40
|
-
if (length === 1) return isNumber(array[0]) ? array[0] : NaN;
|
|
41
40
|
let values = array;
|
|
42
41
|
const callback = getAggregateCallback(key);
|
|
43
42
|
if (callback != null) values = array.map((item, index) => callback(item, index, array));
|
|
44
43
|
const numbers = values.filter(isNumber).sort((first, second) => first - second);
|
|
45
44
|
length = numbers.length;
|
|
45
|
+
if (length === 1) return numbers[0];
|
|
46
46
|
if (length % 2 === 0) {
|
|
47
47
|
const first = length / 2 - 1;
|
|
48
48
|
const second = length / 2;
|
package/package.json
CHANGED
|
@@ -145,9 +145,9 @@
|
|
|
145
145
|
"types": "./types/internal/value/equal.d.ts",
|
|
146
146
|
"default": "./dist/internal/value/equal.js"
|
|
147
147
|
},
|
|
148
|
-
"./value/
|
|
149
|
-
"types": "./types/value/
|
|
150
|
-
"default": "./dist/value/
|
|
148
|
+
"./value/handle": {
|
|
149
|
+
"types": "./types/value/handle.d.ts",
|
|
150
|
+
"default": "./dist/value/handle.js"
|
|
151
151
|
},
|
|
152
152
|
"./value/merge": {
|
|
153
153
|
"types": "./types/value/merge.d.ts",
|
|
@@ -184,5 +184,5 @@
|
|
|
184
184
|
},
|
|
185
185
|
"type": "module",
|
|
186
186
|
"types": "./types/index.d.ts",
|
|
187
|
-
"version": "0.
|
|
187
|
+
"version": "0.155.0"
|
|
188
188
|
}
|
package/src/function/assert.ts
CHANGED
|
@@ -47,7 +47,7 @@ function assertCondition<Value>(
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
|
-
* Asserts that a value is defined
|
|
50
|
+
* Asserts that a value is defined throwing an error if it is not
|
|
51
51
|
* @param value Value to assert
|
|
52
52
|
* @param message Error message
|
|
53
53
|
*/
|
|
@@ -55,7 +55,7 @@ function assertDefined<Value>(
|
|
|
55
55
|
value: unknown,
|
|
56
56
|
message?: string,
|
|
57
57
|
): asserts value is Exclude<Value, null | undefined> {
|
|
58
|
-
assert(() => value != null, message ??
|
|
58
|
+
assert(() => value != null, message ?? MESSAGE_VALUE_DEFINED);
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
/**
|
|
@@ -96,6 +96,6 @@ function assertIs<Value>(
|
|
|
96
96
|
|
|
97
97
|
// #region Variables
|
|
98
98
|
|
|
99
|
-
const
|
|
99
|
+
const MESSAGE_VALUE_DEFINED = 'Expected value to be defined';
|
|
100
100
|
|
|
101
101
|
// #endregion
|
package/src/index.ts
CHANGED
|
@@ -13,6 +13,7 @@ export * from './internal/string';
|
|
|
13
13
|
export * from './internal/value/compare';
|
|
14
14
|
export * from './internal/value/equal';
|
|
15
15
|
export * from './internal/value/get';
|
|
16
|
+
export * from './internal/value/has';
|
|
16
17
|
export * from './internal/value/set';
|
|
17
18
|
|
|
18
19
|
export * from './string/case';
|
package/src/internal/is.ts
CHANGED
|
@@ -20,6 +20,19 @@ export function isConstructor(value: unknown): value is Constructor {
|
|
|
20
20
|
return typeof value === 'function' && value.prototype?.constructor === value;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Is the value an instance of the constructor?
|
|
25
|
+
* @param constructor Class constructor
|
|
26
|
+
* @param value Value to check
|
|
27
|
+
* @returns `true` if the value is an instance of the constructor, otherwise `false`
|
|
28
|
+
*/
|
|
29
|
+
export function isInstanceOf<Instance>(
|
|
30
|
+
constructor: Constructor<Instance>,
|
|
31
|
+
value: unknown,
|
|
32
|
+
): value is Instance {
|
|
33
|
+
return isConstructor(constructor) && value instanceof constructor;
|
|
34
|
+
}
|
|
35
|
+
|
|
23
36
|
/**
|
|
24
37
|
* Is the value a key?
|
|
25
38
|
* @param value Value to check
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type {NestedKeys, NestedValue, PlainObject, ToString} from '../../models';
|
|
2
|
-
import {
|
|
2
|
+
import {getNestedValue} from './misc';
|
|
3
3
|
|
|
4
4
|
// #region Functions
|
|
5
5
|
|
|
@@ -28,32 +28,7 @@ export function getValue<Data extends PlainObject>(
|
|
|
28
28
|
): unknown;
|
|
29
29
|
|
|
30
30
|
export function getValue(data: PlainObject, path: string, ignoreCase?: boolean): unknown {
|
|
31
|
-
|
|
32
|
-
typeof data !== 'object' ||
|
|
33
|
-
data === null ||
|
|
34
|
-
typeof path !== 'string' ||
|
|
35
|
-
path.trim().length === 0
|
|
36
|
-
) {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const shouldIgnoreCase = ignoreCase === true;
|
|
41
|
-
const paths = getPaths(path, shouldIgnoreCase);
|
|
42
|
-
|
|
43
|
-
if (typeof paths === 'string') {
|
|
44
|
-
return handleValue(data, paths, null, true, shouldIgnoreCase);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const {length} = paths;
|
|
48
|
-
|
|
49
|
-
let index = 0;
|
|
50
|
-
let value: PlainObject = data;
|
|
51
|
-
|
|
52
|
-
while (index < length && value != null) {
|
|
53
|
-
value = handleValue(value, paths[index++], null, true, shouldIgnoreCase) as PlainObject;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return value as never;
|
|
31
|
+
return getNestedValue(data, path, ignoreCase === true).value;
|
|
57
32
|
}
|
|
58
33
|
|
|
59
34
|
// #endregion
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type {NestedKeys, NestedValue, PlainObject, ToString} from '../../models';
|
|
2
|
+
import {getNestedValue} from './misc';
|
|
3
|
+
|
|
4
|
+
// #region Types
|
|
5
|
+
|
|
6
|
+
export type HasValue<Value> = {
|
|
7
|
+
exists: boolean;
|
|
8
|
+
value: Value;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// #endregion
|
|
12
|
+
|
|
13
|
+
// #region Functions
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Check if a nested property is defined in an object
|
|
17
|
+
* @param data Object to check in
|
|
18
|
+
* @param path Path for property
|
|
19
|
+
* @return `true` if the property exists, `false` otherwise
|
|
20
|
+
*/
|
|
21
|
+
export function hasValue<Data extends PlainObject, Path extends NestedKeys<Data>>(
|
|
22
|
+
data: Data,
|
|
23
|
+
path: Path,
|
|
24
|
+
): boolean;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Check if a nested property is defined in an object
|
|
28
|
+
* @param data Object to check in
|
|
29
|
+
* @param path Path for property
|
|
30
|
+
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
31
|
+
* @return `true` if the property exists, `false` otherwise
|
|
32
|
+
*/
|
|
33
|
+
export function hasValue<Data extends PlainObject>(
|
|
34
|
+
data: Data,
|
|
35
|
+
path: string,
|
|
36
|
+
ignoreCase?: boolean,
|
|
37
|
+
): boolean;
|
|
38
|
+
|
|
39
|
+
export function hasValue(data: PlainObject, path: string, ignoreCase?: boolean): boolean {
|
|
40
|
+
return getNestedValue(data, path, ignoreCase === true).exists;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
hasValue.get = getWithHasValue;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Check if a nested property is defined in an object, and get its value if it is
|
|
47
|
+
* @param data Object to check in
|
|
48
|
+
* @param path Path for property
|
|
49
|
+
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
50
|
+
* @return Result object
|
|
51
|
+
*/
|
|
52
|
+
function getWithHasValue<Data extends PlainObject, Path extends NestedKeys<Data>>(
|
|
53
|
+
data: Data,
|
|
54
|
+
path: Path,
|
|
55
|
+
ignoreCase?: boolean,
|
|
56
|
+
): HasValue<NestedValue<Data, ToString<Path>>>;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Check if a nested property is defined in an object, and get its value if it is
|
|
60
|
+
* @param data Object to check in
|
|
61
|
+
* @param path Path for property
|
|
62
|
+
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
63
|
+
* @return Result object
|
|
64
|
+
*/
|
|
65
|
+
function getWithHasValue<Data extends PlainObject>(
|
|
66
|
+
data: Data,
|
|
67
|
+
path: string,
|
|
68
|
+
ignoreCase?: boolean,
|
|
69
|
+
): HasValue<unknown>;
|
|
70
|
+
|
|
71
|
+
function getWithHasValue(data: PlainObject, path: string, ignoreCase?: boolean): HasValue<unknown> {
|
|
72
|
+
return getNestedValue(data, path, ignoreCase === true);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// #endregion
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {PlainObject} from '../../models';
|
|
2
2
|
import {ignoreKey} from '../string';
|
|
3
3
|
|
|
4
4
|
// #region Functions
|
|
5
5
|
|
|
6
|
-
function findKey(needle: string, haystack:
|
|
6
|
+
export function findKey(needle: string, haystack: object): string {
|
|
7
7
|
const keys = Object.keys(haystack);
|
|
8
8
|
const normalized = keys.map(key => key.toLowerCase());
|
|
9
9
|
const index = normalized.indexOf(needle.toLowerCase());
|
|
@@ -11,6 +11,46 @@ function findKey(needle: string, haystack: ArrayOrPlainObject): string {
|
|
|
11
11
|
return index > -1 ? keys[index] : needle;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
export function getNestedValue(
|
|
15
|
+
data: object,
|
|
16
|
+
path: string,
|
|
17
|
+
ignoreCase: boolean,
|
|
18
|
+
): {exists: boolean; value: unknown} {
|
|
19
|
+
if (
|
|
20
|
+
typeof data !== 'object' ||
|
|
21
|
+
data === null ||
|
|
22
|
+
typeof path !== 'string' ||
|
|
23
|
+
path.trim().length === 0
|
|
24
|
+
) {
|
|
25
|
+
return {exists: false, value: undefined};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const shouldIgnoreCase = ignoreCase === true;
|
|
29
|
+
const paths = getPaths(path, shouldIgnoreCase);
|
|
30
|
+
|
|
31
|
+
if (typeof paths === 'string') {
|
|
32
|
+
return handleValue(data, paths, null, true, shouldIgnoreCase);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const {length} = paths;
|
|
36
|
+
|
|
37
|
+
let current = data;
|
|
38
|
+
|
|
39
|
+
for (let index = 0; index < length; index += 1) {
|
|
40
|
+
const part = paths[index];
|
|
41
|
+
|
|
42
|
+
const handled = handleValue(current, part, null, true, shouldIgnoreCase);
|
|
43
|
+
|
|
44
|
+
if (!handled.exists) {
|
|
45
|
+
return handled;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
current = handled.value as object;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {exists: true, value: current};
|
|
52
|
+
}
|
|
53
|
+
|
|
14
54
|
export function getPaths(path: string, lowercase: boolean): string | string[] {
|
|
15
55
|
const normalized = lowercase ? path.toLowerCase() : path;
|
|
16
56
|
|
|
@@ -22,21 +62,41 @@ export function getPaths(path: string, lowercase: boolean): string | string[] {
|
|
|
22
62
|
}
|
|
23
63
|
|
|
24
64
|
export function handleValue(
|
|
25
|
-
data:
|
|
65
|
+
data: object,
|
|
66
|
+
path: string,
|
|
67
|
+
value: unknown,
|
|
68
|
+
get: true,
|
|
69
|
+
ignoreCase: boolean,
|
|
70
|
+
): {exists: boolean; value: unknown};
|
|
71
|
+
|
|
72
|
+
export function handleValue(
|
|
73
|
+
data: object,
|
|
74
|
+
path: string,
|
|
75
|
+
value: unknown,
|
|
76
|
+
get: false,
|
|
77
|
+
ignoreCase: boolean,
|
|
78
|
+
): void;
|
|
79
|
+
|
|
80
|
+
export function handleValue(
|
|
81
|
+
data: object,
|
|
26
82
|
path: string,
|
|
27
83
|
value: unknown,
|
|
28
84
|
get: boolean,
|
|
29
85
|
ignoreCase: boolean,
|
|
30
|
-
): unknown {
|
|
86
|
+
): {exists: boolean; value: unknown} | void {
|
|
31
87
|
if (typeof data === 'object' && data !== null && !ignoreKey(path)) {
|
|
32
88
|
const key = ignoreCase ? findKey(path, data) : path;
|
|
33
89
|
|
|
34
90
|
if (get) {
|
|
35
|
-
return data[key as never];
|
|
91
|
+
return {exists: key in data, value: data[key as never]};
|
|
36
92
|
}
|
|
37
93
|
|
|
38
94
|
(data as PlainObject)[key] = typeof value === 'function' ? value(data[key as never]) : value;
|
|
39
95
|
}
|
|
96
|
+
|
|
97
|
+
if (get) {
|
|
98
|
+
return {exists: false, value: undefined};
|
|
99
|
+
}
|
|
40
100
|
}
|
|
41
101
|
|
|
42
102
|
// #endregion
|
|
@@ -44,12 +44,12 @@ export function setValue<Data extends PlainObject>(
|
|
|
44
44
|
ignoreCase?: boolean,
|
|
45
45
|
): Data;
|
|
46
46
|
|
|
47
|
-
export function setValue
|
|
48
|
-
data:
|
|
47
|
+
export function setValue(
|
|
48
|
+
data: object,
|
|
49
49
|
path: string,
|
|
50
50
|
value: unknown,
|
|
51
51
|
ignoreCase?: boolean,
|
|
52
|
-
):
|
|
52
|
+
): object {
|
|
53
53
|
if (
|
|
54
54
|
typeof data !== 'object' ||
|
|
55
55
|
data === null ||
|
|
@@ -71,7 +71,7 @@ export function setValue<Data extends PlainObject>(
|
|
|
71
71
|
const {length} = paths;
|
|
72
72
|
const lastIndex = length - 1;
|
|
73
73
|
|
|
74
|
-
let target
|
|
74
|
+
let target = data;
|
|
75
75
|
|
|
76
76
|
for (let index = 0; index < length; index += 1) {
|
|
77
77
|
const currentPath = paths[index];
|
|
@@ -82,7 +82,7 @@ export function setValue<Data extends PlainObject>(
|
|
|
82
82
|
break;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
let next = handleValue(target, currentPath, null, true, shouldIgnoreCase);
|
|
85
|
+
let next = handleValue(target, currentPath, null, true, shouldIgnoreCase).value;
|
|
86
86
|
|
|
87
87
|
if (typeof next !== 'object' || next === null) {
|
|
88
88
|
const nextPath = paths[index + 1];
|
|
@@ -96,7 +96,7 @@ export function setValue<Data extends PlainObject>(
|
|
|
96
96
|
(target as PlainObject)[currentPath] = next;
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
target = next as
|
|
99
|
+
target = next as object;
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
return data;
|
package/src/is.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {getArray} from './array/get';
|
|
2
|
-
import {
|
|
2
|
+
import {isNumber} from './internal/is';
|
|
3
3
|
import {getString} from './internal/string';
|
|
4
|
-
import type {
|
|
4
|
+
import type {Primitive} from './models';
|
|
5
5
|
|
|
6
6
|
// #region Functions
|
|
7
7
|
|
|
@@ -31,19 +31,6 @@ export function isEmpty(value: unknown): boolean {
|
|
|
31
31
|
return true;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
/**
|
|
35
|
-
* Is the value an instance of the constructor?
|
|
36
|
-
* @param constructor Class constructor
|
|
37
|
-
* @param value Value to check
|
|
38
|
-
* @returns `true` if the value is an instance of the constructor, otherwise `false`
|
|
39
|
-
*/
|
|
40
|
-
export function isInstanceOf<Instance>(
|
|
41
|
-
constructor: Constructor<Instance>,
|
|
42
|
-
value: unknown,
|
|
43
|
-
): value is Instance {
|
|
44
|
-
return isConstructor(constructor) && value instanceof constructor;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
34
|
/**
|
|
48
35
|
* Is the value not `undefined` or `null`?
|
|
49
36
|
* @param value Value to check
|
|
@@ -125,6 +112,7 @@ const EXPRESSION_WHITESPACE = /^\s*$/;
|
|
|
125
112
|
export {
|
|
126
113
|
isArrayOrPlainObject,
|
|
127
114
|
isConstructor,
|
|
115
|
+
isInstanceOf,
|
|
128
116
|
isKey,
|
|
129
117
|
isNumber,
|
|
130
118
|
isPlainObject,
|
|
@@ -1,8 +1,46 @@
|
|
|
1
1
|
import type { Constructor } from '../models';
|
|
2
2
|
export type Asserter<Value> = (value: unknown) => asserts value is Value;
|
|
3
|
+
/**
|
|
4
|
+
* Asserts that a condition is true, throwing an error if it is not
|
|
5
|
+
* @param condition Condition to assert
|
|
6
|
+
* @param message Error message
|
|
7
|
+
* @param error Error constructor
|
|
8
|
+
*/
|
|
3
9
|
export declare function assert<Condition extends () => boolean>(condition: Condition, message: string, error?: ErrorConstructor): asserts condition;
|
|
4
10
|
export declare namespace assert {
|
|
5
|
-
var condition:
|
|
6
|
-
var
|
|
7
|
-
var
|
|
11
|
+
var condition: typeof assertCondition;
|
|
12
|
+
var defined: typeof assertDefined;
|
|
13
|
+
var instanceOf: typeof assertInstanceOf;
|
|
14
|
+
var is: typeof assertIs;
|
|
8
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Creates an asserter that asserts a condition is true, throwing an error if it is not
|
|
18
|
+
* @param condition Condition to assert
|
|
19
|
+
* @param message Error message
|
|
20
|
+
* @param error Error constructor
|
|
21
|
+
* @returns Asserter
|
|
22
|
+
*/
|
|
23
|
+
declare function assertCondition<Value>(condition: (value: unknown) => boolean, message: string, error?: ErrorConstructor): Asserter<Value>;
|
|
24
|
+
/**
|
|
25
|
+
* Asserts that a value is defined throwing an error if it is not
|
|
26
|
+
* @param value Value to assert
|
|
27
|
+
* @param message Error message
|
|
28
|
+
*/
|
|
29
|
+
declare function assertDefined<Value>(value: unknown, message?: string): asserts value is Exclude<Value, null | undefined>;
|
|
30
|
+
/**
|
|
31
|
+
* Creates an asserter that asserts a value is an instance of a constructor, throwing an error if it is not
|
|
32
|
+
* @param constructor Constructor to check against
|
|
33
|
+
* @param message Error message
|
|
34
|
+
* @param error Error constructor
|
|
35
|
+
* @returns Asserter
|
|
36
|
+
*/
|
|
37
|
+
declare function assertInstanceOf<Value>(constructor: Constructor<Value>, message: string, error?: ErrorConstructor): Asserter<Value>;
|
|
38
|
+
/**
|
|
39
|
+
* Creates an asserter that asserts a value is of a specific type, throwing an error if it is not
|
|
40
|
+
* @param condition Type guard function to check the value
|
|
41
|
+
* @param message Error message
|
|
42
|
+
* @param error Error constructor
|
|
43
|
+
* @returns Asserter
|
|
44
|
+
*/
|
|
45
|
+
declare function assertIs<Value>(condition: (value: unknown) => value is Value, message: string, error?: ErrorConstructor): Asserter<Value>;
|
|
46
|
+
export {};
|
package/types/index.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export * from './internal/string';
|
|
|
11
11
|
export * from './internal/value/compare';
|
|
12
12
|
export * from './internal/value/equal';
|
|
13
13
|
export * from './internal/value/get';
|
|
14
|
+
export * from './internal/value/has';
|
|
14
15
|
export * from './internal/value/set';
|
|
15
16
|
export * from './string/case';
|
|
16
17
|
export * from './string/match';
|
package/types/internal/is.d.ts
CHANGED
|
@@ -11,6 +11,13 @@ export declare function isArrayOrPlainObject(value: unknown): value is ArrayOrPl
|
|
|
11
11
|
* @returns `true` if the value is a constructor function, otherwise `false`
|
|
12
12
|
*/
|
|
13
13
|
export declare function isConstructor(value: unknown): value is Constructor;
|
|
14
|
+
/**
|
|
15
|
+
* Is the value an instance of the constructor?
|
|
16
|
+
* @param constructor Class constructor
|
|
17
|
+
* @param value Value to check
|
|
18
|
+
* @returns `true` if the value is an instance of the constructor, otherwise `false`
|
|
19
|
+
*/
|
|
20
|
+
export declare function isInstanceOf<Instance>(constructor: Constructor<Instance>, value: unknown): value is Instance;
|
|
14
21
|
/**
|
|
15
22
|
* Is the value a key?
|
|
16
23
|
* @param value Value to check
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { NestedKeys, NestedValue, PlainObject, ToString } from '../../models';
|
|
2
|
+
export type HasValue<Value> = {
|
|
3
|
+
exists: boolean;
|
|
4
|
+
value: Value;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Check if a nested property is defined in an object
|
|
8
|
+
* @param data Object to check in
|
|
9
|
+
* @param path Path for property
|
|
10
|
+
* @return `true` if the property exists, `false` otherwise
|
|
11
|
+
*/
|
|
12
|
+
export declare function hasValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Check if a nested property is defined in an object
|
|
15
|
+
* @param data Object to check in
|
|
16
|
+
* @param path Path for property
|
|
17
|
+
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
18
|
+
* @return `true` if the property exists, `false` otherwise
|
|
19
|
+
*/
|
|
20
|
+
export declare function hasValue<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): boolean;
|
|
21
|
+
export declare namespace hasValue {
|
|
22
|
+
var get: typeof getWithHasValue;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Check if a nested property is defined in an object, and get its value if it is
|
|
26
|
+
* @param data Object to check in
|
|
27
|
+
* @param path Path for property
|
|
28
|
+
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
29
|
+
* @return Result object
|
|
30
|
+
*/
|
|
31
|
+
declare function getWithHasValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path, ignoreCase?: boolean): HasValue<NestedValue<Data, ToString<Path>>>;
|
|
32
|
+
/**
|
|
33
|
+
* Check if a nested property is defined in an object, and get its value if it is
|
|
34
|
+
* @param data Object to check in
|
|
35
|
+
* @param path Path for property
|
|
36
|
+
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
37
|
+
* @return Result object
|
|
38
|
+
*/
|
|
39
|
+
declare function getWithHasValue<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): HasValue<unknown>;
|
|
40
|
+
export {};
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
export declare function findKey(needle: string, haystack: object): string;
|
|
2
|
+
export declare function getNestedValue(data: object, path: string, ignoreCase: boolean): {
|
|
3
|
+
exists: boolean;
|
|
4
|
+
value: unknown;
|
|
5
|
+
};
|
|
2
6
|
export declare function getPaths(path: string, lowercase: boolean): string | string[];
|
|
3
|
-
export declare function handleValue(data:
|
|
7
|
+
export declare function handleValue(data: object, path: string, value: unknown, get: true, ignoreCase: boolean): {
|
|
8
|
+
exists: boolean;
|
|
9
|
+
value: unknown;
|
|
10
|
+
};
|
|
11
|
+
export declare function handleValue(data: object, path: string, value: unknown, get: false, ignoreCase: boolean): void;
|
package/types/is.d.ts
CHANGED
|
@@ -1,17 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Primitive } from './models';
|
|
2
2
|
/**
|
|
3
3
|
* Is the value empty, or only containing `null` or `undefined` values?
|
|
4
4
|
* @param value Value to check
|
|
5
5
|
* @returns `true` if the value is considered empty, otherwise `false`
|
|
6
6
|
*/
|
|
7
7
|
export declare function isEmpty(value: unknown): boolean;
|
|
8
|
-
/**
|
|
9
|
-
* Is the value an instance of the constructor?
|
|
10
|
-
* @param constructor Class constructor
|
|
11
|
-
* @param value Value to check
|
|
12
|
-
* @returns `true` if the value is an instance of the constructor, otherwise `false`
|
|
13
|
-
*/
|
|
14
|
-
export declare function isInstanceOf<Instance>(constructor: Constructor<Instance>, value: unknown): value is Instance;
|
|
15
8
|
/**
|
|
16
9
|
* Is the value not `undefined` or `null`?
|
|
17
10
|
* @param value Value to check
|
|
@@ -54,4 +47,4 @@ export declare function isObject(value: unknown): value is object;
|
|
|
54
47
|
* @returns `true` if the value matches, otherwise `false`
|
|
55
48
|
*/
|
|
56
49
|
export declare function isPrimitive(value: unknown): value is Primitive;
|
|
57
|
-
export { isArrayOrPlainObject, isConstructor, isKey, isNumber, isPlainObject, isTypedArray, } from './internal/is';
|
|
50
|
+
export { isArrayOrPlainObject, isConstructor, isInstanceOf, isKey, isNumber, isPlainObject, isTypedArray, } from './internal/is';
|