@kizmann/pico-js 2.1.3 → 2.1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kizmann/pico-js",
3
- "version": "2.1.3",
3
+ "version": "2.1.5",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "private": false,
@@ -32,7 +32,9 @@ export class PicoDomBuilder
32
32
  el = document.createElement(el);
33
33
  }
34
34
 
35
- el = Obj.assign(el, options);
35
+ if ( el != null ) {
36
+ el = Obj.assign(el, options);
37
+ }
36
38
 
37
39
  let ini = new Dom(el);
38
40
 
@@ -406,7 +406,7 @@ export class PicoArray
406
406
  * @param {any[]} [result] Initial result
407
407
  * @returns {any} Cascade result
408
408
  */
409
- static cascade(value : any, childs : string, cb: Function, cascade: any[] = [], result = []) : any
409
+ static cascade(value : any, childs : string, cb : Function, cascade : any[] = [], result = []) : any
410
410
  {
411
411
  let fn = (cas : any) => (val : any) => {
412
412
  return this.cascade(val, childs, cb, cas, result);
@@ -439,6 +439,27 @@ export class PicoArray
439
439
  return result;
440
440
  }
441
441
 
442
+ /**
443
+ * Flatten nested array cascade
444
+ *
445
+ * @example Arr.cascadeFlatten([{n:1, children:[{n:2}]}]) // => [{n:1,...}, {n:2}]
446
+ *
447
+ * @param {any[]} items Input list
448
+ * @param {string} [key] Child key
449
+ * @param {any[]} [result] Initial result
450
+ * @returns {any[]} Flattened list
451
+ */
452
+ static cascadeFlatten(items : any[], key : string = 'children', result : any[] = []) : any[]
453
+ {
454
+ let cb = (input : any) => {
455
+ result.push(input), Mix.isArr(input[key]) && input[key].forEach(cb);
456
+ };
457
+
458
+ items.forEach(cb);
459
+
460
+ return result;
461
+ }
462
+
442
463
  /**
443
464
  * Find item in cascade
444
465
  *
@@ -457,11 +478,11 @@ export class PicoArray
457
478
 
458
479
  let result = null
459
480
 
460
- const fn = (val : any, cascade: any[]) => {
481
+ const fn = (val : any, cascade : any[]) => {
461
482
  return cb(val, cascade) ? [...cascade, val] : null;
462
483
  };
463
484
 
464
- this.cascade(value, childs, (val:any, cascade : any[]) => {
485
+ this.cascade(value, childs, (val : any, cascade : any[]) => {
465
486
  if ( result == null ) result = fn(val, cascade);
466
487
  });
467
488
 
@@ -1,4 +1,4 @@
1
- import { Mix, Obj, Locale } from "../index.esm.ts";
1
+ import { Mix, Obj, Locale, Arr } from "../index.esm.ts";
2
2
 
3
3
  export class PicoLocale
4
4
  {
@@ -16,6 +16,13 @@ export class PicoLocale
16
16
  */
17
17
  static $sort : Intl.Collator = null;
18
18
 
19
+ /**
20
+ * Number format
21
+ *
22
+ * @type {string[]}
23
+ */
24
+ static $number : string[] = null;
25
+
19
26
  /**
20
27
  * Active locale code
21
28
  *
@@ -124,6 +131,33 @@ export class PicoLocale
124
131
  return Locale.$sort;
125
132
  }
126
133
 
134
+ /**
135
+ * Get decimal and group separators
136
+ *
137
+ * @returns {string[]} Separators [group, decimal]
138
+ */
139
+ static decimals() : string[]
140
+ {
141
+ if ( Locale.$number != null ) {
142
+ return Locale.$number;
143
+ }
144
+
145
+ const parts = new Intl.NumberFormat(Locale.code())
146
+ .formatToParts(1234.5);
147
+
148
+ const group = Arr.find(parts, {
149
+ type: 'group'
150
+ });
151
+
152
+ const decimal = Arr.find(parts, {
153
+ type: 'decimal'
154
+ });
155
+
156
+ return Locale.$number = [
157
+ group.value || ',', decimal.value || '.'
158
+ ];
159
+ }
160
+
127
161
  /**
128
162
  * Replace :tokens in text
129
163
  *
@@ -224,6 +258,46 @@ export class PicoLocale
224
258
  return splits[0];
225
259
  }
226
260
 
261
+ /**
262
+ * Parse localized number string
263
+ *
264
+ * @example Locale.num("1,234.50") // => 1234.5
265
+ *
266
+ * @param {string} value Localized string
267
+ * @returns {number} Number value
268
+ */
269
+ static num(value : string) : number
270
+ {
271
+ const [group, decimal] = Locale.decimals();
272
+
273
+ const text = value
274
+ .replaceAll(' ', '')
275
+ .replaceAll(group, '')
276
+ .replaceAll(decimal, '.');
277
+
278
+ return Mix.num(text);
279
+ }
280
+
281
+ /**
282
+ * Parse localized integer string
283
+ *
284
+ * @example Locale.int("1,234") // => 1234
285
+ *
286
+ * @param {string} value Localized string
287
+ * @returns {number} Integer value
288
+ */
289
+ static int(value : string) : number
290
+ {
291
+ const [group, decimal] = Locale.decimals();
292
+
293
+ const text = value
294
+ .replaceAll(' ', '')
295
+ .replaceAll(group, '')
296
+ .replaceAll(decimal, '.');
297
+
298
+ return Mix.int(text);
299
+ }
300
+
227
301
  }
