@peter.naydenov/walk-async 1.2.0 → 1.3.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.
package/Changelog.md CHANGED
@@ -1,9 +1,20 @@
1
1
  # Release History
2
2
 
3
3
 
4
+ ### 1.3.1 ( 2023-10-25)
5
+ - [x] Dependencies update. Ask-for-promise version 1.4.0;
6
+
7
+
8
+
9
+ ### 1.3.0 ( 2023-09-23)
10
+ - [x] Provide collection containers to callbacks. Extract data during iteration;
11
+ - [x] Fix: Keys with value 'undefined' are not being copied;
12
+
13
+
4
14
 
5
15
  ### 1.2.0 ( 2023-09-18)
6
16
  - [x] HTML DOM nodes - copy by reference;
17
+ - [ ] Bug: Keys with value 'undefined' are not being copied;
7
18
 
8
19
 
9
20
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peter.naydenov/walk-async",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "Deep copy with fine control during the process. Can be used also as a deep forEach function",
5
5
  "main": "src/main.js",
6
6
  "scripts": {
@@ -14,10 +14,10 @@
14
14
  "author": "Peter Naydenov",
15
15
  "license": "MIT",
16
16
  "dependencies": {
17
- "ask-for-promise": "1.3.1"
17
+ "ask-for-promise": "1.4.0"
18
18
  },
19
19
  "devDependencies": {
20
- "chai": "^4.3.8",
20
+ "chai": "^4.3.10",
21
21
  "mocha": "^10.2.0",
22
22
  "nyc": "^15.1.0"
23
23
  },
package/src/main.js CHANGED
@@ -22,7 +22,7 @@ function walk ({
22
22
  data:origin
23
23
  , objectCallback = null
24
24
  , keyCallback = null
25
- }) {
25
+ }, ...args ) {
26
26
  let
27
27
  type = findType ( origin )
28
28
  , result
@@ -31,15 +31,16 @@ function walk ({
31
31
  , cb = [ keyCallback, objectCallback ]
32
32
  , end = askForPromise ()
33
33
  ;
34
+
34
35
  switch ( type ) {
35
36
  case 'array' :
36
37
  result = []
37
- copyObject ( origin, result, extend, cb, breadcrumbs )
38
+ copyObject ( origin, result, extend, cb, breadcrumbs, ...args )
38
39
  .then ( () => goNext ( extend, result, end ))
39
40
  break
40
41
  case 'object' :
41
42
  result = {}
42
- copyObject ( origin, result, extend, cb, breadcrumbs )
43
+ copyObject ( origin, result, extend, cb, breadcrumbs, ...args )
43
44
  .then ( () => {
44
45
  goNext ( extend, result, end )
45
46
  })
@@ -71,8 +72,8 @@ function findType ( x ) {
71
72
 
72
73
 
73
74
 
74
- async function* generateList ( data, location, ex, cb, breadcrumbs ) {
75
- yield await copyObject ( data , location, ex, cb, breadcrumbs )
75
+ async function* generateList ( data, location, ex, cb, breadcrumbs, args ) {
76
+ yield await copyObject ( data , location, ex, cb, breadcrumbs, ...args )
76
77
  } // generateList func.
77
78
 
78
79
 
@@ -86,7 +87,7 @@ function validateForInsertion ( k, result ) {
86
87
 
87
88
 
88
89
 
89
- function copyObject ( origin, result, extend, cb, breadcrumbs ) {
90
+ function copyObject ( origin, result, extend, cb, breadcrumbs, ...args ) {
90
91
  let
91
92
  [ keyCallback, objectCallback ] = cb
92
93
  , keys = Object.keys ( origin )
@@ -114,14 +115,14 @@ function copyObject ( origin, result, extend, cb, breadcrumbs ) {
114
115
  , value : item
115
116
  , key : k
116
117
  , breadcrumbs : `${breadcrumbs}/${k}`
117
- })
118
+ }, ...args )
118
119
  }
119
120
  else {
120
121
  objectCallbackTask.done ( '$$cancel' )
121
122
  }
122
123
 
123
124
  objectCallbackTask.onComplete ( res => {
124
- if ( res === '$$cancel' && item == null ) { // deep copy, no callbacks
125
+ if ( res === '$$cancel' && origin.hasOwnProperty(item) ) { // deep copy, no callbacks
125
126
  keyCallbackTask.done ( '$$noUpdates' )
126
127
  return
127
128
  }
@@ -144,7 +145,7 @@ function copyObject ( origin, result, extend, cb, breadcrumbs ) {
144
145
  , value : item
145
146
  , key : k
146
147
  , breadcrumbs : `${breadcrumbs}/${k}`
147
- });
148
+ }, ...args );
148
149
  }
149
150
  else {
150
151
  keyCallbackTask.done ( '$$cancel' )
@@ -179,14 +180,14 @@ function copyObject ( origin, result, extend, cb, breadcrumbs ) {
179
180
  const newObject = {};
180
181
  if ( resultIsArray && keyNumber ) result.push ( newObject )
181
182
  else result[k] = newObject
182
- extend.push ( generateList( item, newObject, extend, cb, `${breadcrumbs}/${k}` ) )
183
+ extend.push ( generateList( item, newObject, extend, cb, `${breadcrumbs}/${k}`, args ) )
183
184
  executeCallback[i].done ('object')
184
185
  }
185
186
  if ( type === 'array' ) {
186
187
  const newArray = [];
187
188
  if ( resultIsArray && keyNumber ) result.push ( newArray )
188
189
  else result[k] = newArray
189
- extend.push ( generateList( item, newArray, extend, cb, `${breadcrumbs}/${k}` ) )
190
+ extend.push ( generateList( item, newArray, extend, cb, `${breadcrumbs}/${k}`, args ) )
190
191
  executeCallback[i].done ('array')
191
192
  }
192
193
  })
@@ -222,6 +222,47 @@ describe ( 'Walk-async -> keyCallback function', () => {
222
222
  done ()
223
223
  })
224
224
  }) // it Copy a function
225
+
226
+
227
+
228
+ it ( 'Extract collections', done => {
229
+ let
230
+ x = {
231
+ ls : [ 1,2,3 ]
232
+ , name : 'Peter'
233
+ , props : {
234
+ eyeColor: undefined // Use callback and return this exact value
235
+ , age : function age () { return 47 }
236
+ , height : 176
237
+ , sizes : [12,33,12,21]
238
+ }
239
+ }
240
+ , fnList = []
241
+ , propsCollection = {}
242
+ ;
243
+
244
+ function valueFn ({ key, value, resolve }, fn, p ) {
245
+ const isFn = (typeof value === 'function');
246
+ if ( isFn ) fn.push ( value )
247
+
248
+ if ( ['name','eyeColor', 'age'].includes(key) ) p[key] = isFn ? value() : value
249
+ resolve ( value )
250
+ } // valueFn func.
251
+
252
+ walk ({ data:x, keyCallback:valueFn }, fnList, propsCollection )
253
+ .then ( r => {
254
+ expect ( r.props.age() ).to.be.equal ( 47 )
255
+ expect ( fnList.length ).to.be.equal ( 1 )
256
+ expect ( r.props ).to.have.property ( 'eyeColor' )
257
+
258
+ expect ( propsCollection ).to.have.property ( 'name' )
259
+ expect ( propsCollection ).to.have.property ( 'eyeColor' )
260
+ expect ( propsCollection ).to.have.property ( 'age' )
261
+ expect ( propsCollection.age ).to.be.equal ( 47 )
262
+
263
+ done ()
264
+ })
265
+ }) // it Copy a function
225
266
 
226
267
  }) // describe
227
268