@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.
@@ -0,0 +1 @@
1
+ "use strict";var e=require("ask-for-promise");async function n(e,n,o){for await(const n of e)await n.next();o.done(n)}function o(e){return null==e||e.nodeType?"simple":e instanceof Array?"array":"object"==typeof e?"object":"simple"}async function*r(e,n,o,r,a,s){yield await t(e,n,o,r,a,...s)}function t(n,t,a,s,c,...l){let[i,d]=s,u=Object.keys(n),p=e(u),$=e();return u.forEach(((u,$)=>{let f=o(n[u]),y=n[u],b="simple"!==f&&null!=d,m=e(),k=e(),h=e(),j="array"===o(t),v=!isNaN(u),C=Symbol("ignore___");b?d({resolve:m.done,reject:()=>m.done(C),value:y,key:u,breadcrumbs:`${c}/${u}`},...l):m.done("$$cancel"),m.onComplete((e=>{if("$$cancel"===e&&n.hasOwnProperty(y))k.done("$$noUpdates");else if("$$cancel"!==e&&(y=e,f=o(y)),y!=C)if("simple"===f){if(!i)return void k.done("$$noUpdates");i({resolve:k.done,reject:()=>k.done(C),value:y,key:u,breadcrumbs:`${c}/${u}`},...l)}else k.done("$$cancel");else p[$].done("ignore object")})),k.onComplete((e=>{if(e!=C)if("$$cancel"!==e){if("$$noUpdates"!==e&&(y=e,f=o(y)),"simple"===f){const e=function(e,n){return n instanceof Array&&!isNaN(e)}(u,t);return e?t.push(y):t[u]=y,void p[$].done("key")}h.done()}else h.done();else p[$].done("ignore key")})),h.onComplete((()=>{if("object"===f){const e={};j&&v?t.push(e):t[u]=e,a.push(r(y,e,a,s,`${c}/${u}`,l)),p[$].done("object")}if("array"===f){const e=[];j&&v?t.push(e):t[u]=e,a.push(r(y,e,a,s,`${c}/${u}`,l)),p[$].done("array")}}))})),p.onComplete((e=>$.done())),$.promise}module.exports=function({data:r,objectCallback:a=null,keyCallback:s=null},...c){let l,i=o(r),d=[],u="root",p=[s,a],$=e();switch(i){case"array":l=[],t(r,l,d,p,u,...c).then((()=>n(d,l,$)));break;case"object":l={},t(r,l,d,p,u,...c).then((()=>{n(d,l,$)}));break;case"simple":$.done(r)}return $.promise};
@@ -0,0 +1 @@
1
+ import e from"ask-for-promise";function o({data:o,objectCallback:t=null,keyCallback:s=null},...c){let l,i=a(o),d=[],p="root",u=[s,t],f=e();switch(i){case"array":l=[],r(o,l,d,u,p,...c).then((()=>n(d,l,f)));break;case"object":l={},r(o,l,d,u,p,...c).then((()=>{n(d,l,f)}));break;case"simple":f.done(o)}return f.promise}async function n(e,o,n){for await(const o of e)await o.next();n.done(o)}function a(e){return null==e||e.nodeType?"simple":e instanceof Array?"array":"object"==typeof e?"object":"simple"}async function*t(e,o,n,a,t,s){yield await r(e,o,n,a,t,...s)}function r(o,n,r,s,c,...l){let[i,d]=s,p=Object.keys(o),u=e(p),f=e();return p.forEach(((p,f)=>{let $=a(o[p]),y=o[p],m="simple"!==$&&null!=d,b=e(),k=e(),h=e(),j="array"===a(n),v=!isNaN(p),C=Symbol("ignore___");m?d({resolve:b.done,reject:()=>b.done(C),value:y,key:p,breadcrumbs:`${c}/${p}`},...l):b.done("$$cancel"),b.onComplete((e=>{if("$$cancel"===e&&o.hasOwnProperty(y))k.done("$$noUpdates");else if("$$cancel"!==e&&(y=e,$=a(y)),y!=C)if("simple"===$){if(!i)return void k.done("$$noUpdates");i({resolve:k.done,reject:()=>k.done(C),value:y,key:p,breadcrumbs:`${c}/${p}`},...l)}else k.done("$$cancel");else u[f].done("ignore object")})),k.onComplete((e=>{if(e!=C)if("$$cancel"!==e){if("$$noUpdates"!==e&&(y=e,$=a(y)),"simple"===$){const e=function(e,o){return o instanceof Array&&!isNaN(e)}(p,n);return e?n.push(y):n[p]=y,void u[f].done("key")}h.done()}else h.done();else u[f].done("ignore key")})),h.onComplete((()=>{if("object"===$){const e={};j&&v?n.push(e):n[p]=e,r.push(t(y,e,r,s,`${c}/${p}`,l)),u[f].done("object")}if("array"===$){const e=[];j&&v?n.push(e):n[p]=e,r.push(t(y,e,r,s,`${c}/${p}`,l)),u[f].done("array")}}))})),u.onComplete((e=>f.done())),f.promise}export{o as default};
@@ -0,0 +1 @@
1
+ !function(e,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o(require("ask-for-promise")):"function"==typeof define&&define.amd?define(["ask-for-promise"],o):(e="undefined"!=typeof globalThis?globalThis:e||self).walkAsync=o(e.askForPromise)}(this,(function(e){"use strict";async function o(e,o,n){for await(const o of e)await o.next();n.done(o)}function n(e){return null==e||e.nodeType?"simple":e instanceof Array?"array":"object"==typeof e?"object":"simple"}async function*t(e,o,n,t,a,s){yield await r(e,o,n,t,a,...s)}function r(o,r,a,s,i,...c){let[l,d]=s,f=Object.keys(o),u=e(f),p=e();return f.forEach(((f,p)=>{let y=n(o[f]),$=o[f],m="simple"!==y&&null!=d,b=e(),k=e(),h=e(),j="array"===n(r),v=!isNaN(f),w=Symbol("ignore___");m?d({resolve:b.done,reject:()=>b.done(w),value:$,key:f,breadcrumbs:`${i}/${f}`},...c):b.done("$$cancel"),b.onComplete((e=>{if("$$cancel"===e&&o.hasOwnProperty($))k.done("$$noUpdates");else if("$$cancel"!==e&&($=e,y=n($)),$!=w)if("simple"===y){if(!l)return void k.done("$$noUpdates");l({resolve:k.done,reject:()=>k.done(w),value:$,key:f,breadcrumbs:`${i}/${f}`},...c)}else k.done("$$cancel");else u[p].done("ignore object")})),k.onComplete((e=>{if(e!=w)if("$$cancel"!==e){if("$$noUpdates"!==e&&($=e,y=n($)),"simple"===y){const e=function(e,o){return o instanceof Array&&!isNaN(e)}(f,r);return e?r.push($):r[f]=$,void u[p].done("key")}h.done()}else h.done();else u[p].done("ignore key")})),h.onComplete((()=>{if("object"===y){const e={};j&&v?r.push(e):r[f]=e,a.push(t($,e,a,s,`${i}/${f}`,c)),u[p].done("object")}if("array"===y){const e=[];j&&v?r.push(e):r[f]=e,a.push(t($,e,a,s,`${i}/${f}`,c)),u[p].done("array")}}))})),u.onComplete((e=>p.done())),p.promise}return function({data:t,objectCallback:a=null,keyCallback:s=null},...i){let c,l=n(t),d=[],f="root",u=[s,a],p=e();switch(l){case"array":c=[],r(t,c,d,u,f,...i).then((()=>o(d,c,p)));break;case"object":c={},r(t,c,d,u,f,...i).then((()=>{o(d,c,p)}));break;case"simple":p.done(t)}return p.promise}}));
package/package.json CHANGED
@@ -1,11 +1,23 @@
1
1
  {
2
2
  "name": "@peter.naydenov/walk-async",
3
- "version": "1.3.1",
3
+ "version": "2.0.1",
4
4
  "description": "Deep copy with fine control during the process. Can be used also as a deep forEach function",
5
- "main": "src/main.js",
5
+ "main": ".dist/walk-async.umd.js",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./.dist/walk-async.esm.mjs",
10
+ "require": "./.dist/walk-async.cjs",
11
+ "default": "./dist/walk-async.umd.js"
12
+ },
13
+ "./package.json": "./package.json",
14
+ "./dist/*": "./dist/*",
15
+ "./src/*": "./src/*"
16
+ },
6
17
  "scripts": {
7
18
  "test": "mocha test",
8
- "cover": "nyc mocha test"
19
+ "cover": "c8 mocha test",
20
+ "build": "rollup -c"
9
21
  },
