@kizmann/pico-js 1.0.13 → 2.0.0

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.
Files changed (125) hide show
  1. package/README.md +27 -7
  2. package/dist/pico-js.browser.js +2 -0
  3. package/dist/pico-js.browser.js.map +1 -0
  4. package/dist/pico-js.esm.js +2 -0
  5. package/dist/pico-js.esm.js.map +1 -0
  6. package/package.json +20 -7
  7. package/src/dom/DomAttribute.js +374 -0
  8. package/src/dom/DomBuilder.js +152 -0
  9. package/src/dom/DomEvent.js +253 -0
  10. package/src/dom/DomFinder.js +669 -0
  11. package/src/dom/DomForm.js +57 -0
  12. package/src/dom/DomGlobal.js +193 -0
  13. package/src/dom/DomInview.js +332 -0
  14. package/src/dom/DomMeta.js +66 -0
  15. package/src/dom/DomObserver.js +57 -0
  16. package/src/dom/DomRectangle.js +657 -0
  17. package/src/format/FormatFile.js +54 -0
  18. package/src/format/FormatOption.js +108 -0
  19. package/src/format/FormatParam.js +107 -0
  20. package/src/format/FormatParser.js +156 -0
  21. package/src/format/FormatUrl.js +75 -0
  22. package/src/index.browser.js +10 -0
  23. package/src/index.esm.js +138 -0
  24. package/src/now/NowDefault.js +533 -0
  25. package/src/now/NowFormat.js +196 -0
  26. package/src/now/NowGrid.js +251 -0
  27. package/src/now/NowHuman.js +118 -0
  28. package/src/now/NowMatch.js +175 -0
  29. package/src/now/NowRange.js +70 -0
  30. package/src/now/NowWalker.js +544 -0
  31. package/src/tool/scope.js +103 -0
  32. package/src/utils/Array.js +986 -0
  33. package/src/utils/Cookie.js +184 -0
  34. package/src/utils/Data.js +200 -0
  35. package/src/utils/Dom.js +208 -0
  36. package/src/utils/Event.js +140 -0
  37. package/src/utils/Format.js +62 -0
  38. package/src/utils/Hash.js +164 -0
  39. package/src/utils/Locale.js +229 -0
  40. package/src/utils/Mixed.js +887 -0
  41. package/src/utils/Now.js +234 -0
  42. package/src/utils/Number.js +238 -0
  43. package/src/utils/Object.js +655 -0
  44. package/src/utils/Route.js +67 -0
  45. package/src/utils/Runner.js +327 -0
  46. package/src/utils/String.js +618 -0
  47. package/src/{library/element.js → wip/Element.js} +90 -16
  48. package/src/{library/map.js → wip/Map.js} +256 -40
  49. package/types/dom/DomAttribute.d.ts +137 -0
  50. package/types/dom/DomBuilder.d.ts +67 -0
  51. package/types/dom/DomEvent.d.ts +103 -0
  52. package/types/dom/DomFinder.d.ts +321 -0
  53. package/types/dom/DomForm.d.ts +21 -0
  54. package/types/dom/DomGlobal.d.ts +79 -0
  55. package/types/dom/DomInview.d.ts +114 -0
  56. package/types/dom/DomMeta.d.ts +29 -0
  57. package/types/dom/DomObserver.d.ts +21 -0
  58. package/types/dom/DomRectangle.d.ts +270 -0
  59. package/types/format/FormatFile.d.ts +18 -0
  60. package/types/format/FormatOption.d.ts +40 -0
  61. package/types/format/FormatParam.d.ts +39 -0
  62. package/types/format/FormatParser.d.ts +46 -0
  63. package/types/format/FormatUrl.d.ts +17 -0
  64. package/types/index.browser.d.ts +1 -0
  65. package/types/index.esm.d.ts +52 -0
  66. package/types/now/NowDefault.d.ts +183 -0
  67. package/types/now/NowFormat.d.ts +70 -0
  68. package/types/now/NowGrid.d.ts +107 -0
  69. package/types/now/NowHuman.d.ts +37 -0
  70. package/types/now/NowMatch.d.ts +108 -0
  71. package/types/now/NowRange.d.ts +21 -0
  72. package/types/now/NowWalker.d.ts +301 -0
  73. package/types/tool/scope.d.ts +24 -0
  74. package/types/utils/Array.d.ts +480 -0
  75. package/types/utils/Cookie.d.ts +60 -0
  76. package/types/utils/Data.d.ts +91 -0
  77. package/types/utils/Dom.d.ts +138 -0
  78. package/types/utils/Event.d.ts +58 -0
  79. package/types/utils/Format.d.ts +37 -0
  80. package/types/utils/Hash.d.ts +81 -0
  81. package/types/utils/Locale.d.ts +115 -0
  82. package/types/utils/Mixed.d.ts +469 -0
  83. package/types/utils/Now.d.ts +125 -0
  84. package/types/utils/Number.d.ts +127 -0
  85. package/types/utils/Object.d.ts +255 -0
  86. package/types/utils/Route.d.ts +37 -0
  87. package/types/utils/Runner.d.ts +139 -0
  88. package/types/utils/String.d.ts +330 -0
  89. package/types/wip/Element.d.ts +119 -0
  90. package/types/wip/Map.d.ts +254 -0
  91. package/dist/.ignore.js +0 -0
  92. package/dist/pico-js.js +0 -2
  93. package/dist/pico-js.js.map +0 -1
  94. package/src/element/default.js +0 -46
  95. package/src/element/example.js +0 -58
  96. package/src/index.js +0 -90
  97. package/src/library/cookie.js +0 -123
  98. package/src/library/data.js +0 -111
  99. package/src/library/event.js +0 -91
  100. package/src/library/locale.js +0 -84
  101. package/src/library/queue.js +0 -64
  102. package/src/library/route.js +0 -28
  103. package/src/utility/any.js +0 -369
  104. package/src/utility/array.js +0 -410
  105. package/src/utility/dom.js +0 -1425
  106. package/src/utility/now.js +0 -544
  107. package/src/utility/number.js +0 -128
  108. package/src/utility/object.js +0 -429
  109. package/src/utility/string.js +0 -328
  110. package/types/index.d.ts +0 -77
  111. package/types/library/cookie.d.ts +0 -10
  112. package/types/library/data.d.ts +0 -15
  113. package/types/library/element.d.ts +0 -22
  114. package/types/library/event.d.ts +0 -13
  115. package/types/library/locale.d.ts +0 -14
  116. package/types/library/map.d.ts +0 -43
  117. package/types/library/queue.d.ts +0 -18
  118. package/types/library/route.d.ts +0 -11
  119. package/types/utility/any.d.ts +0 -35
  120. package/types/utility/array.d.ts +0 -46
  121. package/types/utility/dom.d.ts +0 -101
  122. package/types/utility/now.d.ts +0 -79
  123. package/types/utility/number.d.ts +0 -17
  124. package/types/utility/object.d.ts +0 -29
  125. package/types/utility/string.d.ts +0 -26
