UNPKG

4.34 kBMarkdownView Raw
1# @cookbook/dot-notation
2> Object readings and complex transformations made easy by using `dot.notation.syntax[]`
3
4[![NPM Version][npm-image]][npm-url]
5[![CI Status][circleci-image]][circleci-url]
6[![Downloads Stats][npm-downloads]][npm-url]
7[![GitHub stars][stars-image]][stars-url]
8[![Known Vulnerabilities][vulnerabilities-image]][vulnerabilities-url]
9[![GitHub issues][issues-image]][issues-url]
10[![Awesome][awesome-image]][awesome-url]
11[![install size][install-size-image]][install-size-url]
12[![gzip size][gzip-size-image]][gzip-size-url]
13
14![](dot-notation.png)
15
16## Demo
17
18Play around with _dot-notation_ and experience **the magic**!
19
20[![Edit @cookbook/dot-notation](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/cookbookdot-notation-kjwd2?fontsize=14&hidenavigation=1&theme=dark)
21
22## Installation
23
24```sh
25npm install @cookbook/dot-notation --save
26#or
27yarn add @cookbook/dot-notation
28```
29
30## How to use
31
32### Picking a value
33
34```js
35import dot from '@cookbook/dot-notation';
36
37const source = {
38 person: {
39 name: {
40 firstName: 'John',
41 lastName: 'Doe'
42 },
43 address: [
44 {
45 street: 'Infinite Loop',
46 city: 'Cupertino',
47 state: 'CA',
48 postalCode: 95014,
49 country: 'United States'
50 },
51 ]
52 }
53};
54
55dot.pick(source, 'person.name');
56//output { firstName: 'John', lastName: 'Doe' }
57
58dot.pick(source, 'person.address[0].street');
59//output "Infinite Loop"
60```
61
62### Parsing an object
63
64#### Conventional parsing
65
66```js
67import dot from '@cookbook/dot-notation';
68
69const source = {
70 'person.name.firstName': 'John',
71 'person.name.lastName': 'Doe',
72 'person.address[].street': 'Infinite Loop',
73 'person.address[].city': 'Cupertino',
74 'person.address[].postalCode': 95014,
75};
76
77dot.parse(source);
78
79/* output
80{
81 person: {
82 name: {
83 firstName: 'John',
84 lastName: 'Doe',
85 },
86 address: [
87 {
88 street: 'Infinite Loop',
89 city: 'Cupertino',
90 postalCode: 95014,
91 },
92 ],
93 },
94}
95*/
96```
97
98#### With nested array
99
100> where `[n]` represents the array index position to insert the element
101
102```js
103import dot from '@cookbook/dot-notation';
104
105const source = {
106 '[0].street': 'Infinite Loop',
107 '[0].city': 'Cupertino',
108 '[0].postalCode': 95014,
109 '[1].street': '1600 Amphitheatre',
110 '[1].city': 'Mountain View',
111 '[1].postalCode': 94043,
112 '[2][0]': 'hobbies',
113 '[2][1][0]': ['coding'],
114 '[2][1][1]': ['gaming'],
115};
116
117dot.parse(source);
118
119/* output
120[
121 {
122 "postalCode": 95014,
123 "city": "Cupertino",
124 "street": "Infinite Loop"
125 },
126 {
127 "postalCode": 94043,
128 "city": "Mountain View",
129 "street": "1600 Amphitheatre"
130 },
131 [
132 "hobbies",
133 [["coding"],["gaming"]]
134 ]
135]
136*/
137```
138
139### Parsing single key
140
141
142```js
143import dot from '@cookbook/dot-notation';
144
145const path = 'person.name';
146const value = 'John Doe';
147
148dot.parseKey(path, value);
149
150/* output
151{
152 person: {
153 name: 'John Doe',
154 },
155}
156*/
157```
158
159<!-- Markdown link & img dfn's -->
160[npm-image]: https://img.shields.io/npm/v/@cookbook/dot-notation.svg?style=flat-square
161[npm-url]: https://npmjs.org/package/@cookbook/dot-notation
162[npm-downloads]: https://img.shields.io/npm/dm/@cookbook/dot-notation.svg?style=flat-square
163[circleci-image]: https://circleci.com/gh/the-cookbook/dot-notation.svg?style=svg
164[circleci-url]: https://circleci.com/gh/the-cookbook/dot-notation
165[stars-image]: https://img.shields.io/github/stars/the-cookbook/dot-notation.svg
166[stars-url]: https://github.com/the-cookbook/dot-notation/stargazers
167[vulnerabilities-image]: https://snyk.io/test/github/the-cookbook/dot-notation/badge.svg
168[vulnerabilities-url]: https://snyk.io/test/github/the-cookbook/dot-notation
169[issues-image]: https://img.shields.io/github/issues/the-cookbook/dot-notation.svg
170[issues-url]: https://github.com/the-cookbook/dot-notation/issues
171[awesome-image]: https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg
172[awesome-url]: https://github.com/the-cookbook/dot-notation
173[install-size-image]: https://packagephobia.now.sh/badge?p=@cookbook/dot-notation
174[install-size-url]: https://packagephobia.now.sh/result?p=@cookbook/dot-notation
175[gzip-size-image]: http://img.badgesize.io/https://unpkg.com/@cookbook/dot-notation/lib/dot.min.js?compression=gzip
176[gzip-size-url]: https://unpkg.com/@cookbook/dot-notation/lib/dot.min.js