1 | # @cookbook/dot-notation
|
2 | > Typescript Library kick-off.
|
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 |
|
18 | Play 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/arg-defdot-notation-td636?fontsize=14&hidenavigation=1&theme=dark)
|
21 |
|
22 | ## Installation
|
23 |
|
24 | ```sh
|
25 | npm install @cookbook/dot-notation --save
|
26 | #or
|
27 | yarn add @cookbook/dot-notation
|
28 | ```
|
29 |
|
30 | ## How to use
|
31 |
|
32 | ### Picking a value
|
33 |
|
34 | ```js
|
35 | import dot from '@cookbook/dot-notation';
|
36 |
|
37 | const 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 |
|
55 | dot.pick(source, 'person.name');
|
56 | //outputs { firstName: 'John', lastName: 'Doe' }
|
57 |
|
58 | dot.pick(source, 'person.address[0].street');
|
59 | //outputs "Infinite Loop"
|
60 | ```
|
61 |
|
62 | ### Parsing an object
|
63 |
|
64 | #### Conventional parsing
|
65 |
|
66 | ```js
|
67 | import dot from '@cookbook/dot-notation';
|
68 |
|
69 | const 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 |
|
77 | dot.parse(source);
|
78 |
|
79 | /* outputs
|
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
|
103 | import dot from '@cookbook/dot-notation';
|
104 |
|
105 | const 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 |
|
117 | dot.parse(source);
|
118 |
|
119 | /* outputs
|
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
|
143 | import dot from '@cookbook/dot-notation';
|
144 |
|
145 | const path = 'person.name';
|
146 | const value = 'John Doe';
|
147 |
|
148 | dot.parseKey(path, value);
|
149 |
|
150 | /* outputs
|
151 | {
|
152 | person: {
|
153 | name: 'John Doe',
|
154 | },
|
155 | }
|
156 | */
|
157 | ```
|
158 |
|
159 |
|
160 | [npm-image]: https://img.shields.io/npm/v/{PACKAGE_NAMESPACE}.svg?style=flat-square
|
161 | [npm-url]: https://npmjs.org/package/{PACKAGE_NAMESPACE}
|
162 | [npm-downloads]: https://img.shields.io/npm/dm/{PACKAGE_NAMESPACE}.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/cthe-ookbook/navigator/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={PACKAGE_NAMESPACE}
|
174 | [install-size-url]: https://packagephobia.now.sh/result?p={PACKAGE_NAMESPACE}
|
175 | [gzip-size-image]: http://img.badgesize.io/https://unpkg.com/{PACKAGE_NAMESPACE}/lib/navigator.min.js?compression=gzip
|
176 | [gzip-size-url]: https://unpkg.com/{PACKAGE_NAMESPACE}/lib/navigator.min.js
|