228
302
 
229
303
  export default PicoLocale
@@ -200,6 +200,17 @@ export declare class PicoArray {
200
200
  * @returns {any} Cascade result
201
201
  */
202
202
  static cascade(value: any, childs: string, cb: Function, cascade?: any[], result?: any[]): any;
203
+ /**
204
+ * Flatten nested array cascade
205
+ *
206
+ * @example Arr.cascadeFlatten([{n:1, children:[{n:2}]}]) // => [{n:1,...}, {n:2}]
207
+ *
208
+ * @param {any[]} items Input list
209
+ * @param {string} [key] Child key
210
+ * @param {any[]} [result] Initial result
211
+ * @returns {any[]} Flattened list
212
+ */
213
+ static cascadeFlatten(items: any[], key?: string, result?: any[]): any[];
203
214
  /**
204
215
  * Find item in cascade
205
216
  *
@@ -11,6 +11,12 @@ export declare class PicoLocale {
11
11
  * @type {Intl.Collator}
12
12
  */
13
13
  static $sort: Intl.Collator;
14
+ /**
15
+ * Number format
16
+ *
17
+ * @type {string[]}
18
+ */
19
+ static $number: string[];
14
20
  /**
15
21
  * Active locale code
16
22
  *
@@ -67,6 +73,12 @@ export declare class PicoLocale {
67
73
  * @returns {Intl.Collator} Collator instance
68
74
  */
69
75
  static collator(): Intl.Collator;
76
+ /**
77
+ * Get decimal and group separators
78
+ *
79
+ * @returns {string[]} Separators [group, decimal]
80
+ */
81
+ static decimals(): string[];
70
82
  /**
71
83
  * Replace :tokens in text
72
84
  *
@@ -111,5 +123,23 @@ export declare class PicoLocale {
111
123
  * @returns {string} Picked text
112
124
  */
113
125
  static countpick(splits: string[], count?: number): string;
126
+ /**
127
+ * Parse localized number string
128
+ *
129
+ * @example Locale.num("1,234.50") // => 1234.5
130
+ *
131
+ * @param {string} value Localized string
132
+ * @returns {number} Number value
133
+ */
134
+ static num(value: string): number;
135
+ /**
136
+ * Parse localized integer string
137
+ *
138
+ * @example Locale.int("1,234") // => 1234
139
+ *
140
+ * @param {string} value Localized string
141
+ * @returns {number} Integer value
142
+ */
143
+ static int(value: string): number;
114
144
  }
115
145
  export default PicoLocale;