@based/schema 1.0.8 → 1.0.9

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/test/walker.ts CHANGED
@@ -85,8 +85,20 @@ const schema: BasedSchema = {
85
85
  },
86
86
  }
87
87
 
88
- test.only('walker', async (t) => {
89
- const x = await walk<{ lullz: true }>(
88
+ test('walker', async (t) => {
89
+ const results: any[] = []
90
+
91
+ const setObj = {
92
+ x: {
93
+ y: {
94
+ a: 10,
95
+ bla: [1, 2, 3, 4, 5],
96
+ },
97
+ c: 40,
98
+ },
99
+ }
100
+
101
+ await walk<{ lullz: true }>(
90
102
  schema,
91
103
  {
92
104
  init: async () => {
@@ -96,56 +108,100 @@ test.only('walker', async (t) => {
96
108
  keys: {},
97
109
  fields: {},
98
110
  any: async (args) => {
99
- args.collect(args)
111
+ args.collect()
100
112
  return args
101
113
  },
102
114
  },
103
115
  collect: (args) => {
104
- console.info('..', args.path)
105
116
  return args.path.join('.')
106
117
  },
107
118
  backtrack: (args, fromBt, collected) => {
108
- console.log(
109
- ' \n-----------BACK TRACK GOOD GO',
110
- '\n',
111
- 'path:',
112
- args.path.join('.'),
113
- '\n',
114
- 'backtracked:',
115
- JSON.stringify(fromBt),
116
- '\n',
117
- 'collected:',
118
- collected,
119
- '--------------------'
120
- )
119
+ results.push({ path: args.path, bt: [...fromBt] })
121
120
  return fromBt.length ? fromBt : collected
122
121
  },
123
122
  },
123
+ setObj
124
+ )
125
+
126
+ t.deepEqual(results, [
127
+ { path: ['x', 'y', 'bla'], bt: [] },
124
128
  {
125
- gurk: [1, 2, 3, 4],
126
- x: {
127
- y: {
128
- z: {
129
- a: 10,
130
- b: 20,
131
- gur: {
132
- x: true,
133
- y: true,
134
- $list: {
135
- $sort: true,
136
- },
137
- },
138
- c: 40,
139
- $list: true,
140
- },
129
+ path: ['x', 'y'],
130
+ bt: [['x.y.bla.0', 'x.y.bla.1', 'x.y.bla.2', 'x.y.bla.3', 'x.y.bla.4']],
131
+ },
132
+ {
133
+ path: ['x'],
134
+ bt: [[['x.y.bla.0', 'x.y.bla.1', 'x.y.bla.2', 'x.y.bla.3', 'x.y.bla.4']]],
135
+ },
136
+ {
137
+ path: [],
138
+ bt: [
139
+ [[['x.y.bla.0', 'x.y.bla.1', 'x.y.bla.2', 'x.y.bla.3', 'x.y.bla.4']]],
140
+ ],
141
+ },
142
+ ])
143
+
144
+ console.log('--------------------------------------')
145
+ const results2: any[] = []
146
+
147
+ await walk<{ lullz: true }>(
148
+ schema,
149
+ {
150
+ init: async () => {
151
+ return { target: { lullz: true } }
152
+ },
153
+ parsers: {
154
+ keys: {},
155
+ fields: {},
156
+ any: async (args) => {
157
+ args.collect()
158
+ return { target: { lullz: true } }
141
159
  },
142
160
  },
143
- }
161
+ collect: (args) => {
162
+ return args.path.join('.')
163
+ },
164
+ backtrack: (args, fromBt, collected) => {
165
+ results2.push({ path: args.path, bt: [...fromBt] })
166
+ return fromBt.length ? fromBt : collected
167
+ },
168
+ },
169
+ setObj
144
170
  )
145
171
 
146
- console.info('------------')
172
+ t.deepEqual(results, results2)
147
173
 
148
- t.true(true)
174
+ console.log('--------------------------------------')
175
+ const results3: any[] = []
176
+
177
+ let cnt = 0
178
+ await walk<{ lullz: true }>(
179
+ schema,
180
+ {
181
+ init: async () => {
182
+ return { target: { lullz: true } }
183
+ },
184
+ parsers: {
185
+ keys: {},
186
+ fields: {},
187
+ any: async (args) => {
188
+ cnt++
189
+ args.collect()
190
+ return cnt % 2 ? args : { target: { lullz: true } }
191
+ },
192
+ },
193
+ collect: (args) => {
194
+ return args.path.join('.')
195
+ },
196
+ backtrack: (args, fromBt, collected) => {
197
+ results3.push({ path: args.path, bt: [...fromBt] })
198
+ return fromBt.length ? fromBt : collected
199
+ },
200
+ },
201
+ setObj
202
+ )
203
+
204
+ t.deepEqual(results, results3)
149
205
  })
150
206
 
151
207
  test('set walker', async (t) => {
@@ -251,7 +307,7 @@ test('set walker', async (t) => {
251
307
  t.true(true)
252
308
  })
253
309
 
254
- test('perf setWalker', async (t) => {
310
+ test.serial('perf setWalker', async (t) => {
255
311
  let d = Date.now()
256
312
  let collected = 0
257
313
  let errs = 0
@@ -264,6 +320,8 @@ test('perf setWalker', async (t) => {
264
320
  errs += x.errors.length
265
321
  collected += x.collected.length
266
322
  }
323
+ d = Date.now() - d
324
+ console.info('setting 200k', d, 'ms')
267
325
  t.true(d < 1e3)
268
326
  })
269
327
 
@@ -664,10 +722,6 @@ test('string', async (t) => {
664
722
  },
665
723
  })
666
724
 
667
- r.collected.forEach((v) => {
668
- console.info(v.root.typeSchema)
669
- })
670
-
671
725
  console.log(r.errors)
672
726
  console.dir(
673
727
  r.collected.map((v) => ({ path: v.path, value: v.value })),
@@ -682,10 +736,6 @@ test('string', async (t) => {
682
736
  },
683
737
  })
684
738
 
685
- r.collected.forEach((v) => {
686
- console.info(v.root.typeSchema)
687
- })
688
-
689
739
  console.log(r.errors)
690
740
  console.dir(
691
741
  r.collected.map((v) => ({ path: v.path, value: v.value })),
@@ -700,11 +750,71 @@ test('string', async (t) => {
700
750
  },
701
751
  })
702
752
 
703
- r.collected.forEach((v) => {
704
- console.info(v.root.typeSchema)
753
+ console.log(r.errors)
754
+ console.dir(
755
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
756
+ { depth: 10 }
757
+ )
758
+
759
+ console.info('---- doink 29 ------')
760
+ r = await setWalker(schema, {
761
+ $id: 'bl120',
762
+ text: {
763
+ en: 'bla',
764
+ },
765
+ })
766
+
767
+ console.dir(
768
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
769
+ { depth: 10 }
770
+ )
771
+
772
+ t.true(true)
773
+
774
+ console.info('---- doink 30 ------')
775
+ r = await setWalker(schema, {
776
+ $id: 'bl120',
777
+ text: {
778
+ $delete: true,
779
+ },
705
780
  })
706
781
 
707
- console.log(r.errors)
782
+ console.dir(
783
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
784
+ { depth: 10 }
785
+ )
786
+
787
+ console.info('---- doink 31 ------')
788
+ r = await setWalker(schema, {
789
+ $id: 'bl120',
790
+ $delete: true,
791
+ })
792
+
793
+ console.dir(r.errors)
794
+ console.dir(
795
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
796
+ { depth: 10 }
797
+ )
798
+
799
+ console.info('---- doink 32 ------')
800
+ r = await setWalker(schema, {
801
+ $id: 'bl120',
802
+ $alias: 'bla',
803
+ })
804
+
805
+ console.dir(r.errors)
806
+ console.dir(
807
+ r.collected.map((v) => ({ path: v.path, value: v.value })),
808
+ { depth: 10 }
809
+ )
810
+
811
+ console.info('---- doink 33 ------')
812
+ r = await setWalker(schema, {
813
+ $id: 'bl120',
814
+ $alias: ['bla'],
815
+ })
816
+
817
+ console.dir(r.errors)
708
818
  console.dir(
709
819
  r.collected.map((v) => ({ path: v.path, value: v.value })),
710
820
  { depth: 10 }