Skip to content

Instantly share code, notes, and snippets.

@kirilloid
Created October 6, 2016 16:24
Show Gist options
  • Save kirilloid/04f37b3b83276dc53b794f87bfb5b148 to your computer and use it in GitHub Desktop.
Save kirilloid/04f37b3b83276dc53b794f87bfb5b148 to your computer and use it in GitHub Desktop.

Revisions

  1. kirilloid created this gist Oct 6, 2016.
    21 changes: 21 additions & 0 deletions assign.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    function assingImmutable(obj, toAdd) {
    if (!_.isObjectLike(toAdd)) {
    return toAdd;
    }
    key = _.first(path);
    if (_.isArray(obj)) {
    copy = _.slice(obj);
    toAdd.forEach(function (value, idx) {
    copy[idx] = assingImmutable(obj[idx], value);
    });
    return copy;
    } else if (_.isObject(obj)) {
    copy = _.clone(obj);
    Object.keys(toAdd).forEach(function (key) {
    copy[key] = assingImmutable(obj[key], toAdd[key]);
    });
    return copy;
    } else {
    return toAdd;
    }
    }