Git Product home page Git Product logo

dottie.js's Introduction

Build Status

Dottie helps you easily (and without sacrificing too much performance) look up and play with nested keys in objects, without them throwing up in your face.

Install

npm install dottie

Usage

For detailed usage, check source or tests.

Get value

Gets nested value, or undefined if unreachable, or a default value if passed.

var values = {
  some: {
    nested: {
        key: 'foobar';
    }
  },
  'some.dot.included': {
    key: 'barfoo'
  }
}

dottie.get(values, 'some.nested.key'); // returns 'foobar'
dottie.get(values, 'some.undefined.key'); // returns undefined
dottie.get(values, 'some.undefined.key', 'defaultval'); // returns 'defaultval'
dottie.get(values, ['some.dot.included', 'key']); // returns 'barfoo'

Note: lodash.get() also works fine for this

Set value

Sets nested value, creates nested structure if needed

dottie.set(values, 'some.nested.value', someValue);
dottie.set(values, ['some.dot.included', 'value'], someValue);
dottie.set(values, 'some.nested.object', someValue, {
  force: true // force overwrite defined non-object keys into objects if needed
});

Transform object

Transform object from keys with dottie notation to nested objects

var values = {
  'user.name': 'Gummy Bear',
  'user.email': '[email protected]',
  'user.professional.title': 'King',
  'user.professional.employer': 'Candy Mountain'
};
var transformed = dottie.transform(values);

/*
{
  user: {
    name: 'Gummy Bear',
    email: '[email protected]',
    professional: {
      title: 'King',
      employer: 'Candy Mountain'
    }
  }
}
*/

With a custom delimiter

var values = {
  'user_name': 'Mick Hansen',
  'user_email': '[email protected]'
};
var transformed = dottie.transform(values, { delimiter: '_' });

/*
{
  user: {
    name: 'Mick Hansen',
    email: '[email protected]'
  }
}
*/

Get paths in object

var object = {
  a: 1,
  b: {
    c: 2,
    d: { e: 3 }
  }
};

dottie.paths(object); // ["a", "b.c", "b.d.e"];

Performance

0.3.1 and up ships with dottie.memoizePath: true by default, if this causes any bugs, please try setting it to false

License

MIT

dottie.js's People

Contributors

mickhansen avatar sdepold avatar redsandro avatar scottrain avatar durango avatar dawsbot avatar firoze avatar henrikhaugboelle avatar janmeier avatar linkiwi avatar noosxe avatar robboerman avatar simonschick avatar

Stargazers

Davemafy avatar

Watchers

James Cloos 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.