diff --git a/dist/index.cjs b/dist/index.cjs index 2653d1c..54f1cdd 100644 --- a/dist/index.cjs +++ b/dist/index.cjs @@ -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; }, {}); } @@ -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; diff --git a/dist/index.es.js b/dist/index.es.js index 6b5b477..fccca4f 100644 --- a/dist/index.es.js +++ b/dist/index.es.js @@ -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; }, {}); } @@ -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 }; diff --git a/dist/types/index.d.ts b/dist/types/index.d.ts index ccc33ba..731b41d 100644 --- a/dist/types/index.d.ts +++ b/dist/types/index.d.ts @@ -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. * diff --git a/package-lock.json b/package-lock.json index b8c285f..00cba7d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "find-and-replace-anything", - "version": "3.0.2", + "version": "3.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "find-and-replace-anything", - "version": "3.0.2", + "version": "3.1.0", "license": "MIT", "dependencies": { "is-what": "^4.1.6" diff --git a/package.json b/package.json index 10f9091..1a8db57 100644 --- a/package.json +++ b/package.json @@ -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", @@ -45,6 +45,7 @@ "replace-if" ], "author": "Luca Ban - Mesqueeb", + "contributors": ["Ricardo Sánchez (http://soknife.dev/)"], "funding": "https://github.com/sponsors/mesqueeb", "license": "MIT", "bugs": { diff --git a/src/index.ts b/src/index.ts index ed2bc5b..effddd4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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((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 }, {}) } @@ -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) }