@cookbook/dot-notation
Object readings and complex transformations made easy by using dot.notation.syntax[]
Demo
Play around with dot-notation and experience the magic!
Installation
npm install @cookbook/dot-notation --save
yarn add @cookbook/dot-notation
How to use
Picking a value
import { pick } from '@cookbook/dot-notation';
const source = {
person: {
name: {
firstName: 'John',
lastName: 'Doe'
},
address: [
{
street: 'Infinite Loop',
city: 'Cupertino',
state: 'CA',
postalCode: 95014,
country: 'United States'
},
]
}
};
pick(source, 'person.name');
pick(source, 'person.address[0].street');
Parsing an object
Conventional parsing
import { parse } from '@cookbook/dot-notation';
const source = {
'person.name.firstName': 'John',
'person.name.lastName': 'Doe',
'person.address[].street': 'Infinite Loop',
'person.address[].city': 'Cupertino',
'person.address[].postalCode': 95014,
};
parse(source);
With nested array
where [n]
represents the array index position to insert the element
import { parse } from '@cookbook/dot-notation';
const source = {
'[0].street': 'Infinite Loop',
'[0].city': 'Cupertino',
'[0].postalCode': 95014,
'[1].street': '1600 Amphitheatre',
'[1].city': 'Mountain View',
'[1].postalCode': 94043,
'[2][0]': 'hobbies',
'[2][1][0]': ['coding'],
'[2][1][1]': ['gaming'],
};
parse(source);
Parsing single key
import { parseKey } from '@cookbook/dot-notation';
const path = 'person.name';
const value = 'John Doe';
parseKey(path, value);