@kizmann/pico-js 2.0.3 → 2.0.5

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.
@@ -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;
@@ -1,169 +1,147 @@
1
- import { Arr, Hash, Mix } from "../index.esm.js";
1
+ import { Arr, Hash, Mix, Obj } from "../index.esm.js";
2
2
 
3
3
  export class PicoRunner
4
4
  {
5
- static $idler = {
6
- native: {}, debounce: {}, throttle: {}
7
- };
5
+ static $idler = null;
8
6
 
9
- static $timer = {
10
- date: 0, func: null
11
- };
7
+ static $timer = 0;
12
8
 
13
9
  static $buffer = [];
14
10
 
15
11
  /**
16
- * Run callback after delay (id)
12
+ * Clear timer or call function
17
13
  *
18
- * @example Run.timeout(() => {}, 100) // => "t-..."
14
+ * @example Run.clear(timer)
19
15
  *
20
- * @param {function} fn Callback to run
21
- * @param {number} [delay] Delay ms
22
- * @param {string|null} [index] Timer id
23
- * @returns {string} Timer id
16
+ * @param {any} timer Timer ID, array of IDs, or function
17
+ * @returns {typeof PicoRunner} Static class
24
18
  */
25
- static timeout(fn, delay = 0, index = null)
19
+ static clear(timer)
26
20
  {
27
- let idler = PicoRunner.$idler.native;
21
+ if ( Mix.isArr(timer) ) {
22
+ return Arr.each(timer, (t) => this.clear(t), this);
23
+ }
28
24
 
29
- if ( index == null ) {
30
- index = Hash.make(12);
25
+ if ( Mix.isFunc(timer) ) {
26
+ return (timer(), this);
31
27
  }
32
28
 
33
- idler[(index = 't-' + index)] = setInterval(() => {
34
- fn();
35
- }, delay);
29
+ clearTimeout(timer);
30
+ clearInterval(timer);
36
31
 
37
- return index;
32
+ return this;
38
33
  }
39
34
 
40
35
  /**
41
- * Run callback on interval (id)
42
- *
43
- * @example Run.interval(() => {}, 250) // => "i-..."
36
+ * Request idle callback with fallback
44
37
  *
45
- * @param {function} fn Callback to run
46
- * @param {number} [intval] Interval ms
47
- * @param {string|null} [index] Timer id
48
- * @returns {string} Timer id
38
+ * @param {function} cb Callback function
39
+ * @returns {typeof PicoRunner} Static class
49
40
  */
50
- static interval(fn, intval = 0, index = null)
41
+ static tryin(cb)
51
42
  {
52
- let idler = PicoRunner.$idler.native;
53
-
54
- if ( index == null ) {
55
- index = Hash.make(12);
43
+ try {
44
+ requestIdleCallback(cb);
45
+ } catch (e) {
46
+ //
56
47
  }
57
48
 
58
- idler[(index = 'i-' + index)] = setInterval(() => {
59
- fn();
60
- }, intval);
61
-
62
- return index;
49
+ return this;
63
50
  }
64
51
 
65
52
  /**
66
- * Clear timer(s) by id
53
+ * Wait for condition to be true
67
54
  *
68
- * @example Run.clear("i-abc") // => Run
69
- * @example Run.clear(["t-a","i-b"]) // => Run
55
+ * @example Run.wait(() => window.foo, 10, 100)
70
56
  *
71
- * @param {string|Array<string>} index Timer id(s)
72
- * @param {string} [scope] Idler scope key
73
- * @returns {typeof PicoRunner} Runner class
57
+ * @param {function} fn Condition function
58
+ * @param {number} [intval] Interval ms
59
+ * @param {number} [limit] Max iterations
60
+ * @returns {function} Clear function
74
61
  */
75
- static clear(index, scope = 'native')
62
+ static wait(fn, intval = 0, limit = 500)
76
63
  {
77
- if ( Mix.isArr(index) ) {
78
- return (Arr.each(index, (e) => this.clear(e, scope)), this);
79
- }
80
-
81
- let idler = PicoRunner.$idler[scope];
64
+ let idler, timer;
82
65
 
83
- if ( /^i-/.test(index) === false ) {
84
- clearInterval(idler[index]);
85
- }
66
+ timer = setTimeout(() => {
67
+ this.clear([idler, timer]);
68
+ }, intval * limit);
86
69
 
87
- if ( /^t-/.test(index) === false ) {
88
- clearTimeout(idler[index]);
89
- }
70
+ idler = setInterval(() => {
71
+ if ( fn() ) this.clear([timer, idler]);
72
+ }, intval);
90
73
 
91
- return this;
74
+ return () => clearInterval(idler);
92
75
  }
93
76
 
94
77
  /**
95
- * Poll until callback is true
78
+ * Run function in next animation frame
96
79
  *
97
- * @example Run.wait(() => ready, 50) // polls
80
+ * @example Run.frame(cb)
98
81
  *
99
- * @param {function} fn Condition callback
100
- * @param {number} [intval] Poll interval ms
101
- * @param {number} [limit] Max poll count
102
- * @returns {void} No return value
82
+ * @param {function} fn Callback function
83
+ * @param {any} [...args] Callback arguments
84
+ * @returns {function} Noop clear function
103
85
  */
104
- static wait(fn, intval = 0, limit = 500)
86
+ static frame(fn, ...args)
105
87
  {
106
- let idler, timer;
107
-
108
- timer = this.timeout(() => {
109
- this.clear([idler, timer]);
110
- }, intval * limit);
88
+ requestAnimationFrame(() => {
89
+ fn(...args)
90
+ });
111
91
 
112
- idler = this.interval(() => {
113
- if ( fn() ) this.clear([idler, timer]);
114
- }, intval);
92
+ return () => null;
115
93
  }
116
94
 
117
95
  /**
118
- * Run callback in next frame
96
+ * Run function when browser is idle
119
97
  *
120
- * @example Run.frame(() => {}) // => Run
98
+ * @example Run.idle(cb)
121
99
  *
122
- * @param {function} fn Callback to run
123
- * @param {...any} args Callback args
124
- * @returns {typeof PicoRunner} Runner class
100
+ * @param {function} fn Callback function
101
+ * @param {any} [...args] Callback arguments
102
+ * @returns {function} Noop clear function
125
103
  */
126
- static frame(fn, ...args)
104
+ static idle(fn, ...args)
127
105
  {
128
- requestAnimationFrame(function() {
129
- fn(...args);
106
+ requestIdleCallback(() => {
107
+ fn(...args)
130
108
  });
131
109
 
132
- return this;
110
+ return () => null;
133
111
  }
134
112
 
135
113
  /**
136
- * Run callback async soon
114
+ * Run function asynchronously
137
115
  *
138
- * @example Run.async(() => {}) // => Run
116
+ * @example Run.async(cb)
139
117
  *
140
- * @param {function} fn Callback to run
141
- * @param {...any} args Callback args
142
- * @returns {typeof PicoRunner} Runner class
118
+ * @param {function} fn Callback function
119
+ * @param {any} [...args] Callback arguments
120
+ * @returns {function} Noop clear function
143
121
  */
144
122
  static async(fn, ...args)
145
123
  {
146
124
  setTimeout(() => {
147
- fn(...args);
125
+ fn(...args)
148
126
  });
149
127
 
150
- return this;
128
+ return () => null;
151
129
  }
152
130
 
153
131
  /**
154
- * Run callback after delay
132
+ * Run function after delay
155
133
  *
156
- * @example const cancel = Run.delay(() => {}, 50)
134
+ * @example Run.delay(cb, 100)
157
135
  *
158
- * @param {function} fn Callback to run
136
+ * @param {function} fn Callback function
159
137
  * @param {number} [delay] Delay ms
160
- * @param {...any} args Callback args
161
- * @returns {function} Cancel function
138
+ * @param {any} [...args] Callback arguments
139
+ * @returns {function} Clear function
162
140
  */
163
141
  static delay(fn, delay = 0, ...args)
164
142
  {
165
143
  let idler = setTimeout(() => {
166
- this.async(fn, ...args);
144
+ fn(...args);
167
145
  }, delay);
168
146
 
169
147
  return () => clearTimeout(idler);
@@ -176,24 +154,19 @@ export class PicoRunner
176
154
  *
177
155
  * @param {function} cb Callback to run
178
156
  * @param {number} [timeout] Wait ms
179
- * @param {string|null} [index] Debounce id
180
157
  * @returns {function} Debounced fn
181
158
  */
182
- static debounce(cb, timeout = 100, index = null)
159
+ static debounce(cb, timeout = 100)
183
160
  {
184
- let idler = PicoRunner.$idler.debounce;
185
-
186
- if ( index == null ) {
187
- index = Hash.make(12);
188
- }
161
+ let idler = null;
189
162
 
190
163
  return (...args) => {
191
164
 
192
- if ( idler[index] ) {
193
- clearTimeout(idler[index]);
165
+ if ( idler ) {
166
+ clearTimeout(idler);
194
167
  }
195
168
 
196
- idler[index] = setTimeout(() => {
169
+ idler = setTimeout(() => {
197
170
  this.frame(cb, ...args);
198
171
  }, timeout);
199
172
  };
@@ -206,24 +179,19 @@ export class PicoRunner
206
179
  *
207
180
  * @param {function} cb Callback to run
208
181
  * @param {number} [timeout] Wait ms
209
- * @param {string|null} [index] Throttle id
210
182
  * @returns {function} Throttled fn
211
183
  */
212
- static throttle(cb, timeout = 100, index = null)
184
+ static throttle(cb, timeout = 100)
213
185
  {
214
- let queued, idler = PicoRunner.$idler.throttle;
215
-
216
- if ( index == null ) {
217
- index = Hash.make(12);
218
- }
186
+ let queued, idler = null;
219
187
 
220
188
  return (...args) => {
221
189
 
222
- if ( idler[index] ) {
223
- clearTimeout(idler[index]);
190
+ if ( idler ) {
191
+ clearTimeout(idler);
224
192
  }
225
193
 
226
- idler[index] = setTimeout(() => {
194
+ idler = setTimeout(() => {
227
195
  queued = false;
228
196
  }, timeout);
229
197
 
@@ -231,7 +199,7 @@ export class PicoRunner
231
199
  return;
232
200
  }
233
201
 
234
- (this.frame(cb, ...args), queued = true)
202
+ (this.frame(cb, ...args), queued = true);
235
203
  };
236
204
  }
237
205
 
@@ -289,23 +257,23 @@ export class PicoRunner
289
257
  */
290
258
  static runFramebuffer()
291
259
  {
292
- if ( this.$timer.func ) {
293
- this.$timer.func();
260
+ if ( this.$idler ) {
261
+ clearTimeout(this.$idler);
294
262
  }
295
263
 
296
- this.$timer.func = this.delay(() => {
264
+ this.$idler = setTimeout(() => {
297
265
  this.runFramebuffer();
298
266
  }, 50);
299
267
 
300
- if ( Date.now() - this.$timer.date <= 50 ) {
268
+ if ( Date.now() - this.$timer <= 50 ) {
301
269
  return;
302
270
  }
303
271
 
304
- if ( this.$timer.func ) {
305
- this.$timer.func();
272
+ if ( this.$idler ) {
273
+ clearTimeout(this.$idler);
306
274
  }
307
275
 
308
- this.$timer.date = Date.now();
276
+ this.$timer = Date.now();
309
277
 
310
278
  let buffer = Arr.filter(this.$buffer, {
311
279
  active: true
@@ -2,7 +2,7 @@
2
2
  * @memberof PicoDom
3
3
  */
4
4
  export class PicoDomEventStatic {
5
- static events: any[];
5
+ static $events: any[];
6
6
  }
7
7
  /**
8
8
  * @memberof PicoDom
@@ -59,6 +59,15 @@ export class PicoDomEventInstance {
59
59
  * @returns {PicoDom} Current instance
60
60
  */
61
61
  off(event: any, selector?: string, options?: any): PicoDom;
62
+ /**
63
+ * Stop listening to event with specific options
64
+ *
65
+ * @example Dom.find("div").optoff({ id: "my-id" })
66
+ *
67
+ * @param {any} [options] Listener options
68
+ * @returns {PicoDom} Current instance
69
+ */
70
+ optoff(options?: any): PicoDom;
62
71
  /**
63
72
  * Listen to event once
64
73
  *
@@ -1,3 +1,8 @@
1
+ /**
2
+ * @type {typeof PicoMixed}
3
+ */
4
+ export const Any: typeof PicoMixed;
5
+ export function UUID(): string;
1
6
  /**
2
7
  * @type {typeof PicoDom}
3
8
  */
@@ -10,51 +15,16 @@ export const Now: typeof PicoNow;
10
15
  * @type {typeof PicoFormat}
11
16
  */
12
17
  export const For: typeof PicoFormat;
13
- /**
14
- * @type {typeof PicoRunner}
15
- */
16
- export const Run: typeof PicoRunner;
17
- /**
18
- * @type {typeof PicoString}
19
- */
20
- export const Str: typeof PicoString;
21
- /**
22
- * @type {typeof PicoNumber}
23
- */
24
- export const Num: typeof PicoNumber;
25
- /**
26
- * @type {typeof PicoArray}
27
- */
28
- export const Arr: typeof PicoArray;
29
- /**
30
- * @type {typeof PicoObject}
31
- */
32
- export const Obj: typeof PicoObject;
33
- /**
34
- * @type {typeof PicoMixed}
35
- */
36
- export const Mix: typeof PicoMixed;
37
- /**
38
- * @type {typeof PicoHash}
39
- */
40
- export const Hash: typeof PicoHash;
41
- /**
42
- * @type {typeof PicoEvent}
43
- */
44
- export const Event: typeof PicoEvent;
45
- /**
46
- * @type {typeof PicoLocale}
47
- */
48
- export const Locale: typeof PicoLocale;
49
- /**
50
- * @type {typeof PicoCookie}
51
- */
52
- export const Cookie: typeof PicoCookie;
53
- /**
54
- * @type {typeof PicoMixed}
55
- */
56
- export const Any: typeof PicoMixed;
57
- export function UUID(): string;
18
+ import { default as Run } from "./utils/Runner.js";
19
+ import { default as Str } from "./utils/String.js";
20
+ import { default as Num } from "./utils/Number.js";
21
+ import { default as Arr } from "./utils/Array.js";
22
+ import { default as Obj } from "./utils/Object.js";
23
+ import { default as Mix } from "./utils/Mixed.js";
24
+ import { default as Hash } from "./utils/Hash.js";
25
+ import { default as Event } from "./utils/Event.js";
26
+ import { default as Locale } from "./utils/Locale.js";
27
+ import { default as Cookie } from "./utils/Cookie.js";
58
28
  import { go } from "./tool/scope.js";
59
29
  import { browser } from "./tool/scope.js";
60
30
  import { device } from "./tool/scope.js";
@@ -62,17 +32,8 @@ import { default as Map } from "./wip/Map.js";
62
32
  import { default as Data } from "./utils/Data.js";
63
33
  import { default as Route } from "./utils/Route.js";
64
34
  import { default as Element } from "./wip/Element.js";
35
+ import { PicoMixed } from "./utils/Mixed.js";
65
36
  import { PicoDom } from "./utils/Dom.js";
66
37
  import { PicoNow } from "./utils/Now.js";
67
38
  import { PicoFormat } from "./utils/Format.js";
68
- import { default as PicoRunner } from "./utils/Runner.js";
69
- import { default as PicoString } from "./utils/String.js";
70
- import { default as PicoNumber } from "./utils/Number.js";
71
- import { default as PicoArray } from "./utils/Array.js";
72
- import { default as PicoObject } from "./utils/Object.js";
73
- import { default as PicoMixed } from "./utils/Mixed.js";
74
- import { default as PicoHash } from "./utils/Hash.js";
75
- import { default as PicoEvent } from "./utils/Event.js";
76
- import { default as PicoLocale } from "./utils/Locale.js";
77
- import { default as PicoCookie } from "./utils/Cookie.js";
78
- export { go, browser, device, Map, Data, Route, Element };
39
+ export { Run, Str, Num, Arr, Obj, Mix, Hash, Event, Locale, Cookie, go, browser, device, Map, Data, Route, Element };
@@ -179,6 +179,70 @@ export class PicoNowDefaultInstance {
179
179
  * @returns {PicoNow} Current instance
180
180
  */
181
181
  applyTime(value: any, format?: string): PicoNow;
182
+ /**
183
+ * @see PicoNow.add
184
+ */
185
+ addSecond(value: any): PicoNow;
186
+ /**
187
+ * @see PicoNow.sub
188
+ */
189
+ subSecond(value: any): PicoNow;
190
+ /**
191
+ * @see PicoNow.add
192
+ */
193
+ addMinute(value: any): PicoNow;
194
+ /**
195
+ * @see PicoNow.sub
196
+ */
197
+ subMinute(value: any): PicoNow;
198
+ /**
199
+ * @see PicoNow.add
200
+ */
201
+ addHour(value: any): PicoNow;
202
+ /**
203
+ * @see PicoNow.sub
204
+ */
205
+ subHour(value: any): PicoNow;
206
+ /**
207
+ * @see PicoNow.add
208
+ */
209
+ addDates(value: any): PicoNow;
210
+ /**
211
+ * @see PicoNow.sub
212
+ */
213
+ subDates(value: any): PicoNow;
214
+ /**
215
+ * @see PicoNow.add
216
+ */
217
+ addMonths(value: any): PicoNow;
218
+ /**
219
+ * @see PicoNow.sub
220
+ */
221
+ subMonths(value: any): PicoNow;
222
+ /**
223
+ * @see PicoNow.add
224
+ */
225
+ addYears(value: any): PicoNow;
226
+ /**
227
+ * @see PicoNow.sub
228
+ */
229
+ subYears(value: any): PicoNow;
230
+ /**
231
+ * @see PicoNow.add
232
+ */
233
+ addDecades(value: any): PicoNow;
234
+ /**
235
+ * @see PicoNow.sub
236
+ */
237
+ subDecades(value: any): PicoNow;
238
+ /**
239
+ * @see PicoNow.day
240
+ */
241
+ humanDay(): number | PicoNow;
242
+ /**
243
+ * @see PicoNow.month
244
+ */
245
+ humanMonth(): number | PicoNow;
182
246
  }
183
247
  export function PicoNowDefaultPlugin(self: typeof PicoNow): typeof PicoNow;
184
248
  import { PicoNow } from "../utils/Now.js";