dottie
Object traversing/manipulation util
Last updated 11 years ago by mickhansen .
Repository · Bugs · Original npm · Tarball · package.json
$ npm install dottie 
SYNC missed versions from official npm registry.

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.

Not actively maintained. You are likely better off using lodash or ES6+

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': 'gummybear@candymountain.com',
  'user.professional.title': 'King',
  'user.professional.employer': 'Candy Mountain'
};
var transformed = dottie.transform(values);

/*
{
  user: {
    name: 'Gummy Bear',
    email: 'gummybear@candymountain.com',
    professional: {
      title: 'King',
      employer: 'Candy Mountain'
    }
  }
}
*/

With a custom delimiter

var values = {
  'user_name': 'Mick Hansen',
  'user_email': 'maker@mhansen.io'
};
var transformed = dottie.transform(values, { delimiter: '_' });

/*
{
  user: {
    name: 'Mick Hansen',
    email: 'maker@mhansen.io'
  }
}
*/

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

Current Tags

  • 2.0.6                                ...           latest (2 years ago)

25 Versions

  • 2.0.6                                ...           2 years ago
  • 2.0.4                                ...           2 years ago
  • 2.0.3                                ...           2 years ago
  • 2.0.2                                ...           5 years ago
  • 2.0.1                                ...           6 years ago
  • 2.0.0                                ...           8 years ago
  • 1.1.1                                ...           9 years ago
  • 1.1.0                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
  • 0.3.1                                ...           10 years ago
  • 0.3.0                                ...           10 years ago
  • 0.2.6                                ...           10 years ago
  • 0.2.5                                ...           10 years ago
  • 0.2.4                                ...           11 years ago
  • 0.2.3                                ...           11 years ago
  • 0.2.2                                ...           11 years ago
  • 0.2.1                                ...           11 years ago
  • 0.2.0                                ...           11 years ago
  • 0.1.0                                ...           11 years ago
  • 0.0.9-0                                ...           11 years ago
  • 0.0.8-0                                ...           11 years ago
  • 0.0.7-0                                ...           12 years ago
  • 0.0.6-1                                ...           12 years ago
  • 0.0.6                                ...           12 years ago
  • 0.0.5                                ...           12 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (1)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE