diff --git a/.prettierrc b/.prettierrc index a0d1c9a..94d3ae6 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,5 @@ { "printWidth": 120, - "trailingComma": "all", + "trailingComma": "none", "singleQuote": true } diff --git a/README.md b/README.md index ff9454a..37a6844 100644 --- a/README.md +++ b/README.md @@ -161,24 +161,24 @@ You can see more examples in directory [`./test/browser`](./test/browser) #### Convert String -* [`toCamelCase`](#tocamelcase) -* [`toSnakeCase`](#tosnakecase) -* [`toPascalCase`](#topascalcase) -* [`toDotCase`](#todotcase) -* [`toPathCase`](#topathcase) -* [`toTextCase`](#totextcase) -* [`toSentenceCase`](#tosentencecase) -* [`toHeaderCase`](#toheadercase) -* [`toLowerCase`](#tolowercase) -* [`toUpperCase`](#touppercase) +- [`toCamelCase`](#tocamelcase) +- [`toSnakeCase`](#tosnakecase) +- [`toPascalCase`](#topascalcase) +- [`toDotCase`](#todotcase) +- [`toPathCase`](#topathcase) +- [`toTextCase`](#totextcase) +- [`toSentenceCase`](#tosentencecase) +- [`toHeaderCase`](#toheadercase) +- [`toLowerCase`](#tolowercase) +- [`toUpperCase`](#touppercase) #### Convert Keys of Object -* [`lowerKeys`](#lowerkeys) -* [`upperKeys`](#upperkeys) -* [`camelKeys`](#camelkeys) -* [`snakeKeys`](#snakekeys) -* [`pascalKeys`](#pascalkeys) +- [`lowerKeys`](#lowerkeys) +- [`upperKeys`](#upperkeys) +- [`camelKeys`](#camelkeys) +- [`snakeKeys`](#snakekeys) +- [`pascalKeys`](#pascalkeys) #### toCamelCase @@ -263,15 +263,17 @@ console.log(jsConvert.toUpperCase('param-case')); // PARAM-CASE #### lowerKeys ```ts -const lowerKeys(obj: any, { recursive: boolean = false }): object | null +const lowerKeys(obj: any, { recursive: boolean = false, recursiveInArray: boolean = false }): object | null ``` -Return a new object which keys is *lowercase* format. Support lowerKeys **recursively**. Default is `false`. +Return a new object which keys is _lowercase_ format. Support lowerKeys **recursively**. Default is `false`. ```js console.log(jsConvert.lowerKeys(obj)); // or recursive console.log(jsConvert.lowerKeys(obj, { recursive: true })); +// or recursive in sub-keys with value is an array +console.log(jsConvert.lowerKeys(obj, { recursive: true, recursiveInArray: true })); /** { camelcase: 1, @@ -298,15 +300,17 @@ console.log(jsConvert.lowerKeys([1, 2, 3])); #### upperKeys ```ts -const upperKeys(obj: any, { recursive: boolean = false }): object | null +const upperKeys(obj: any, { recursive: boolean = false, recursiveInArray: boolean = false }): object | null ``` -Return a new object which keys is *UPPERCASE* format. Support upperKeys **recursively**. Default is `false` +Return a new object which keys is _UPPERCASE_ format. Support upperKeys **recursively**. Default is `false` ```js console.log(jsConvert.upperKeys(obj)); // or recursive console.log(jsConvert.upperKeys(obj, { recursive: true })); +// or recursive in sub-keys with value is an array +console.log(jsConvert.upperKeys(obj, { recursive: true, recursiveInArray: true })); /** { CAMELCASE: 1, @@ -333,15 +337,17 @@ console.log(jsConvert.upperKeys([1, 2, 3])); #### camelKeys ```ts -const camelKeys(obj: any, { recursive: boolean = false }): object | null +const camelKeys(obj: any, { recursive: boolean = false, recursiveInArray: boolean = false }): object | null ``` -Return a new object which keys is *camelCase* format. Support camelKeys **recursively**. Default is `false`. +Return a new object which keys is _camelCase_ format. Support camelKeys **recursively**. Default is `false`. ```js console.log(jsConvert.camelKeys(obj)); // or recursive console.log(jsConvert.camelKeys(obj, { recursive: true })); +// or recursive in sub-keys with value is an array +console.log(jsConvert.camelKeys(obj, { recursive: true, recursiveInArray: true })); /** { camelCase: 1, @@ -368,15 +374,17 @@ console.log(jsConvert.camelKeys([1, 2, 3])); #### snakeKeys ```ts -const snakeKeys(obj: any, { recursive: boolean = false }): object | null +const snakeKeys(obj: any, { recursive: boolean = false, recursiveInArray: boolean = false }): object | null ``` -Return a new object which keys is *snake_case* format. Support snakeKeys **recursively**. Default is `false`. +Return a new object which keys is _snake_case_ format. Support snakeKeys **recursively**. Default is `false`. ```js console.log(jsConvert.snakeKeys(obj)); // or recursive console.log(jsConvert.snakeKeys(obj, { recursive: true })); +// or recursive in sub-keys with value is an array +console.log(jsConvert.snakeKeys(obj, { recursive: true, recursiveInArray: true })); /** { camel_case: 1, @@ -403,15 +411,17 @@ console.log(jsConvert.snakeKeys([1, 2, 3])); #### pascalKeys ```ts -const pascalKeys(obj: any, { recursive: boolean = false }): object | null +const pascalKeys(obj: any, { recursive: boolean = false, recursiveInArray: boolean = false }): object | null ``` -Return a new object which keys is *PascalCase* format. Support pascalKeys **recursively**. Default is `false`. +Return a new object which keys is _PascalCase_ format. Support pascalKeys **recursively**. Default is `false`. ```js console.log(jsConvert.pascalKeys(obj)); // or recursive console.log(jsConvert.pascalKeys(obj, { recursive: true })); +// or recursive in sub-keys with value is an array +console.log(jsConvert.pascalKeys(obj, { recursive: true, recursiveInArray: true })); /** { CamelCase: 1, @@ -435,7 +445,6 @@ console.log(jsConvert.pascalKeys('abc')); console.log(jsConvert.pascalKeys([1, 2, 3])); ``` - ## Examples ### Convert string between cases @@ -490,7 +499,8 @@ console.log(jsConvert.toPascalCase(undefined)); // => '' ### Complicated values ```js -const str = '!@#$ -- Hello___world ..<>| \\ 123_ _456 &\l sn_ca - cmCa - PcCa - dot.ca - txt ca - Sen ca - Hd Ca %^$^%&'; +const str = + '!@#$ -- Hello___world ..<>| \\ 123_ _456 &l sn_ca - cmCa - PcCa - dot.ca - txt ca - Sen ca - Hd Ca %^$^%&'; console.log(jsConvert.toCamelCase(str)); // => 'helloWorld123456LSnCaCmCaPcCaDotCaTxtCaSenCaHdCa' console.log(jsConvert.toPascalCase(str)); // => 'HelloWorld123456LSnCaCmCaPcCaDotCaTxtCaSenCaHdCa' console.log(jsConvert.toSnakeCase(str)); // => 'hello_world_123_456_l_sn_ca_cm_ca_pc_ca_dot_ca_txt_ca_sen_ca_hd_ca' @@ -507,27 +517,27 @@ console.log(jsConvert.toHeaderCase(str)); // => 'Hello World 123 456 L Sn Ca Cm ```js const core = { - camelCase: 1, - UPPERCASE: 2, - lowercase: 3, - snake_case: 4, - PascalCase: 5, - 'Title Case': 6, - 'dot.case': 7, - 'param-case': 8, - 'Sentence case': 9, - 'path/case': 10, - 'Header-Case': 11 + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 }; const obj = { - ...core, - lv1: { - ...core, - lv2: { - ...core - } - } + ...core, + lv1: { + ...core, + lv2: { + ...core + } + } }; ``` @@ -542,43 +552,43 @@ Output ```json { - "CAMELCASE": 1, - "UPPERCASE": 2, - "LOWERCASE": 3, - "SNAKE_CASE": 4, - "PASCALCASE": 5, - "TITLE CASE": 6, - "DOT.CASE": 7, - "PARAM-CASE": 8, - "SENTENCE CASE": 9, - "PATH/CASE": 10, - "HEADER-CASE": 11, - "LV1": { - "CAMELCASE": 1, - "UPPERCASE": 2, - "LOWERCASE": 3, - "SNAKE_CASE": 4, - "PASCALCASE": 5, - "TITLE CASE": 6, - "DOT.CASE": 7, - "PARAM-CASE": 8, - "SENTENCE CASE": 9, - "PATH/CASE": 10, - "HEADER-CASE": 11, - "LV2": { - "CAMELCASE": 1, - "UPPERCASE": 2, - "LOWERCASE": 3, - "SNAKE_CASE": 4, - "PASCALCASE": 5, - "TITLE CASE": 6, - "DOT.CASE": 7, - "PARAM-CASE": 8, - "SENTENCE CASE": 9, - "PATH/CASE": 10, - "HEADER-CASE": 11 - } - } + "CAMELCASE": 1, + "UPPERCASE": 2, + "LOWERCASE": 3, + "SNAKE_CASE": 4, + "PASCALCASE": 5, + "TITLE CASE": 6, + "DOT.CASE": 7, + "PARAM-CASE": 8, + "SENTENCE CASE": 9, + "PATH/CASE": 10, + "HEADER-CASE": 11, + "LV1": { + "CAMELCASE": 1, + "UPPERCASE": 2, + "LOWERCASE": 3, + "SNAKE_CASE": 4, + "PASCALCASE": 5, + "TITLE CASE": 6, + "DOT.CASE": 7, + "PARAM-CASE": 8, + "SENTENCE CASE": 9, + "PATH/CASE": 10, + "HEADER-CASE": 11, + "LV2": { + "CAMELCASE": 1, + "UPPERCASE": 2, + "LOWERCASE": 3, + "SNAKE_CASE": 4, + "PASCALCASE": 5, + "TITLE CASE": 6, + "DOT.CASE": 7, + "PARAM-CASE": 8, + "SENTENCE CASE": 9, + "PATH/CASE": 10, + "HEADER-CASE": 11 + } + } } ``` @@ -593,47 +603,76 @@ Output ```json { - "CamelCase": 1, - "Uppercase": 2, - "Lowercase": 3, - "SnakeCase": 4, - "PascalCase": 5, - "TitleCase": 6, - "DotCase": 7, - "ParamCase": 8, - "SentenceCase": 9, - "PathCase": 10, - "HeaderCase": 11, - "Lv1": { - "CamelCase": 1, - "Uppercase": 2, - "Lowercase": 3, - "SnakeCase": 4, - "PascalCase": 5, - "TitleCase": 6, - "DotCase": 7, - "ParamCase": 8, - "SentenceCase": 9, - "PathCase": 10, - "HeaderCase": 11, - "Lv2": { - "CamelCase": 1, - "Uppercase": 2, - "Lowercase": 3, - "SnakeCase": 4, - "PascalCase": 5, - "TitleCase": 6, - "DotCase": 7, - "ParamCase": 8, - "SentenceCase": 9, - "PathCase": 10, - "HeaderCase": 11 - } - } + "CamelCase": 1, + "Uppercase": 2, + "Lowercase": 3, + "SnakeCase": 4, + "PascalCase": 5, + "TitleCase": 6, + "DotCase": 7, + "ParamCase": 8, + "SentenceCase": 9, + "PathCase": 10, + "HeaderCase": 11, + "Lv1": { + "CamelCase": 1, + "Uppercase": 2, + "Lowercase": 3, + "SnakeCase": 4, + "PascalCase": 5, + "TitleCase": 6, + "DotCase": 7, + "ParamCase": 8, + "SentenceCase": 9, + "PathCase": 10, + "HeaderCase": 11, + "Lv2": { + "CamelCase": 1, + "Uppercase": 2, + "Lowercase": 3, + "SnakeCase": 4, + "PascalCase": 5, + "TitleCase": 6, + "DotCase": 7, + "ParamCase": 8, + "SentenceCase": 9, + "PathCase": 10, + "HeaderCase": 11 + } + } +} +``` + +**Example with `snakeKeys` use recursive in array-sub-keys** + +```js +const obj = { + camelCase: 1, + PascalCase: { + camelCase: [1, 'a', null, { PascalCase: 1 }, undefined], + PascalCase: [{ PascalCase: [1] }, [1, { PascalCase: 2 }]], + snake_case: { camelCase: [{ PascalCase: 1 }] } + } +}; + +const res = jsConvert.snakeKeys(obj, { recursive: true, recursiveInArray: true }); +console.log(JSON.stringify(res)); +``` + +Output + +```json +{ + "camel_case": 1, + "pascal_case": { + "camel_case": [1, "a", null, { "pascal_case": 1 }, null], + "pascal_case": [{ "pascal_case": [1] }, [1, { "pascal_case": 2 }]], + "snake_case": { "camel_case": [{ "pascal_case": 1 }] } + } } ``` -*Note: You can browse more examples at folder [`./test/example`](./test/example)*. +_Note: You can browse more examples at folder [`./test/example`](./test/example)_. ## Dependencies @@ -643,27 +682,27 @@ No dependencies ### Quickstart -+ Clone the repository and enter the project +- Clone the repository and enter the project ```bash git clone https://github.com/huynhsamha/js-convert-case.git cd js-convert-case ``` -+ Install dependencies +- Install dependencies ```bash yarn ``` -+ Lint and format source (directory `src`) +- Lint and format source (directory `src`) ```bash yarn format # defined in package.json yarn lint # defined in package.json ``` -+ Build package for Node +- Build package for Node ```bash yarn dist:node # defined in package.json @@ -671,13 +710,13 @@ yarn dist:node # defined in package.json Output directory is `dist` (defined in file `tsconfig.json`) -+ Build package for Browser +- Build package for Browser ```bash yarn dist:browser # defined in package.json and rollup.config.js ``` -+ Build dist (both Node and Browser) +- Build dist (both Node and Browser) ```bash yarn dist # defined in package.json diff --git a/dist/js-convert-case.js b/dist/js-convert-case.js index 006fa23..409ef62 100644 --- a/dist/js-convert-case.js +++ b/dist/js-convert-case.js @@ -1,6 +1,8 @@ var jsConvert = (function () { 'use strict'; + var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; + function unwrapExports (x) { return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; } @@ -151,9 +153,26 @@ var jsConvert = (function () { var utils = createCommonjsModule(function (module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); - exports.isValidObject = exports.DefaultOption = void 0; + exports.isValidObject = exports.isArrayObject = exports.validateOptions = exports.DefaultOption = void 0; + /** + * Default options for convert function. This option is not recursive. + */ exports.DefaultOption = { recursive: false, + recursiveInArray: false + }; + exports.validateOptions = function (opt) { + if (opt === void 0) { opt = exports.DefaultOption; } + if (opt.recursive == null) { + opt = exports.DefaultOption; + } + else if (opt.recursiveInArray == null) { + opt.recursiveInArray = false; + } + return opt; + }; + exports.isArrayObject = function (obj) { + return obj != null && Array.isArray(obj); }; exports.isValidObject = function (obj) { return obj != null && typeof obj === 'object' && !Array.isArray(obj); @@ -162,19 +181,30 @@ var jsConvert = (function () { unwrapExports(utils); var utils_1 = utils.isValidObject; - var utils_2 = utils.DefaultOption; + var utils_2 = utils.isArrayObject; + var utils_3 = utils.validateOptions; + var utils_4 = utils.DefaultOption; var lowercaseKeysObject = createCommonjsModule(function (module, exports) { + var __spreadArrays = (commonjsGlobal && commonjsGlobal.__spreadArrays) || function () { + for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; + for (var r = Array(s), k = 0, i = 0; i < il; i++) + for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) + r[k] = a[j]; + return r; + }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Convert string keys in an object to lowercase format. - * If `obj` isn't a json object, `null` is returned. + * @param obj: object to convert keys. If `obj` isn't a json object, `null` is returned. + * @param opt: (optional) Options parameter, default is non-recursive. */ function lowerKeys(obj, opt) { if (opt === void 0) { opt = utils.DefaultOption; } if (!utils.isValidObject(obj)) return null; + opt = utils.validateOptions(opt); var res = {}; Object.keys(obj).forEach(function (key) { var value = obj[key]; @@ -183,6 +213,22 @@ var jsConvert = (function () { if (utils.isValidObject(value)) { value = lowerKeys(value, opt); } + else if (opt.recursiveInArray && utils.isArrayObject(value)) { + value = __spreadArrays(value).map(function (v) { + var ret = v; + if (utils.isValidObject(v)) { + // object in array + ret = lowerKeys(v, opt); + } + else if (utils.isArrayObject(v)) { + // array in array + // workaround by using an object holding array value + var temp = lowerKeys({ key: v }, opt); + ret = temp.key; + } + return ret; + }); + } } res[nkey] = value; }); @@ -194,16 +240,25 @@ var jsConvert = (function () { unwrapExports(lowercaseKeysObject); var uppercaseKeysObject = createCommonjsModule(function (module, exports) { + var __spreadArrays = (commonjsGlobal && commonjsGlobal.__spreadArrays) || function () { + for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; + for (var r = Array(s), k = 0, i = 0; i < il; i++) + for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) + r[k] = a[j]; + return r; + }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Convert string keys in an object to UPPERCASE format. - * If `obj` isn't a json object, `null` is returned. + * @param obj: object to convert keys. If `obj` isn't a json object, `null` is returned. + * @param opt: (optional) Options parameter, default is non-recursive. */ function upperKeys(obj, opt) { if (opt === void 0) { opt = utils.DefaultOption; } if (!utils.isValidObject(obj)) return null; + opt = utils.validateOptions(opt); var res = {}; Object.keys(obj).forEach(function (key) { var value = obj[key]; @@ -212,6 +267,22 @@ var jsConvert = (function () { if (utils.isValidObject(value)) { value = upperKeys(value, opt); } + else if (opt.recursiveInArray && utils.isArrayObject(value)) { + value = __spreadArrays(value).map(function (v) { + var ret = v; + if (utils.isValidObject(v)) { + // object in array + ret = upperKeys(v, opt); + } + else if (utils.isArrayObject(v)) { + // array in array + // workaround by using an object holding array value + var temp = upperKeys({ key: v }, opt); + ret = temp.key; + } + return ret; + }); + } } res[nkey] = value; }); @@ -223,17 +294,26 @@ var jsConvert = (function () { unwrapExports(uppercaseKeysObject); var camelcaseKeysObject = createCommonjsModule(function (module, exports) { + var __spreadArrays = (commonjsGlobal && commonjsGlobal.__spreadArrays) || function () { + for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; + for (var r = Array(s), k = 0, i = 0; i < il; i++) + for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) + r[k] = a[j]; + return r; + }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Convert string keys in an object to camelCase format. - * If `obj` isn't a json object, `null` is returned. + * @param obj: object to convert keys. If `obj` isn't a json object, `null` is returned. + * @param opt: (optional) Options parameter, default is non-recursive. */ function camelKeys(obj, opt) { if (opt === void 0) { opt = utils.DefaultOption; } if (!utils.isValidObject(obj)) return null; + opt = utils.validateOptions(opt); var res = {}; Object.keys(obj).forEach(function (key) { var value = obj[key]; @@ -242,6 +322,22 @@ var jsConvert = (function () { if (utils.isValidObject(value)) { value = camelKeys(value, opt); } + else if (opt.recursiveInArray && utils.isArrayObject(value)) { + value = __spreadArrays(value).map(function (v) { + var ret = v; + if (utils.isValidObject(v)) { + // object in array + ret = camelKeys(v, opt); + } + else if (utils.isArrayObject(v)) { + // array in array + // workaround by using an object holding array value + var temp = camelKeys({ key: v }, opt); + ret = temp.key; + } + return ret; + }); + } } res[nkey] = value; }); @@ -253,17 +349,26 @@ var jsConvert = (function () { unwrapExports(camelcaseKeysObject); var snakecaseKeysObject = createCommonjsModule(function (module, exports) { + var __spreadArrays = (commonjsGlobal && commonjsGlobal.__spreadArrays) || function () { + for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; + for (var r = Array(s), k = 0, i = 0; i < il; i++) + for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) + r[k] = a[j]; + return r; + }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Convert string keys in an object to snake_case format. - * If `obj` isn't a json object, `null` is returned. + * @param obj: object to convert keys. If `obj` isn't a json object, `null` is returned. + * @param opt: (optional) Options parameter, default is non-recursive. */ function snakeKeys(obj, opt) { if (opt === void 0) { opt = utils.DefaultOption; } if (!utils.isValidObject(obj)) return null; + opt = utils.validateOptions(opt); var res = {}; Object.keys(obj).forEach(function (key) { var value = obj[key]; @@ -272,6 +377,22 @@ var jsConvert = (function () { if (utils.isValidObject(value)) { value = snakeKeys(value, opt); } + else if (opt.recursiveInArray && utils.isArrayObject(value)) { + value = __spreadArrays(value).map(function (v) { + var ret = v; + if (utils.isValidObject(v)) { + // object in array + ret = snakeKeys(v, opt); + } + else if (utils.isArrayObject(v)) { + // array in array + // workaround by using an object holding array value + var temp = snakeKeys({ key: v }, opt); + ret = temp.key; + } + return ret; + }); + } } res[nkey] = value; }); @@ -283,17 +404,26 @@ var jsConvert = (function () { unwrapExports(snakecaseKeysObject); var pascalcaseKeysObject = createCommonjsModule(function (module, exports) { + var __spreadArrays = (commonjsGlobal && commonjsGlobal.__spreadArrays) || function () { + for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; + for (var r = Array(s), k = 0, i = 0; i < il; i++) + for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) + r[k] = a[j]; + return r; + }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Convert string keys in an object to PascalCase format. - * If `obj` isn't a json object, `null` is returned. + * @param obj: object to convert keys. If `obj` isn't a json object, `null` is returned. + * @param opt: (optional) Options parameter, default is non-recursive. */ function pascalKeys(obj, opt) { if (opt === void 0) { opt = utils.DefaultOption; } if (!utils.isValidObject(obj)) return null; + opt = utils.validateOptions(opt); var res = {}; Object.keys(obj).forEach(function (key) { var value = obj[key]; @@ -302,6 +432,22 @@ var jsConvert = (function () { if (utils.isValidObject(value)) { value = pascalKeys(value, opt); } + else if (opt.recursiveInArray && utils.isArrayObject(value)) { + value = __spreadArrays(value).map(function (v) { + var ret = v; + if (utils.isValidObject(v)) { + // object in array + ret = pascalKeys(v, opt); + } + else if (utils.isArrayObject(v)) { + // array in array + // workaround by using an object holding array value + var temp = pascalKeys({ key: v }, opt); + ret = temp.key; + } + return ret; + }); + } } res[nkey] = value; }); @@ -365,7 +511,7 @@ var jsConvert = (function () { upperKeys: uppercaseKeysObject.default, camelKeys: camelcaseKeysObject.default, snakeKeys: snakecaseKeysObject.default, - pascalKeys: pascalcaseKeysObject.default, + pascalKeys: pascalcaseKeysObject.default }; exports.default = jsConvert; }); diff --git a/dist/js-convert-case.min.js b/dist/js-convert-case.min.js index 659826d..af8d7aa 100644 --- a/dist/js-convert-case.min.js +++ b/dist/js-convert-case.min.js @@ -1 +1 @@ -var jsConvert=function(){'use strict';function a(a){return a&&a.__esModule&&Object.prototype.hasOwnProperty.call(a,"default")?a["default"]:a}function b(a,b){return b={exports:{}},a(b,b.exports),b.exports}var c=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.default=function(a){return void 0===a&&(a=""),a?(a+"").replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g,"").replace(/[^A-Za-z0-9]+/g,"$").replace(/([a-z])([A-Z])/g,function(c,d,a){return d+"$"+a}).toLowerCase().replace(/(\$)(\w)/g,function(c,d,a){return a.toUpperCase()}):""}});a(c);var d=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.default=function(a){return void 0===a&&(a=""),a?(a+"").replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g,"").replace(/([a-z])([A-Z])/g,function(c,d,a){return d+"_"+a.toLowerCase()}).replace(/[^A-Za-z0-9]+|_+/g,"_").toLowerCase():""}});a(d);var e=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.default=function(a){return void 0===a&&(a=""),a?(a+"").replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g,"$").replace(/[^A-Za-z0-9]+/g,"$").replace(/([a-z])([A-Z])/g,function(c,d,a){return d+"$"+a}).toLowerCase().replace(/(\$)(\w?)/g,function(c,d,a){return a.toUpperCase()}):""}});a(e);var f=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.default=function(a){return void 0===a&&(a=""),a?(a+"").replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g,"").replace(/([a-z])([A-Z])/g,function(c,d,a){return d+"_"+a.toLowerCase()}).replace(/[^A-Za-z0-9]+|_+/g,".").toLowerCase():""}});a(f);var g=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.default=function(a){return void 0===a&&(a=""),a?(a+"").replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g,"").replace(/([a-z])([A-Z])/g,function(c,d,a){return d+"_"+a.toLowerCase()}).replace(/[^A-Za-z0-9]+|_+/g,"/").toLowerCase():""}});a(g);var h=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.default=function(a){return void 0===a&&(a=""),a?(a+"").replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g,"").replace(/([a-z])([A-Z])/g,function(c,d,a){return d+"_"+a.toLowerCase()}).replace(/[^A-Za-z0-9]+|_+/g," ").toLowerCase():""}});a(h);var i=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.default=function(a){if(void 0===a&&(a=""),!a)return"";var b=(a+"").replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g,"").replace(/([a-z])([A-Z])/g,function(c,d,a){return d+"_"+a.toLowerCase()}).replace(/[^A-Za-z0-9]+|_+/g," ").toLowerCase();return b.charAt(0).toUpperCase()+b.slice(1)}});a(i);var j=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.default=function(a){return void 0===a&&(a=""),a?(a+"").replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g,"").replace(/([a-z])([A-Z])/g,function(c,d,a){return d+"_"+a.toLowerCase()}).replace(/[^A-Za-z0-9]+|_+/g," ").toLowerCase().replace(/( ?)(\w+)( ?)/g,function(d,e,a,b){return e+a.charAt(0).toUpperCase()+a.slice(1)+b}):""}});a(j);var k=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.isValidObject=b.DefaultOption=void 0,b.DefaultOption={recursive:!1},b.isValidObject=function(a){return null!=a&&"object"==typeof a&&!Array.isArray(a)}});a(k);var l=k.isValidObject,m=k.DefaultOption,n=b(function(a,b){function c(a,b){if(void 0===b&&(b=k.DefaultOption),!k.isValidObject(a))return null;var d={};return Object.keys(a).forEach(function(e){var f=a[e],g=e.toLowerCase();b.recursive&&k.isValidObject(f)&&(f=c(f,b)),d[g]=f}),d}Object.defineProperty(b,"__esModule",{value:!0}),b.default=c});a(n);var o=b(function(a,b){function c(a,b){if(void 0===b&&(b=k.DefaultOption),!k.isValidObject(a))return null;var d={};return Object.keys(a).forEach(function(e){var f=a[e],g=e.toUpperCase();b.recursive&&k.isValidObject(f)&&(f=c(f,b)),d[g]=f}),d}Object.defineProperty(b,"__esModule",{value:!0}),b.default=c});a(o);var p=b(function(a,b){function d(a,b){if(void 0===b&&(b=k.DefaultOption),!k.isValidObject(a))return null;var e={};return Object.keys(a).forEach(function(f){var g=a[f],h=c.default(f);b.recursive&&k.isValidObject(g)&&(g=d(g,b)),e[h]=g}),e}Object.defineProperty(b,"__esModule",{value:!0}),b.default=d});a(p);var q=b(function(a,b){function c(a,b){if(void 0===b&&(b=k.DefaultOption),!k.isValidObject(a))return null;var e={};return Object.keys(a).forEach(function(f){var g=a[f],h=d.default(f);b.recursive&&k.isValidObject(g)&&(g=c(g,b)),e[h]=g}),e}Object.defineProperty(b,"__esModule",{value:!0}),b.default=c});a(q);var r=b(function(a,b){function c(a,b){if(void 0===b&&(b=k.DefaultOption),!k.isValidObject(a))return null;var d={};return Object.keys(a).forEach(function(f){var g=a[f],h=e.default(f);b.recursive&&k.isValidObject(g)&&(g=c(g,b)),d[h]=g}),d}Object.defineProperty(b,"__esModule",{value:!0}),b.default=c});a(r);var s=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.pascalKeys=b.snakeKeys=b.camelKeys=b.upperKeys=b.lowerKeys=b.toLowerCase=b.toUpperCase=b.toHeaderCase=b.toSentenceCase=b.toTextCase=b.toPathCase=b.toDotCase=b.toPascalCase=b.toSnakeCase=b.toCamelCase=void 0,b.toCamelCase=c.default,b.toSnakeCase=d.default,b.toPascalCase=e.default,b.toDotCase=f.default,b.toPathCase=g.default,b.toTextCase=h.default,b.toSentenceCase=i.default,b.toHeaderCase=j.default,b.lowerKeys=n.default,b.upperKeys=o.default,b.camelKeys=p.default,b.snakeKeys=q.default,b.pascalKeys=r.default;var k=function(a){return((a||"")+"").toLowerCase()};b.toLowerCase=k;var l=function(a){return((a||"")+"").toUpperCase()};b.toUpperCase=l;var m={toCamelCase:c.default,toSnakeCase:d.default,toPascalCase:e.default,toDotCase:f.default,toPathCase:g.default,toTextCase:h.default,toSentenceCase:i.default,toHeaderCase:j.default,toUpperCase:l,toLowerCase:k,lowerKeys:n.default,upperKeys:o.default,camelKeys:p.default,snakeKeys:q.default,pascalKeys:r.default};b.default=m});a(s);var t=s.pascalKeys,u=s.snakeKeys,v=s.camelKeys,w=s.upperKeys,x=s.lowerKeys,y=s.toLowerCase,z=s.toUpperCase,A=s.toHeaderCase,B=s.toSentenceCase,C=s.toTextCase,D=s.toPathCase,E=s.toDotCase,F=s.toPascalCase,G=s.toSnakeCase,H=s.toCamelCase;return s}(); +var jsConvert=function(){'use strict';function a(a){return a&&a.__esModule&&Object.prototype.hasOwnProperty.call(a,"default")?a["default"]:a}function b(a,b){return b={exports:{}},a(b,b.exports),b.exports}var c="undefined"==typeof globalThis?"undefined"==typeof window?"undefined"==typeof global?"undefined"==typeof self?{}:self:global:window:globalThis,d=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.default=function(a){return void 0===a&&(a=""),a?(a+"").replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g,"").replace(/[^A-Za-z0-9]+/g,"$").replace(/([a-z])([A-Z])/g,function(c,d,a){return d+"$"+a}).toLowerCase().replace(/(\$)(\w)/g,function(c,d,a){return a.toUpperCase()}):""}});a(d);var e=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.default=function(a){return void 0===a&&(a=""),a?(a+"").replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g,"").replace(/([a-z])([A-Z])/g,function(c,d,a){return d+"_"+a.toLowerCase()}).replace(/[^A-Za-z0-9]+|_+/g,"_").toLowerCase():""}});a(e);var f=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.default=function(a){return void 0===a&&(a=""),a?(a+"").replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g,"$").replace(/[^A-Za-z0-9]+/g,"$").replace(/([a-z])([A-Z])/g,function(c,d,a){return d+"$"+a}).toLowerCase().replace(/(\$)(\w?)/g,function(c,d,a){return a.toUpperCase()}):""}});a(f);var g=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.default=function(a){return void 0===a&&(a=""),a?(a+"").replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g,"").replace(/([a-z])([A-Z])/g,function(c,d,a){return d+"_"+a.toLowerCase()}).replace(/[^A-Za-z0-9]+|_+/g,".").toLowerCase():""}});a(g);var h=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.default=function(a){return void 0===a&&(a=""),a?(a+"").replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g,"").replace(/([a-z])([A-Z])/g,function(c,d,a){return d+"_"+a.toLowerCase()}).replace(/[^A-Za-z0-9]+|_+/g,"/").toLowerCase():""}});a(h);var i=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.default=function(a){return void 0===a&&(a=""),a?(a+"").replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g,"").replace(/([a-z])([A-Z])/g,function(c,d,a){return d+"_"+a.toLowerCase()}).replace(/[^A-Za-z0-9]+|_+/g," ").toLowerCase():""}});a(i);var j=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.default=function(a){if(void 0===a&&(a=""),!a)return"";var b=(a+"").replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g,"").replace(/([a-z])([A-Z])/g,function(c,d,a){return d+"_"+a.toLowerCase()}).replace(/[^A-Za-z0-9]+|_+/g," ").toLowerCase();return b.charAt(0).toUpperCase()+b.slice(1)}});a(j);var k=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.default=function(a){return void 0===a&&(a=""),a?(a+"").replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g,"").replace(/([a-z])([A-Z])/g,function(c,d,a){return d+"_"+a.toLowerCase()}).replace(/[^A-Za-z0-9]+|_+/g," ").toLowerCase().replace(/( ?)(\w+)( ?)/g,function(d,e,a,b){return e+a.charAt(0).toUpperCase()+a.slice(1)+b}):""}});a(k);var l=b(function(a,b){Object.defineProperty(b,"__esModule",{value:!0}),b.isValidObject=b.isArrayObject=b.validateOptions=b.DefaultOption=void 0,b.DefaultOption={recursive:!1,recursiveInArray:!1},b.validateOptions=function(a){return void 0===a&&(a=b.DefaultOption),null==a.recursive?a=b.DefaultOption:null==a.recursiveInArray&&(a.recursiveInArray=!1),a},b.isArrayObject=function(a){return null!=a&&Array.isArray(a)},b.isValidObject=function(a){return null!=a&&"object"==typeof a&&!Array.isArray(a)}});a(l);var m=l.isValidObject,n=l.isArrayObject,o=l.validateOptions,p=l.DefaultOption,q=b(function(a,b){function d(a,b){if(void 0===b&&(b=l.DefaultOption),!l.isValidObject(a))return null;b=l.validateOptions(b);var c={};return Object.keys(a).forEach(function(f){var g=a[f],h=f.toLowerCase();b.recursive&&(l.isValidObject(g)?g=d(g,b):b.recursiveInArray&&l.isArrayObject(g)&&(g=e(g).map(function(a){var c=a;if(l.isValidObject(a))c=d(a,b);else if(l.isArrayObject(a)){var e=d({key:a},b);c=e.key}return c}))),c[h]=g}),c}var e=c&&c.__spreadArrays||function(){for(var b=0,c=0,d=arguments.length;c Options; +export declare const isArrayObject: (obj: any) => boolean; export declare const isValidObject: (obj: any) => boolean; diff --git a/lib/modules/extends/utils.js b/lib/modules/extends/utils.js index 8d4acd5..d441055 100644 --- a/lib/modules/extends/utils.js +++ b/lib/modules/extends/utils.js @@ -1,8 +1,25 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.isValidObject = exports.DefaultOption = void 0; +exports.isValidObject = exports.isArrayObject = exports.validateOptions = exports.DefaultOption = void 0; +/** + * Default options for convert function. This option is not recursive. + */ exports.DefaultOption = { recursive: false, + recursiveInArray: false +}; +exports.validateOptions = function (opt) { + if (opt === void 0) { opt = exports.DefaultOption; } + if (opt.recursive == null) { + opt = exports.DefaultOption; + } + else if (opt.recursiveInArray == null) { + opt.recursiveInArray = false; + } + return opt; +}; +exports.isArrayObject = function (obj) { + return obj != null && Array.isArray(obj); }; exports.isValidObject = function (obj) { return obj != null && typeof obj === 'object' && !Array.isArray(obj); diff --git a/package.json b/package.json index 16ed5cd..b0b2a64 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "js-convert-case", - "version": "4.0.1", + "version": "4.1.0", "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", "scripts": { "build": "tsc", diff --git a/src/index.ts b/src/index.ts index e1122c2..e34e3e0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,7 +37,7 @@ const jsConvert = { upperKeys, camelKeys, snakeKeys, - pascalKeys, + pascalKeys }; export { @@ -55,7 +55,7 @@ export { upperKeys, camelKeys, snakeKeys, - pascalKeys, + pascalKeys }; export default jsConvert; diff --git a/src/modules/extends/camelcase-keys-object/index.ts b/src/modules/extends/camelcase-keys-object/index.ts index 94bff9c..04b9bf1 100644 --- a/src/modules/extends/camelcase-keys-object/index.ts +++ b/src/modules/extends/camelcase-keys-object/index.ts @@ -1,12 +1,14 @@ -import { isValidObject, Options, DefaultOption } from '../utils'; +import { isValidObject, Options, DefaultOption, isArrayObject, validateOptions } from '../utils'; import toCamelCase from '../../js-camelcase'; /** * Convert string keys in an object to camelCase format. - * If `obj` isn't a json object, `null` is returned. + * @param obj: object to convert keys. If `obj` isn't a json object, `null` is returned. + * @param opt: (optional) Options parameter, default is non-recursive. */ export default function camelKeys(obj: any, opt: Options = DefaultOption): object | null { if (!isValidObject(obj)) return null; + opt = validateOptions(opt); const res: any = {}; Object.keys(obj).forEach((key) => { @@ -15,6 +17,20 @@ export default function camelKeys(obj: any, opt: Options = DefaultOption): objec if (opt.recursive) { if (isValidObject(value)) { value = camelKeys(value, opt); + } else if (opt.recursiveInArray && isArrayObject(value)) { + value = [...value].map((v) => { + let ret = v; + if (isValidObject(v)) { + // object in array + ret = camelKeys(v, opt); + } else if (isArrayObject(v)) { + // array in array + // workaround by using an object holding array value + const temp: any = camelKeys({ key: v }, opt); + ret = temp.key; + } + return ret; + }); } } res[nkey] = value; diff --git a/src/modules/extends/lowercase-keys-object/index.ts b/src/modules/extends/lowercase-keys-object/index.ts index e03435b..099ad7b 100644 --- a/src/modules/extends/lowercase-keys-object/index.ts +++ b/src/modules/extends/lowercase-keys-object/index.ts @@ -1,11 +1,13 @@ -import { isValidObject, Options, DefaultOption } from '../utils'; +import { isValidObject, Options, DefaultOption, isArrayObject, validateOptions } from '../utils'; /** * Convert string keys in an object to lowercase format. - * If `obj` isn't a json object, `null` is returned. + * @param obj: object to convert keys. If `obj` isn't a json object, `null` is returned. + * @param opt: (optional) Options parameter, default is non-recursive. */ export default function lowerKeys(obj: any, opt: Options = DefaultOption): object | null { if (!isValidObject(obj)) return null; + opt = validateOptions(opt); const res: any = {}; Object.keys(obj).forEach((key) => { @@ -14,6 +16,20 @@ export default function lowerKeys(obj: any, opt: Options = DefaultOption): objec if (opt.recursive) { if (isValidObject(value)) { value = lowerKeys(value, opt); + } else if (opt.recursiveInArray && isArrayObject(value)) { + value = [...value].map((v) => { + let ret = v; + if (isValidObject(v)) { + // object in array + ret = lowerKeys(v, opt); + } else if (isArrayObject(v)) { + // array in array + // workaround by using an object holding array value + const temp: any = lowerKeys({ key: v }, opt); + ret = temp.key; + } + return ret; + }); } } res[nkey] = value; diff --git a/src/modules/extends/pascalcase-keys-object/index.ts b/src/modules/extends/pascalcase-keys-object/index.ts index 21ead74..f5b88f0 100644 --- a/src/modules/extends/pascalcase-keys-object/index.ts +++ b/src/modules/extends/pascalcase-keys-object/index.ts @@ -1,12 +1,14 @@ -import { isValidObject, Options, DefaultOption } from '../utils'; +import { isValidObject, Options, DefaultOption, isArrayObject, validateOptions } from '../utils'; import toPascalCase from '../../js-pascalcase'; /** * Convert string keys in an object to PascalCase format. - * If `obj` isn't a json object, `null` is returned. + * @param obj: object to convert keys. If `obj` isn't a json object, `null` is returned. + * @param opt: (optional) Options parameter, default is non-recursive. */ export default function pascalKeys(obj: any, opt: Options = DefaultOption): object | null { if (!isValidObject(obj)) return null; + opt = validateOptions(opt); const res: any = {}; Object.keys(obj).forEach((key) => { @@ -15,6 +17,20 @@ export default function pascalKeys(obj: any, opt: Options = DefaultOption): obje if (opt.recursive) { if (isValidObject(value)) { value = pascalKeys(value, opt); + } else if (opt.recursiveInArray && isArrayObject(value)) { + value = [...value].map((v) => { + let ret = v; + if (isValidObject(v)) { + // object in array + ret = pascalKeys(v, opt); + } else if (isArrayObject(v)) { + // array in array + // workaround by using an object holding array value + const temp: any = pascalKeys({ key: v }, opt); + ret = temp.key; + } + return ret; + }); } } res[nkey] = value; diff --git a/src/modules/extends/snakecase-keys-object/index.ts b/src/modules/extends/snakecase-keys-object/index.ts index 3680fb6..b236b7a 100644 --- a/src/modules/extends/snakecase-keys-object/index.ts +++ b/src/modules/extends/snakecase-keys-object/index.ts @@ -1,12 +1,14 @@ -import { isValidObject, Options, DefaultOption } from '../utils'; +import { isValidObject, Options, DefaultOption, isArrayObject, validateOptions } from '../utils'; import toSnakeCase from '../../js-snakecase'; /** * Convert string keys in an object to snake_case format. - * If `obj` isn't a json object, `null` is returned. + * @param obj: object to convert keys. If `obj` isn't a json object, `null` is returned. + * @param opt: (optional) Options parameter, default is non-recursive. */ export default function snakeKeys(obj: any, opt: Options = DefaultOption): object | null { if (!isValidObject(obj)) return null; + opt = validateOptions(opt); const res: any = {}; Object.keys(obj).forEach((key) => { @@ -15,6 +17,20 @@ export default function snakeKeys(obj: any, opt: Options = DefaultOption): objec if (opt.recursive) { if (isValidObject(value)) { value = snakeKeys(value, opt); + } else if (opt.recursiveInArray && isArrayObject(value)) { + value = [...value].map((v) => { + let ret = v; + if (isValidObject(v)) { + // object in array + ret = snakeKeys(v, opt); + } else if (isArrayObject(v)) { + // array in array + // workaround by using an object holding array value + const temp: any = snakeKeys({ key: v }, opt); + ret = temp.key; + } + return ret; + }); } } res[nkey] = value; diff --git a/src/modules/extends/uppercase-keys-object/index.ts b/src/modules/extends/uppercase-keys-object/index.ts index fb62d8e..663ec8d 100644 --- a/src/modules/extends/uppercase-keys-object/index.ts +++ b/src/modules/extends/uppercase-keys-object/index.ts @@ -1,11 +1,13 @@ -import { isValidObject, Options, DefaultOption } from '../utils'; +import { isValidObject, Options, DefaultOption, isArrayObject, validateOptions } from '../utils'; /** * Convert string keys in an object to UPPERCASE format. - * If `obj` isn't a json object, `null` is returned. + * @param obj: object to convert keys. If `obj` isn't a json object, `null` is returned. + * @param opt: (optional) Options parameter, default is non-recursive. */ export default function upperKeys(obj: any, opt: Options = DefaultOption): object | null { if (!isValidObject(obj)) return null; + opt = validateOptions(opt); const res: any = {}; Object.keys(obj).forEach((key) => { @@ -14,6 +16,20 @@ export default function upperKeys(obj: any, opt: Options = DefaultOption): objec if (opt.recursive) { if (isValidObject(value)) { value = upperKeys(value, opt); + } else if (opt.recursiveInArray && isArrayObject(value)) { + value = [...value].map((v) => { + let ret = v; + if (isValidObject(v)) { + // object in array + ret = upperKeys(v, opt); + } else if (isArrayObject(v)) { + // array in array + // workaround by using an object holding array value + const temp: any = upperKeys({ key: v }, opt); + ret = temp.key; + } + return ret; + }); } } res[nkey] = value; diff --git a/src/modules/extends/utils.ts b/src/modules/extends/utils.ts index e32a4b4..efcf46d 100644 --- a/src/modules/extends/utils.ts +++ b/src/modules/extends/utils.ts @@ -1,9 +1,35 @@ +/** + * Options parameter for convert function + * + * @param recursive: recursive if value of subkey is an object that is not an array + * @param recursiveInArray: recursive if ${recursive} is `true` and value of subkey + * is an array. All elements in array (value of subkey) will be recursive. + * If ${recursiveInArray} is not set, default is `false`. + */ export interface Options { recursive: boolean; + recursiveInArray?: boolean; } +/** + * Default options for convert function. This option is not recursive. + */ export const DefaultOption: Options = { recursive: false, + recursiveInArray: false +}; + +export const validateOptions = (opt: Options = DefaultOption): Options => { + if (opt.recursive == null) { + opt = DefaultOption; + } else if (opt.recursiveInArray == null) { + opt.recursiveInArray = false; + } + return opt; +}; + +export const isArrayObject = (obj: any): boolean => { + return obj != null && Array.isArray(obj); }; export const isValidObject = (obj: any): boolean => { diff --git a/test/prepublish/package.json b/test/prepublish/package.json index b75ab38..3b41603 100644 --- a/test/prepublish/package.json +++ b/test/prepublish/package.json @@ -8,7 +8,7 @@ "clean": "rm -rf *.tgz" }, "dependencies": { - "js-convert-case": "file:js-convert-case-4.0.0.tgz" + "js-convert-case": "file:js-convert-case-4.1.0.tgz" }, "devDependencies": { "@babel/core": "^7.10.1", diff --git a/test/prepublish/yarn.lock b/test/prepublish/yarn.lock index 972df0e..d19f6eb 100644 --- a/test/prepublish/yarn.lock +++ b/test/prepublish/yarn.lock @@ -922,9 +922,9 @@ is-symbol@^1.0.2: dependencies: has-symbols "^1.0.1" -"js-convert-case@file:js-convert-case-4.0.0.tgz": - version "4.0.0" - resolved "file:js-convert-case-4.0.0.tgz#5b1067956d2741f05032bf0259b82178ae610a70" +"js-convert-case@file:js-convert-case-4.1.0.tgz": + version "4.1.0" + resolved "file:js-convert-case-4.1.0.tgz#fca0d6ff202dfee487e531d986a10f97525d09c4" "js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0: version "4.0.0" diff --git a/test/recursive/_genexample.js b/test/recursive/_genexample.js new file mode 100644 index 0000000..eb9051c --- /dev/null +++ b/test/recursive/_genexample.js @@ -0,0 +1,14 @@ +const jsConvert = require('../../index'); + +const obj = { + camelCase: 1, + PascalCase: { + camelCase: [1, 'a', null, { PascalCase: 1 }, undefined], + PascalCase: [{ PascalCase: [1] }, [1, { PascalCase: 2 }]], + snake_case: { camelCase: [{ PascalCase: 1 }] } + } +}; + +const res = jsConvert.snakeKeys(obj, { recursive: true, recursiveInArray: true }); + +console.log(JSON.stringify(res)); diff --git a/test/recursive/_shared.js b/test/recursive/_shared.js new file mode 100644 index 0000000..3391193 --- /dev/null +++ b/test/recursive/_shared.js @@ -0,0 +1,48 @@ +const util = require('util'); + +const log = (obj) => { + console.log(util.inspect(obj, { showHidden: false, depth: null })); +}; + +const core = { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 +}; + +const obj = { + ...core, + lv11: { ...core }, + lv12: { + ...core, + lv21: { + ...core, + lv31: { ...core }, + lv32: { ...core } + }, + lv22: { ...core }, + lv23: [ + { ...core }, + { + lv31: [1, 'A', null, { ...core }, '123', undefined], + lv32: { ...core } + }, + [{ ...core }, 5, '123', { a: 1, b: 2 }, null], + null, + undefined + ] + } +}; + +module.exports = { + obj, + log +}; diff --git a/test/recursive/camelKeys.js b/test/recursive/camelKeys.js index 63d619c..8fe4eb2 100644 --- a/test/recursive/camelKeys.js +++ b/test/recursive/camelKeys.js @@ -1,12 +1,13 @@ const jsConvert = require('../../index'); -const obj = require('./object'); +const { obj, log } = require('./_shared'); -console.log(jsConvert.camelKeys(obj)); +log(jsConvert.camelKeys(obj)); console.log('============================================'); -console.log(jsConvert.camelKeys(obj, { recursive: true })); +log(jsConvert.camelKeys(obj, { recursive: true })); /** -{ camelCase: 1, +{ + camelCase: 1, uppercase: 2, lowercase: 3, snakeCase: 4, @@ -17,32 +18,45 @@ console.log(jsConvert.camelKeys(obj, { recursive: true })); sentenceCase: 9, pathCase: 10, headerCase: 11, - lv11: - { camelCase: 1, - UPPERCASE: 2, - lowercase: 3, - snake_case: 4, - PascalCase: 5, - 'Title Case': 6, - 'dot.case': 7, - 'param-case': 8, - 'Sentence case': 9, - 'path/case': 10, - 'Header-Case': 11 }, - lv12: - { camelCase: 1, - UPPERCASE: 2, - lowercase: 3, - snake_case: 4, - PascalCase: 5, - 'Title Case': 6, - 'dot.case': 7, - 'param-case': 8, - 'Sentence case': 9, - 'path/case': 10, - 'Header-Case': 11, - lv21: - { camelCase: 1, + lv11: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + lv12: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11, + lv21: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11, + lv31: { + camelCase: 1, UPPERCASE: 2, lowercase: 3, snake_case: 4, @@ -52,11 +66,10 @@ console.log(jsConvert.camelKeys(obj, { recursive: true })); 'param-case': 8, 'Sentence case': 9, 'path/case': 10, - 'Header-Case': 11, - lv31: [Object], - lv32: [Object] }, - lv22: - { camelCase: 1, + 'Header-Case': 11 + }, + lv32: { + camelCase: 1, UPPERCASE: 2, lowercase: 3, snake_case: 4, @@ -66,9 +79,98 @@ console.log(jsConvert.camelKeys(obj, { recursive: true })); 'param-case': 8, 'Sentence case': 9, 'path/case': 10, - 'Header-Case': 11 } } } + 'Header-Case': 11 + } + }, + lv22: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + lv23: [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + { + lv31: [ + 1, + 'A', + null, + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + '123', + undefined + ], + lv32: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + } + }, + [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + 5, + '123', + { a: 1, b: 2 }, + null + ], + null, + undefined + ] + } +} ============================================ -{ camelCase: 1, +{ + camelCase: 1, uppercase: 2, lowercase: 3, snakeCase: 4, @@ -79,32 +181,45 @@ console.log(jsConvert.camelKeys(obj, { recursive: true })); sentenceCase: 9, pathCase: 10, headerCase: 11, - lv11: - { camelCase: 1, - uppercase: 2, - lowercase: 3, - snakeCase: 4, - pascalCase: 5, - titleCase: 6, - dotCase: 7, - paramCase: 8, - sentenceCase: 9, - pathCase: 10, - headerCase: 11 }, - lv12: - { camelCase: 1, - uppercase: 2, - lowercase: 3, - snakeCase: 4, - pascalCase: 5, - titleCase: 6, - dotCase: 7, - paramCase: 8, - sentenceCase: 9, - pathCase: 10, - headerCase: 11, - lv21: - { camelCase: 1, + lv11: { + camelCase: 1, + uppercase: 2, + lowercase: 3, + snakeCase: 4, + pascalCase: 5, + titleCase: 6, + dotCase: 7, + paramCase: 8, + sentenceCase: 9, + pathCase: 10, + headerCase: 11 + }, + lv12: { + camelCase: 1, + uppercase: 2, + lowercase: 3, + snakeCase: 4, + pascalCase: 5, + titleCase: 6, + dotCase: 7, + paramCase: 8, + sentenceCase: 9, + pathCase: 10, + headerCase: 11, + lv21: { + camelCase: 1, + uppercase: 2, + lowercase: 3, + snakeCase: 4, + pascalCase: 5, + titleCase: 6, + dotCase: 7, + paramCase: 8, + sentenceCase: 9, + pathCase: 10, + headerCase: 11, + lv31: { + camelCase: 1, uppercase: 2, lowercase: 3, snakeCase: 4, @@ -114,11 +229,10 @@ console.log(jsConvert.camelKeys(obj, { recursive: true })); paramCase: 8, sentenceCase: 9, pathCase: 10, - headerCase: 11, - lv31: [Object], - lv32: [Object] }, - lv22: - { camelCase: 1, + headerCase: 11 + }, + lv32: { + camelCase: 1, uppercase: 2, lowercase: 3, snakeCase: 4, @@ -128,5 +242,94 @@ console.log(jsConvert.camelKeys(obj, { recursive: true })); paramCase: 8, sentenceCase: 9, pathCase: 10, - headerCase: 11 } } } + headerCase: 11 + } + }, + lv22: { + camelCase: 1, + uppercase: 2, + lowercase: 3, + snakeCase: 4, + pascalCase: 5, + titleCase: 6, + dotCase: 7, + paramCase: 8, + sentenceCase: 9, + pathCase: 10, + headerCase: 11 + }, + lv23: [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + { + lv31: [ + 1, + 'A', + null, + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + '123', + undefined + ], + lv32: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + } + }, + [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + 5, + '123', + { a: 1, b: 2 }, + null + ], + null, + undefined + ] + } +} + */ diff --git a/test/recursive/camelKeysArray.js b/test/recursive/camelKeysArray.js new file mode 100644 index 0000000..cfe05f8 --- /dev/null +++ b/test/recursive/camelKeysArray.js @@ -0,0 +1,170 @@ +const jsConvert = require('../../index'); +const { obj, log } = require('./_shared'); + +log(jsConvert.camelKeys(obj, { recursive: true, recursiveInArray: true })); + +/** +{ + camelCase: 1, + uppercase: 2, + lowercase: 3, + snakeCase: 4, + pascalCase: 5, + titleCase: 6, + dotCase: 7, + paramCase: 8, + sentenceCase: 9, + pathCase: 10, + headerCase: 11, + lv11: { + camelCase: 1, + uppercase: 2, + lowercase: 3, + snakeCase: 4, + pascalCase: 5, + titleCase: 6, + dotCase: 7, + paramCase: 8, + sentenceCase: 9, + pathCase: 10, + headerCase: 11 + }, + lv12: { + camelCase: 1, + uppercase: 2, + lowercase: 3, + snakeCase: 4, + pascalCase: 5, + titleCase: 6, + dotCase: 7, + paramCase: 8, + sentenceCase: 9, + pathCase: 10, + headerCase: 11, + lv21: { + camelCase: 1, + uppercase: 2, + lowercase: 3, + snakeCase: 4, + pascalCase: 5, + titleCase: 6, + dotCase: 7, + paramCase: 8, + sentenceCase: 9, + pathCase: 10, + headerCase: 11, + lv31: { + camelCase: 1, + uppercase: 2, + lowercase: 3, + snakeCase: 4, + pascalCase: 5, + titleCase: 6, + dotCase: 7, + paramCase: 8, + sentenceCase: 9, + pathCase: 10, + headerCase: 11 + }, + lv32: { + camelCase: 1, + uppercase: 2, + lowercase: 3, + snakeCase: 4, + pascalCase: 5, + titleCase: 6, + dotCase: 7, + paramCase: 8, + sentenceCase: 9, + pathCase: 10, + headerCase: 11 + } + }, + lv22: { + camelCase: 1, + uppercase: 2, + lowercase: 3, + snakeCase: 4, + pascalCase: 5, + titleCase: 6, + dotCase: 7, + paramCase: 8, + sentenceCase: 9, + pathCase: 10, + headerCase: 11 + }, + lv23: [ + { + camelCase: 1, + uppercase: 2, + lowercase: 3, + snakeCase: 4, + pascalCase: 5, + titleCase: 6, + dotCase: 7, + paramCase: 8, + sentenceCase: 9, + pathCase: 10, + headerCase: 11 + }, + { + lv31: [ + 1, + 'A', + null, + { + camelCase: 1, + uppercase: 2, + lowercase: 3, + snakeCase: 4, + pascalCase: 5, + titleCase: 6, + dotCase: 7, + paramCase: 8, + sentenceCase: 9, + pathCase: 10, + headerCase: 11 + }, + '123', + undefined + ], + lv32: { + camelCase: 1, + uppercase: 2, + lowercase: 3, + snakeCase: 4, + pascalCase: 5, + titleCase: 6, + dotCase: 7, + paramCase: 8, + sentenceCase: 9, + pathCase: 10, + headerCase: 11 + } + }, + [ + { + camelCase: 1, + uppercase: 2, + lowercase: 3, + snakeCase: 4, + pascalCase: 5, + titleCase: 6, + dotCase: 7, + paramCase: 8, + sentenceCase: 9, + pathCase: 10, + headerCase: 11 + }, + 5, + '123', + { a: 1, b: 2 }, + null + ], + null, + undefined + ] + } +} + + */ diff --git a/test/recursive/lowerKeys.js b/test/recursive/lowerKeys.js index 6ffdd07..4ca2978 100644 --- a/test/recursive/lowerKeys.js +++ b/test/recursive/lowerKeys.js @@ -1,12 +1,13 @@ const jsConvert = require('../../index'); -const obj = require('./object'); +const { obj, log } = require('./_shared'); -console.log(jsConvert.lowerKeys(obj)); +log(jsConvert.lowerKeys(obj)); console.log('============================================'); -console.log(jsConvert.lowerKeys(obj, { recursive: true })); +log(jsConvert.lowerKeys(obj, { recursive: true })); /** -{ camelcase: 1, +{ + camelcase: 1, uppercase: 2, lowercase: 3, snake_case: 4, @@ -17,32 +18,45 @@ console.log(jsConvert.lowerKeys(obj, { recursive: true })); 'sentence case': 9, 'path/case': 10, 'header-case': 11, - lv11: - { camelCase: 1, - UPPERCASE: 2, - lowercase: 3, - snake_case: 4, - PascalCase: 5, - 'Title Case': 6, - 'dot.case': 7, - 'param-case': 8, - 'Sentence case': 9, - 'path/case': 10, - 'Header-Case': 11 }, - lv12: - { camelCase: 1, - UPPERCASE: 2, - lowercase: 3, - snake_case: 4, - PascalCase: 5, - 'Title Case': 6, - 'dot.case': 7, - 'param-case': 8, - 'Sentence case': 9, - 'path/case': 10, - 'Header-Case': 11, - lv21: - { camelCase: 1, + lv11: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + lv12: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11, + lv21: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11, + lv31: { + camelCase: 1, UPPERCASE: 2, lowercase: 3, snake_case: 4, @@ -52,11 +66,10 @@ console.log(jsConvert.lowerKeys(obj, { recursive: true })); 'param-case': 8, 'Sentence case': 9, 'path/case': 10, - 'Header-Case': 11, - lv31: [Object], - lv32: [Object] }, - lv22: - { camelCase: 1, + 'Header-Case': 11 + }, + lv32: { + camelCase: 1, UPPERCASE: 2, lowercase: 3, snake_case: 4, @@ -66,9 +79,98 @@ console.log(jsConvert.lowerKeys(obj, { recursive: true })); 'param-case': 8, 'Sentence case': 9, 'path/case': 10, - 'Header-Case': 11 } } } + 'Header-Case': 11 + } + }, + lv22: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + lv23: [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + { + lv31: [ + 1, + 'A', + null, + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + '123', + undefined + ], + lv32: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + } + }, + [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + 5, + '123', + { a: 1, b: 2 }, + null + ], + null, + undefined + ] + } +} ============================================ -{ camelcase: 1, +{ + camelcase: 1, uppercase: 2, lowercase: 3, snake_case: 4, @@ -79,32 +181,45 @@ console.log(jsConvert.lowerKeys(obj, { recursive: true })); 'sentence case': 9, 'path/case': 10, 'header-case': 11, - lv11: - { camelcase: 1, - uppercase: 2, - lowercase: 3, - snake_case: 4, - pascalcase: 5, - 'title case': 6, - 'dot.case': 7, - 'param-case': 8, - 'sentence case': 9, - 'path/case': 10, - 'header-case': 11 }, - lv12: - { camelcase: 1, - uppercase: 2, - lowercase: 3, - snake_case: 4, - pascalcase: 5, - 'title case': 6, - 'dot.case': 7, - 'param-case': 8, - 'sentence case': 9, - 'path/case': 10, - 'header-case': 11, - lv21: - { camelcase: 1, + lv11: { + camelcase: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascalcase: 5, + 'title case': 6, + 'dot.case': 7, + 'param-case': 8, + 'sentence case': 9, + 'path/case': 10, + 'header-case': 11 + }, + lv12: { + camelcase: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascalcase: 5, + 'title case': 6, + 'dot.case': 7, + 'param-case': 8, + 'sentence case': 9, + 'path/case': 10, + 'header-case': 11, + lv21: { + camelcase: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascalcase: 5, + 'title case': 6, + 'dot.case': 7, + 'param-case': 8, + 'sentence case': 9, + 'path/case': 10, + 'header-case': 11, + lv31: { + camelcase: 1, uppercase: 2, lowercase: 3, snake_case: 4, @@ -114,11 +229,10 @@ console.log(jsConvert.lowerKeys(obj, { recursive: true })); 'param-case': 8, 'sentence case': 9, 'path/case': 10, - 'header-case': 11, - lv31: [Object], - lv32: [Object] }, - lv22: - { camelcase: 1, + 'header-case': 11 + }, + lv32: { + camelcase: 1, uppercase: 2, lowercase: 3, snake_case: 4, @@ -128,5 +242,95 @@ console.log(jsConvert.lowerKeys(obj, { recursive: true })); 'param-case': 8, 'sentence case': 9, 'path/case': 10, - 'header-case': 11 } } } - */ + 'header-case': 11 + } + }, + lv22: { + camelcase: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascalcase: 5, + 'title case': 6, + 'dot.case': 7, + 'param-case': 8, + 'sentence case': 9, + 'path/case': 10, + 'header-case': 11 + }, + lv23: [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + { + lv31: [ + 1, + 'A', + null, + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + '123', + undefined + ], + lv32: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + } + }, + [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + 5, + '123', + { a: 1, b: 2 }, + null + ], + null, + undefined + ] + } +} + + +*/ diff --git a/test/recursive/lowerKeysArray.js b/test/recursive/lowerKeysArray.js new file mode 100644 index 0000000..32cea14 --- /dev/null +++ b/test/recursive/lowerKeysArray.js @@ -0,0 +1,170 @@ +const jsConvert = require('../../index'); +const { obj, log } = require('./_shared'); + +log(jsConvert.lowerKeys(obj, { recursive: true, recursiveInArray: true })); + +/** +{ + camelcase: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascalcase: 5, + 'title case': 6, + 'dot.case': 7, + 'param-case': 8, + 'sentence case': 9, + 'path/case': 10, + 'header-case': 11, + lv11: { + camelcase: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascalcase: 5, + 'title case': 6, + 'dot.case': 7, + 'param-case': 8, + 'sentence case': 9, + 'path/case': 10, + 'header-case': 11 + }, + lv12: { + camelcase: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascalcase: 5, + 'title case': 6, + 'dot.case': 7, + 'param-case': 8, + 'sentence case': 9, + 'path/case': 10, + 'header-case': 11, + lv21: { + camelcase: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascalcase: 5, + 'title case': 6, + 'dot.case': 7, + 'param-case': 8, + 'sentence case': 9, + 'path/case': 10, + 'header-case': 11, + lv31: { + camelcase: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascalcase: 5, + 'title case': 6, + 'dot.case': 7, + 'param-case': 8, + 'sentence case': 9, + 'path/case': 10, + 'header-case': 11 + }, + lv32: { + camelcase: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascalcase: 5, + 'title case': 6, + 'dot.case': 7, + 'param-case': 8, + 'sentence case': 9, + 'path/case': 10, + 'header-case': 11 + } + }, + lv22: { + camelcase: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascalcase: 5, + 'title case': 6, + 'dot.case': 7, + 'param-case': 8, + 'sentence case': 9, + 'path/case': 10, + 'header-case': 11 + }, + lv23: [ + { + camelcase: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascalcase: 5, + 'title case': 6, + 'dot.case': 7, + 'param-case': 8, + 'sentence case': 9, + 'path/case': 10, + 'header-case': 11 + }, + { + lv31: [ + 1, + 'A', + null, + { + camelcase: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascalcase: 5, + 'title case': 6, + 'dot.case': 7, + 'param-case': 8, + 'sentence case': 9, + 'path/case': 10, + 'header-case': 11 + }, + '123', + undefined + ], + lv32: { + camelcase: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascalcase: 5, + 'title case': 6, + 'dot.case': 7, + 'param-case': 8, + 'sentence case': 9, + 'path/case': 10, + 'header-case': 11 + } + }, + [ + { + camelcase: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascalcase: 5, + 'title case': 6, + 'dot.case': 7, + 'param-case': 8, + 'sentence case': 9, + 'path/case': 10, + 'header-case': 11 + }, + 5, + '123', + { a: 1, b: 2 }, + null + ], + null, + undefined + ] + } +} + +*/ diff --git a/test/recursive/object.js b/test/recursive/object.js deleted file mode 100644 index 09fab9e..0000000 --- a/test/recursive/object.js +++ /dev/null @@ -1,35 +0,0 @@ -const core = { - camelCase: 1, - UPPERCASE: 2, - lowercase: 3, - snake_case: 4, - PascalCase: 5, - 'Title Case': 6, - 'dot.case': 7, - 'param-case': 8, - 'Sentence case': 9, - 'path/case': 10, - 'Header-Case': 11 -}; - -const obj = { - ...core, - lv11: { ...core }, - lv12: { - ...core, - lv21: { - ...core, - lv31: { - ...core - }, - lv32: { - ...core - } - }, - lv22: { - ...core - } - } -}; - -module.exports = obj; diff --git a/test/recursive/pascalKeys.js b/test/recursive/pascalKeys.js index 9d44964..d3675f9 100644 --- a/test/recursive/pascalKeys.js +++ b/test/recursive/pascalKeys.js @@ -1,12 +1,13 @@ const jsConvert = require('../../index'); -const obj = require('./object'); +const { obj, log } = require('./_shared'); -console.log(jsConvert.pascalKeys(obj)); +log(jsConvert.pascalKeys(obj)); console.log('============================================'); -console.log(jsConvert.pascalKeys(obj, { recursive: true })); +log(jsConvert.pascalKeys(obj, { recursive: true })); /** -{ CamelCase: 1, +{ + CamelCase: 1, Uppercase: 2, Lowercase: 3, SnakeCase: 4, @@ -17,32 +18,45 @@ console.log(jsConvert.pascalKeys(obj, { recursive: true })); SentenceCase: 9, PathCase: 10, HeaderCase: 11, - Lv11: - { camelCase: 1, - UPPERCASE: 2, - lowercase: 3, - snake_case: 4, - PascalCase: 5, - 'Title Case': 6, - 'dot.case': 7, - 'param-case': 8, - 'Sentence case': 9, - 'path/case': 10, - 'Header-Case': 11 }, - Lv12: - { camelCase: 1, - UPPERCASE: 2, - lowercase: 3, - snake_case: 4, - PascalCase: 5, - 'Title Case': 6, - 'dot.case': 7, - 'param-case': 8, - 'Sentence case': 9, - 'path/case': 10, - 'Header-Case': 11, - lv21: - { camelCase: 1, + Lv11: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + Lv12: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11, + lv21: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11, + lv31: { + camelCase: 1, UPPERCASE: 2, lowercase: 3, snake_case: 4, @@ -52,11 +66,10 @@ console.log(jsConvert.pascalKeys(obj, { recursive: true })); 'param-case': 8, 'Sentence case': 9, 'path/case': 10, - 'Header-Case': 11, - lv31: [Object], - lv32: [Object] }, - lv22: - { camelCase: 1, + 'Header-Case': 11 + }, + lv32: { + camelCase: 1, UPPERCASE: 2, lowercase: 3, snake_case: 4, @@ -66,9 +79,98 @@ console.log(jsConvert.pascalKeys(obj, { recursive: true })); 'param-case': 8, 'Sentence case': 9, 'path/case': 10, - 'Header-Case': 11 } } } + 'Header-Case': 11 + } + }, + lv22: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + lv23: [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + { + lv31: [ + 1, + 'A', + null, + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + '123', + undefined + ], + lv32: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + } + }, + [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + 5, + '123', + { a: 1, b: 2 }, + null + ], + null, + undefined + ] + } +} ============================================ -{ CamelCase: 1, +{ + CamelCase: 1, Uppercase: 2, Lowercase: 3, SnakeCase: 4, @@ -79,32 +181,45 @@ console.log(jsConvert.pascalKeys(obj, { recursive: true })); SentenceCase: 9, PathCase: 10, HeaderCase: 11, - Lv11: - { CamelCase: 1, - Uppercase: 2, - Lowercase: 3, - SnakeCase: 4, - PascalCase: 5, - TitleCase: 6, - DotCase: 7, - ParamCase: 8, - SentenceCase: 9, - PathCase: 10, - HeaderCase: 11 }, - Lv12: - { CamelCase: 1, - Uppercase: 2, - Lowercase: 3, - SnakeCase: 4, - PascalCase: 5, - TitleCase: 6, - DotCase: 7, - ParamCase: 8, - SentenceCase: 9, - PathCase: 10, - HeaderCase: 11, - Lv21: - { CamelCase: 1, + Lv11: { + CamelCase: 1, + Uppercase: 2, + Lowercase: 3, + SnakeCase: 4, + PascalCase: 5, + TitleCase: 6, + DotCase: 7, + ParamCase: 8, + SentenceCase: 9, + PathCase: 10, + HeaderCase: 11 + }, + Lv12: { + CamelCase: 1, + Uppercase: 2, + Lowercase: 3, + SnakeCase: 4, + PascalCase: 5, + TitleCase: 6, + DotCase: 7, + ParamCase: 8, + SentenceCase: 9, + PathCase: 10, + HeaderCase: 11, + Lv21: { + CamelCase: 1, + Uppercase: 2, + Lowercase: 3, + SnakeCase: 4, + PascalCase: 5, + TitleCase: 6, + DotCase: 7, + ParamCase: 8, + SentenceCase: 9, + PathCase: 10, + HeaderCase: 11, + Lv31: { + CamelCase: 1, Uppercase: 2, Lowercase: 3, SnakeCase: 4, @@ -114,11 +229,10 @@ console.log(jsConvert.pascalKeys(obj, { recursive: true })); ParamCase: 8, SentenceCase: 9, PathCase: 10, - HeaderCase: 11, - Lv31: [Object], - Lv32: [Object] }, - Lv22: - { CamelCase: 1, + HeaderCase: 11 + }, + Lv32: { + CamelCase: 1, Uppercase: 2, Lowercase: 3, SnakeCase: 4, @@ -128,5 +242,94 @@ console.log(jsConvert.pascalKeys(obj, { recursive: true })); ParamCase: 8, SentenceCase: 9, PathCase: 10, - HeaderCase: 11 } } } + HeaderCase: 11 + } + }, + Lv22: { + CamelCase: 1, + Uppercase: 2, + Lowercase: 3, + SnakeCase: 4, + PascalCase: 5, + TitleCase: 6, + DotCase: 7, + ParamCase: 8, + SentenceCase: 9, + PathCase: 10, + HeaderCase: 11 + }, + Lv23: [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + { + lv31: [ + 1, + 'A', + null, + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + '123', + undefined + ], + lv32: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + } + }, + [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + 5, + '123', + { a: 1, b: 2 }, + null + ], + null, + undefined + ] + } +} + */ diff --git a/test/recursive/pascalKeysArray.js b/test/recursive/pascalKeysArray.js new file mode 100644 index 0000000..a474ffc --- /dev/null +++ b/test/recursive/pascalKeysArray.js @@ -0,0 +1,152 @@ +const jsConvert = require('../../index'); +const { obj, log } = require('./_shared'); + +log(jsConvert.pascalKeys(obj, { recursive: true, recursiveInArray: true })); + +/** +{ + CamelCase: 1, + Uppercase: 2, + Lowercase: 3, + SnakeCase: 4, + PascalCase: 5, + TitleCase: 6, + DotCase: 7, + ParamCase: 8, + SentenceCase: 9, + PathCase: 10, + HeaderCase: 11, + Lv11: { + CamelCase: 1, + Uppercase: 2, + Lowercase: 3, + SnakeCase: 4, + PascalCase: 5, + TitleCase: 6, + DotCase: 7, + ParamCase: 8, + SentenceCase: 9, + PathCase: 10, + HeaderCase: 11 + }, + Lv12: { + CamelCase: 1, + Uppercase: 2, + Lowercase: 3, + SnakeCase: 4, + PascalCase: 5, + TitleCase: 6, + DotCase: 7, + ParamCase: 8, + SentenceCase: 9, + PathCase: 10, + HeaderCase: 11, + Lv21: { + CamelCase: 1, + Uppercase: 2, + Lowercase: 3, + SnakeCase: 4, + PascalCase: 5, + TitleCase: 6, + DotCase: 7, + ParamCase: 8, + SentenceCase: 9, + PathCase: 10, + HeaderCase: 11, + Lv31: { + CamelCase: 1, + Uppercase: 2, + Lowercase: 3, + SnakeCase: 4, + PascalCase: 5, + TitleCase: 6, + DotCase: 7, + ParamCase: 8, + SentenceCase: 9, + PathCase: 10, + HeaderCase: 11 + }, + Lv32: { + CamelCase: 1, + Uppercase: 2, + Lowercase: 3, + SnakeCase: 4, + PascalCase: 5, + TitleCase: 6, + DotCase: 7, + ParamCase: 8, + SentenceCase: 9, + PathCase: 10, + HeaderCase: 11 + } + }, + Lv22: { + CamelCase: 1, + Uppercase: 2, + Lowercase: 3, + SnakeCase: 4, + PascalCase: 5, + TitleCase: 6, + DotCase: 7, + ParamCase: 8, + SentenceCase: 9, + PathCase: 10, + HeaderCase: 11 + }, + Lv23: [ + { + CamelCase: 1, + Uppercase: 2, + Lowercase: 3, + SnakeCase: 4, + PascalCase: 5, + TitleCase: 6, + DotCase: 7, + ParamCase: 8, + SentenceCase: 9, + PathCase: 10, + HeaderCase: 11 + }, + { + Lv31: [ + 1, + 'A', + null, + { + CamelCase: 1, + Uppercase: 2, + Lowercase: 3, + SnakeCase: 4, + PascalCase: 5, + TitleCase: 6, + DotCase: 7, + ParamCase: 8, + SentenceCase: 9, + PathCase: 10, + HeaderCase: 11 + }, + '123', + undefined + ], + Lv32: { + CamelCase: 1, + Uppercase: 2, + Lowercase: 3, + SnakeCase: 4, + PascalCase: 5, + TitleCase: 6, + DotCase: 7, + ParamCase: 8, + SentenceCase: 9, + PathCase: 10, + HeaderCase: 11 + } + }, + undefined, + null, + undefined + ] + } +} + + */ diff --git a/test/recursive/snakeKeys.js b/test/recursive/snakeKeys.js index 53d3096..65d64a7 100644 --- a/test/recursive/snakeKeys.js +++ b/test/recursive/snakeKeys.js @@ -1,12 +1,13 @@ const jsConvert = require('../../index'); -const obj = require('./object'); +const { obj, log } = require('./_shared'); -console.log(jsConvert.snakeKeys(obj)); -console.log('============================================'); -console.log(jsConvert.snakeKeys(obj, { recursive: true })); +log(jsConvert.snakeKeys(obj)); +console.log('==========================================='); +log(jsConvert.snakeKeys(obj, { recursive: true })); /** -{ camel_case: 1, +{ + camel_case: 1, uppercase: 2, lowercase: 3, snake_case: 4, @@ -17,32 +18,45 @@ console.log(jsConvert.snakeKeys(obj, { recursive: true })); sentence_case: 9, path_case: 10, header_case: 11, - lv11: - { camelCase: 1, - UPPERCASE: 2, - lowercase: 3, - snake_case: 4, - PascalCase: 5, - 'Title Case': 6, - 'dot.case': 7, - 'param-case': 8, - 'Sentence case': 9, - 'path/case': 10, - 'Header-Case': 11 }, - lv12: - { camelCase: 1, - UPPERCASE: 2, - lowercase: 3, - snake_case: 4, - PascalCase: 5, - 'Title Case': 6, - 'dot.case': 7, - 'param-case': 8, - 'Sentence case': 9, - 'path/case': 10, - 'Header-Case': 11, - lv21: - { camelCase: 1, + lv11: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + lv12: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11, + lv21: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11, + lv31: { + camelCase: 1, UPPERCASE: 2, lowercase: 3, snake_case: 4, @@ -52,11 +66,10 @@ console.log(jsConvert.snakeKeys(obj, { recursive: true })); 'param-case': 8, 'Sentence case': 9, 'path/case': 10, - 'Header-Case': 11, - lv31: [Object], - lv32: [Object] }, - lv22: - { camelCase: 1, + 'Header-Case': 11 + }, + lv32: { + camelCase: 1, UPPERCASE: 2, lowercase: 3, snake_case: 4, @@ -66,9 +79,98 @@ console.log(jsConvert.snakeKeys(obj, { recursive: true })); 'param-case': 8, 'Sentence case': 9, 'path/case': 10, - 'Header-Case': 11 } } } -============================================ -{ camel_case: 1, + 'Header-Case': 11 + } + }, + lv22: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + lv23: [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + { + lv31: [ + 1, + 'A', + null, + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + '123', + undefined + ], + lv32: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + } + }, + [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + 5, + '123', + { a: 1, b: 2 }, + null + ], + null, + undefined + ] + } +} +=========================================== +{ + camel_case: 1, uppercase: 2, lowercase: 3, snake_case: 4, @@ -79,32 +181,45 @@ console.log(jsConvert.snakeKeys(obj, { recursive: true })); sentence_case: 9, path_case: 10, header_case: 11, - lv11: - { camel_case: 1, - uppercase: 2, - lowercase: 3, - snake_case: 4, - pascal_case: 5, - title_case: 6, - dot_case: 7, - param_case: 8, - sentence_case: 9, - path_case: 10, - header_case: 11 }, - lv12: - { camel_case: 1, - uppercase: 2, - lowercase: 3, - snake_case: 4, - pascal_case: 5, - title_case: 6, - dot_case: 7, - param_case: 8, - sentence_case: 9, - path_case: 10, - header_case: 11, - lv21: - { camel_case: 1, + lv11: { + camel_case: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascal_case: 5, + title_case: 6, + dot_case: 7, + param_case: 8, + sentence_case: 9, + path_case: 10, + header_case: 11 + }, + lv12: { + camel_case: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascal_case: 5, + title_case: 6, + dot_case: 7, + param_case: 8, + sentence_case: 9, + path_case: 10, + header_case: 11, + lv21: { + camel_case: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascal_case: 5, + title_case: 6, + dot_case: 7, + param_case: 8, + sentence_case: 9, + path_case: 10, + header_case: 11, + lv31: { + camel_case: 1, uppercase: 2, lowercase: 3, snake_case: 4, @@ -114,11 +229,10 @@ console.log(jsConvert.snakeKeys(obj, { recursive: true })); param_case: 8, sentence_case: 9, path_case: 10, - header_case: 11, - lv31: [Object], - lv32: [Object] }, - lv22: - { camel_case: 1, + header_case: 11 + }, + lv32: { + camel_case: 1, uppercase: 2, lowercase: 3, snake_case: 4, @@ -128,5 +242,94 @@ console.log(jsConvert.snakeKeys(obj, { recursive: true })); param_case: 8, sentence_case: 9, path_case: 10, - header_case: 11 } } } - */ + header_case: 11 + } + }, + lv22: { + camel_case: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascal_case: 5, + title_case: 6, + dot_case: 7, + param_case: 8, + sentence_case: 9, + path_case: 10, + header_case: 11 + }, + lv23: [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + { + lv31: [ + 1, + 'A', + null, + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + '123', + undefined + ], + lv32: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + } + }, + [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + 5, + '123', + { a: 1, b: 2 }, + null + ], + null, + undefined + ] + } +} + +*/ diff --git a/test/recursive/snakeKeysArray.js b/test/recursive/snakeKeysArray.js new file mode 100644 index 0000000..134c037 --- /dev/null +++ b/test/recursive/snakeKeysArray.js @@ -0,0 +1,171 @@ +const jsConvert = require('../../index'); +const { obj, log } = require('./_shared'); + +log(jsConvert.snakeKeys(obj, { recursive: true, recursiveInArray: true })); + +/** +{ + camel_case: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascal_case: 5, + title_case: 6, + dot_case: 7, + param_case: 8, + sentence_case: 9, + path_case: 10, + header_case: 11, + lv11: { + camel_case: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascal_case: 5, + title_case: 6, + dot_case: 7, + param_case: 8, + sentence_case: 9, + path_case: 10, + header_case: 11 + }, + lv12: { + camel_case: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascal_case: 5, + title_case: 6, + dot_case: 7, + param_case: 8, + sentence_case: 9, + path_case: 10, + header_case: 11, + lv21: { + camel_case: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascal_case: 5, + title_case: 6, + dot_case: 7, + param_case: 8, + sentence_case: 9, + path_case: 10, + header_case: 11, + lv31: { + camel_case: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascal_case: 5, + title_case: 6, + dot_case: 7, + param_case: 8, + sentence_case: 9, + path_case: 10, + header_case: 11 + }, + lv32: { + camel_case: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascal_case: 5, + title_case: 6, + dot_case: 7, + param_case: 8, + sentence_case: 9, + path_case: 10, + header_case: 11 + } + }, + lv22: { + camel_case: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascal_case: 5, + title_case: 6, + dot_case: 7, + param_case: 8, + sentence_case: 9, + path_case: 10, + header_case: 11 + }, + lv23: [ + { + camel_case: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascal_case: 5, + title_case: 6, + dot_case: 7, + param_case: 8, + sentence_case: 9, + path_case: 10, + header_case: 11 + }, + { + lv31: [ + 1, + 'A', + null, + { + camel_case: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascal_case: 5, + title_case: 6, + dot_case: 7, + param_case: 8, + sentence_case: 9, + path_case: 10, + header_case: 11 + }, + '123', + undefined + ], + lv32: { + camel_case: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascal_case: 5, + title_case: 6, + dot_case: 7, + param_case: 8, + sentence_case: 9, + path_case: 10, + header_case: 11 + } + }, + [ + { + camel_case: 1, + uppercase: 2, + lowercase: 3, + snake_case: 4, + pascal_case: 5, + title_case: 6, + dot_case: 7, + param_case: 8, + sentence_case: 9, + path_case: 10, + header_case: 11 + }, + 5, + '123', + { a: 1, b: 2 }, + null + ], + null, + undefined + ] + } +} + + +*/ diff --git a/test/recursive/upperKeys.js b/test/recursive/upperKeys.js index d405e53..5c2903a 100644 --- a/test/recursive/upperKeys.js +++ b/test/recursive/upperKeys.js @@ -1,12 +1,13 @@ const jsConvert = require('../../index'); -const obj = require('./object'); +const { obj, log } = require('./_shared'); -console.log(jsConvert.upperKeys(obj)); +log(jsConvert.upperKeys(obj)); console.log('============================================'); -console.log(jsConvert.upperKeys(obj, { recursive: true })); +log(jsConvert.upperKeys(obj, { recursive: true })); /** -{ CAMELCASE: 1, +{ + CAMELCASE: 1, UPPERCASE: 2, LOWERCASE: 3, SNAKE_CASE: 4, @@ -17,32 +18,45 @@ console.log(jsConvert.upperKeys(obj, { recursive: true })); 'SENTENCE CASE': 9, 'PATH/CASE': 10, 'HEADER-CASE': 11, - LV11: - { camelCase: 1, - UPPERCASE: 2, - lowercase: 3, - snake_case: 4, - PascalCase: 5, - 'Title Case': 6, - 'dot.case': 7, - 'param-case': 8, - 'Sentence case': 9, - 'path/case': 10, - 'Header-Case': 11 }, - LV12: - { camelCase: 1, - UPPERCASE: 2, - lowercase: 3, - snake_case: 4, - PascalCase: 5, - 'Title Case': 6, - 'dot.case': 7, - 'param-case': 8, - 'Sentence case': 9, - 'path/case': 10, - 'Header-Case': 11, - lv21: - { camelCase: 1, + LV11: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + LV12: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11, + lv21: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11, + lv31: { + camelCase: 1, UPPERCASE: 2, lowercase: 3, snake_case: 4, @@ -52,11 +66,10 @@ console.log(jsConvert.upperKeys(obj, { recursive: true })); 'param-case': 8, 'Sentence case': 9, 'path/case': 10, - 'Header-Case': 11, - lv31: [Object], - lv32: [Object] }, - lv22: - { camelCase: 1, + 'Header-Case': 11 + }, + lv32: { + camelCase: 1, UPPERCASE: 2, lowercase: 3, snake_case: 4, @@ -66,9 +79,98 @@ console.log(jsConvert.upperKeys(obj, { recursive: true })); 'param-case': 8, 'Sentence case': 9, 'path/case': 10, - 'Header-Case': 11 } } } + 'Header-Case': 11 + } + }, + lv22: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + lv23: [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + { + lv31: [ + 1, + 'A', + null, + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + '123', + undefined + ], + lv32: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + } + }, + [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + 5, + '123', + { a: 1, b: 2 }, + null + ], + null, + undefined + ] + } +} ============================================ -{ CAMELCASE: 1, +{ + CAMELCASE: 1, UPPERCASE: 2, LOWERCASE: 3, SNAKE_CASE: 4, @@ -79,32 +181,45 @@ console.log(jsConvert.upperKeys(obj, { recursive: true })); 'SENTENCE CASE': 9, 'PATH/CASE': 10, 'HEADER-CASE': 11, - LV11: - { CAMELCASE: 1, - UPPERCASE: 2, - LOWERCASE: 3, - SNAKE_CASE: 4, - PASCALCASE: 5, - 'TITLE CASE': 6, - 'DOT.CASE': 7, - 'PARAM-CASE': 8, - 'SENTENCE CASE': 9, - 'PATH/CASE': 10, - 'HEADER-CASE': 11 }, - LV12: - { CAMELCASE: 1, - UPPERCASE: 2, - LOWERCASE: 3, - SNAKE_CASE: 4, - PASCALCASE: 5, - 'TITLE CASE': 6, - 'DOT.CASE': 7, - 'PARAM-CASE': 8, - 'SENTENCE CASE': 9, - 'PATH/CASE': 10, - 'HEADER-CASE': 11, - LV21: - { CAMELCASE: 1, + LV11: { + CAMELCASE: 1, + UPPERCASE: 2, + LOWERCASE: 3, + SNAKE_CASE: 4, + PASCALCASE: 5, + 'TITLE CASE': 6, + 'DOT.CASE': 7, + 'PARAM-CASE': 8, + 'SENTENCE CASE': 9, + 'PATH/CASE': 10, + 'HEADER-CASE': 11 + }, + LV12: { + CAMELCASE: 1, + UPPERCASE: 2, + LOWERCASE: 3, + SNAKE_CASE: 4, + PASCALCASE: 5, + 'TITLE CASE': 6, + 'DOT.CASE': 7, + 'PARAM-CASE': 8, + 'SENTENCE CASE': 9, + 'PATH/CASE': 10, + 'HEADER-CASE': 11, + LV21: { + CAMELCASE: 1, + UPPERCASE: 2, + LOWERCASE: 3, + SNAKE_CASE: 4, + PASCALCASE: 5, + 'TITLE CASE': 6, + 'DOT.CASE': 7, + 'PARAM-CASE': 8, + 'SENTENCE CASE': 9, + 'PATH/CASE': 10, + 'HEADER-CASE': 11, + LV31: { + CAMELCASE: 1, UPPERCASE: 2, LOWERCASE: 3, SNAKE_CASE: 4, @@ -114,11 +229,10 @@ console.log(jsConvert.upperKeys(obj, { recursive: true })); 'PARAM-CASE': 8, 'SENTENCE CASE': 9, 'PATH/CASE': 10, - 'HEADER-CASE': 11, - LV31: [Object], - LV32: [Object] }, - LV22: - { CAMELCASE: 1, + 'HEADER-CASE': 11 + }, + LV32: { + CAMELCASE: 1, UPPERCASE: 2, LOWERCASE: 3, SNAKE_CASE: 4, @@ -128,5 +242,94 @@ console.log(jsConvert.upperKeys(obj, { recursive: true })); 'PARAM-CASE': 8, 'SENTENCE CASE': 9, 'PATH/CASE': 10, - 'HEADER-CASE': 11 } } } + 'HEADER-CASE': 11 + } + }, + LV22: { + CAMELCASE: 1, + UPPERCASE: 2, + LOWERCASE: 3, + SNAKE_CASE: 4, + PASCALCASE: 5, + 'TITLE CASE': 6, + 'DOT.CASE': 7, + 'PARAM-CASE': 8, + 'SENTENCE CASE': 9, + 'PATH/CASE': 10, + 'HEADER-CASE': 11 + }, + LV23: [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + { + lv31: [ + 1, + 'A', + null, + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + '123', + undefined + ], + lv32: { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + } + }, + [ + { + camelCase: 1, + UPPERCASE: 2, + lowercase: 3, + snake_case: 4, + PascalCase: 5, + 'Title Case': 6, + 'dot.case': 7, + 'param-case': 8, + 'Sentence case': 9, + 'path/case': 10, + 'Header-Case': 11 + }, + 5, + '123', + { a: 1, b: 2 }, + null + ], + null, + undefined + ] + } +} + */ diff --git a/test/recursive/upperKeysArray.js b/test/recursive/upperKeysArray.js new file mode 100644 index 0000000..b725733 --- /dev/null +++ b/test/recursive/upperKeysArray.js @@ -0,0 +1,152 @@ +const jsConvert = require('../../index'); +const { obj, log } = require('./_shared'); + +log(jsConvert.upperKeys(obj, { recursive: true, recursiveInArray: true })); + +/** +{ + CAMELCASE: 1, + UPPERCASE: 2, + LOWERCASE: 3, + SNAKE_CASE: 4, + PASCALCASE: 5, + 'TITLE CASE': 6, + 'DOT.CASE': 7, + 'PARAM-CASE': 8, + 'SENTENCE CASE': 9, + 'PATH/CASE': 10, + 'HEADER-CASE': 11, + LV11: { + CAMELCASE: 1, + UPPERCASE: 2, + LOWERCASE: 3, + SNAKE_CASE: 4, + PASCALCASE: 5, + 'TITLE CASE': 6, + 'DOT.CASE': 7, + 'PARAM-CASE': 8, + 'SENTENCE CASE': 9, + 'PATH/CASE': 10, + 'HEADER-CASE': 11 + }, + LV12: { + CAMELCASE: 1, + UPPERCASE: 2, + LOWERCASE: 3, + SNAKE_CASE: 4, + PASCALCASE: 5, + 'TITLE CASE': 6, + 'DOT.CASE': 7, + 'PARAM-CASE': 8, + 'SENTENCE CASE': 9, + 'PATH/CASE': 10, + 'HEADER-CASE': 11, + LV21: { + CAMELCASE: 1, + UPPERCASE: 2, + LOWERCASE: 3, + SNAKE_CASE: 4, + PASCALCASE: 5, + 'TITLE CASE': 6, + 'DOT.CASE': 7, + 'PARAM-CASE': 8, + 'SENTENCE CASE': 9, + 'PATH/CASE': 10, + 'HEADER-CASE': 11, + LV31: { + CAMELCASE: 1, + UPPERCASE: 2, + LOWERCASE: 3, + SNAKE_CASE: 4, + PASCALCASE: 5, + 'TITLE CASE': 6, + 'DOT.CASE': 7, + 'PARAM-CASE': 8, + 'SENTENCE CASE': 9, + 'PATH/CASE': 10, + 'HEADER-CASE': 11 + }, + LV32: { + CAMELCASE: 1, + UPPERCASE: 2, + LOWERCASE: 3, + SNAKE_CASE: 4, + PASCALCASE: 5, + 'TITLE CASE': 6, + 'DOT.CASE': 7, + 'PARAM-CASE': 8, + 'SENTENCE CASE': 9, + 'PATH/CASE': 10, + 'HEADER-CASE': 11 + } + }, + LV22: { + CAMELCASE: 1, + UPPERCASE: 2, + LOWERCASE: 3, + SNAKE_CASE: 4, + PASCALCASE: 5, + 'TITLE CASE': 6, + 'DOT.CASE': 7, + 'PARAM-CASE': 8, + 'SENTENCE CASE': 9, + 'PATH/CASE': 10, + 'HEADER-CASE': 11 + }, + LV23: [ + { + CAMELCASE: 1, + UPPERCASE: 2, + LOWERCASE: 3, + SNAKE_CASE: 4, + PASCALCASE: 5, + 'TITLE CASE': 6, + 'DOT.CASE': 7, + 'PARAM-CASE': 8, + 'SENTENCE CASE': 9, + 'PATH/CASE': 10, + 'HEADER-CASE': 11 + }, + { + LV31: [ + 1, + 'A', + null, + { + CAMELCASE: 1, + UPPERCASE: 2, + LOWERCASE: 3, + SNAKE_CASE: 4, + PASCALCASE: 5, + 'TITLE CASE': 6, + 'DOT.CASE': 7, + 'PARAM-CASE': 8, + 'SENTENCE CASE': 9, + 'PATH/CASE': 10, + 'HEADER-CASE': 11 + }, + '123', + undefined + ], + LV32: { + CAMELCASE: 1, + UPPERCASE: 2, + LOWERCASE: 3, + SNAKE_CASE: 4, + PASCALCASE: 5, + 'TITLE CASE': 6, + 'DOT.CASE': 7, + 'PARAM-CASE': 8, + 'SENTENCE CASE': 9, + 'PATH/CASE': 10, + 'HEADER-CASE': 11 + } + }, + undefined, + null, + undefined + ] + } +} + +*/