Code Monkey home page Code Monkey logo

dot-wild's People

Contributors

wadackel 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

Watchers

 avatar  avatar  avatar

dot-wild's Issues

Possibility to get undefined values in objects using wildcard path

Hey,

Here is an example:

import * as dot from 'dot-wild-tiny'

const data = {
  pets: [
    {},
    { name: 'Rex' },
    'Sam',
    3,
    null 
  ]
}

dot.forEach(data, 'pets.*.name', (value, _, __, path) => {
  console.log(`${path} - ${value}`)
})

This will return pets.1.name - Rex. However, there is another object, string, number and null in this path which doesn't have name property and I can't get it as undefined. I would like the result to be:

pets.0.name - undefined
pets.1.name - Rex
pets.2.name - undefined
pets.3.name - undefined
pets.4.name - undefined

Same goes for map function, it would be great to have the same result.

Is it somehow possible to achieve? I'm using dot-wild-tiny, but opened issue here as it's more feature rich.

Null vs undefined

Hey

When we use get method and don't have a value by specified path, the returned value is null. I'd like to have this value as undefined, but it seems like it's not possible to do it. I tried default value as undefined, but since it's undefined it can't be set.

import { get } from 'dot-wild'

let obj = {
  a: {
    c: 1
  }
}

console.log(get(obj, 'a.c', undefined)) // 1
console.log(get(obj, 'a.c.f', undefined)) // null <- here

Am I missing something?

Prototype Pollution

dot-wild has prototype pollution vulnerablity
and it may lead to logic vulnerability or DOS in application

const dot = require('dot-wild');

dot.set({}, '__proto__.polluted', true);
console.log(polluted); // true

Add possibility to iterate only arrays when wildcard is used

const data = {
  name: 'James'
}

const anotherData = [1, 2, 3]

console.log(dot.get(data, '*'))

If wildcard is used here, it will iterate over the data object fields and return their values. I need it to iterate only over arrays and return the values of the array. So, in the above example I want it to return null. Same search on anotherData would return array values ([1, 2, 3]).

Is this possible to add to the library?

Thanks

Filter function

Ok so the get function is there, but it gets the end obj/arr. A filter function will be great that does this:

const members = [
  { username: 'gumbouser', profile: { age: 33 } },
  { username: 'tsuyoshiwada', profile: { age: 24 } },
  { username: 'sampleuser', profile: { age: 30 } },
  { username: 'foobarbaz', profile: { age: 33 } }
];
 
dot.filter(members, '*.age', 33);
// => [  { username: 'gumbouser', profile: { age: 33 } },
//         { username: 'foobarbaz', profile: { age: 33 } }  ]

returns the objects that have hits from the root.

Implementation is simple here (sketch):

Filter function. takes in obj (object to filter) and path (the dot-wild path to use)

  // filter function
 return  obj.filter(o => dot.has(o, path)); 

the path given can/should take into account that this is an inner part of the obj

Array brackets within keys sometimes mess up processing

Hi! Thanks for the great library. I'm noticing an issue when array brackets are within keys. For example:

const data = {
  "a[]b": "c"
};

dot.forEach(data, 'a[]b', () => {
  console.log('match found');
})

I would expect "match found" to be logged, but it's not. It seems that dot-wild isn't treating the brackets in some special way when it should be treating it as regular characters within the key.

Default for undefined value

Related to #10

import { get } from 'dot-wild'

let obj = {
  a: {
    c: 1,
    d: undefined,
    e: null
  }
}

console.log(get(obj, 'a.c', 'default')) // 1
console.log(get(obj, 'a.c.f', 'default')) // default

console.log(get(obj, 'a.d', 'default')) // undefined <-- here
console.log(get(obj, 'a.d.f', 'default')) // default

console.log(get(obj, 'a.e', 'default')) // null
console.log(get(obj, 'a.e.f', 'default')) // default

When I have undefined value and specify a default value for it, the returned value is undefined instead of the default one. Since it's undefined I expect it to return specified default value.

Get value returned from function [deeply]

This package is awesome. I was actually writting something exactly like this. With a few exceptional rules tho. The difference was on the wildcard *. mine doesn't require that. but seeing this is already fully featured. Let's expand on that.

Key panther does something nice, take a look at this:

var getKeypath = require('keypather/get');
var obj = {
  foo: function () {
    return function () {
      return function () {
        return 'val';
      };
    };
  }
};
getKeypath(obj, "foo()()()"); // val

This should be easy to implement. Just call the function at the path end and return its value. Do that deeply as much as it takes.

Release a separate get version

Hey

I've got a small request. I wanted to find a small library that can get nested properties with array support using *. I checked other libraries like dlv or dot-prop, which are small (less than 1 kb), but they are lacking some features that I need.

I like how dot-wild works, but it's a bit heavy for me (almost 10kb) because it's using clone package for setters, which is more than 7kb itself.

So I thought, maybe you can release a separate version with only getters? It will be small (under 2kb I guess) and is a really good alternative for above ones.

Any possibility for .map() or .forEach()?

I am parsing some search results for issues and I get back a structure like this:

[
    {
        user: {
            id: 12345,
            name: "Tyler Akins"
        },
        ... other properties that aren't useful for this example ...
    },
    {
        user: {
            id: 12345,
            name: "Tyler Akins"
        },
        ... other properties that aren't useful for this example ...
    }
]

The object content is the same content but are different objects. My code combines them so they point to the same object instance in memory. What I would love is a way to have code that does something like this:

dotWild.map(data, "*.user", (item) => remapUserToOneObject(item.user));

Another possible solution, though less useful for my particular case

dotWild.forEach(data, "*", (item) => {
    item.user = remapUserToOneObject(item.user);
}));

Currently I have a list of several wildcards and I'm splitting on ".", taking off the last chunk and using the rest.

[
    "*.user",
    "*.project",
    ... many more ...
].forEach((wildcardPattern) => {
    var propertyName, list;

    wildcardPattern = wildcardPattern.split(".");
    propertyName = wildcardPattern.pop();
    list = dotWild.get(data, wildcardPattern);

    // Due to how things work, the wildcard pattern may not have a *
    // so this might not be an array.
    list = [].concat(list);
    list.forEach((item) => {
        item[propertyName] = remapToOneObject(propertyName, item[propertyName]);
    });
});

As you can see, the above example would be a couple lines shorter with .forEach() and greatly simplified with support for .map.

// .map() example
[
    "*.user",
    "*.project",
    ... many more ...
].forEach((wildcardPattern) => {
    var propertyName;

    propertyName = wildcardPattern.match(/\.([^.]+)$/)[0];
    dotWild.map(data, wildcardPattern, (item) => remapToOneObject(propertyName, item));
})

get all possible paths?

Example input:

{
  a: {
    b: {
      c: 123,
      d: 'yo'
    }
  },
  z: 'text'
}

Expected output:

a.b.c
a.b.d
a.b
a
z

Is there any way to use dot-wild to say "show me all possible paths and their values"? Flatten seems the closest, but doesn't show objects or arrays. forEach there doesn't seem to be a way to make it recurse all the way down. Maybe the internal each method?

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.