-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
174 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
var isWhat = require('is-what'); | ||
|
||
/** | ||
* Goes through an object recursively and replaces all occurences of the `find` value with `replaceWith`. Also works no non-objects. | ||
* | ||
* @export | ||
* @param {*} target Target can be anything | ||
* @param {*} find val to find | ||
* @param {*} replaceWith val to replace | ||
* @param {Config} [config={onlyPlainObjects: false, checkArrayValues: false}] | ||
* @returns {*} the target with replaced values | ||
*/ | ||
function findAndReplace(target, find, replaceWith, config = { onlyPlainObjects: false, checkArrayValues: false }) { | ||
const _target = target; | ||
// arrays | ||
if (config.checkArrayValues && isWhat.isArray(_target) && !isWhat.isAnyObject(_target)) { | ||
return _target.map(value => findAndReplace(value, find, replaceWith, config)); | ||
} | ||
// non-objects | ||
if ((!config.onlyPlainObjects && !isWhat.isAnyObject(_target)) || | ||
(config.onlyPlainObjects === true && !isWhat.isPlainObject(_target))) { | ||
if (_target === find || (isWhat.isNaNValue(_target) && isWhat.isNaNValue(find))) { | ||
return replaceWith; | ||
} | ||
return _target; | ||
} | ||
// objects | ||
return Object.entries(target).reduce((carry, [key, val]) => { | ||
carry[key] = findAndReplace(val, find, replaceWith, config); | ||
return carry; | ||
}, {}); | ||
} | ||
function _findAndReplaceIf(target, checkFn, propKey, config = { onlyPlainObjects: true, checkArrayValues: false }) { | ||
const _target = checkFn(target, propKey); | ||
if (config.checkArrayValues && isWhat.isArray(_target) && !isWhat.isAnyObject(_target)) { | ||
return _target.map(value => _findAndReplaceIf(value, checkFn, undefined, config)); | ||
} | ||
if (!isWhat.isPlainObject(_target)) | ||
return _target; | ||
return Object.entries(_target).reduce((carry, [key, val]) => { | ||
carry[key] = _findAndReplaceIf(val, checkFn, key, config); | ||
return carry; | ||
}, {}); | ||
} | ||
/** | ||
* 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. | ||
* | ||
* @export | ||
* @param {*} target Target can be anything | ||
* @param {(foundVal: any, propKey: string | undefined) => any} checkFn a function that will receive the `foundVal` | ||
* @param {Config} [config={onlyPlainObjects: true, checkArrayValues: false}] | ||
* @returns {*} the target with replaced values | ||
*/ | ||
function findAndReplaceIf(target, checkFn, config = { onlyPlainObjects: true, checkArrayValues: false }) { | ||
return _findAndReplaceIf(target, checkFn, undefined, config); | ||
} | ||
|
||
exports._findAndReplaceIf = _findAndReplaceIf; | ||
exports.findAndReplace = findAndReplace; | ||
exports.findAndReplaceIf = findAndReplaceIf; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { isArray, isAnyObject, isPlainObject, isNaNValue } from 'is-what'; | ||
|
||
/** | ||
* Goes through an object recursively and replaces all occurences of the `find` value with `replaceWith`. Also works no non-objects. | ||
* | ||
* @export | ||
* @param {*} target Target can be anything | ||
* @param {*} find val to find | ||
* @param {*} replaceWith val to replace | ||
* @param {Config} [config={onlyPlainObjects: false, checkArrayValues: false}] | ||
* @returns {*} the target with replaced values | ||
*/ | ||
function findAndReplace(target, find, replaceWith, config = { onlyPlainObjects: false, checkArrayValues: false }) { | ||
const _target = target; | ||
// arrays | ||
if (config.checkArrayValues && isArray(_target) && !isAnyObject(_target)) { | ||
return _target.map(value => findAndReplace(value, find, replaceWith, config)); | ||
} | ||
// non-objects | ||
if ((!config.onlyPlainObjects && !isAnyObject(_target)) || | ||
(config.onlyPlainObjects === true && !isPlainObject(_target))) { | ||
if (_target === find || (isNaNValue(_target) && isNaNValue(find))) { | ||
return replaceWith; | ||
} | ||
return _target; | ||
} | ||
// objects | ||
return Object.entries(target).reduce((carry, [key, val]) => { | ||
carry[key] = findAndReplace(val, find, replaceWith, config); | ||
return carry; | ||
}, {}); | ||
} | ||
function _findAndReplaceIf(target, checkFn, propKey, config = { onlyPlainObjects: true, checkArrayValues: false }) { | ||
const _target = checkFn(target, propKey); | ||
if (config.checkArrayValues && isArray(_target) && !isAnyObject(_target)) { | ||
return _target.map(value => _findAndReplaceIf(value, checkFn, undefined, config)); | ||
} | ||
if (!isPlainObject(_target)) | ||
return _target; | ||
return Object.entries(_target).reduce((carry, [key, val]) => { | ||
carry[key] = _findAndReplaceIf(val, checkFn, key, config); | ||
return carry; | ||
}, {}); | ||
} | ||
/** | ||
* 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. | ||
* | ||
* @export | ||
* @param {*} target Target can be anything | ||
* @param {(foundVal: any, propKey: string | undefined) => any} checkFn a function that will receive the `foundVal` | ||
* @param {Config} [config={onlyPlainObjects: true, checkArrayValues: false}] | ||
* @returns {*} the target with replaced values | ||
*/ | ||
function findAndReplaceIf(target, checkFn, config = { onlyPlainObjects: true, checkArrayValues: false }) { | ||
return _findAndReplaceIf(target, checkFn, undefined, config); | ||
} | ||
|
||
export { _findAndReplaceIf, findAndReplace, findAndReplaceIf }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
declare type Config = { | ||
onlyPlainObjects?: boolean; | ||
checkArrayValues?: boolean; | ||
}; | ||
/** | ||
* Goes through an object recursively and replaces all occurences of the `find` value with `replaceWith`. Also works no non-objects. | ||
* | ||
* @export | ||
* @param {*} target Target can be anything | ||
* @param {*} find val to find | ||
* @param {*} replaceWith val to replace | ||
* @param {Config} [config={onlyPlainObjects: false, checkArrayValues: false}] | ||
* @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; | ||
/** | ||
* 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. | ||
* | ||
* @export | ||
* @param {*} target Target can be anything | ||
* @param {(foundVal: any, propKey: string | undefined) => any} checkFn a function that will receive the `foundVal` | ||
* @param {Config} [config={onlyPlainObjects: true, checkArrayValues: false}] | ||
* @returns {*} the target with replaced values | ||
*/ | ||
export declare function findAndReplaceIf(target: any, checkFn: (foundVal: any, propKey: string | undefined) => any, config?: Config): any; | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters