GithubHelp home page GithubHelp logo

deoxxa / dotty Goto Github PK

View Code? Open in Web Editor NEW
127.0 6.0 20.0 385 KB

Access properties of nested objects using dot-path notation

License: BSD 3-Clause "New" or "Revised" License

JavaScript 100.00%

dotty's Introduction

Dotty Build and Test npm

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"])); // undefined

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.search(object, "a.b.*", function (value, parent, key) {
    parent[key] = value + "!";
  })
);

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

console.log(dotty.removeSearch(object, "a.*.x"));

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

console.log(object);

License

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

Contact

dotty's People

Contributors

deoxxa avatar dependabot[bot] avatar jinhei avatar miketheprogrammer avatar reggi avatar stramel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

dotty's Issues

I have a dot (.) in my key!

So I did something pretty silly and I put a dot in my key which means when I check if row["product.title"] exists via dotty.exists(row,"product.title") it returns false because it's looking for row.product.title. Besides changing them all to underscore which I can do, is there a way to compensate this using dotty?

Add a remove method of some kind

Maybe it can return true or false based on its ability to actually delete the given property, eg.

var obj = {a: {b: {}}};
dotty.remove(obj, 'a.b') // true
obj // {a: {}}
dotty.remove(obj, 'a.b') // false
obj // {a: {}}

Issue with nested arrays

The issue in dotty.put method, which creates not exactly the same object based on strings from 'deepKeys'.

`
var dotty = require("dotty");

var testSourceObject = {group1: {childNodes: [{link: {openInNewTab: true}}]}};

var renderPropsKeys = dotty.deepKeys(
testSourceObject, {leavesOnly: true, asStrings: true}
);

var targetObject = {};
dotty.put(targetObject, renderPropsKeys[0], "test");
console.log('Incorrectly constructed object: ');
console.log(targetObject);

var lodashSet = require("lodash.set");
targetObject = {};
lodashSet(targetObject, renderPropsKeys[0], "test lodash");
console.log('Correctly constructed object: ');
console.log(targetObject);
`
Live example:
https://runkit.com/vik-buchinski/dotty-array-bug

Screenshot:
image

Current behavior:
'childNodes' is an object
Expected result:
'childNodes' should be an array.

deep_keys?

We often find ourselves using dotty in conjunction with a home-rolled deep_keys function. We've come to think that it should be part of dotty. @deoxxa would you be open to a pull request for dotty.deep_keys(...)?

Documentation page is down

The link to the documentation page seems to be down (404). Any chance it could be brought back up / updated?

Add another param to `get`

What do you think of adding a default value param into get?

(dotty.get(obj, 'a.b.c') || []).length

// instead, we could do this

dotty.get(obj, 'a.b.c', []).length

// hence, interface would look like

var get = module.exports.get = function get(object, path, defaultVal) {
...

Returns True to ""

Is there any way to not return true for object values that are empty strings?

Trying to get in touch regarding a security issue

Hey there!

I'd like to report a security issue but cannot find contact instructions on your repository.

If not a hassle, might you kindly add a SECURITY.md file with an email, or another contact method? GitHub recommends this best practice to ensure security issues are responsibly disclosed, and it would serve as a simple instruction for security researchers in the future.

Thank you for your consideration, and I look forward to hearing from you!

(cc @huntr-helper)

Dotty won't work on a static instance of a function

function Foo() {
}
Foo.settings = {
  a: 'abc'
};

console.log(Foo.settings);  // This returns {a: 'abc'}
console.log(Foo['settings']); // This returns {a: 'abc'}
console.log(dotty.get(Foo, 'settings')); // This returns undefined. Why?

I looked into dotty code and it's using elem[key], which is what I'm doing there with console.log(Foo['settings']) and it seems to work fine.
Not with dotty though.

'undefined is not a function' @ lib/index.js:225

I keep getting the following error when trying to run these two lines from the example code:

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

I don't think this repository is actively being maintained anymore?

Add support for arrays

It'd be great if dotty could work with nested arrays as well, for example, with something like this:

{
  some: 'property',
  other: [{ properties: true }, { properties: true }]
}

Contact

Hi team,

I've contacted you via email, please let me know if you've received my email.

Thanks

deepKeys displays unexpected keys

var obj = {
  some_leaf: 'val1',
  some_nested: {
    some_leaf_2: 'val2',
    some_leaf_3: 'val3'
  }
}
dotty.deepKeys(obj);
/*
Actual:
[
  ['some_leaf'],
  ['some_nested'],
  ['some_nested', 'some_leaf_2'],
  ['some_nested', 'some_leaf_3']
]
Expected:
[
  ['some_leaf'],
  ['some_nested', 'some_leaf_2'],
  ['some_nested', 'some_leaf_3']
]
*/

Basically, I would expect it to show only leaf keys but it does not.

On an unrelated note, could there be a way to make it automatically do the joining? Every time we use this, we want something like ['some_leaf', 'some_nested.some_leaf2', 'some_nested.some_leaf3'] - so we CAN do the join everywhere, it's just kind of inconvenient.

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.