From 83fba81d5545e4e055846515532dee5b2e6bdca9 Mon Sep 17 00:00:00 2001 From: Luca Ban Date: Fri, 26 Jun 2020 15:40:04 +0900 Subject: [PATCH] feat: improve logic --- dist/index.cjs.js | 2 +- dist/index.esm.js | 2 +- package.json | 2 +- src/index.ts | 2 +- test/index.ts | 13 ++++--------- 5 files changed, 8 insertions(+), 13 deletions(-) diff --git a/dist/index.cjs.js b/dist/index.cjs.js index b56ec5e..a814301 100644 --- a/dist/index.cjs.js +++ b/dist/index.cjs.js @@ -40,7 +40,7 @@ function _findAndReplaceIf(target, checkFn, propKey, config) { if (config === void 0) { config = { onlyPlainObjects: true, checkArrayValues: false }; } var _target = checkFn(target, propKey); if (config.checkArrayValues && isWhat.isArray(_target) && !isWhat.isAnyObject(_target)) { - return _target.map(function (value) { return checkFn(value, undefined); }); + return _target.map(function (value) { return _findAndReplaceIf(value, checkFn, undefined, config); }); } if (!isWhat.isPlainObject(_target)) return _target; diff --git a/dist/index.esm.js b/dist/index.esm.js index 899f488..954d738 100644 --- a/dist/index.esm.js +++ b/dist/index.esm.js @@ -36,7 +36,7 @@ function _findAndReplaceIf(target, checkFn, propKey, config) { if (config === void 0) { config = { onlyPlainObjects: true, checkArrayValues: false }; } var _target = checkFn(target, propKey); if (config.checkArrayValues && isArray(_target) && !isAnyObject(_target)) { - return _target.map(function (value) { return checkFn(value, undefined); }); + return _target.map(function (value) { return _findAndReplaceIf(value, checkFn, undefined, config); }); } if (!isPlainObject(_target)) return _target; diff --git a/package.json b/package.json index 8e1faba..367bf9b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "find-and-replace-anything", "sideEffects": false, - "version": "2.2.0", + "version": "2.2.1", "description": "Replace one val with another or all occurrences in an object recursively. A simple & small integration.", "main": "dist/index.cjs.js", "module": "dist/index.esm.js", diff --git a/src/index.ts b/src/index.ts index 24fa83b..56a1dcd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,7 +51,7 @@ export function _findAndReplaceIf ( ): any { const _target = checkFn(target, propKey) if (config.checkArrayValues && isArray(_target) && !isAnyObject(_target)) { - return (_target as any[]).map(value => checkFn(value, undefined)) + return (_target as any[]).map(value => _findAndReplaceIf(value, checkFn, undefined, config)) } if (!isPlainObject(_target)) return _target return Object.entries(_target).reduce((carry, [key, val]) => { diff --git a/test/index.ts b/test/index.ts index cd8cdca..6db2915 100644 --- a/test/index.ts +++ b/test/index.ts @@ -10,21 +10,16 @@ test('findAndReplace in arrays', t => { }) test('findAndReplaceIf in arrays', t => { - function replacer (foundVal) { - return foundVal === 'c' ? 'd' : foundVal - } + const replacer = foundVal => (foundVal === 'c' ? 'd' : foundVal) t.deepEqual(findAndReplaceIf({ a: ['c'] }, replacer, { checkArrayValues: true }), { a: ['d'], }) }) test('findAndReplaceIf in arrays double nested', t => { - const replace = foundVal => (foundVal === 'c' ? 'd' : foundVal) - function replacer (foundVal) { - return isPlainObject(foundVal) ? findAndReplaceIf(foundVal, replace) : foundVal - } - t.deepEqual(findAndReplaceIf({ a: [{ b: 'c' }] }, replacer, { checkArrayValues: true }), { - a: [{ b: 'd' }], + const replacer = foundVal => (foundVal === 'c' ? 'd' : foundVal) + t.deepEqual(findAndReplaceIf({ a: [{ b: 'c' }, 'c'] }, replacer, { checkArrayValues: true }), { + a: [{ b: 'd' }, 'd'], }) })