@peter.naydenov/walk-async 2.0.0 → 2.0.2

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,5 +1,16 @@
1
1
  # Release History
2
2
 
3
+
4
+
5
+ ### 2.0.2 ( 2024-01-31)
6
+ - [x] Dev dependencies updates. Chai to version 5.0.3;
7
+ - [x] Dev dependencies updates. C8 to version 9.1.0;
8
+ - [x] Folder 'dist' was added to the project. Includes commonjs, umd and esm versions of the library;
9
+ - [x] Package.json: "exports" section was added. Allows you to use package as commonjs or es6 module without additional configuration;
10
+ - [x] Rollup was added to the project. Used to build the library versions;
11
+
12
+
13
+
3
14
  ### 2.0.0 ( 2024-01-01)
4
15
  - [x] Module converted to ES module;
5
16
  - [x] Dev dependencies updates;
@@ -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.2",
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
-
@@ -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
-