@kizmann/pico-js 2.0.5 → 2.0.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kizmann/pico-js",
3
- "version": "2.0.5",
3
+ "version": "2.0.6",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "private": false,
@@ -1,4 +1,4 @@
1
- import { Obj, Mix } from "../index.esm.js";
1
+ import { Obj, Mix, Any } from "../index.esm.js";
2
2
 
3
3
  export class PicoArray
4
4
  {
@@ -30,7 +30,7 @@ export class PicoArray
30
30
  */
31
31
  static get(value, index, fallback = null)
32
32
  {
33
- if ( ! Mix.isArr(value) ) {
33
+ if ( !Mix.isArr(value) ) {
34
34
  return value;
35
35
  }
36
36
 
@@ -111,7 +111,7 @@ export class PicoArray
111
111
  return this.findIndex(value, search) !== - 1;
112
112
  }
113
113
 
114
- if ( ! Mix.isArr(value) ) {
114
+ if ( !Mix.isArr(value) ) {
115
115
  value = [value];
116
116
  }
117
117
 
@@ -241,13 +241,23 @@ export class PicoArray
241
241
 
242
242
  let result = new Array(value.length);
243
243
 
244
- for (let i = 0; i < value.length; i++) {
244
+ for ( let i = 0; i < value.length; i ++ ) {
245
245
  result[i] = cb(value[i], i);
246
246
  }
247
247
 
248
248
  return retval != null ? retval : result;
249
249
  }
250
250
 
251
+ /**
252
+ * Iterate over object keys
253
+ *
254
+ * @example Arr.eachObj({a:1}, (v,k) => v+1) // => [2]
255
+ *
256
+ * @param {any} value Input object
257
+ * @param {function} cb Iterate callback
258
+ * @param {any} [retval] Return value override
259
+ * @returns {any} Mapped values or retval
260
+ */
251
261
  static eachObj(value, cb, retval = null)
252
262
  {
253
263
  if ( Mix.isArr(value) ) {
@@ -304,32 +314,32 @@ export class PicoArray
304
314
  */
305
315
  static recursive(value, key, cb, cascade = [])
306
316
  {
307
- if ( value == null ) {
308
- return value;
317
+ // NIE WIEDER ANFASSEN !!!
318
+ let fn = (cas) => (val) => {
319
+ return this.recursive(val, key, cb, cas);
320
+ };
321
+
322
+ if ( Mix.isArr(value) ) {
323
+ return this.map(value, fn(cascade));
309
324
  }
310
325
 
311
326
  if ( Mix.isObj(value) ) {
312
- return this.recursiveObj(value, key, cb, cascade);
327
+ value = cb(value, cascade) ?? value;
313
328
  }
314
329
 
315
- return this.each(value, (item) => {
316
- return (this.recursive(item[key], key, cb, [
317
- ...cascade, value
318
- ]), cb(item, this.clone(cascade)));
319
- });
330
+ cascade = [
331
+ ...this.clone(cascade), value
332
+ ];
320
333
 
321
- // [{childs: [{ childs: [] } ] }, { childs: [] } ] }]
322
- }
334
+ if ( Mix.isObj(value[key]) ) {
335
+ value[key] = fn(cascade)(value[key]);
336
+ }
323
337
 
324
- static recursiveObj(value, key, cb, cascade = [])
325
- {
326
- if ( value == null ) {
327
- return value;
338
+ if ( Mix.isArr(value[key]) ) {
339
+ value[key] = this.map(value[key], fn(cascade));
328
340
  }
329
341
 
330
- return this.recursive(value[key], key, cb, [
331
- ...cascade, value
332
- ]), cb(item, this.clone(cascade))
342
+ return value;
333
343
  }
334
344
 
335
345
  /**
@@ -365,6 +375,15 @@ export class PicoArray
365
375
  });
366
376
  }
367
377
 
378
+ /**
379
+ * Remove values matching filter (mutates)
380
+ *
381
+ * @example Arr.filterRemove([1,2], 2) // => [1]
382
+ *
383
+ * @param {any} value Input list
384
+ * @param {any} [filter] Filter spec
385
+ * @returns {any} Mutated list
386
+ */
368
387
  static filterRemove(value, filter = null)
369
388
  {
370
389
  if ( value == null ) {
@@ -531,8 +550,12 @@ export class PicoArray
531
550
  */
532
551
  static sortDeep(value, key)
533
552
  {
553
+ let fn = (val) => {
554
+ return Obj.get(val, key);
555
+ }
556
+
534
557
  let keys = Mix.keys(value).sort((a, b) => {
535
- return Mix.compare(Obj.get(a, key), Obj.get(b, key));
558
+ return Mix.compare(fn(value[a]), fn(value[b]));
536
559
  });
537
560
 
538
561
  let result = [];
@@ -625,7 +648,7 @@ export class PicoArray
625
648
  finder = target;
626
649
  }
627
650
 
628
- if ( this.findIndex(value, finder) !== -1 ) {
651
+ if ( this.findIndex(value, finder) !== - 1 ) {
629
652
  return value;
630
653
  }
631
654
 
@@ -650,7 +673,7 @@ export class PicoArray
650
673
 
651
674
  let index = this.findIndex(value, finder);
652
675
 
653
- if ( index !== -1 ) {
676
+ if ( index !== - 1 ) {
654
677
  this.splice(value, index, 1);
655
678
  }
656
679
 
@@ -675,7 +698,7 @@ export class PicoArray
675
698
 
676
699
  let index = this.findIndex(value, finder);
677
700
 
678
- if ( index === -1 ) {
701
+ if ( index === - 1 ) {
679
702
  return value;
680
703
  }
681
704
 
@@ -701,7 +724,7 @@ export class PicoArray
701
724
 
702
725
  let index = this.findIndex(value, finder);
703
726
 
704
- if ( index === -1 ) {
727
+ if ( index === - 1 ) {
705
728
  return (value.push(target), value);
706
729
  }
707
730
 
@@ -791,7 +814,7 @@ export class PicoArray
791
814
  return Obj.clone(value);
792
815
  }
793
816
 
794
- if ( ! Mix.isArr(value) ) {
817
+ if ( !Mix.isArr(value) ) {
795
818
  return value;
796
819
  }
797
820
 
@@ -905,7 +928,7 @@ export class PicoArray
905
928
  return Obj.includes(value, search);
906
929
  }
907
930
 
908
- if ( ! Mix.isArr(search) ) {
931
+ if ( !Mix.isArr(search) ) {
909
932
  return value === search;
910
933
  }
911
934
 
@@ -917,7 +940,7 @@ export class PicoArray
917
940
  return true;
918
941
  }
919
942
 
920
- for ( let i = 0; result === false && i < length; i++) {
943
+ for ( let i = 0; result === false && i < length; i ++ ) {
921
944
  result ||= this.has(value, search[i]);
922
945
  }
923
946
 
@@ -938,7 +961,7 @@ export class PicoArray
938
961
  let result = true;
939
962
 
940
963
  for ( let key of Mix.vals(val) ) {
941
- result &&= Mix.vals(arr).indexOf(key) !== -1;
964
+ result &&= Mix.vals(arr).indexOf(key) !== - 1;
942
965
  }
943
966
 
944
967
  return result;
@@ -959,7 +982,7 @@ export class PicoArray
959
982
  return Obj.matches(value, search);
960
983
  }
961
984
 
962
- if ( ! Mix.isArr(value) ) {
985
+ if ( !Mix.isArr(value) ) {
963
986
  return value === search;
964
987
  }
965
988
 
@@ -973,7 +996,7 @@ export class PicoArray
973
996
  return false;
974
997
  }
975
998
 
976
- for ( let i = 0; result === true && i < length; i++) {
999
+ for ( let i = 0; result === true && i < length; i ++ ) {
977
1000
  result &&= this.has(value, search[i]);
978
1001
  }
979
1002
 
@@ -233,18 +233,28 @@ export class PicoRunner
233
233
  *
234
234
  * @param {function} cb Callback to run
235
235
  * @param {string} key Buffer key
236
- * @param {number} [order] Sort order
236
+ * @param {number} [priority] Sort priority
237
237
  * @returns {function} Buffered handler
238
238
  */
239
- static framebuffer(cb, key, order = 1000)
239
+ static framebuffer(cb, key, priority = 1000)
240
240
  {
241
+ const item = {
242
+ key, cb, priority, args: [], active: false
243
+ };
244
+
245
+ Arr.add(this.$buffer, item);
246
+
247
+ let fn = () => {
248
+ this.runFramebuffer();
249
+ };
250
+
241
251
  return (e, ...args) => {
242
252
 
243
- Arr.replace(PicoRunner.$buffer, {
244
- key, cb, order, args: [e, ...args], active: true
245
- }, { key });
253
+ Obj.assign(item, {
254
+ args: [e, ...args], active: true
255
+ });
246
256
 
247
- (e.preventDefault(), PicoRunner.runFramebuffer());
257
+ (e.preventDefault(), fn());
248
258
  };
249
259
  }
250
260
 
@@ -262,10 +272,11 @@ export class PicoRunner
262
272
  }
263
273
 
264
274
  this.$idler = setTimeout(() => {
275
+ console.log('delayed runFramebuffer')
265
276
  this.runFramebuffer();
266
277
  }, 50);
267
278
 
268
- if ( Date.now() - this.$timer <= 50 ) {
279
+ if ( Date.now() - this.$timer < 50 ) {
269
280
  return;
270
281
  }
271
282
 
@@ -283,10 +294,10 @@ export class PicoRunner
283
294
  return;
284
295
  }
285
296
 
286
- this.frame(() => {
287
- Arr.each(Arr.sort(buffer, 'order'), (item) => {
288
- item.cb(...item.args); item.active = false;
289
- });
297
+ buffer = Arr.sort(buffer, 'priority');
298
+
299
+ Arr.each(buffer.reverse(), (item) => {
300
+ item.cb(...item.args); item.active = false;
290
301
  });
291
302
  }
292
303
 
@@ -134,7 +134,17 @@ export class PicoArray {
134
134
  * @returns {any} Mapped array
135
135
  */
136
136
  static each(value: any, cb: Function, retval?: any): any;
137
- static eachObj(value: any, cb: any, retval?: any): any;
137
+ /**
138
+ * Iterate over object keys
139
+ *
140
+ * @example Arr.eachObj({a:1}, (v,k) => v+1) // => [2]
141
+ *
142
+ * @param {any} value Input object
143
+ * @param {function} cb Iterate callback
144
+ * @param {any} [retval] Return value override
145
+ * @returns {any} Mapped values or retval
146
+ */
147
+ static eachObj(value: any, cb: Function, retval?: any): any;
138
148
  /**
139
149
  * Map values in place (mutates)
140
150
  *
@@ -157,7 +167,6 @@ export class PicoArray {
157
167
  * @returns {any} Mapped tree
158
168
  */
159
169
  static recursive(value: any, key: string, cb: Function, cascade?: Array<any>): any;
160
- static recursiveObj(value: any, key: any, cb: any, cascade?: any[]): any;
161
170
  /**
162
171
  * Get matching indexes by filter
163
172
  *
@@ -168,6 +177,15 @@ export class PicoArray {
168
177
  * @returns {Array<string>} Matching keys
169
178
  */
170
179
  static filterIndex(value: any, filter?: any): Array<string>;
180
+ /**
181
+ * Remove values matching filter (mutates)
182
+ *
183
+ * @example Arr.filterRemove([1,2], 2) // => [1]
184
+ *
185
+ * @param {any} value Input list
186
+ * @param {any} [filter] Filter spec
187
+ * @returns {any} Mutated list
188
+ */
171
189
  static filterRemove(value: any, filter?: any): any;
172
190
  /**
173
191
  * Filter values by filter
@@ -107,10 +107,10 @@ export class PicoRunner {
107
107
  *
108
108
  * @param {function} cb Callback to run
109
109
  * @param {string} key Buffer key
110
- * @param {number} [order] Sort order
110
+ * @param {number} [priority] Sort priority
111
111
  * @returns {function} Buffered handler
112
112
  */
113
- static framebuffer(cb: Function, key: string, order?: number): Function;
113
+ static framebuffer(cb: Function, key: string, priority?: number): Function;
114
114
  /**
115
115
  * Flush buffered frame events
116
116
  *