Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot pick value from object if key contains dot #37

Closed
thierry2015 opened this issue Jul 24, 2019 · 4 comments
Closed

Cannot pick value from object if key contains dot #37

thierry2015 opened this issue Jul 24, 2019 · 4 comments

Comments

@thierry2015
Copy link

As a developer, I want to be able to pick a value from an object if the key contains dots.

// Node JS: 12.5.0
const dot = require('dot-object');

test('Can pick value with dotted key', () => {
    const object = { 'foo.bar': 'baz' };
    expect(dot.pick('foo.bar', object)).toBe('baz');
});

Currently, I am receiving undefined instead of baz.

@rhalff
Copy link
Owner

rhalff commented Jul 24, 2019

A key with dots is ambiguous, e.g how should this work:

const object = {
  foo: {
    bar: 'baz',
  },
 'foo.bar': 'baz,
}

Perhaps adding an escape makes sense? (not currently supported)

dot.pick('foo\.bar', object)

@thierry2015
Copy link
Author

thierry2015 commented Jul 24, 2019

I do not think that escaping would make sense since some strings and objects may come from an external source that is not aware of the plugin.

How about this:

const object = {
    foo: {
        bar: 'one'
    }
    'foo.bar': 'two',
    'some.thing': 'three'
};

dot.pick('foo.bar', object); // "one"
dot.pick('some.thing', object); // "three"
object['foo.bar']; // "two"

We could prefer sub-properties to dotted properties since they are way more frequent. So, if there is an ambiguous situation, this would need additional code by the developer. And I think that it would be simpler to implement.

As soon as the property cannot be resolved, we go with the next iteration and concatenate the previous key with the current one.

@rhalff
Copy link
Owner

rhalff commented Jul 25, 2019

Perhaps it's better to solve it in your own code:

const value = object['foo.bar'] || dot.pick('foo.bar', object);

For the other methods like set, move transfer etc. the behavior would become rather unpredictable.

@rhalff rhalff closed this as completed Nov 2, 2019
@cburatto
Copy link

Sorry to reopen this here:

@rhalff you mention "it's better to solve it in your own code" but we don't always know the object schema.
Is it possible to implement the following?

const object = {
    foo: {
        bar: 'one'
    },
    'foo.bar': 'two',
    'some': {
        'other.node': {
            is: 'three'
        }
    }
}

let value = dot.pick('some["other.node"].is', object);

// three

This is similar to what I would do in code, and it should cover scenarios where the property has spaces and characters that might be used as alternative separators https://github.com/rhalff/dot-object#using-a-different-separator

This could work interchangeably, even for non-dotted properties:

dot.pick('foo.bar', object) === dot.pick('["foo"]["bar"]', object)

dot.pick('["foo.bar"]', object) === object["foo.bar"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants