rhalff / dot-object

Transform json objects using dot notation
MIT License
471 stars 46 forks source link

Querying keys with spaces and special separators in them #77

Open cburatto opened 1 year ago

cburatto commented 1 year ago

I am reopening this issue after making comments to a closed issue

https://github.com/rhalff/dot-object/issues/37#issuecomment-1185068085

@rhalff 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

let value = dot.pick('some["foo.bar"]', object);

// two 

let value = dot.pick('some.foo.bar', object);

// one

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"]