From deb8fd51d6b69ab4f22999a8639c4c3c94620f93 Mon Sep 17 00:00:00 2001 From: JohannMoller Date: Thu, 22 Jul 2021 12:01:02 +0200 Subject: [PATCH] fix(operators): Comments fixes and catch nested objects --- packages/operators/src/common/change_case.js | 65 ++++++++++++-------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/packages/operators/src/common/change_case.js b/packages/operators/src/common/change_case.js index 1817c71b6..d4cfe66c6 100644 --- a/packages/operators/src/common/change_case.js +++ b/packages/operators/src/common/change_case.js @@ -13,24 +13,36 @@ See the License for the specific language governing permissions and limitations under the License. */ - -import * as changeCase from 'change-case'; +/* eslint-disable import/namespace */ +import { + camelCase, + capitalCase, + constantCase, + dotCase, + headerCase, + noCase, + paramCase, + pascalCase, + pathCase, + sentenceCase, + snakeCase, +} from 'change-case'; import { get, type } from '@lowdefy/helpers'; import runClass from '../runClass'; -const supportedMethods = [ - 'camelCase', - 'capitalCase', - 'constantCase', - 'dotCase', - 'headerCase', - 'noCase', - 'paramCase', - 'pascalCase', - 'pathCase', - 'sentenceCase', - 'snakeCase', -]; +const changeCase = { + camelCase, + capitalCase, + constantCase, + dotCase, + headerCase, + noCase, + paramCase, + pascalCase, + pathCase, + sentenceCase, + snakeCase, +}; const convertArray = ({ methodName, on, options }) => { return on.map((item) => { @@ -41,17 +53,21 @@ const convertArray = ({ methodName, on, options }) => { }); }; -const convertObject = ({ methodName, on, options = {} }) => { +const convertObject = ({ methodName, on, options }) => { const result = {}; - const keyConverter = options.convertKeys + const keyConverter = get(options, 'convertKeys') ? (key) => changeCase[methodName](key, options) : (key) => key; const valueConverter = get(options, 'convertValues', { default: true }) ? (val) => changeCase[methodName](val, options) : (val) => val; - Object.entries(on).forEach((item) => { - result[keyConverter(item[0])] = valueConverter(item[1]); + Object.entries(on).forEach(([key, value]) => { + if (type.isString(value)) { + result[keyConverter(key)] = valueConverter(value); + } else { + result[keyConverter(key)] = value; + } }); return result; }; @@ -59,21 +75,22 @@ const convertObject = ({ methodName, on, options = {} }) => { const makeCaseChanger = ({ methodName }) => (on, options = {}) => { - if (type.isObject(on)) { - return convertObject({ methodName, on, options }); + if (type.isString(on)) { + return changeCase[methodName](on, options); } if (type.isArray(on)) { return convertArray({ methodName, on, options }); } - if (type.isString(on)) { - return changeCase[methodName](on, options); + if (type.isObject(on)) { + return convertObject({ methodName, on, options }); } return on; }; const functions = {}; const meta = {}; -supportedMethods.forEach((methodName) => { + +Object.keys(changeCase).forEach((methodName) => { functions[methodName] = makeCaseChanger({ methodName }); meta[methodName] = { namedArgs: ['on', 'options'], validTypes: ['array', 'object'] }; });