@peter.naydenov/walk-async 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,301 +0,0 @@
1
- "use strict"
2
-
3
- import { expect } from 'chai'
4
- import walk from '../src/main.js'
5
-
6
-
7
-
8
- describe ( 'Walk-async -> objectCallback function', () => {
9
-
10
- it ( 'Object callback function only', () => {
11
- let
12
- x = {
13
- ls : [ 1,2,3 ]
14
- , name : 'Peter'
15
- , props : {
16
- eyeColor: 'blue'
17
- , age : 47
18
- , height : 176
19
- , sizes : [12,33,12,21]
20
- }
21
- };
22
-
23
- function oCallbackFn ({value,resolve}) {
24
- const { age, height } = value;
25
- if ( age === 47 ) resolve ({ eyeColor:'dark', height })
26
- else resolve ( value )
27
- }
28
-
29
- walk ( {
30
- data : x
31
- , objectCallback : oCallbackFn
32
- })
33
- .then ( r => {
34
- expect ( r.props ).to.not.have.property ( 'age' )
35
- expect ( r.props ).to.not.have.property ( 'sizes' )
36
- expect ( r.props.eyeColor ).to.be.equal ( 'dark' )
37
- })
38
- }) // it object callback
39
-
40
-
41
-
42
- it ( 'Object callback returns null', () => {
43
- let
44
- x = {
45
- ls : [ 1,2,3 ]
46
- , name : 'Peter'
47
- , props : {
48
- eyeColor: 'blue'
49
- , age : 47
50
- , height : 176
51
- , sizes : [12,33,12,21]
52
- }
53
- };
54
-
55
- function oCallbackFn ({ value:o, resolve, reject }) {
56
- const { sizes } = o;
57
- if ( sizes ) reject ()
58
- else resolve (o)
59
- }
60
-
61
- walk ({
62
- data : x
63
- , objectCallback : oCallbackFn
64
- })
65
- .then ( r => {
66
- expect ( r ).to.not.have.property ( 'props' )
67
- })
68
- }) // it object callback null
69
-
70
-
71
-
72
- it ( 'Object callback returns a string', () => {
73
- let
74
- x = {
75
- ls : [ 1,2,3 ]
76
- , name : 'Peter'
77
- , props : {
78
- eyeColor: 'blue'
79
- , age : 47
80
- , height : 176
81
- , sizes : [12,33,12,21]
82
- }
83
- };
84
-
85
- function oCallbackFn ({ value:o, resolve }) {
86
- const { sizes } = o;
87
- if ( sizes ) resolve ( 'list' )
88
- else resolve ( o )
89
- }
90
-
91
- walk ({
92
- data: x
93
- , objectCallback : oCallbackFn
94
- })
95
- .then ( r => {
96
- expect ( r ).to.have.property ( 'props' )
97
- expect ( r.props ).to.be.equal ( 'list' )
98
- })
99
- }) // it object callback null
100
-
101
-
102
-
103
- it ( 'Object callback changes the data', () => {
104
- let
105
- x = {
106
- ls : [ 1,2,3 ]
107
- , name : 'Peter'
108
- , props : {
109
- eyeColor: 'blue'
110
- , age : 47
111
- , height : 176
112
- , sizes : [12,33,12,21]
113
- }
114
- };
115
-
116
- function oCallbackFn ({ value:o, resolve }) {
117
- const { sizes } = o;
118
- if ( sizes ) o.sizes = [ 'list of sizes' ]
119
- resolve ( o )
120
- }
121
-
122
- walk ({
123
- data : x
124
- , objectCallback : oCallbackFn
125
- })
126
- .then ( r => {
127
- expect ( r ).to.have.property ( 'props' )
128
- expect ( r.props ).to.have.property ( 'sizes' )
129
- expect ( r.props.sizes ).to.have.length ( 1 )
130
- expect ( r.props.sizes[0]).to.be.equal ( 'list of sizes' )
131
- })
132
- }) // it object callback changes the data
133
-
134
-
135
-
136
- it ( 'Object callback checks key', () => {
137
- let
138
- x = {
139
- ls : [ 1,2,3 ]
140
- , name : 'Peter'
141
- , props : {
142
- eyeColor: 'blue'
143
- , age : 47
144
- , height : 176
145
- , sizes : [12,33,12,21]
146
- }
147
- };
148
-
149
- function oCallbackFn ({ value:o, key, resolve, reject }) {
150
- if ( key === 'props' ) reject ()
151
- resolve ( o )
152
- }
153
-
154
- walk ({
155
- data: x
156
- , objectCallback: oCallbackFn
157
- })
158
- .then ( r => {
159
- expect ( r ).to.not.have.property ( 'props' )
160
- })
161
- }) // it Object callback checks key
162
-
163
-
164
-
165
- it ( 'Object callback checks breadcrumbs', () => {
166
- let
167
- x = {
168
- ls : [ 1,2,3 ]
169
- , name : 'Peter'
170
- , props : {
171
- eyeColor: 'blue'
172
- , age : 47
173
- , height : 176
174
- , sizes : [12,33,12,21]
175
- }
176
- };
177
-
178
- function oCallbackFn ({ value:o, breadcrumbs, resolve, reject } ) {
179
- if ( breadcrumbs === 'root/props' ) reject ()
180
- resolve ( o )
181
- }
182
-
183
- walk ({
184
- data : x
185
- , objectCallback : oCallbackFn
186
- })
187
- .then ( r => {
188
- expect ( r ).to.not.have.property ( 'props' )
189
- })
190
- }) // it Object callback checks breadcrumbs
191
-
192
-
193
-
194
- it ( 'Prevent array empty items', () => {
195
- let
196
- x = [
197
- { id: 1 }
198
- , { id: 2 }
199
- , { id: 3 }
200
- , { id: 5 }
201
- ];
202
-
203
- function oCallbackFn ({ value:o, resolve, reject }) {
204
- if ( o.id === 5 ) resolve ( o )
205
- reject ()
206
- }
207
-
208
- walk ({
209
- data : x
210
- , objectCallback : oCallbackFn
211
- })
212
- .then ( r => {
213
- expect ( r.length ).to.be.equal ( 1 )
214
- })
215
- }) // it Prevent array empty items
216
-
217
-
218
-
219
- it ( 'Prevent array empty items 2', () => {
220
- let
221
- x = [
222
- [1]
223
- , [2]
224
- , [3]
225
- , [5]
226
- ];
227
-
228
- function oCallbackFn ({ value:o, resolve, reject }) {
229
- if ( o[0] === 5 ) resolve ( o )
230
- reject ()
231
- }
232
-
233
- walk ({
234
- data : x
235
- , objectCallback : oCallbackFn
236
- })
237
- .then ( r => {
238
- expect ( r.length ).to.be.equal ( 1 )
239
- expect ( r[0] ).to.be.equal ( 5 )
240
- })
241
- }) // it Prevent array empty items 2
242
-
243
-
244
-
245
- it ( 'Set a object to NULL', done => {
246
- let
247
- x = {
248
- ls : [ 1,2,3 ]
249
- , name : 'Peter'
250
- , props : {
251
- eyeColor: 'blue'
252
- , age : 47
253
- , height : 176
254
- , sizes : [12,33,12,21]
255
- }
256
- };
257
-
258
- function objToNull ({value,key, resolve }) {
259
- if ( key === 'props' ) resolve ( null )
260
- else resolve (value)
261
- } // objToNull func.
262
-
263
- walk ({ data:x, objectCallback:objToNull })
264
- .then ( r => {
265
- expect ( r.props ).to.be.equal ( null )
266
- done ()
267
- })
268
- }) // it Set a object to NULL
269
-
270
-
271
-
272
- it ( 'Set a object to undefined', done => {
273
- let
274
- x = {
275
- ls : [ 1,2,3 ]
276
- , name : 'Peter'
277
- , props : {
278
- eyeColor: 'blue'
279
- , age : 47
280
- , height : 176
281
- , sizes : [12,33,12,21]
282
- }
283
- };
284
-
285
- function objToNull ({value,key, resolve }) {
286
- if ( key === 'props' ) resolve ( undefined )
287
- else resolve ( value )
288
- } // objToNull func.
289
-
290
- walk ({ data:x, objectCallback:objToNull })
291
- .then ( r => {
292
- expect ( r.props ).to.be.equal ( undefined )
293
- done ()
294
- })
295
- }) // it Set a object to undefined
296
-
297
-
298
-
299
- }) // describe
300
-
301
-
@@ -1,80 +0,0 @@
1
- "use strict"
2
-
3
- import { expect } from 'chai'
4
- import walk from '../src/main.js'
5
-
6
-
7
-
8
- describe ( 'Walk-async -> Asynchronous tests', () => {
9
-
10
- it ( 'Call walk inside other walk', () => {
11
- let data = {
12
- users : [
13
- {
14
- name: 'Peter'
15
- , friends : ['Tisho', 'Radostin', 'Tony', 'Kalin' ]
16
- }
17
- , {
18
- name: 'Joro'
19
- , friends : [ 'Katya', 'Ivan', 'Stefan', 'Valentin' ]
20
- }
21
- ]
22
- }
23
- , friendsWithPermition = [ 'Kalin', 'Aleks', 'Tony', 'Ivan' ]
24
- ;
25
-
26
- async function filterObj ({value:obj, task, breadcrumbs }) {
27
- if ( obj.friends ) {
28
- function filterNames ({value:person, key, breadcrumbs, task:scanning} ) {
29
- let validated = friendsWithPermition.includes(person)
30
- if ( validated ) scanning.done ( person )
31
- else scanning.done ( null )
32
- } // filterNames func.
33
- walk ({
34
- data: obj.friends
35
- , keyCallback : filterNames
36
- })
37
- .then ( r => {
38
- obj.friends = r
39
- task.done ( obj )
40
- })
41
- return
42
- }
43
- // if ( obj. ) {
44
- // function filterNames ({value:user, key, breadcrumbs, task:scanning} ) {
45
- // let valid = friendsWithPermition.includes(user) ;
46
- // // if ( valid ) console.log ( user )
47
- // if ( valid ) scanning ( user )
48
- // else scanning ( null )
49
- // } // filterNames func.
50
- // walk ({
51
- // data : obj
52
- // , keyCallback : filterNames
53
- // }).then ( r => {
54
- // // console.log ( r )
55
- // task.done ( r )
56
- // })
57
- // return
58
- // }
59
- // console.log ( 'Object callback' )
60
- // console.log ( obj )
61
- task.done (obj)
62
- } // filterObj func.
63
-
64
- // let res = walk ( data, [null, filterObj])
65
- // console.log ( res.users[0] )
66
- // console.log ( )
67
- // walk ( data )
68
- walk ({
69
- data
70
- , objectCallback : filterObj
71
- })
72
- .then ( x => {
73
- console.log ( x.users[0] )
74
- })
75
- // console.log ( x )
76
-
77
-
78
-
79
- }) // it call walk inside other walk
80
- }) // describe
@@ -1,54 +0,0 @@
1
- "use strict"
2
-
3
- import { expect } from 'chai'
4
- import walk from '../src/main.js'
5
-
6
-
7
-
8
- describe ( 'Walk-async -> Both callbacks together', () => {
9
-
10
- it ( 'Modify on object and key callback level', () => {
11
- let
12
- x = {
13
- ls : [ 1,2,3 ]
14
- , name : 'Peter'
15
- , props : {
16
- eyeColor: 'blue'
17
- , age : 47
18
- , height : 176
19
- , sizes : [12,33,12,21]
20
- }
21
- }
22
- ;
23
-
24
- function keysModifier ({value, key, resolve, reject }) {
25
- if ( key === 'age') reject () // remove 'age'
26
- else if ( key=== 'eyeColor' ) resolve ( 'xxx' )
27
- else resolve ( value )
28
- }
29
-
30
- function objectModifier ({value, key, resolve }) {
31
- if ( key === 'props' ) {
32
- const { sizes, ... others } = value // remove 'sizes'
33
- resolve ( others )
34
- }
35
- else resolve ( value )
36
- }
37
-
38
-
39
- walk ({
40
- data : x
41
- , keyCallback : keysModifier
42
- , objectCallback : objectModifier
43
- })
44
- .then ( r => {
45
- expect ( r.props ).to.not.have.property ( 'age' )
46
- expect ( r.props ).to.not.have.property ( 'sizes' )
47
- expect ( r.props.eyeColor ).to.be.equal ( 'xxx' )
48
- })
49
- }) // it Modify on object and key callback level
50
-
51
-
52
- }) // describe
53
-
54
-