@kizmann/pico-js 2.0.4 → 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,12 +1,13 @@
1
1
  {
2
2
  "name": "@kizmann/pico-js",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "private": false,
7
7
  "author": "Eduard Kizmann <kizmann@protonmail.ch>",
8
8
  "repository": "https://github.com/vankizmann/pico-js",
9
9
  "main": "./src/index.esm.js",
10
+ "unpkg": "./dist/pico-js.browser.js",
10
11
  "jsnext:main": "./src/index.esm.js",
11
12
  "typings": "./types/index.esm.d.ts",
12
13
  "imports": {
@@ -42,6 +43,7 @@
42
43
  "lodash": "^4.17.23",
43
44
  "mini-css-extract-plugin": "^2.0.0",
44
45
  "mitata": "^1.0.34",
46
+ "moment": "^2.30.1",
45
47
  "postcss": "^8.4.31",
46
48
  "postcss-inline-svg": "^5.0.0",
47
49
  "postcss-loader": "^5.0.0",
@@ -1,4 +1,4 @@
1
- import { Arr, Hash, Mix, Obj, Dom } from "../index.esm.js";
1
+ import { Arr, Hash, Mix, Obj, Dom, Run } from "../index.esm.js";
2
2
  import { PicoDom } from "../utils/Dom.js";
3
3
 
4
4
  /**
@@ -6,7 +6,7 @@ import { PicoDom } from "../utils/Dom.js";
6
6
  */
7
7
  export class PicoDomEventStatic
8
8
  {
9
- static events = [];
9
+ static $events = [];
10
10
  }
11
11
 
12
12
  /**
@@ -35,7 +35,7 @@ export class PicoDomEventInstance
35
35
  options = { id: options };
36
36
  }
37
37
 
38
- Dom.events = Arr.append(Dom.events, {
38
+ Dom.$events = Arr.append(Dom.$events, {
39
39
  el, event, cb, selector, pause, options
40
40
  });
41
41
 
@@ -61,7 +61,7 @@ export class PicoDomEventInstance
61
61
  options = { id: options };
62
62
  }
63
63
 
64
- let indexes = Arr.filterIndex(Dom.events, {
64
+ let indexes = Arr.filterIndex(Dom.$events, {
65
65
  el, event, selector, options
66
66
  });
67
67
 
@@ -74,10 +74,10 @@ export class PicoDomEventInstance
74
74
  };
75
75
 
76
76
  Arr.each(indexes.reverse(), (index) => {
77
- el.removeEventListener(...args(Dom.events[index]));
77
+ el.removeEventListener(...args(Dom.$events[index]));
78
78
  });
79
79
 
80
- Arr.splices(Dom.events, indexes);
80
+ Arr.splices(Dom.$events, indexes);
81
81
 
82
82
  return this;
83
83
  }
@@ -142,6 +142,27 @@ export class PicoDomEventInstance
142
142
  return this;
143
143
  }
144
144
 
145
+ /**
146
+ * Stop listening to event with specific options
147
+ *
148
+ * @example Dom.find("div").optoff({ id: "my-id" })
149
+ *
150
+ * @param {any} [options] Listener options
151
+ * @returns {PicoDom} Current instance
152
+ */
153
+ optoff(options = {})
154
+ {
155
+ Run.idle(() => {
156
+ Arr.filterRemove(Dom.$events, { options });
157
+ });
158
+
159
+ this.each((el) => {
160
+ el && el.removeAllListeners();
161
+ });
162
+
163
+ return this;
164
+ }
165
+
145
166
  /**
146
167
  * Listen to event once
147
168
  *
@@ -1,4 +1,4 @@
1
- import { Run, Mix, Obj, Dom } from "../index.esm.js";
1
+ import { Run, Mix, Obj, Dom, Hash } from "../index.esm.js";
2
2
  import { PicoDom } from "../utils/Dom.js";
3
3
 
4
4
  /**
@@ -60,18 +60,18 @@ export class PicoDomGlobalStatic
60
60
  */
61
61
  static ready(cb, delay = 0, limit = 6000)
62
62
  {
63
+ let [id, el, event] = [
64
+ Hash.make(), Dom.find(document), 'DOMContentLoaded'
65
+ ];
66
+
63
67
  let fn = () => {
64
- Run.delay(cb, delay);
68
+ (Run.delay(cb, delay), el.off(event, null, { id }));
65
69
  };
66
70
 
67
- let [el, event] = [
68
- Dom.find(document), 'DOMContentLoaded'
69
- ];
70
-
71
71
  let ready = Dom.isDomReady;
72
72
 
73
73
  if ( ! ready() ) {
74
- return (el.on(event, fn), this);
74
+ return (el.on(event, fn, { id }), this);
75
75
  }
76
76
 
77
77
  Run.wait(() => {
@@ -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
 
@@ -231,25 +231,48 @@ export class PicoArray
231
231
  */
232
232
  static each(value, cb, retval = null)
233
233
  {
234
- let [isArr, keys] = [
235
- Mix.isArr(value), Mix.keys(value)
236
- ];
234
+ if ( Mix.isObj(value) ) {
235
+ return this.eachObj(value, cb, retval);
236
+ }
237
237
 
238
- let fn = (key) => {
239
- return isArr ? parseInt(key) : key;
240
- };
238
+ if ( value == null ) {
239
+ value = [];
240
+ }
241
241
 
242
- let result = new Array(keys.length);
242
+ let result = new Array(value.length);
243
243
 
244
- for (let i = 0; i < keys.length; i++) {
245
- result[i] = cb(value[keys[i]], fn(keys[i]));
244
+ for ( let i = 0; i < value.length; i ++ ) {
245
+ result[i] = cb(value[i], i);
246
246
  }
247
247
 
248
- if ( retval != null ) {
249
- return retval;
248
+ return retval != null ? retval : result;
249
+ }
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
+ */
261
+ static eachObj(value, cb, retval = null)
262
+ {
263
+ if ( Mix.isArr(value) ) {
264
+ return this.each(value, cb, retval);
250
265
  }
251
266
 
252
- return result;
267
+ if ( value == null ) {
268
+ value = {};
269
+ }
270
+
271
+ let result = Mix.keys(value).map((key) => {
272
+ return cb(value[key], key);
273
+ });
274
+
275
+ return retval != null ? retval : result;
253
276
  }
254
277
 
255
278
  /**
@@ -291,17 +314,32 @@ export class PicoArray
291
314
  */
292
315
  static recursive(value, key, cb, cascade = [])
293
316
  {
294
- if ( value == null ) {
295
- 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));
296
324
  }
297
325
 
298
- return this.map(value, (item) => {
299
- return (this.recursive(item[key], key, cb, [
300
- ...cascade, value
301
- ]), cb(item, cascade));
302
- });
326
+ if ( Mix.isObj(value) ) {
327
+ value = cb(value, cascade) ?? value;
328
+ }
329
+
330
+ cascade = [
331
+ ...this.clone(cascade), value
332
+ ];
333
+
334
+ if ( Mix.isObj(value[key]) ) {
335
+ value[key] = fn(cascade)(value[key]);
336
+ }
337
+
338
+ if ( Mix.isArr(value[key]) ) {
339
+ value[key] = this.map(value[key], fn(cascade));
340
+ }
303
341
 
304
- // [{childs: [{ childs: [] } ] }, { childs: [] } ] }]
342
+ return value;
305
343
  }
306
344
 
307
345
  /**
@@ -315,6 +353,10 @@ export class PicoArray
315
353
  */
316
354
  static filterIndex(value, filter = null)
317
355
  {
356
+ if ( value == null ) {
357
+ return [];
358
+ }
359
+
318
360
  if ( filter == null ) {
319
361
  filter = (val) => !Mix.isEmpty(val);
320
362
  }
@@ -333,6 +375,30 @@ export class PicoArray
333
375
  });
334
376
  }
335
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
+ */
387
+ static filterRemove(value, filter = null)
388
+ {
389
+ if ( value == null ) {
390
+ return value;
391
+ }
392
+
393
+ let indexes = this.filterIndex(value, filter);
394
+
395
+ if ( indexes.length === 0 ) {
396
+ return value;
397
+ }
398
+
399
+ return this.splices(value, indexes);
400
+ }
401
+
336
402
  /**
337
403
  * Filter values by filter
338
404
  *
@@ -344,6 +410,10 @@ export class PicoArray
344
410
  */
345
411
  static filter(value, filter = null)
346
412
  {
413
+ if ( value == null ) {
414
+ return [];
415
+ }
416
+
347
417
  if ( filter == null ) {
348
418
  filter = (val) => !Mix.isEmpty(val);
349
419
  }
@@ -374,6 +444,10 @@ export class PicoArray
374
444
  */
375
445
  static findIndex(value, filter = null, fallback = - 1)
376
446
  {
447
+ if ( value == null ) {
448
+ return fallback;
449
+ }
450
+
377
451
  if ( filter == null ) {
378
452
  filter = (val) => !Mix.isEmpty(val);
379
453
  }
@@ -476,8 +550,12 @@ export class PicoArray
476
550
  */
477
551
  static sortDeep(value, key)
478
552
  {
553
+ let fn = (val) => {
554
+ return Obj.get(val, key);
555
+ }
556
+
479
557
  let keys = Mix.keys(value).sort((a, b) => {
480
- return Mix.compare(Obj.get(a, key), Obj.get(b, key));
558
+ return Mix.compare(fn(value[a]), fn(value[b]));
481
559
  });
482
560
 
483
561
  let result = [];
@@ -570,7 +648,7 @@ export class PicoArray
570
648
  finder = target;
571
649
  }
572
650
 
573
- if ( this.findIndex(value, finder) !== -1 ) {
651
+ if ( this.findIndex(value, finder) !== - 1 ) {
574
652
  return value;
575
653
  }
576
654
 
@@ -595,7 +673,7 @@ export class PicoArray
595
673
 
596
674
  let index = this.findIndex(value, finder);
597
675
 
598
- if ( index !== -1 ) {
676
+ if ( index !== - 1 ) {
599
677
  this.splice(value, index, 1);
600
678
  }
601
679
 
@@ -620,7 +698,7 @@ export class PicoArray
620
698
 
621
699
  let index = this.findIndex(value, finder);
622
700
 
623
- if ( index === -1 ) {
701
+ if ( index === - 1 ) {
624
702
  return value;
625
703
  }
626
704
 
@@ -646,7 +724,7 @@ export class PicoArray
646
724
 
647
725
  let index = this.findIndex(value, finder);
648
726
 
649
- if ( index === -1 ) {
727
+ if ( index === - 1 ) {
650
728
  return (value.push(target), value);
651
729
  }
652
730
 
@@ -736,7 +814,7 @@ export class PicoArray
736
814
  return Obj.clone(value);
737
815
  }
738
816
 
739
- if ( ! Mix.isArr(value) ) {
817
+ if ( !Mix.isArr(value) ) {
740
818
  return value;
741
819
  }
742
820
 
@@ -850,7 +928,7 @@ export class PicoArray
850
928
  return Obj.includes(value, search);
851
929
  }
852
930
 
853
- if ( ! Mix.isArr(search) ) {
931
+ if ( !Mix.isArr(search) ) {
854
932
  return value === search;
855
933
  }
856
934
 
@@ -862,7 +940,7 @@ export class PicoArray
862
940
  return true;
863
941
  }
864
942
 
865
- for ( let i = 0; result === false && i < length; i++) {
943
+ for ( let i = 0; result === false && i < length; i ++ ) {
866
944
  result ||= this.has(value, search[i]);
867
945
  }
868
946
 
@@ -883,7 +961,7 @@ export class PicoArray
883
961
  let result = true;
884
962
 
885
963
  for ( let key of Mix.vals(val) ) {
886
- result &&= Mix.vals(arr).indexOf(key) !== -1;
964
+ result &&= Mix.vals(arr).indexOf(key) !== - 1;
887
965
  }
888
966
 
889
967
  return result;
@@ -904,7 +982,7 @@ export class PicoArray
904
982
  return Obj.matches(value, search);
905
983
  }
906
984
 
907
- if ( ! Mix.isArr(value) ) {
985
+ if ( !Mix.isArr(value) ) {
908
986
  return value === search;
909
987
  }
910
988
 
@@ -918,7 +996,7 @@ export class PicoArray
918
996
  return false;
919
997
  }
920
998
 
921
- for ( let i = 0; result === true && i < length; i++) {
999
+ for ( let i = 0; result === true && i < length; i ++ ) {
922
1000
  result &&= this.has(value, search[i]);
923
1001
  }
924
1002
 
@@ -930,57 +1008,57 @@ export class PicoArray
930
1008
  /**
931
1009
  * @see PicoArray.unset
932
1010
  */
933
- PicoArray.removeIndex = (...args) => {
1011
+ PicoArray.removeIndex = function (...args) {
934
1012
  console.warn('Arr.removeIndex() is deprecated, use Arr.unset() instead.');
935
- return Arr.unset(...args);
1013
+ return this.unset(...args);
936
1014
  };
937
1015
 
938
1016
  /**
939
1017
  * @see PicoArray.sortPrim
940
1018
  */
941
- PicoArray.sortString = (...args) => {
1019
+ PicoArray.sortString = function (...args) {
942
1020
  console.warn('Arr.sortString() is deprecated, use Arr.sortPrim() instead.');
943
- return Arr.sortPrim(...args);
1021
+ return this.sortPrim(...args);
944
1022
  };
945
1023
 
946
1024
  /**
947
1025
  * @see PicoArray.append
948
1026
  */
949
- PicoArray.push = (...args) => {
1027
+ PicoArray.push = function (...args) {
950
1028
  console.warn('Arr.push() is deprecated, use Arr.append() instead.');
951
- return Arr.append(...args);
1029
+ return this.append(...args);
952
1030
  };
953
1031
 
954
1032
  /**
955
1033
  * @see PicoArray.merge
956
1034
  */
957
- PicoArray.concat = (...args) => {
1035
+ PicoArray.concat = function (...args) {
958
1036
  console.warn('Arr.concat() is deprecated, use Arr.merge() instead.');
959
- return Arr.merge(...args);
1037
+ return this.merge(...args);
960
1038
  };
961
1039
 
962
1040
  /**
963
1041
  * @see PicoArray.matches
964
1042
  */
965
- PicoArray.equal = (...args) => {
1043
+ PicoArray.equal = function (...args) {
966
1044
  console.warn('Arr.equal() is deprecated, use Arr.matches() instead.');
967
- return Arr.matches(...args);
1045
+ return this.matches(...args);
968
1046
  };
969
1047
 
970
1048
  /**
971
1049
  * @see PicoArray.diff
972
1050
  */
973
- PicoArray.diffrence = (...args) => {
1051
+ PicoArray.diffrence = function (...args) {
974
1052
  console.warn('Arr.diffrence() is deprecated, use Arr.diff() instead.');
975
- return Arr.diff(...args);
1053
+ return this.diff(...args);
976
1054
  };
977
1055
 
978
1056
  /**
979
1057
  * @see PicoArray.isect
980
1058
  */
981
- PicoArray.intersect = (...args) => {
1059
+ PicoArray.intersect = function (...args) {
982
1060
  console.warn('Arr.intersect() is deprecated, use Arr.isect() instead.');
983
- return Arr.isect(...args);
1061
+ return this.isect(...args);
984
1062
  };
985
1063
 
986
1064
  export default PicoArray;
@@ -134,7 +134,7 @@ export class PicoCookie
134
134
  this.$cookie[key] = value;
135
135
  }
136
136
 
137
- let plain = key + '=' + value.replaceAll(...[
137
+ let plain = key + '=' + Mix.str(value).replaceAll(...[
138
138
  ';', '{:}'
139
139
  ]);
140
140
 
@@ -279,7 +279,7 @@ export class PicoMixed
279
279
  return false;
280
280
  }
281
281
 
282
- return Object.getPrototypeOf(value) === Object.prototype;
282
+ return typeof value === 'object' && value.constructor === Object;
283
283
  }
284
284
 
285
285
  /**
@@ -302,7 +302,7 @@ export class PicoMixed
302
302
  return false;
303
303
  }
304
304
 
305
- return Object.getPrototypeOf(value) === Array.prototype;
305
+ return typeof value === 'object' && value.constructor === Array;
306
306
  }
307
307
 
308
308
  /**
@@ -305,13 +305,11 @@ export class PicoObject
305
305
  */
306
306
  static map(value, cb)
307
307
  {
308
- let result = {};
309
-
310
308
  for ( let key of Mix.keys(value) ) {
311
- result[key] = cb(value[key], key);
309
+ value[key] = cb(value[key], key);
312
310
  }
313
311
 
314
- return result;
312
+ return value;
315
313
  }
316
314
 
317
315
  /**
@@ -610,6 +608,56 @@ export class PicoObject
610
608
  return result;
611
609
  }
612
610
 
611
+ static sort(obj, key)
612
+ {
613
+ let keys = Mix.keys(obj);
614
+
615
+ if ( Mix.isFunction(key) ) {
616
+ keys = keys.sort((a, b) => {
617
+ return key.call({}, obj[a], obj[b]);
618
+ });
619
+ }
620
+
621
+ if ( !Mix.isFunction(key) ) {
622
+ keys = keys.sort((a, b) => {
623
+ return Mix.integer(this.get(obj[a], key)) - Mix.integer(this.get(obj[b], key));
624
+ })
625
+ }
626
+
627
+ let result = [];
628
+
629
+ Arr.each(keys, (key, index) => {
630
+ obj[key]['_key'] = key;
631
+ result[index] = obj[key];
632
+ });
633
+
634
+ return result;
635
+ }
636
+
637
+ static sortString(obj, key)
638
+ {
639
+ let keys = Mix.keys(obj);
640
+
641
+ if ( !Mix.isFunction(key) ) {
642
+ keys = keys.sort((a, b) => {
643
+
644
+ let va = Mix.string(this.get(obj[a], key)).toLowerCase();
645
+ let vb = Mix.string(this.get(obj[b], key)).toLowerCase();
646
+
647
+ return (va < vb) ? - 1 : (va > vb) ? 1 : 0;
648
+ })
649
+ }
650
+
651
+ let result = [];
652
+
653
+ Arr.each(keys, (key, index) => {
654
+ obj[key]['_key'] = key;
655
+ result[index] = obj[key];
656
+ });
657
+
658
+ return result;
659
+ }
660
+
613
661
  }
614
662
 
615
663
  /**
@@ -636,20 +684,20 @@ PicoObject.findIndex = (...args) => {
636
684
  return Arr.findIndex(...args);
637
685
  };
638
686
 
639
- /**
640
- * @see PicoArray.sort
641
- */
642
- PicoObject.sort = (...args) => {
643
- console.warn('Obj.sort() is deprecated, use Arr.sort() instead.');
644
- return Arr.sort(...args);
645
- };
646
-
647
- /**
648
- * @see PicoArray.sortDeep
649
- */
650
- PicoObject.sortString = (...args) => {
651
- console.warn('Obj.sortString() is deprecated, use Arr.sortDeep() instead.');
652
- return Arr.sortDeep(...args);
653
- };
687
+ // /**
688
+ // * @see PicoArray.sort
689
+ // */
690
+ // PicoObject.sort = (...args) => {
691
+ // console.warn('Obj.sort() is deprecated, use Arr.sort() instead.');
692
+ // return Arr.sort(...args);
693
+ // };
694
+ //
695
+ // /**
696
+ // * @see PicoArray.sortDeep
697
+ // */
698
+ // PicoObject.sortString = (...args) => {
699
+ // console.warn('Obj.sortString() is deprecated, use Arr.sortDeep() instead.');
700
+ // return Arr.sortDeep(...args);
701
+ // };
654
702
 
655
703
  export default PicoObject
@@ -38,10 +38,12 @@ export class PicoRoute
38
38
  route = this.$routes[key] || key
39
39
  }
40
40
 
41
- route = Locale.replace(route, values);
41
+ Obj.each(values, (val, key) => {
42
+ route = route.replace(new RegExp('{' + key + '}', 'g'), val);
43
+ });
42
44
 
43
45
  if ( ! Mix.isEmpty(query) ) {
44
- route += '?' + For.castParams(values);
46
+ route += '?' + For.castParams(query);
45
47
  }
46
48
 
47
49
  return route;