Socket
Socket
Sign inDemoInstall

dottie

Package Overview
Dependencies
0
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    dottie

Fast and safe nested object access and manipulation in JavaScript


Version published
Weekly downloads
1.9M
decreased by-2.37%
Maintainers
1
Install size
10.2 kB
Created
Weekly downloads
 

Package description

What is dottie?

The dottie npm package is a utility for safely reading and writing deep properties of objects in JavaScript. It uses dot notation strings to access deeply nested properties and provides a set of functions to manipulate these properties without worrying about whether the intermediate properties exist.

What are dottie's main functionalities?

Get

Retrieve the value of a nested property using a dot notation string.

const dottie = require('dottie');
const value = dottie.get({a: {b: {c: 'd'}}}, 'a.b.c'); // 'd'

Set

Set the value of a nested property using a dot notation string. If intermediate properties do not exist, they are created.

const dottie = require('dottie');
const obj = {};
dottie.set(obj, 'a.b.c', 'd'); // obj is now {a: {b: {c: 'd'}}}

Transform

Transform an object with dot notation string keys into a nested object structure.

const dottie = require('dottie');
const obj = { 'a.b.c': 'd' };
const transformed = dottie.transform(obj); // transformed is now {a: {b: {c: 'd'}}}

Exists

Check if a nested property exists within an object using a dot notation string.

const dottie = require('dottie');
const exists = dottie.exists({a: {b: {c: 'd'}}}, 'a.b.c'); // true

Paths

Get an array of dot notation paths for all properties in an object.

const dottie = require('dottie');
const paths = dottie.paths({a: {b: {c: 'd'}}}); // ['a.b.c']

Other packages similar to dottie

Readme

Source

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

FAQs

Last updated on 13 Jun 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc