Skip to content

Commit

Permalink
[update] add setter for genTraversalHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
uc authored and uc committed Nov 25, 2017
1 parent fd68d5a commit 2cab67a
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import {isArray, isObject, isPrimitive, isString, isVoid} from 'toxic-predicate-
* @param {Function} fn the function you wanna run when you reach in the deep property
* @return {Function} the handler
*/
export function genTraversalHandler (fn: Function): Function {
export function genTraversalHandler (fn: Function, setter: Function = (target, key, value) => { target[key] = value; }): Function {
// use recursive to move what in source to the target
// if you do not provide a target, we will create a new target
function recursiveFn (source, target, key) {
if(isArray(source) || isObject(source)) {
target = isPrimitive(target)
? (isObject(source) ? {} : [])
: target;
for(const key in source) {
// $FlowFixMe: support computed key here
target[key] = recursiveFn(source[key], target[key], key);
setter(target, key, recursiveFn(source[key], target[key], key));
// target[key] = recursiveFn(source[key], target[key], key);
}
return target;
}
Expand Down

0 comments on commit 2cab67a

Please sign in to comment.