Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dotty-bindable

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dotty-bindable

(Maintained fork of Dotty) Access properties of nested objects using dot-path notation

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

(MAINTAINED Version) (Works on 0.6 - 0.11) Previous version not tested but should work

  • note: After Dotty fell into the hole of forgotten projects, I have uploaded this module. I will work to get the current pull requests merged into my branch as well as improve any existing functionality as best I can without breaking backwards compatibility with Dotty.

Dotty (Maintained as "dotty-bindable" on NPM) build status

Access properties of nested objects using dot-path notation.

Overview

Dotty makes it easy to programmatically access arbitrarily nested objects and their properties.

Installation

Here's a link to the npm page.

npm install dotty

Usage

Also see the documentation and example.

var dotty = require("dotty");

var object = {
  a: {
    b: {
      x: "y",
    },
    c: {
      x: "z",
    },
  },
};

console.log(dotty.exists(object, "a.b.x")); // true
console.log(dotty.exists(object, ["a", "b", "x"])); // true
console.log(dotty.exists(object, "a.b.z")); // false
console.log(dotty.exists(object, ["a", "b", "z"])); // false

console.log(dotty.get(object, "a.b.x")); // "y"
console.log(dotty.get(object, ["a", "b", "x"])); // "y"
console.log(dotty.get(object, "a.b.z")); // undefined
console.log(dotty.get(object, ["a", "b", "z"])); // undefine

dotty.put(object, "a.b.hello", "hi");
dotty.put(object, ["a", "c", "yo"], "sup");

console.log(dotty.search(object, "a.b.*"));
console.log(dotty.search(object, ["a", "b", "*"]));
console.log(dotty.search(object, "a.*.x"));
console.log(dotty.search(object, ["a", "*", "x"]));
console.log(dotty.search(object, ["a", "*", /..+/]));

console.log(dotty.remove(object, "a.b.x"));
console.log(dotty.remove(object, "a.b.y"));

console.log(dotty.deepKeys(object));
console.log(dotty.deepKeys(object, {leavesOnly: true}));
console.log(dotty.deepKeys(object, {leavesOnly: true, asStrings: true}));

console.log(object);

Binding and Currying

Note please read below for the one Caveat of this new feature, which affects put.

var obj = 
{
  user: {
    name: {
      primary: "John",
      surname: "Doe"
    },
    age: 26,
    location: {
      city: "New York",
      region: "New York",
      region_abbrev: "NY",
    }
  }
}
// Dotty GET - supports dot and array notation and single string mixed in, as long as arguments length > 2
var user = dotty.get.bind(null, obj, ['user']);

var name
// name can either inherit from user, or from obj lets see both in action

name = user.bind(null, ['name']);

console.log('Curried from user object')
console.log(name('primary'));

console.log('Curried from top level object');
name = dotty.get.bind(null, obj, ['user'], 'name');

console.log(name('primary'));
console.log(name('surname'));
console.log('Accessed via return name as an object and just accessing its properties.');
console.log(name().primary);
console.log(name().surname);

console.log('Let try make a function to dynamically return the city even if it is changed from somewhere else in the program');

console.log('Curry from top level object');
var city = dotty.get.bind(null, obj)
                .bind(null, 'user.location')
                .bind(null, 'city');

// Nottice we used 3 binds, this does not really matter, it could have been one, it is just to prove the point of currying.

console.log('My city is ' + city() );
console.log('someones changing my city');
obj.user.location.city = 'Albany';
console.log('My city is now ' + city() );

// The Same applies to PUT EXISTS REMOVE SEARCH
// HOWEVER one caveat does exist for PUT, the last argument will never be deconstructed, it is strictly the value.

console.log('Now showing PUT caveat');
// So in the syntax
obj1 = {a: { b: {c: {d: { e: 1}}}}}
var ans = dotty.get(obj1, 'a', 'b', 'c', ['d', 'e']);
// the last item is an array and is deconstructed to be
console.log(ans);
obj1['a']['b']['c']['d']['e'];

dotty.put(obj1, 'a', 'b', 'c', ['d', 'e']);
// The last item is strictly a value and is not deconstructed
console.log(dotty.get(obj1, 'a', 'b', 'c'));
// output: ['d', 'e']
// Thus this is essentially equal to
obj1['a']['b']['c'] = ['d', 'e'];

License

3-clause BSD. A copy is included with the source.

Contact

Keywords

FAQs

Package last updated on 05 Sep 2014

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc