Coder Social home page Coder Social logo

tony / undescoredeepextend Goto Github PK

View Code? Open in Web Editor NEW

This project forked from xioup/underscoredeepextend

0.0 1.0 0.0 112 KB

A deepExtend implementation for underscore, lodash and friends.

Home Page: https://gist.github.com/1868955

License: Other

JavaScript 100.00%

undescoredeepextend's Introduction

_.deepExtend

A deep extend implementation for underscore, lodash and their (AFAIK non-existent) friends.

Based conceptually on the _.extend() function in underscore.js.

Copyright (C) 2012 Kurt Milam - http://xioup.com

Licensed under the GPL V3+.

Original source: https://gist.github.com/1868955

Installation:

npm install git://github.com/pygy/undescoreDeepExtend.git

or, in the package.json dependencies:

"underscoreDeepExtend": "git://github.com/pygy/undescoreDeepExtend.git"

Dependency:

One of

Usage:

Mix it in with the parent library (it must be explicitly injected):

_.mixin({deepExtend: underscoreDeepExtend(_)});

Call it like this:

var myObj = _.deepExtend(grandparent, child, grandchild, greatgrandchild)

Notes:

Keep it DRY.

This function is especially useful if you're working with JSON config documents. It allows you to create a default config document with the most common settings, then override those settings for specific cases. It accepts any number of objects as arguments, giving you fine-grained control over your config document hierarchy.

Special Features and Considerations:

  • parentRE allows you to concatenate strings. example:

    var obj = _.deepExtend({url: "www.example.com"}, {url: "http://#{_}/path/to/file.html"});
        
    console.log(obj.url);

    output: http://www.example.com/path/to/file.html

  • parentRE also acts as a placeholder, which can be useful when you need to change one value in an array, while leaving the others untouched. example:

        var arr = _.deepExtend([100,    {id: 1234}, true,  "foo",  [250, 500]],
                               ["#{_}", "#{_}",     false, "#{_}", "#{_}"]);
        console.log(arr);

    output: [100, {id: 1234}, false, "foo", [250, 500]]

  • The previous example can also be written like this:

    var arr = .deepExtend([100, {id:1234}, true, "foo", [250, 500]], ["#{}", {}, false, "#{_}", []]); console.log(arr);

    output: [100, {id: 1234}, false, "foo", [250, 500]]

  • And also like this:

    var arr = _.deepExtend([100,    {id:1234},   true,  "foo",  [250, 500]],
                           ["#{_}", {},          false]);
    console.log(arr);

    output: [100, {id: 1234}, false, "foo", [250, 500]]

  • Array order is important. example:

    var arr = _.deepExtend([1, 2, 3, 4], [1, 4, 3, 2]);
    console.log(arr);

    output: [1, 4, 3, 2]

  • You can remove an array element set in a parent object by setting the same index value to null in a child object. Example:

    var obj = _.deepExtend({arr: [1, 2, 3, 4]}, {arr: ["#{_}", null]});
    console.log(obj.arr);

    output: [1, 3, 4]

undescoredeepextend's People

Contributors

pygy avatar

Watchers

Tony Narlock avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.