Skip to content

Commit 92c836b

Browse files
committed
add keep types on recursion
1 parent 682f62f commit 92c836b

15 files changed

Lines changed: 135 additions & 41 deletions

File tree

lib/modules/extends/camelcase-keys-object/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ function camelKeys(obj, opt) {
2525
var nkey = js_camelcase_1.default(key);
2626
if (opt.recursive) {
2727
if (utils_1.isValidObject(value)) {
28-
value = camelKeys(value, opt);
28+
if (!utils_1.belongToTypes(value, opt.keepTypesOnRecursion)) {
29+
value = camelKeys(value, opt);
30+
}
2931
}
3032
else if (opt.recursiveInArray && utils_1.isArrayObject(value)) {
3133
value = __spreadArrays(value).map(function (v) {
3234
var ret = v;
3335
if (utils_1.isValidObject(v)) {
3436
// object in array
35-
ret = camelKeys(v, opt);
37+
if (!utils_1.belongToTypes(ret, opt.keepTypesOnRecursion)) {
38+
ret = camelKeys(v, opt);
39+
}
3640
}
3741
else if (utils_1.isArrayObject(v)) {
3842
// array in array

lib/modules/extends/lowercase-keys-object/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ function lowerKeys(obj, opt) {
2424
var nkey = key.toLowerCase();
2525
if (opt.recursive) {
2626
if (utils_1.isValidObject(value)) {
27-
value = lowerKeys(value, opt);
27+
if (!utils_1.belongToTypes(value, opt.keepTypesOnRecursion)) {
28+
value = lowerKeys(value, opt);
29+
}
2830
}
2931
else if (opt.recursiveInArray && utils_1.isArrayObject(value)) {
3032
value = __spreadArrays(value).map(function (v) {
3133
var ret = v;
3234
if (utils_1.isValidObject(v)) {
3335
// object in array
34-
ret = lowerKeys(v, opt);
36+
if (!utils_1.belongToTypes(ret, opt.keepTypesOnRecursion)) {
37+
ret = lowerKeys(v, opt);
38+
}
3539
}
3640
else if (utils_1.isArrayObject(v)) {
3741
// array in array

lib/modules/extends/pascalcase-keys-object/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ function pascalKeys(obj, opt) {
2525
var nkey = js_pascalcase_1.default(key);
2626
if (opt.recursive) {
2727
if (utils_1.isValidObject(value)) {
28-
value = pascalKeys(value, opt);
28+
if (!utils_1.belongToTypes(value, opt.keepTypesOnRecursion)) {
29+
value = pascalKeys(value, opt);
30+
}
2931
}
3032
else if (opt.recursiveInArray && utils_1.isArrayObject(value)) {
3133
value = __spreadArrays(value).map(function (v) {
3234
var ret = v;
3335
if (utils_1.isValidObject(v)) {
3436
// object in array
35-
ret = pascalKeys(v, opt);
37+
if (!utils_1.belongToTypes(ret, opt.keepTypesOnRecursion)) {
38+
ret = pascalKeys(v, opt);
39+
}
3640
}
3741
else if (utils_1.isArrayObject(v)) {
3842
// array in array

lib/modules/extends/snakecase-keys-object/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ function snakeKeys(obj, opt) {
2525
var nkey = js_snakecase_1.default(key);
2626
if (opt.recursive) {
2727
if (utils_1.isValidObject(value)) {
28-
value = snakeKeys(value, opt);
28+
if (!utils_1.belongToTypes(value, opt.keepTypesOnRecursion)) {
29+
value = snakeKeys(value, opt);
30+
}
2931
}
3032
else if (opt.recursiveInArray && utils_1.isArrayObject(value)) {
3133
value = __spreadArrays(value).map(function (v) {
3234
var ret = v;
3335
if (utils_1.isValidObject(v)) {
3436
// object in array
35-
ret = snakeKeys(v, opt);
37+
if (!utils_1.belongToTypes(ret, opt.keepTypesOnRecursion)) {
38+
ret = snakeKeys(v, opt);
39+
}
3640
}
3741
else if (utils_1.isArrayObject(v)) {
3842
// array in array

lib/modules/extends/uppercase-keys-object/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ function upperKeys(obj, opt) {
2424
var nkey = key.toUpperCase();
2525
if (opt.recursive) {
2626
if (utils_1.isValidObject(value)) {
27-
value = upperKeys(value, opt);
27+
if (!utils_1.belongToTypes(value, opt.keepTypesOnRecursion)) {
28+
value = upperKeys(value, opt);
29+
}
2830
}
2931
else if (opt.recursiveInArray && utils_1.isArrayObject(value)) {
3032
value = __spreadArrays(value).map(function (v) {
3133
var ret = v;
3234
if (utils_1.isValidObject(v)) {
3335
// object in array
34-
ret = upperKeys(v, opt);
36+
if (!utils_1.belongToTypes(ret, opt.keepTypesOnRecursion)) {
37+
ret = upperKeys(v, opt);
38+
}
3539
}
3640
else if (utils_1.isArrayObject(v)) {
3741
// array in array

lib/modules/extends/utils.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
* @param recursiveInArray: recursive if ${recursive} is `true` and value of subkey
66
* is an array. All elements in array (value of subkey) will be recursive.
77
* If ${recursiveInArray} is not set, default is `false`.
8+
* @param keepTypesOnRecursion: list of types will be keep value on recursion.
9+
* Example Date, RegExp. These types will be right-hand side of 'instanceof' operator.
810
*/
911
export interface Options {
1012
recursive: boolean;
1113
recursiveInArray?: boolean;
14+
keepTypesOnRecursion?: any[];
1215
}
1316
/**
1417
* Default options for convert function. This option is not recursive.
@@ -17,3 +20,4 @@ export declare const DefaultOption: Options;
1720
export declare const validateOptions: (opt?: Options) => Options;
1821
export declare const isArrayObject: (obj: any) => boolean;
1922
export declare const isValidObject: (obj: any) => boolean;
23+
export declare const belongToTypes: (obj: any, types?: any[] | undefined) => boolean;

lib/modules/extends/utils.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.isValidObject = exports.isArrayObject = exports.validateOptions = exports.DefaultOption = void 0;
3+
exports.belongToTypes = exports.isValidObject = exports.isArrayObject = exports.validateOptions = exports.DefaultOption = void 0;
44
/**
55
* Default options for convert function. This option is not recursive.
66
*/
77
exports.DefaultOption = {
88
recursive: false,
9-
recursiveInArray: false
9+
recursiveInArray: false,
10+
keepTypesOnRecursion: []
1011
};
1112
exports.validateOptions = function (opt) {
1213
if (opt === void 0) { opt = exports.DefaultOption; }
@@ -18,9 +19,6 @@ exports.validateOptions = function (opt) {
1819
}
1920
return opt;
2021
};
21-
exports.isArrayObject = function (obj) {
22-
return obj != null && Array.isArray(obj);
23-
};
24-
exports.isValidObject = function (obj) {
25-
return obj != null && typeof obj === 'object' && !Array.isArray(obj);
26-
};
22+
exports.isArrayObject = function (obj) { return obj != null && Array.isArray(obj); };
23+
exports.isValidObject = function (obj) { return obj != null && typeof obj === 'object' && !Array.isArray(obj); };
24+
exports.belongToTypes = function (obj, types) { return (types || []).some(function (Type) { return obj instanceof Type; }); };

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-convert-case",
3-
"version": "4.1.1",
3+
"version": "4.1.2",
44
"description": "JavaScript Convert String and Keys of Object between cases (camelCase, snake_case, PascalCase, dot.case, path/case, text case, Sentence case, Header Case, UPPERCASE, lowercase). Use for both Node.JS and Browser",
55
"scripts": {
66
"build": "tsc",

src/modules/extends/camelcase-keys-object/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isValidObject, Options, DefaultOption, isArrayObject, validateOptions } from '../utils';
1+
import { isValidObject, Options, DefaultOption, isArrayObject, validateOptions, belongToTypes } from '../utils';
22
import toCamelCase from '../../js-camelcase';
33

44
/**
@@ -16,13 +16,17 @@ export default function camelKeys(obj: any, opt: Options = DefaultOption): objec
1616
const nkey = toCamelCase(key);
1717
if (opt.recursive) {
1818
if (isValidObject(value)) {
19-
value = camelKeys(value, opt);
19+
if (!belongToTypes(value, opt.keepTypesOnRecursion)) {
20+
value = camelKeys(value, opt);
21+
}
2022
} else if (opt.recursiveInArray && isArrayObject(value)) {
2123
value = [...value].map((v) => {
2224
let ret = v;
2325
if (isValidObject(v)) {
2426
// object in array
25-
ret = camelKeys(v, opt);
27+
if (!belongToTypes(ret, opt.keepTypesOnRecursion)) {
28+
ret = camelKeys(v, opt);
29+
}
2630
} else if (isArrayObject(v)) {
2731
// array in array
2832
// workaround by using an object holding array value

src/modules/extends/lowercase-keys-object/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isValidObject, Options, DefaultOption, isArrayObject, validateOptions } from '../utils';
1+
import { isValidObject, Options, DefaultOption, isArrayObject, validateOptions, belongToTypes } from '../utils';
22

33
/**
44
* Convert string keys in an object to lowercase format.
@@ -15,13 +15,17 @@ export default function lowerKeys(obj: any, opt: Options = DefaultOption): objec
1515
const nkey = key.toLowerCase();
1616
if (opt.recursive) {
1717
if (isValidObject(value)) {
18-
value = lowerKeys(value, opt);
18+
if (!belongToTypes(value, opt.keepTypesOnRecursion)) {
19+
value = lowerKeys(value, opt);
20+
}
1921
} else if (opt.recursiveInArray && isArrayObject(value)) {
2022
value = [...value].map((v) => {
2123
let ret = v;
2224
if (isValidObject(v)) {
2325
// object in array
24-
ret = lowerKeys(v, opt);
26+
if (!belongToTypes(ret, opt.keepTypesOnRecursion)) {
27+
ret = lowerKeys(v, opt);
28+
}
2529
} else if (isArrayObject(v)) {
2630
// array in array
2731
// workaround by using an object holding array value

0 commit comments

Comments
 (0)