Skip to content

Commit

Permalink
feat: improve logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb committed Jun 26, 2020
1 parent ba969fb commit 83fba81
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dist/index.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion dist/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]) => {
Expand Down
13 changes: 4 additions & 9 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
})
})

Expand Down

0 comments on commit 83fba81

Please sign in to comment.