Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ function findAndReplace(target, find, replaceWith, config = { onlyPlainObjects:
return carry;
}, {});
}
function _findAndReplaceIf(target, checkFn, propKey, config = { onlyPlainObjects: true, checkArrayValues: false }) {
const _target = checkFn(target, propKey);
function _findAndReplaceIf(target, mainObject, checkFn, propKey, propPath, config = { onlyPlainObjects: true, checkArrayValues: false }) {
const _target = checkFn(target, propKey, propPath, mainObject);
if (config.checkArrayValues && isWhat.isArray(_target) && !isWhat.isAnyObject(_target)) {
return _target.map(value => _findAndReplaceIf(value, checkFn, undefined, config));
return _target.map((value, index) => {
const ppath = propPath !== undefined ? [propPath, index].join('.') : String(index);
return _findAndReplaceIf(value, mainObject, checkFn, undefined, ppath, config);
});
}
if (!isWhat.isPlainObject(_target))
return _target;
return Object.entries(_target).reduce((carry, [key, val]) => {
carry[key] = _findAndReplaceIf(val, checkFn, key, config);
const ppath = propPath !== undefined ? [propPath, key].join('.') : key;
carry[key] = _findAndReplaceIf(val, mainObject, checkFn, key, ppath, config);
return carry;
}, {});
}
Expand All @@ -56,7 +60,7 @@ function _findAndReplaceIf(target, checkFn, propKey, config = { onlyPlainObjects
* @returns {*} the target with replaced values
*/
function findAndReplaceIf(target, checkFn, config = { onlyPlainObjects: true, checkArrayValues: false }) {
return _findAndReplaceIf(target, checkFn, undefined, config);
return _findAndReplaceIf(target, target, checkFn, undefined, undefined, config);
}

exports._findAndReplaceIf = _findAndReplaceIf;
Expand Down
14 changes: 9 additions & 5 deletions dist/index.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ function findAndReplace(target, find, replaceWith, config = { onlyPlainObjects:
return carry;
}, {});
}
function _findAndReplaceIf(target, checkFn, propKey, config = { onlyPlainObjects: true, checkArrayValues: false }) {
const _target = checkFn(target, propKey);
function _findAndReplaceIf(target, mainObject, checkFn, propKey, propPath, config = { onlyPlainObjects: true, checkArrayValues: false }) {
const _target = checkFn(target, propKey, propPath, mainObject);
if (config.checkArrayValues && isArray(_target) && !isAnyObject(_target)) {
return _target.map(value => _findAndReplaceIf(value, checkFn, undefined, config));
return _target.map((value, index) => {
const ppath = propPath !== undefined ? [propPath, index].join('.') : String(index);
return _findAndReplaceIf(value, mainObject, checkFn, undefined, ppath, config);
});
}
if (!isPlainObject(_target))
return _target;
return Object.entries(_target).reduce((carry, [key, val]) => {
carry[key] = _findAndReplaceIf(val, checkFn, key, config);
const ppath = propPath !== undefined ? [propPath, key].join('.') : key;
carry[key] = _findAndReplaceIf(val, mainObject, checkFn, key, ppath, config);
return carry;
}, {});
}
Expand All @@ -52,7 +56,7 @@ function _findAndReplaceIf(target, checkFn, propKey, config = { onlyPlainObjects
* @returns {*} the target with replaced values
*/
function findAndReplaceIf(target, checkFn, config = { onlyPlainObjects: true, checkArrayValues: false }) {
return _findAndReplaceIf(target, checkFn, undefined, config);
return _findAndReplaceIf(target, target, checkFn, undefined, undefined, config);
}

export { _findAndReplaceIf, findAndReplace, findAndReplaceIf };
2 changes: 1 addition & 1 deletion dist/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ declare type Config = {
* @returns {*} the target with replaced values
*/
export declare function findAndReplace(target: any, find: any, replaceWith: any, config?: Config): any;
export declare function _findAndReplaceIf(target: any, checkFn: (foundVal: any, propKey: string | undefined) => any, propKey: string | undefined, config?: Config): any;
export declare function _findAndReplaceIf(target: any, mainObject: any, checkFn: (foundVal: any, propKey: string | undefined, propPath: string | undefined, mainObject: any) => any, propKey: string | undefined, propPath: string | undefined, config?: Config): any;
/**
* Goes through an object recursively and replaces all props with what's is returned in the `checkFn`. Also works on non-objects. `checkFn` is triggered on every single level of any value/object.
*
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "find-and-replace-anything",
"sideEffects": false,
"type": "module",
"version": "3.0.2",
"version": "3.1.0",
"description": "Replace one val with another or all occurrences in an object recursively. A simple & small integration.",
"module": "./dist/index.es.js",
"main": "./dist/index.cjs",
Expand Down Expand Up @@ -45,6 +45,7 @@
"replace-if"
],
"author": "Luca Ban - Mesqueeb",
"contributors": ["Ricardo Sánchez <me@soknife.dev> (http://soknife.dev/)"],
"funding": "https://github.com/sponsors/mesqueeb",
"license": "MIT",
"bugs": {
Expand Down
16 changes: 11 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,23 @@ export function findAndReplace (

export function _findAndReplaceIf (
target: any,
checkFn: (foundVal: any, propKey: string | undefined) => any,
mainObject: any,
checkFn: (foundVal: any, propKey: string | undefined, propPath: string | undefined, mainObject: any ) => any,
propKey: string | undefined,
propPath: string | undefined,
config: Config = { onlyPlainObjects: true, checkArrayValues: false }
): any {
const _target = checkFn(target, propKey)
const _target = checkFn(target, propKey, propPath, mainObject)
if (config.checkArrayValues && isArray(_target) && !isAnyObject(_target)) {
return (_target as any[]).map(value => _findAndReplaceIf(value, checkFn, undefined, config))
return (_target as any[]).map((value, index) => {
const ppath = propPath !== undefined ? [propPath, index].join('.') : String(index);
return _findAndReplaceIf(value, mainObject, checkFn, undefined, ppath, config)
})
}
if (!isPlainObject(_target)) return _target
return Object.entries(_target).reduce<any>((carry, [key, val]) => {
carry[key] = _findAndReplaceIf(val, checkFn, key, config)
const ppath = propPath !== undefined ? [propPath, key].join('.') : key;
carry[key] = _findAndReplaceIf(val, mainObject, checkFn, key, ppath, config)
return carry
}, {})
}
Expand All @@ -74,5 +80,5 @@ export function findAndReplaceIf (
checkFn: (foundVal: any, propKey: string | undefined) => any,
config: Config = { onlyPlainObjects: true, checkArrayValues: false }
): any {
return _findAndReplaceIf(target, checkFn, undefined, config)
return _findAndReplaceIf(target, target, checkFn, undefined, undefined, config)
}