@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.
@@ -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,12 +1,23 @@
1
1
  {
2
2
  "name": "@peter.naydenov/walk-async",
3
- "version": "2.0.0",
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
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
+ },
7
17
  "scripts": {
8
18
  "test": "mocha test",
9
- "cover": "c8 mocha test"
19
+ "cover": "c8 mocha test",
20
+ "build": "rollup -c"
10
21
  },
11
22
  "repository": {
12
23
  "type": "git",
@@ -15,12 +26,16 @@
15
26
  "author": "Peter Naydenov",
16
27
  "license": "MIT",
17
28
  "dependencies": {
18
- "ask-for-promise": "1.5.0"
29
+ "ask-for-promise": "2.0.3"
19
30
  },
20
31
  "devDependencies": {
21
- "c8": "^8.0.1",
22
- "chai": "^5.0.0",
23
- "mocha": "^10.2.0"
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",
37
+ "mocha": "^10.2.0",
38
+ "rollup": "^4.9.6"
24
39
  },
25
40
  "c8": {
26
41
  "include": [
@@ -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
+ ];
@@ -1,116 +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 -> Deep copy', () => {
9
-
10
- it ( 'Copy a primitive value', () => {
11
- let x = 12;
12
- walk ({data:x})
13
- .then ( r => {
14
- x = 64
15
- expect ( r ).to.be.equal ( 12 )
16
- expect ( r !== x )
17
- })
18
- }) // it copy a primitives
19
-
20
-
21
-
22
- it ( 'Copy array of strings', () => {
23
- let x = [ 'one', 'two', 'three' ];
24
- walk ({data:x})
25
- .then ( r => {
26
- x.push ( 'four' )
27
- expect ( r ).to.have.length ( 3 )
28
- })
29
- }) // it copy array of strings
30
-
31
-
32
-
33
- it ( 'Copy a single level deep object', () => {
34
- let
35
- x = { name:'Peter', age: 47 }
36
- , z = x
37
- ;
38
- walk ( {data:x})
39
- .then ( r => {
40
- x.test = 'hello'
41
- expect ( r ).to.not.have.property ( 'test' )
42
- expect ( z ).to.have.property ( 'test' )
43
- })
44
- }) // it copy a single level deep object
45
-
46
-
47
-
48
- it ( 'Copy a mixed structure', () => {
49
- let
50
- x = {
51
- ls : [ 1,2,3 ]
52
- , name : 'Peter'
53
- , props : {
54
- eyeColor: 'blue'
55
- , age : 47
56
- , height : 176
57
- , sizes : [12,33,12,21]
58
- }
59
- }
60
- ;
61
- walk ( x )
62
- .then ( r => {
63
- r.props.sizes.push ( 66 )
64
- x.props.sizes[0] = 222222
65
- x.props.test = 'hello'
66
-
67
- expect ( x.props.sizes ).to.have.length ( 4 )
68
- expect ( r.props.sizes ).to.have.length ( 5 )
69
- expect ( x.props.sizes[0] !== r.props.sizes[0])
70
-
71
- expect ( r.props ).to.not.have.property ( 'test' )
72
- })
73
- }) // it Copy a mixed structure
74
-
75
- it ( 'Data property has value "null"', done => {
76
- // Fix: Deep copy process is losing object properties that are equal to 'null'
77
- const x = { name : null };
78
- walk ({data:x}).then ( r => {
79
- expect ( r ).to.have.property ( 'name' )
80
- expect ( r.name ).to.be.equal ( null )
81
- done ()
82
- })
83
- }) // it Data property has value null
84
-
85
-
86
-
87
- it ( 'html nodes - copy by reference', done => {
88
- const data = {
89
- name : 'Peter'
90
- , pretendHTML : { nodeType: 1 }
91
- };
92
- walk ({ data })
93
- .then ( r => {
94
- r.pretendHTML.something = 'hello'
95
- expect ( data.pretendHTML.something ).to.be.equal ( 'hello' ) // Recognize html nodes and keep them as a reference
96
- done ()
97
- })
98
- }) // html nodes - copy by reference
99
-
100
-
101
-
102
- it ( 'Functions type - copy by reference', () => {
103
- const data = {
104
- name : 'Peter'
105
- , func : () => 12
106
- };
107
-
108
- walk ({ data })
109
- .then ( r => {
110
- expect ( r.func() ).to.be.equal ( 12 )
111
- })
112
- }) // it Functions type - copy by reference
113
-
114
- }) // describe
115
-
116
-
@@ -1,267 +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 -> keyCallback function', () => {
9
-
10
- it ( 'Hide a property', () => {
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
- walk ({
24
- data : x
25
- , keyCallback : ({
26
- key : k
27
- , value
28
- , resolve
29
- , reject
30
- }) => {
31
- if ( k === 'name' ) reject ()
32
- else resolve ( value )
33
- }
34
- })
35
- .then ( r => {
36
- expect ( r ).to.not.have.property ( 'name' )
37
- })
38
- }) // it Hide a property
39
-
40
-
41
-
42
- it ( 'Provide a structure. Hide values approaches', () => {
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
- walk ({
55
- data:x
56
- , keyCallback : ({ resolve, reject, breadcrumbs }) => {
57
- const hasSizes = breadcrumbs.includes('root/props/sizes');
58
- if ( hasSizes ) reject ()
59
- else resolve ( 'xxx' )
60
- }
61
- })
62
- .then ( r => {
63
- expect ( r.name ).to.be.equal ( 'xxx' )
64
- expect ( r.props.age ).to.be.equal ( 'xxx' )
65
- expect ( r.props.sizes.length ).to.be.equal ( 0 )
66
- })
67
- }) // it Provide a structure
68
-
69
-
70
- it ( 'No properties. Just structures', () => {
71
- let
72
- x = {
73
- ls : [ 1,2,3 ]
74
- , name : 'Peter'
75
- , props : {
76
- eyeColor: 'blue'
77
- , age : 47
78
- , height : 176
79
- , sizes : [12,33,12,21]
80
- }
81
- };
82
- walk ({
83
- data : x
84
- , keyCallback : ({reject}) => reject ()
85
- })
86
- .then ( r => {
87
- expect ( r ).to.have.property ( 'ls' )
88
- expect ( r ).to.have.property ( 'props' )
89
- expect ( r.props ).to.have.property ( 'sizes' )
90
-
91
- expect ( r ).to.not.have.property ( 'name' )
92
- expect ( r.props ).to.not.have.property ( 'age' )
93
-
94
- expect ( r.ls.length ).to.be.equal ( 0 )
95
- expect ( r.props.sizes.length ).to.be.equal ( 0 )
96
- })
97
- }) // it No properties
98
-
99
-
100
- it ( 'Convert primitive property to object', () => {
101
- let
102
- x = [ 'peter', 'ivan', 'petkan', 'rosica' ]
103
- , data = {
104
- "ivan" : { name: 'Ivan', age: 45, gender: 'male' }
105
- , "petkan" : { name: 'Petkan', age: 32, gender: 'male' }
106
- , "rosica" : { name: "Rosica", age: 75, gender: 'female'}
107
- }
108
- ;
109
- function keyCallback ({ value, resolve, reject }) {
110
- const person= data[value];
111
- if ( person ) {
112
- if ( person.age > 33 ) resolve ( person )
113
- else reject ()
114
- return
115
- }
116
- else resolve ( value )
117
- }
118
- walk ({
119
- data : x
120
- , keyCallback
121
- })
122
- .then ( r => {
123
- expect ( r.length ).to.be.equal ( 3 ) // ignore 'petkan'
124
- expect ( r[0] ).to.be.equal ( 'Peter' ) // no description for Peter available
125
- expect ( r[1]['name']).to.be.equal ( 'Ivan' )
126
- expect ( r[2]['name']).to.be.equal ( 'Rosica' )
127
- })
128
- }) // it Convert primitive property to object
129
-
130
-
131
-
132
- it ( 'Properties of the array object', () => {
133
- let data = [ 'peter', 'ivan' ];
134
- data.group = 'work'
135
-
136
- function keyCallback ({value, resolve }) {
137
- resolve ( value )
138
- }
139
-
140
- walk ({ data, keyCallback })
141
- .then ( r => {
142
- expect ( r.length ).to.be.equal ( 2 )
143
- expect ( r.group ).to.be.equal ( 'work' )
144
- })
145
- }) // it Properties of the array
146
-
147
-
148
-
149
- it ( 'Set a value to NULL', done => {
150
- let
151
- x = {
152
- ls : [ 1,2,3 ]
153
- , name : 'Peter'
154
- , props : {
155
- eyeColor: null // Use callback and return this exact value
156
- , age : 47
157
- , height : 176
158
- , sizes : [12,33,12,21]
159
- }
160
- };
161
-
162
- function checkNull ({ value, resolve }) {
163
- resolve (value)
164
- } // checkNull func.
165
-
166
- walk ({ data:x, keyCallback:checkNull })
167
- .then ( r => {
168
- expect ( r.props.eyeColor ).to.be.equal ( null )
169
- done ()
170
- })
171
- }) // it set a value to NULL
172
-
173
-
174
-
175
- it ( 'Set a value to undefined', done => {
176
- let
177
- x = {
178
- ls : [ 1,2,3 ]
179
- , name : 'Peter'
180
- , props : {
181
- eyeColor: undefined // Use callback and return this exact value
182
- , age : 47
183
- , height : 176
184
- , sizes : [12,33,12,21]
185
- }
186
- };
187
-
188
- function checkNull ({ value, resolve }) {
189
- resolve(value)
190
- } // checkNull func.
191
-
192
- walk ({ data:x, keyCallback:checkNull })
193
- .then ( r => {
194
- expect ( r.props.eyeColor ).to.be.equal ( undefined )
195
- done ()
196
- })
197
- }) // it Set a value to undefined
198
-
199
-
200
- it ( 'Copy a function', done => {
201
- let
202
- x = {
203
- ls : [ 1,2,3 ]
204
- , name : 'Peter'
205
- , props : {
206
- eyeColor: undefined // Use callback and return this exact value
207
- , age : function age () { return 47 }
208
- , height : 176
209
- , sizes : [12,33,12,21]
210
- }
211
- };
212
-
213
- function checkNull ({ value, resolve }) {
214
- resolve ( value )
215
- } // checkNull func.
216
-
217
- walk ({ data:x, keyCallback:checkNull })
218
- .then ( r => {
219
- expect ( r.props.age() ).to.be.equal ( 47 )
220
- done ()
221
- })
222
- }) // it Copy a function
223
-
224
-
225
-
226
- it ( 'Extract collections', done => {
227
- let
228
- x = {
229
- ls : [ 1,2,3 ]
230
- , name : 'Peter'
231
- , props : {
232
- eyeColor: undefined // Use callback and return this exact value
233
- , age : function age () { return 47 }
234
- , height : 176
235
- , sizes : [12,33,12,21]
236
- }
237
- }
238
- , fnList = []
239
- , propsCollection = {}
240
- ;
241
-
242
- function valueFn ({ key, value, resolve }, fn, p ) {
243
- const isFn = (typeof value === 'function');
244
- if ( isFn ) fn.push ( value )
245
-
246
- if ( ['name','eyeColor', 'age'].includes(key) ) p[key] = isFn ? value() : value
247
- resolve ( value )
248
- } // valueFn func.
249
-
250
- walk ({ data:x, keyCallback:valueFn }, fnList, propsCollection )
251
- .then ( r => {
252
- expect ( r.props.age() ).to.be.equal ( 47 )
253
- expect ( fnList.length ).to.be.equal ( 1 )
254
- expect ( r.props ).to.have.property ( 'eyeColor' )
255
-
256
- expect ( propsCollection ).to.have.property ( 'name' )
257
- expect ( propsCollection ).to.have.property ( 'eyeColor' )
258
- expect ( propsCollection ).to.have.property ( 'age' )
259
- expect ( propsCollection.age ).to.be.equal ( 47 )
260
-
261
- done ()
262
- })
263
- }) // it Copy a function
264
-
265
- }) // describe
266
-
267
-