10
22
  "repository": {
11
23
  "type": "git",
@@ -14,14 +26,18 @@
14
26
  "author": "Peter Naydenov",
15
27
  "license": "MIT",
16
28
  "dependencies": {
17
- "ask-for-promise": "1.4.0"
29
+ "ask-for-promise": "2.0.3"
18
30
  },
19
31
  "devDependencies": {
20
- "chai": "^4.3.10",
32
+ "@rollup/plugin-commonjs": "^25.0.7",
33
+ "@rollup/plugin-node-resolve": "^15.2.3",
34
+ "@rollup/plugin-terser": "^0.4.4",
35
+ "c8": "^9.1.0",
36
+ "chai": "^5.0.3",
21
37
  "mocha": "^10.2.0",
22
- "nyc": "^15.1.0"
38
+ "rollup": "^4.9.6"
23
39
  },
24
- "nyc": {
40
+ "c8": {
25
41
  "include": [
26
42
  "src/**/*.js"
27
43
  ],
@@ -0,0 +1,41 @@
1
+ import resolve from '@rollup/plugin-node-resolve'
2
+ import commonjs from '@rollup/plugin-commonjs'
3
+ import terser from '@rollup/plugin-terser';
4
+
5
+
6
+ export default [
7
+ // browser-friendly UMD build
8
+ {
9
+ input: 'src/main.js',
10
+ output: {
11
+ name: 'walkAsync',
12
+ file: 'dist/walk-async.umd.js',
13
+ format: 'umd',
14
+ globals: {
15
+ 'ask-for-promise': 'askForPromise'
16
+ }
17
+ },
18
+ external : [ 'ask-for-promise' ],
19
+ plugins: [
20
+ resolve(), // so Rollup can find `ms`
21
+ commonjs() // so Rollup can convert `ms` to an ES module
22
+ , terser()
23
+ ]
24
+ },
25
+
26
+ // CommonJS (for Node) and ES module (for bundlers) build.
27
+ // (We could have three entries in the configuration array
28
+ // instead of two, but it's quicker to generate multiple
29
+ // builds from a single configuration where possible, using
30
+ // an array for the `output` option, where we can specify
31
+ // `file` and `format` for each target)
32
+ {
33
+ input: 'src/main.js',
34
+ external: ['ask-for-promise'],
35
+ output: [
36
+ { file: 'dist/walk-async.cjs' , format: 'cjs' },
37
+ { file: 'dist/walk-async.esm.mjs', format: 'es' }
38
+ ],
39
+ plugins: [ terser() ]
40
+ }
41
+ ];
package/src/main.js CHANGED
@@ -9,12 +9,12 @@
9
9
  *
10
10
  * History notes:
11
11
  * - Walk-async was born on September 18th, 2022
12
+ * - Converted to ES module on January 1st, 2024
12
13
  *
13
14
  */
14
15
 
15
16
 
16
-
17
- const askForPromise = require("ask-for-promise");
17
+ import askForPromise from "ask-for-promise"
18
18
 
19
19
 
20
20
 
@@ -198,6 +198,6 @@ function copyObject ( origin, result, extend, cb, breadcrumbs, ...args ) {
198
198
 
199
199
 
200
200
 
201
- module.exports = walk
201
+ export default walk
202
202
 
203
203
 
@@ -1,118 +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 -> Deep copy', () => {
11
-
12
- it ( 'Copy a primitive value', () => {
13
- let x = 12;
14
- walk ({data:x})
15
- .then ( r => {
16
- x = 64
17
- expect ( r ).to.be.equal ( 12 )
18
- expect ( r !== x )
19
- })
20
- }) // it copy a primitives
21
-
22
-
23
-
24
- it ( 'Copy array of strings', () => {
25
- let x = [ 'one', 'two', 'three' ];
26
- walk ({data:x})
27
- .then ( r => {
28
- x.push ( 'four' )
29
- expect ( r ).to.have.length ( 3 )
30
- })
31
- }) // it copy array of strings
32
-
33
-
34
-
35
- it ( 'Copy a single level deep object', () => {
36
- let
37
- x = { name:'Peter', age: 47 }
38
- , z = x
39
- ;
40
- walk ( {data:x})
41
- .then ( r => {
42
- x.test = 'hello'
43
- expect ( r ).to.not.have.property ( 'test' )
44
- expect ( z ).to.have.property ( 'test' )
45
- })
46
- }) // it copy a single level deep object
47
-
48
-
49
-
50
- it ( 'Copy a mixed structure', () => {
51
- let
52
- x = {
53
- ls : [ 1,2,3 ]
54
- , name : 'Peter'
55
- , props : {
56
- eyeColor: 'blue'
57
- , age : 47
58
- , height : 176
59
- , sizes : [12,33,12,21]
60
- }
61
- }
62
- ;
63
- walk ( x )
64
- .then ( r => {
65
- r.props.sizes.push ( 66 )
66
- x.props.sizes[0] = 222222
67
- x.props.test = 'hello'
68
-
69
- expect ( x.props.sizes ).to.have.length ( 4 )
70
- expect ( r.props.sizes ).to.have.length ( 5 )
71
- expect ( x.props.sizes[0] !== r.props.sizes[0])
72
-
73
- expect ( r.props ).to.not.have.property ( 'test' )
74
- })
75
- }) // it Copy a mixed structure
76
-
77
- it ( 'Data property has value "null"', done => {
78
- // Fix: Deep copy process is losing object properties that are equal to 'null'
79
- const x = { name : null };
80
- walk ({data:x}).then ( r => {
81
- expect ( r ).to.have.property ( 'name' )
82
- expect ( r.name ).to.be.equal ( null )
83
- done ()
84
- })
85
- }) // it Data property has value null
86
-
87
-
88
-
89
- it ( 'html nodes - copy by reference', done => {
90
- const data = {
91
- name : 'Peter'
92
- , pretendHTML : { nodeType: 1 }
93
- };
94
- walk ({ data })
95
- .then ( r => {
96
- r.pretendHTML.something = 'hello'
97
- expect ( data.pretendHTML.something ).to.be.equal ( 'hello' ) // Recognize html nodes and keep them as a reference
98
- done ()
99
- })
100
- }) // html nodes - copy by reference
101
-
102
-
103
-
104
- it ( 'Functions type - copy by reference', () => {
105
- const data = {
106
- name : 'Peter'
107
- , func : () => 12
108
- };
109
-
110
- walk ({ data })
111
- .then ( r => {
112
- expect ( r.func() ).to.be.equal ( 12 )
113
- })
114
- }) // it Functions type - copy by reference
115
-
116
- }) // describe
117
-
118
-
@@ -1,269 +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 -> keyCallback function', () => {
11
-
12
- it ( 'Hide a property', () => {
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
- walk ({
26
- data : x
27
- , keyCallback : ({
28
- key : k
29
- , value
30
- , resolve
31
- , reject
32
- }) => {
33
- if ( k === 'name' ) reject ()
34
- else resolve ( value )
35
- }
36
- })
37
- .then ( r => {
38
- expect ( r ).to.not.have.property ( 'name' )
39
- })
40
- }) // it Hide a property
41
-
42
-
43
-
44
- it ( 'Provide a structure. Hide values approaches', () => {
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
- walk ({
57
- data:x
58
- , keyCallback : ({ resolve, reject, breadcrumbs }) => {
59
- const hasSizes = breadcrumbs.includes('root/props/sizes');
60
- if ( hasSizes ) reject ()
61
- else resolve ( 'xxx' )
62
- }
63
- })
64
- .then ( r => {
65
- expect ( r.name ).to.be.equal ( 'xxx' )
66
- expect ( r.props.age ).to.be.equal ( 'xxx' )
67
- expect ( r.props.sizes.length ).to.be.equal ( 0 )
68
- })
69
- }) // it Provide a structure
70
-
71
-
72
- it ( 'No properties. Just structures', () => {
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
- walk ({
85
- data : x
86
- , keyCallback : ({reject}) => reject ()
87
- })
88
- .then ( r => {
89
- expect ( r ).to.have.property ( 'ls' )
90
- expect ( r ).to.have.property ( 'props' )
91
- expect ( r.props ).to.have.property ( 'sizes' )
92
-
93
- expect ( r ).to.not.have.property ( 'name' )
94
- expect ( r.props ).to.not.have.property ( 'age' )
95
-
96
- expect ( r.ls.length ).to.be.equal ( 0 )
97
- expect ( r.props.sizes.length ).to.be.equal ( 0 )
98
- })
99
- }) // it No properties
100
-
101
-
102
- it ( 'Convert primitive property to object', () => {
103
- let
104
- x = [ 'peter', 'ivan', 'petkan', 'rosica' ]
105
- , data = {
106
- "ivan" : { name: 'Ivan', age: 45, gender: 'male' }
107
- , "petkan" : { name: 'Petkan', age: 32, gender: 'male' }
108
- , "rosica" : { name: "Rosica", age: 75, gender: 'female'}
109
- }
110
- ;
111
- function keyCallback ({ value, resolve, reject }) {
112
- const person= data[value];
113
- if ( person ) {
114
- if ( person.age > 33 ) resolve ( person )
115
- else reject ()
116
- return
117
- }
118
- else resolve ( value )
119
- }
120
- walk ({
121
- data : x
122
- , keyCallback
123
- })
124
- .then ( r => {
125
- expect ( r.length ).to.be.equal ( 3 ) // ignore 'petkan'
126
- expect ( r[0] ).to.be.equal ( 'Peter' ) // no description for Peter available
127
- expect ( r[1]['name']).to.be.equal ( 'Ivan' )
128
- expect ( r[2]['name']).to.be.equal ( 'Rosica' )
129
- })
130
- }) // it Convert primitive property to object
131
-
132
-
133
-
134
- it ( 'Properties of the array object', () => {
135
- let data = [ 'peter', 'ivan' ];
136
- data.group = 'work'
137
-
138
- function keyCallback ({value, resolve }) {
139
- resolve ( value )
140
- }
141
-
142
- walk ({ data, keyCallback })
143
- .then ( r => {
144
- expect ( r.length ).to.be.equal ( 2 )
145
- expect ( r.group ).to.be.equal ( 'work' )
146
- })
147
- }) // it Properties of the array
148
-
149
-
150
-
151
- it ( 'Set a value to NULL', done => {
152
- let
153
- x = {
154
- ls : [ 1,2,3 ]
155
- , name : 'Peter'
156
- , props : {
157
- eyeColor: null // Use callback and return this exact value
158
- , age : 47
159
- , height : 176
160
- , sizes : [12,33,12,21]
161
- }
162
- };
163
-
164
- function checkNull ({ value, resolve }) {
165
- resolve (value)
166
- } // checkNull func.
167
-
168
- walk ({ data:x, keyCallback:checkNull })
169
- .then ( r => {
170
- expect ( r.props.eyeColor ).to.be.equal ( null )
171
- done ()
172
- })
173
- }) // it set a value to NULL
174
-
175
-
176
-
177
- it ( 'Set a value to undefined', done => {
178
- let
179
- x = {
180
- ls : [ 1,2,3 ]
181
- , name : 'Peter'
182
- , props : {
183
- eyeColor: undefined // Use callback and return this exact value
184
- , age : 47
185
- , height : 176
186
- , sizes : [12,33,12,21]
187
- }
188
- };
189
-
190
- function checkNull ({ value, resolve }) {
191
- resolve(value)
192
- } // checkNull func.
193
-
194
- walk ({ data:x, keyCallback:checkNull })
195
- .then ( r => {
196
- expect ( r.props.eyeColor ).to.be.equal ( undefined )
197
- done ()
198
- })
199
- }) // it Set a value to undefined
200
-
201
-
202
- it ( 'Copy a function', done => {
203
- let
204
- x = {
205
- ls : [ 1,2,3 ]
206
- , name : 'Peter'
207
- , props : {
208
- eyeColor: undefined // Use callback and return this exact value
209
- , age : function age () { return 47 }
210
- , height : 176
211
- , sizes : [12,33,12,21]
212
- }
213
- };
214
-
215
- function checkNull ({ value, resolve }) {
216
- resolve ( value )
217
- } // checkNull func.
218
-
219
- walk ({ data:x, keyCallback:checkNull })
220
- .then ( r => {
221
- expect ( r.props.age() ).to.be.equal ( 47 )
222
- done ()
223
- })
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
266
-
267
- }) // describe
268
-
269
-