1 | const SECOND = 1000;
|
2 | const MINUTE = 60 * SECOND;
|
3 | const HOUR = 60 * MINUTE;
|
4 | const DAY = 24 * HOUR;
|
5 | const lodash = require('lodash');
|
6 |
|
7 |
|
8 | function NumberFormatter (unit, rear=false, defaultPrecision, defaultDecimal, defaultComma) {
|
9 | return function (number, precision = defaultPrecision, decimal = defaultDecimal, comma = defaultComma) {
|
10 | precision = isNaN(precision = Math.abs(precision)) ? 2 : precision;
|
11 | decimal = decimal === undefined ? '.' : decimal;
|
12 | comma = comma === undefined ? ',' : comma;
|
13 | var prefix = number < 0 ? '-' : '',
|
14 | integerPortion = parseInt(number = Math.abs(+number || 0).toFixed(precision)) + '',
|
15 | modulo = (modulo = integerPortion.length) > 3 ? modulo % 3 : 0;
|
16 |
|
17 | return (rear ? '' : (unit || '')) +
|
18 | (prefix +
|
19 | (modulo ? integerPortion.substr(0, modulo) + comma : '') +
|
20 | integerPortion.substr(modulo).replace(/(\d{3})(?=\d)/g, '$1' + comma) +
|
21 | (precision ? decimal + Math.abs(number - integerPortion).toFixed(precision).slice(2) : '')
|
22 | ) + (rear ? (unit || '') : '');
|
23 | };
|
24 | }
|
25 |
|
26 | module.exports = {
|
27 | SECOND,
|
28 | MINUTE,
|
29 | HOUR,
|
30 | DAY,
|
31 | NumberFormatter,
|
32 |
|
33 | parse (str) {
|
34 | return (typeof str === typeof "") ? parseFloat(str.replace(/[^0-9\.-]/g, '')) : str;
|
35 | },
|
36 |
|
37 | random (...args) {
|
38 | return lodash.random.apply(lodash, args);
|
39 | },
|
40 |
|
41 | toCurrency : new NumberFormatter('$'),
|
42 |
|
43 | toPercent : new NumberFormatter('%', true),
|
44 |
|
45 | withDelimiter : new NumberFormatter(),
|
46 |
|
47 | toPhone (number = "", formatAreaCode = false, extension = false) {
|
48 | let digits = String(number).split('');
|
49 | let areaCode = digits.slice(-10, -7).join('');
|
50 | let prefix = digits.slice(-7, -4).join('');
|
51 | let line = digits.slice(-4).join('');
|
52 |
|
53 | if (formatAreaCode) {
|
54 | number = `(${areaCode}) ${prefix}-${line}`;
|
55 | } else {
|
56 | number = `${areaCode}-${prefix}-${line}`;
|
57 | }
|
58 |
|
59 | if (extension) {
|
60 | number += ` x${extension}`
|
61 | }
|
62 |
|
63 | return number;
|
64 | },
|
65 |
|
66 | toAbbr (n, decimals) {
|
67 | decimals = decimals ? decimals + 2 : 3;
|
68 |
|
69 | var base = Math.floor(Math.log(Math.abs(n)) / Math.log(1000)),
|
70 | suffix = 'kmb'[base - 1],
|
71 | out = suffix ? String(n / Math.pow(1000, base)).substring(0, decimals) + suffix : '' + n;
|
72 |
|
73 | if (decimals === 4) {
|
74 |
|
75 | if (out.match(/^[0-9]\.[0-9]m/)) {
|
76 | out = out.replace('m', '0m');
|
77 | } else if (out.match(/^[0-9]m/)) {
|
78 | out = out.replace('m', '.00m');
|
79 | }
|
80 | }
|
81 |
|
82 | if (out.match(/\.k/)) {
|
83 | out = out.replace(/\.k/, 'k');
|
84 | }
|
85 |
|
86 | if (out.match(/\.m/)) {
|
87 | out = out.replace(/\.m/, 'm');
|
88 | }
|
89 |
|
90 | return out;
|
91 | },
|
92 |
|
93 | toData (bytes) {
|
94 | var si = true;
|
95 |
|
96 | var thresh = si ? 1000 : 1024;
|
97 | if(Math.abs(bytes) < thresh) {
|
98 | return bytes + ' B';
|
99 | }
|
100 | var units = si ?
|
101 | ['kB','MB','GB','TB','PB','EB','ZB','YB'] :
|
102 | ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
|
103 | var u = -1;
|
104 | do {
|
105 | bytes /= thresh;
|
106 | ++u;
|
107 | } while(Math.abs(bytes) >= thresh && u < units.length - 1);
|
108 | return bytes.toFixed(1)+' '+units[u];
|
109 | },
|
110 |
|
111 | toTime (seconds, returnArray = false) {
|
112 |
|
113 | let minutes = (seconds / 60) | 0;
|
114 | let hours = (minutes / 60) | 0;
|
115 | let days = (hours / 24) | 0;
|
116 |
|
117 | hours -= (days * 24);
|
118 | minutes -= (hours * 60) + (days * 24 * 60);
|
119 | seconds -= (minutes * 60) + (hours * 60 * 60) + (days * 24 * 60 * 60);
|
120 |
|
121 | if (returnArray) {
|
122 | return [days, hours, minutes, seconds];
|
123 | } else {
|
124 | return `${days === 1 ? "1 day " : days > 1 ? (days + " days ") : ""}${hours > 0 ? (hours + " hr ") : ""}${minutes > 0 ? (minutes + " min ") : ""}${seconds > 0 ? (seconds + " sec") : ""}`.trim();
|
125 | }
|
126 | }
|
127 |
|
128 |
|
129 | };
|