@@ -0,0 +1,327 @@
1
+ import { Arr, Hash, Mix } from "#src/index.esm.js";
2
+
3
+ export class PicoRunner
4
+ {
5
+ static $idler = {
6
+ native: {}, debounce: {}, throttle: {}
7
+ };
8
+
9
+ static $timer = {
10
+ date: 0, func: null
11
+ };
12
+
13
+ static $buffer = [];
14
+
15
+ /**
16
+ * Run callback after delay (id)
17
+ *
18
+ * @example Run.timeout(() => {}, 100) // => "t-..."
19
+ *
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
24
+ */
25
+ static timeout(fn, delay = 0, index = null)
26
+ {
27
+ let idler = PicoRunner.$idler.native;
28
+
29
+ if ( index == null ) {
30
+ index = Hash.make(12);
31
+ }
32
+
33
+ idler[(index = 't-' + index)] = setInterval(() => {
34
+ fn();
35
+ }, delay);
36
+
37
+ return index;
38
+ }
39
+
40
+ /**
41
+ * Run callback on interval (id)
42
+ *
43
+ * @example Run.interval(() => {}, 250) // => "i-..."
44
+ *
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
49
+ */
50
+ static interval(fn, intval = 0, index = null)
51
+ {
52
+ let idler = PicoRunner.$idler.native;
53
+
54
+ if ( index == null ) {
55
+ index = Hash.make(12);
56
+ }
57
+
58
+ idler[(index = 'i-' + index)] = setInterval(() => {
59
+ fn();
60
+ }, intval);
61
+
62
+ return index;
63
+ }
64
+
65
+ /**
66
+ * Clear timer(s) by id
67
+ *
68
+ * @example Run.clear("i-abc") // => Run
69
+ * @example Run.clear(["t-a","i-b"]) // => Run
70
+ *
71
+ * @param {string|Array<string>} index Timer id(s)
72
+ * @param {string} [scope] Idler scope key
73
+ * @returns {typeof PicoRunner} Runner class
74
+ */
75
+ static clear(index, scope = 'native')
76
+ {
77
+ if ( Mix.isArr(index) ) {
78
+ return (Arr.each(index, (e) => this.clear(e, scope)), this);
79
+ }
80
+
81
+ let idler = PicoRunner.$idler[scope];
82
+
83
+ if ( /^i-/.test(index) === false ) {
84
+ clearInterval(idler[index]);
85
+ }
86
+
87
+ if ( /^t-/.test(index) === false ) {
88
+ clearTimeout(idler[index]);
89
+ }
90
+
91
+ return this;
92
+ }
93
+
94
+ /**
95
+ * Poll until callback is true
96
+ *
97
+ * @example Run.wait(() => ready, 50) // polls
98
+ *
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
103
+ */
104
+ static wait(fn, intval = 0, limit = 500)
105
+ {
106
+ let idler, timer;
107
+
108
+ timer = this.timeout(() => {
109
+ this.clear([idler, timer]);
110
+ }, intval * limit);
111
+
112
+ idler = this.interval(() => {
113
+ if ( fn() ) this.clear([idler, timer]);
114
+ }, intval);
115
+ }
116
+
117
+ /**
118
+ * Run callback in next frame
119
+ *
120
+ * @example Run.frame(() => {}) // => Run
121
+ *
122
+ * @param {function} fn Callback to run
123
+ * @param {...any} [args] Callback args
124
+ * @returns {typeof PicoRunner} Runner class
125
+ */
126
+ static frame(fn, ...args)
127
+ {
128
+ requestAnimationFrame(function() {
129
+ fn(...args);
130
+ });
131
+
132
+ return this;
133
+ }
134
+
135
+ /**
136
+ * Run callback async soon
137
+ *
138
+ * @example Run.async(() => {}) // => Run
139
+ *
140
+ * @param {function} fn Callback to run
141
+ * @param {...any} [args] Callback args
142
+ * @returns {typeof PicoRunner} Runner class
143
+ */
144
+ static async(fn, ...args)
145
+ {
146
+ setTimeout(() => {
147
+ fn(...args);
148
+ });
149
+
150
+ return this;
151
+ }
152
+
153
+ /**
154
+ * Run callback after delay
155
+ *
156
+ * @example const cancel = Run.delay(() => {}, 50)
157
+ *
158
+ * @param {function} fn Callback to run
159
+ * @param {number} [delay] Delay ms
160
+ * @param {...any} [args] Callback args
161
+ * @returns {function} Cancel function
162
+ */
163
+ static delay(fn, delay = 0, ...args)
164
+ {
165
+ let idler = setTimeout(() => {
166
+ this.async(fn, ...args);
167
+ }, delay);
168
+
169
+ return () => clearTimeout(idler);
170
+ }
171
+
172
+ /**
173
+ * Create debounced callback
174
+ *
175
+ * @example const fn = Run.debounce(cb, 100)
176
+ *
177
+ * @param {function} cb Callback to run
178
+ * @param {number} [timeout] Wait ms
179
+ * @param {string|null} [index] Debounce id
180
+ * @returns {function} Debounced fn
181
+ */
182
+ static debounce(cb, timeout = 100, index = null)
183
+ {
184
+ let idler = PicoRunner.$idler.debounce;
185
+
186
+ if ( index == null ) {
187
+ index = Hash.make(12);
188
+ }
189
+
190
+ return (...args) => {
191
+
192
+ if ( idler[index] ) {
193
+ clearTimeout(idler[index]);
194
+ }
195
+
196
+ idler[index] = setTimeout(() => {
197
+ this.frame(cb, ...args);
198
+ }, timeout);
199
+ };
200
+ }
201
+
202
+ /**
203
+ * Create throttled callback
204
+ *
205
+ * @example const fn = Run.throttle(cb, 100)
206
+ *
207
+ * @param {function} cb Callback to run
208
+ * @param {number} [timeout] Wait ms
209
+ * @param {string|null} [index] Throttle id
210
+ * @returns {function} Throttled fn
211
+ */
212
+ static throttle(cb, timeout = 100, index = null)
213
+ {
214
+ let queued, idler = PicoRunner.$idler.throttle;
215
+
216
+ if ( index == null ) {
217
+ index = Hash.make(12);
218
+ }
219
+
220
+ return (...args) => {
221
+
222
+ if ( idler[index] ) {
223
+ clearTimeout(idler[index]);
224
+ }
225
+
226
+ idler[index] = setTimeout(() => {
227
+ queued = false;
228
+ }, timeout);
229
+
230
+ if ( queued ) {
231
+ return;
232
+ }
233
+
234
+ (this.frame(cb, ...args), queued = true)
235
+ };
236
+ }
237
+
238
+ /**
239
+ * Create framerate-limited callback
240
+ *
241
+ * @example const fn = Run.framerate(cb, 30)
242
+ *
243
+ * @param {function} cb Callback to run
244
+ * @param {number} [fps] Max frames per sec
245
+ * @returns {function} Rate-limited fn
246
+ */
247
+ static framerate(cb, fps = 30)
248
+ {
249
+ let last = 0;
250
+
251
+ return (...args) => {
252
+
253
+ if ( Date.now() - last <= (1000 / fps) ) {
254
+ return;
255
+ }
256
+
257
+ (this.frame(cb, ...args), last = Date.now());
258
+ };
259
+ }
260
+
261
+ /**
262
+ * Buffer events into single frame
263
+ *
264
+ * @example el.onwheel = Run.framebuffer(cb,"wheel")
265
+ *
266
+ * @param {function} cb Callback to run
267
+ * @param {string} key Buffer key
268
+ * @param {number} [order] Sort order
269
+ * @returns {function} Buffered handler
270
+ */
271
+ static framebuffer(cb, key, order = 1000)
272
+ {
273
+ return (e, ...args) => {
274
+
275
+ Arr.replace(PicoRunner.$buffer, {
276
+ key, cb, order, args: [e, ...args], active: true
277
+ }, { key });
278
+
279
+ (e.preventDefault(), PicoRunner.runFramebuffer());
280
+ };
281
+ }
282
+
283
+ /**
284
+ * Flush buffered frame events
285
+ *
286
+ * @example Run.runFramebuffer() // flush
287
+ *
288
+ * @returns {void} No return value
289
+ */
290
+ static runFramebuffer()
291
+ {
292
+ if ( this.$timer.func ) {
293
+ this.$timer.func();
294
+ }
295
+
296
+ this.$timer.func = this.delay(() => {
297
+ this.runFramebuffer();
298
+ }, 50);
299
+
300
+ if ( Date.now() - this.$timer.date <= 50 ) {
301
+ return;
302
+ }
303
+
304
+ if ( this.$timer.func ) {
305
+ this.$timer.func();
306
+ }
307
+
308
+ this.$timer.date = Date.now();
309
+
310
+ let buffer = Arr.filter(this.$buffer, {
311
+ active: true
312
+ });
313
+
314
+ if ( buffer.length === 0 ) {
315
+ return;
316
+ }
317
+
318
+ this.frame(() => {
319
+ Arr.each(Arr.sort(buffer, 'order'), (item) => {
320
+ item.cb(...item.args); item.active = false;
321
+ });
322
+ });
323
+ }
324
+
325
+ }
326
+
327
+ export default PicoRunner;