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