@vuu-ui/vuu-utils 0.5.1 → 0.5.2

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/cjs/index.js CHANGED
@@ -20,11 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
- ASC: () => ASC,
24
23
  DOWN1: () => DOWN1,
25
24
  DOWN2: () => DOWN2,
26
- DSC: () => DSC,
27
- DataTypes: () => DataTypes,
28
25
  DataWindow: () => DataWindow,
29
26
  EventEmitter: () => EventEmitter,
30
27
  UP1: () => UP1,
@@ -51,6 +48,7 @@ __export(src_exports, {
51
48
  projectColumns: () => projectColumns,
52
49
  projectUpdates: () => projectUpdates,
53
50
  resetRange: () => resetRange,
51
+ roundDecimal: () => roundDecimal,
54
52
  toColumnDescriptor: () => toColumnDescriptor,
55
53
  toKeyedColumn: () => toKeyedColumn,
56
54
  update: () => update,
@@ -58,109 +56,6 @@ __export(src_exports, {
58
56
  });
59
57
  module.exports = __toCommonJS(src_exports);
60
58
 
61
- // src/event-emitter.ts
62
- function isArrayOfListeners(listeners) {
63
- return Array.isArray(listeners);
64
- }
65
- function isOnlyListener(listeners) {
66
- return !Array.isArray(listeners);
67
- }
68
- var EventEmitter = class {
69
- _events;
70
- constructor() {
71
- this._events = {};
72
- }
73
- addListener(type, listener) {
74
- if (!this._events) {
75
- this._events = {};
76
- }
77
- const listeners = this._events[type];
78
- if (!listeners) {
79
- this._events[type] = listener;
80
- } else if (isArrayOfListeners(listeners)) {
81
- listeners.push(listener);
82
- } else if (isOnlyListener(listeners)) {
83
- this._events[type] = [listeners, listener];
84
- }
85
- }
86
- removeListener(type, listener) {
87
- if (!this._events || !this._events[type]) {
88
- return;
89
- }
90
- const listenerOrListeners = this._events[type];
91
- let position = -1;
92
- if (listenerOrListeners === listener) {
93
- delete this._events[type];
94
- } else if (Array.isArray(listenerOrListeners)) {
95
- for (let i = length; i-- > 0; ) {
96
- if (listenerOrListeners[i] === listener) {
97
- position = i;
98
- break;
99
- }
100
- }
101
- if (position < 0) {
102
- return;
103
- }
104
- if (listenerOrListeners.length === 1) {
105
- listenerOrListeners.length = 0;
106
- delete this._events[type];
107
- } else {
108
- listenerOrListeners.splice(position, 1);
109
- }
110
- }
111
- }
112
- removeAllListeners(type) {
113
- if (!this._events) {
114
- return;
115
- } else if (type === void 0) {
116
- delete this._events;
117
- } else {
118
- delete this._events[type];
119
- }
120
- }
121
- emit(type, ...args) {
122
- if (this._events) {
123
- const handler = this._events[type];
124
- if (handler) {
125
- invokeHandler(handler, type, args);
126
- }
127
- const wildcardHandler = this._events["*"];
128
- if (wildcardHandler) {
129
- invokeHandler(wildcardHandler, type, args);
130
- }
131
- }
132
- }
133
- once(type, listener) {
134
- const handler = (evtName, message) => {
135
- this.removeListener(evtName, handler);
136
- listener(evtName, message);
137
- };
138
- this.on(type, handler);
139
- }
140
- on(type, listener) {
141
- return this.addListener(type, listener);
142
- }
143
- };
144
- function invokeHandler(handler, type, args) {
145
- if (isArrayOfListeners(handler)) {
146
- handler.slice().forEach((listener) => invokeHandler(listener, type, args));
147
- } else {
148
- switch (args.length) {
149
- case 0:
150
- handler(type);
151
- break;
152
- case 1:
153
- handler(type, args[0]);
154
- break;
155
- case 2:
156
- handler(type, args[0], args[1]);
157
- break;
158
- default:
159
- handler.call(null, type, ...args);
160
- }
161
- }
162
- }
163
-
164
59
  // src/array-utils.ts
165
60
  function arrayOfIndices(length2) {
166
61
  const result = Array(length2);
@@ -430,6 +325,109 @@ var formatDate = (date, format) => {
430
325
  return date.toUTCString();
431
326
  };
432
327
 
328
+ // src/event-emitter.ts
329
+ function isArrayOfListeners(listeners) {
330
+ return Array.isArray(listeners);
331
+ }
332
+ function isOnlyListener(listeners) {
333
+ return !Array.isArray(listeners);
334
+ }
335
+ var EventEmitter = class {
336
+ _events;
337
+ constructor() {
338
+ this._events = {};
339
+ }
340
+ addListener(type, listener) {
341
+ if (!this._events) {
342
+ this._events = {};
343
+ }
344
+ const listeners = this._events[type];
345
+ if (!listeners) {
346
+ this._events[type] = listener;
347
+ } else if (isArrayOfListeners(listeners)) {
348
+ listeners.push(listener);
349
+ } else if (isOnlyListener(listeners)) {
350
+ this._events[type] = [listeners, listener];
351
+ }
352
+ }
353
+ removeListener(type, listener) {
354
+ if (!this._events || !this._events[type]) {
355
+ return;
356
+ }
357
+ const listenerOrListeners = this._events[type];
358
+ let position = -1;
359
+ if (listenerOrListeners === listener) {
360
+ delete this._events[type];
361
+ } else if (Array.isArray(listenerOrListeners)) {
362
+ for (let i = length; i-- > 0; ) {
363
+ if (listenerOrListeners[i] === listener) {
364
+ position = i;
365
+ break;
366
+ }
367
+ }
368
+ if (position < 0) {
369
+ return;
370
+ }
371
+ if (listenerOrListeners.length === 1) {
372
+ listenerOrListeners.length = 0;
373
+ delete this._events[type];
374
+ } else {
375
+ listenerOrListeners.splice(position, 1);
376
+ }
377
+ }
378
+ }
379
+ removeAllListeners(type) {
380
+ if (!this._events) {
381
+ return;
382
+ } else if (type === void 0) {
383
+ delete this._events;
384
+ } else {
385
+ delete this._events[type];
386
+ }
387
+ }
388
+ emit(type, ...args) {
389
+ if (this._events) {
390
+ const handler = this._events[type];
391
+ if (handler) {
392
+ invokeHandler(handler, type, args);
393
+ }
394
+ const wildcardHandler = this._events["*"];
395
+ if (wildcardHandler) {
396
+ invokeHandler(wildcardHandler, type, args);
397
+ }
398
+ }
399
+ }
400
+ once(type, listener) {
401
+ const handler = (evtName, message) => {
402
+ this.removeListener(evtName, handler);
403
+ listener(evtName, message);
404
+ };
405
+ this.on(type, handler);
406
+ }
407
+ on(type, listener) {
408
+ return this.addListener(type, listener);
409
+ }
410
+ };
411
+ function invokeHandler(handler, type, args) {
412
+ if (isArrayOfListeners(handler)) {
413
+ handler.slice().forEach((listener) => invokeHandler(listener, type, args));
414
+ } else {
415
+ switch (args.length) {
416
+ case 0:
417
+ handler(type);
418
+ break;
419
+ case 1:
420
+ handler(type, args[0]);
421
+ break;
422
+ case 2:
423
+ handler(type, args[0], args[1]);
424
+ break;
425
+ default:
426
+ handler.call(null, type, ...args);
427
+ }
428
+ }
429
+ }
430
+
433
431
  // src/getUniqueId.ts
434
432
  var getUniqueId = () => `hw-${Math.round(Math.random() * 1e5)}`;
435
433
 
@@ -508,14 +506,88 @@ var uuid = (size = 21) => {
508
506
  return id;
509
507
  };
510
508
 
511
- // src/constants.ts
512
- var DataTypes = {
513
- ROW_DATA: "rowData",
514
- FILTER_DATA: "filterData",
515
- FILTER_BINS: "filterBins"
509
+ // src/round-decimal.ts
510
+ var PUNCTUATION_STR = String.fromCharCode(8200);
511
+ var DIGIT_STR = String.fromCharCode(8199);
512
+ var DECIMALS_AUTO = -1;
513
+ var Space = {
514
+ DIGIT: DIGIT_STR,
515
+ TWO_DIGITS: DIGIT_STR + DIGIT_STR,
516
+ THREE_DIGITS: DIGIT_STR + DIGIT_STR + DIGIT_STR,
517
+ FULL_PADDING: [
518
+ null,
519
+ PUNCTUATION_STR + DIGIT_STR,
520
+ PUNCTUATION_STR + DIGIT_STR + DIGIT_STR,
521
+ PUNCTUATION_STR + DIGIT_STR + DIGIT_STR + DIGIT_STR,
522
+ PUNCTUATION_STR + DIGIT_STR + DIGIT_STR + DIGIT_STR + DIGIT_STR
523
+ ]
524
+ };
525
+ var Zero = {
526
+ DIGIT: "0",
527
+ TWO_DIGITS: "00",
528
+ THREE_DIGITS: "000",
529
+ FULL_PADDING: [null, "0", "00", "000", "0000"]
516
530
  };
517
- var ASC = "asc";
518
- var DSC = "dsc";
531
+ function padLeft(value, maxLength = 6) {
532
+ return (LEADING_FILL + value).slice(-maxLength);
533
+ }
534
+ var LEADING_FILL = DIGIT_STR + DIGIT_STR + DIGIT_STR + DIGIT_STR + DIGIT_STR + DIGIT_STR + DIGIT_STR + DIGIT_STR + DIGIT_STR;
535
+ var Align = {
536
+ Right: "right",
537
+ Center: "center",
538
+ Left: "left"
539
+ };
540
+ function pad(n, dp, Pad) {
541
+ let len = n.length;
542
+ const diff = dp - len;
543
+ if (diff > 0) {
544
+ if (diff === 1) {
545
+ n = n + Pad.DIGIT;
546
+ } else if (diff === 2) {
547
+ n = n + Pad.TWO_DIGITS;
548
+ } else if (diff === 3) {
549
+ n = n + Pad.THREE_DIGITS;
550
+ }
551
+ } else {
552
+ if (diff < 0) {
553
+ n = n.slice(0, dp);
554
+ len = dp;
555
+ }
556
+ if (Pad === Space && n.charAt(len - 1) === "0") {
557
+ n = n.replace(/0+$/, "");
558
+ return pad(n, dp, Pad);
559
+ }
560
+ }
561
+ return n;
562
+ }
563
+ function roundDecimal(value, align = Align.Right, decimals = 4, zeroPad, alignOnDecimals) {
564
+ if (value === void 0 || typeof value !== "number" || isNaN(value)) {
565
+ return "";
566
+ }
567
+ let integral, fraction, Pad;
568
+ const [part1, part2 = ""] = value.toString().split(".");
569
+ const actualDecimals = part2.length;
570
+ integral = parseFloat(part1).toLocaleString();
571
+ if (align === Align.Left && alignOnDecimals) {
572
+ integral = padLeft(integral);
573
+ }
574
+ if (decimals === DECIMALS_AUTO || actualDecimals === decimals) {
575
+ fraction = part2;
576
+ } else if (actualDecimals > decimals) {
577
+ fraction = parseFloat("0." + part2).toFixed(decimals).slice(2);
578
+ } else {
579
+ if (Pad = zeroPad ? Zero : alignOnDecimals && align !== Align.Left ? Space : null) {
580
+ if (actualDecimals === 0) {
581
+ fraction = Pad.FULL_PADDING[decimals];
582
+ } else {
583
+ fraction = pad(part2, decimals, Pad);
584
+ }
585
+ } else {
586
+ fraction = part2;
587
+ }
588
+ }
589
+ return integral + (fraction ? "." + fraction : "");
590
+ }
519
591
 
520
592
  // src/row-utils.ts
521
593
  function addRowsToIndex(rows, index, indexField) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vuu-ui/vuu-utils",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "author": "heswell",
5
5
  "license": "Apache-2.0",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -1,14 +1,14 @@
1
- export { EventEmitter } from "./event-emitter";
2
1
  export * from "./array-utils";
3
2
  export * from "./column-utils";
4
3
  export * from "./DataWindow";
5
4
  export * from "./data-utils";
6
5
  export * from "./date-utils";
6
+ export * from "./event-emitter";
7
7
  export * from "./getUniqueId";
8
8
  export * from "./input-utils";
9
9
  export * from "./invariant";
10
10
  export * from "./nanoid";
11
- export * from "./constants";
11
+ export * from "./round-decimal";
12
12
  export * from "./range-utils";
13
13
  export * from "./row-utils";
14
14
  export * from "./text-utils";
@@ -0,0 +1,126 @@
1
+ const PUNCTUATION_STR = String.fromCharCode(8200);
2
+ const DIGIT_STR = String.fromCharCode(8199);
3
+ const DECIMALS_AUTO = -1;
4
+
5
+ type PadMap = {
6
+ DIGIT: string;
7
+ TWO_DIGITS: string;
8
+ THREE_DIGITS: string;
9
+ FULL_PADDING: [null, string, string, string, string];
10
+ };
11
+
12
+ const Space: PadMap = {
13
+ DIGIT: DIGIT_STR,
14
+ TWO_DIGITS: DIGIT_STR + DIGIT_STR,
15
+ THREE_DIGITS: DIGIT_STR + DIGIT_STR + DIGIT_STR,
16
+ FULL_PADDING: [
17
+ null,
18
+ PUNCTUATION_STR + DIGIT_STR,
19
+ PUNCTUATION_STR + DIGIT_STR + DIGIT_STR,
20
+ PUNCTUATION_STR + DIGIT_STR + DIGIT_STR + DIGIT_STR,
21
+ PUNCTUATION_STR + DIGIT_STR + DIGIT_STR + DIGIT_STR + DIGIT_STR,
22
+ ],
23
+ };
24
+
25
+ const Zero: PadMap = {
26
+ DIGIT: "0",
27
+ TWO_DIGITS: "00",
28
+ THREE_DIGITS: "000",
29
+ FULL_PADDING: [null, "0", "00", "000", "0000"],
30
+ };
31
+
32
+ function padLeft(value: string, maxLength = 6) {
33
+ return (LEADING_FILL + value).slice(-maxLength);
34
+ }
35
+
36
+ const LEADING_FILL =
37
+ DIGIT_STR +
38
+ DIGIT_STR +
39
+ DIGIT_STR +
40
+ DIGIT_STR +
41
+ DIGIT_STR +
42
+ DIGIT_STR +
43
+ DIGIT_STR +
44
+ DIGIT_STR +
45
+ DIGIT_STR;
46
+
47
+ const Align = {
48
+ Right: "right",
49
+ Center: "center",
50
+ Left: "left",
51
+ };
52
+
53
+ function pad(n: string, dp: number, Pad: PadMap): string {
54
+ let len = n.length;
55
+ const diff = dp - len;
56
+
57
+ if (diff > 0) {
58
+ if (diff === 1) {
59
+ n = n + Pad.DIGIT;
60
+ } else if (diff === 2) {
61
+ n = n + Pad.TWO_DIGITS;
62
+ } else if (diff === 3) {
63
+ n = n + Pad.THREE_DIGITS;
64
+ }
65
+ } else {
66
+ if (diff < 0) {
67
+ n = n.slice(0, dp);
68
+ len = dp;
69
+ }
70
+
71
+ if (Pad === Space && n.charAt(len - 1) === "0") {
72
+ n = n.replace(/0+$/, "");
73
+ return pad(n, dp, Pad);
74
+ }
75
+ }
76
+ return n;
77
+ }
78
+
79
+ export function roundDecimal(
80
+ value?: number,
81
+ align = Align.Right,
82
+ decimals = 4,
83
+ zeroPad?: boolean,
84
+ alignOnDecimals?: boolean
85
+ ) {
86
+ if (value === undefined || typeof value !== "number" || isNaN(value)) {
87
+ return "";
88
+ }
89
+ let integral, fraction, Pad;
90
+
91
+ const [part1, part2 = ""] = value.toString().split(".");
92
+ const actualDecimals = part2.length;
93
+
94
+ integral = parseFloat(part1).toLocaleString();
95
+
96
+ if (align === Align.Left && alignOnDecimals) {
97
+ integral = padLeft(integral);
98
+ }
99
+
100
+ if (decimals === DECIMALS_AUTO || actualDecimals === decimals) {
101
+ fraction = part2;
102
+ } else if (actualDecimals > decimals) {
103
+ fraction = parseFloat("0." + part2)
104
+ .toFixed(decimals)
105
+ .slice(2);
106
+ } else {
107
+ /* eslint-disable no-cond-assign */
108
+ if (
109
+ (Pad = zeroPad
110
+ ? Zero
111
+ : alignOnDecimals && align !== Align.Left
112
+ ? Space
113
+ : null)
114
+ ) {
115
+ if (actualDecimals === 0) {
116
+ fraction = Pad.FULL_PADDING[decimals];
117
+ } else {
118
+ fraction = pad(part2, decimals, Pad);
119
+ }
120
+ } else {
121
+ fraction = part2;
122
+ }
123
+ }
124
+
125
+ return integral + (fraction ? "." + fraction : "");
126
+ }
package/src/constants.ts DELETED
@@ -1,8 +0,0 @@
1
- export const DataTypes = {
2
- ROW_DATA: 'rowData',
3
- FILTER_DATA: 'filterData',
4
- FILTER_BINS: 'filterBins'
5
- };
6
-
7
- export const ASC = 'asc';
8
- export const DSC = 'dsc';