store-scrapper-js-common 1.0.172 → 1.0.174
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.
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CurrencyEnum } from '../entities';
|
|
2
|
+
export declare const addThousandSeparator: (str: string, separator?: string) => string;
|
|
2
3
|
export declare const formatMoney: (money: number | string, currency?: CurrencyEnum) => string;
|
|
3
4
|
export declare const formatDate: (str: string, timezone?: string, format?: string) => string;
|
|
4
5
|
export declare const formatPct: (pct: number | string) => string;
|
|
@@ -19,17 +19,20 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
exports.formatPct = exports.formatDate = exports.formatMoney = void 0;
|
|
22
|
+
exports.formatPct = exports.formatDate = exports.formatMoney = exports.addThousandSeparator = void 0;
|
|
23
23
|
const moment = __importStar(require("moment-timezone"));
|
|
24
24
|
const entities_1 = require("../entities");
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
exports.addThousandSeparator = (str, separator = '.') => {
|
|
26
|
+
let nStr = (`${str}`);
|
|
27
|
+
if (nStr && separator === ',') {
|
|
28
|
+
nStr = nStr.replace(',', '.');
|
|
29
|
+
}
|
|
30
|
+
const x = nStr.split(separator);
|
|
28
31
|
let x1 = x[0];
|
|
29
|
-
const x2 = x.length > 1 ?
|
|
32
|
+
const x2 = x.length > 1 ? `${separator}${x[1]}` : '';
|
|
30
33
|
const rgx = /(\d+)(\d{3})/;
|
|
31
34
|
while (rgx.test(x1)) {
|
|
32
|
-
x1 = x1.replace(rgx,
|
|
35
|
+
x1 = x1.replace(rgx, `$1${separator}$2`);
|
|
33
36
|
}
|
|
34
37
|
return x1 + x2;
|
|
35
38
|
};
|
|
@@ -37,20 +40,28 @@ exports.formatMoney = (money, currency) => {
|
|
|
37
40
|
if (!money) {
|
|
38
41
|
return '';
|
|
39
42
|
}
|
|
43
|
+
let moneyStr = money.toString();
|
|
44
|
+
if (typeof money === 'number') {
|
|
45
|
+
moneyStr = moneyStr.replace(/\./g, ',');
|
|
46
|
+
}
|
|
40
47
|
let appendSign = currency && currency.toLowerCase().includes('pts') ? ' (pts)' : '';
|
|
41
48
|
let prependSign = '';
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
let thousandSeparator = '.';
|
|
50
|
+
if (currency === entities_1.CurrencyEnum.USD) {
|
|
51
|
+
prependSign = 'U$';
|
|
52
|
+
thousandSeparator = ',';
|
|
53
|
+
}
|
|
54
|
+
else if (currency === entities_1.CurrencyEnum.EUR) {
|
|
55
|
+
appendSign = `€ ${appendSign}`.trim();
|
|
56
|
+
}
|
|
57
|
+
else if (currency === entities_1.CurrencyEnum.MXN) {
|
|
58
|
+
prependSign = '$';
|
|
59
|
+
thousandSeparator = ',';
|
|
60
|
+
}
|
|
61
|
+
else if (!currency || !currency.toLowerCase().includes('pts')) {
|
|
62
|
+
prependSign = '$';
|
|
52
63
|
}
|
|
53
|
-
return prependSign + addThousandSeparator(
|
|
64
|
+
return prependSign + exports.addThousandSeparator(moneyStr, thousandSeparator) + appendSign;
|
|
54
65
|
};
|
|
55
66
|
exports.formatDate = (str, timezone = 'America/Santiago', format = 'DD-MM-YYYY HH:mm:ss') => {
|
|
56
67
|
if (!str) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string-formatter.js","sourceRoot":"/","sources":["utils/string-formatter.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,wDAA0C;AAC1C,0CAA2C;
|
|
1
|
+
{"version":3,"file":"string-formatter.js","sourceRoot":"/","sources":["utils/string-formatter.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,wDAA0C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-timezone';\nimport { CurrencyEnum } from '../entities';\n\nexport const addThousandSeparator = (str: string, separator = '.') => {\n let nStr = (`${str}`);\n if (nStr && separator === ',') {\n nStr = nStr.replace(',', '.');\n }\n\n const x = nStr.split(separator);\n let x1 = x[0];\n const x2 = x.length > 1 ? `${separator}${x[1]}` : '';\n const rgx = /(\\d+)(\\d{3})/;\n while (rgx.test(x1)) {\n x1 = x1.replace(rgx, `$1${separator}$2`);\n }\n return x1 + x2;\n};\n\nexport const formatMoney = (money: number | string, currency?: CurrencyEnum): string => {\n if (!money) {\n return '';\n }\n\n let moneyStr = money.toString();\n if (typeof money === 'number') {\n moneyStr = moneyStr.replace(/\\./g, ',');\n }\n\n let appendSign = currency && currency.toLowerCase().includes('pts') ? ' (pts)' : '';\n let prependSign = '';\n let thousandSeparator = '.';\n\n if (currency === CurrencyEnum.USD) {\n prependSign = 'U$';\n thousandSeparator = ',';\n } else if (currency === CurrencyEnum.EUR) {\n appendSign = `€ ${appendSign}`.trim();\n } else if (currency === CurrencyEnum.MXN) {\n prependSign = '$';\n thousandSeparator = ',';\n } else if (!currency || !currency.toLowerCase().includes('pts')) {\n prependSign = '$';\n }\n\n return prependSign + addThousandSeparator(moneyStr, thousandSeparator) + appendSign;\n};\n\nexport const formatDate = (str: string, timezone = 'America/Santiago', format = 'DD-MM-YYYY HH:mm:ss'): string => {\n if (!str) {\n return '';\n }\n\n const date = new Date(str);\n\n return moment.tz(date, timezone).format(format);\n};\n\nexport const formatPct = (pct: number | string): string => {\n if (!pct) {\n return '';\n }\n\n return `${pct}%`.replace('.', ',');\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import * as moment from 'moment-timezone';
|
|
2
2
|
import { CurrencyEnum } from '../entities';
|
|
3
3
|
|
|
4
|
-
const addThousandSeparator = (str: string) => {
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
export const addThousandSeparator = (str: string, separator = '.') => {
|
|
5
|
+
let nStr = (`${str}`);
|
|
6
|
+
if (nStr && separator === ',') {
|
|
7
|
+
nStr = nStr.replace(',', '.');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const x = nStr.split(separator);
|
|
7
11
|
let x1 = x[0];
|
|
8
|
-
const x2 = x.length > 1 ?
|
|
12
|
+
const x2 = x.length > 1 ? `${separator}${x[1]}` : '';
|
|
9
13
|
const rgx = /(\d+)(\d{3})/;
|
|
10
14
|
while (rgx.test(x1)) {
|
|
11
|
-
x1 = x1.replace(rgx,
|
|
15
|
+
x1 = x1.replace(rgx, `$1${separator}$2`);
|
|
12
16
|
}
|
|
13
17
|
return x1 + x2;
|
|
14
18
|
};
|
|
@@ -18,19 +22,28 @@ export const formatMoney = (money: number | string, currency?: CurrencyEnum): st
|
|
|
18
22
|
return '';
|
|
19
23
|
}
|
|
20
24
|
|
|
25
|
+
let moneyStr = money.toString();
|
|
26
|
+
if (typeof money === 'number') {
|
|
27
|
+
moneyStr = moneyStr.replace(/\./g, ',');
|
|
28
|
+
}
|
|
29
|
+
|
|
21
30
|
let appendSign = currency && currency.toLowerCase().includes('pts') ? ' (pts)' : '';
|
|
22
31
|
let prependSign = '';
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
let thousandSeparator = '.';
|
|
33
|
+
|
|
34
|
+
if (currency === CurrencyEnum.USD) {
|
|
35
|
+
prependSign = 'U$';
|
|
36
|
+
thousandSeparator = ',';
|
|
37
|
+
} else if (currency === CurrencyEnum.EUR) {
|
|
38
|
+
appendSign = `€ ${appendSign}`.trim();
|
|
39
|
+
} else if (currency === CurrencyEnum.MXN) {
|
|
40
|
+
prependSign = '$';
|
|
41
|
+
thousandSeparator = ',';
|
|
42
|
+
} else if (!currency || !currency.toLowerCase().includes('pts')) {
|
|
43
|
+
prependSign = '$';
|
|
31
44
|
}
|
|
32
45
|
|
|
33
|
-
return prependSign + addThousandSeparator(
|
|
46
|
+
return prependSign + addThousandSeparator(moneyStr, thousandSeparator) + appendSign;
|
|
34
47
|
};
|
|
35
48
|
|
|
36
49
|
export const formatDate = (str: string, timezone = 'America/Santiago', format = 'DD-MM-YYYY HH:mm:ss'): string => {
|
|
@@ -1,8 +1,25 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
addThousandSeparator, formatDate, formatMoney, formatPct,
|
|
3
|
+
} from '../../src/utils/string-formatter';
|
|
2
4
|
import { CurrencyEnum } from '../../src/entities';
|
|
3
5
|
|
|
4
6
|
describe('string formatter', () => {
|
|
5
|
-
it('should
|
|
7
|
+
it('should add thousand separator', () => {
|
|
8
|
+
expect.hasAssertions();
|
|
9
|
+
|
|
10
|
+
expect(addThousandSeparator('10')).toStrictEqual('10');
|
|
11
|
+
expect(addThousandSeparator('100')).toStrictEqual('100');
|
|
12
|
+
expect(addThousandSeparator('1000')).toStrictEqual('1.000');
|
|
13
|
+
expect(addThousandSeparator('1000000')).toStrictEqual('1.000.000');
|
|
14
|
+
expect(addThousandSeparator('1000000,5')).toStrictEqual('1.000.000,5');
|
|
15
|
+
|
|
16
|
+
expect(addThousandSeparator('1000', ',')).toStrictEqual('1,000');
|
|
17
|
+
expect(addThousandSeparator('1000000', ',')).toStrictEqual('1,000,000');
|
|
18
|
+
expect(addThousandSeparator('1000000,5', ',')).toStrictEqual('1,000,000.5');
|
|
19
|
+
expect(addThousandSeparator('1000000,56', ',')).toStrictEqual('1,000,000.56');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('should return number as a formatted Money Currency', () => {
|
|
6
23
|
expect.hasAssertions();
|
|
7
24
|
|
|
8
25
|
expect(formatMoney(null)).toStrictEqual('');
|
|
@@ -12,15 +29,33 @@ describe('string formatter', () => {
|
|
|
12
29
|
expect(formatMoney(100000)).toStrictEqual('$100.000');
|
|
13
30
|
expect(formatMoney(1000000)).toStrictEqual('$1.000.000');
|
|
14
31
|
|
|
32
|
+
expect(formatMoney(1434591.1)).toStrictEqual('$1.434.591,1');
|
|
33
|
+
|
|
15
34
|
expect(formatMoney(1000, CurrencyEnum.CMR_PTS)).toStrictEqual('1.000 (pts)');
|
|
16
35
|
expect(formatMoney(1000000, CurrencyEnum.CMR_PTS)).toStrictEqual('1.000.000 (pts)');
|
|
17
36
|
|
|
18
37
|
expect(formatMoney(100.99, CurrencyEnum.USD)).toStrictEqual('U$100.99');
|
|
19
|
-
expect(formatMoney(1000, CurrencyEnum.USD)).toStrictEqual('U$1
|
|
20
|
-
expect(formatMoney(
|
|
38
|
+
expect(formatMoney(1000, CurrencyEnum.USD)).toStrictEqual('U$1,000');
|
|
39
|
+
expect(formatMoney(1649.99, CurrencyEnum.USD)).toStrictEqual('U$1,649.99');
|
|
40
|
+
expect(formatMoney(1000000, CurrencyEnum.USD)).toStrictEqual('U$1,000,000');
|
|
21
41
|
|
|
22
42
|
expect(formatMoney(1000, CurrencyEnum.EUR)).toStrictEqual('1.000€');
|
|
23
43
|
expect(formatMoney(1000000, CurrencyEnum.EUR)).toStrictEqual('1.000.000€');
|
|
44
|
+
|
|
45
|
+
expect(formatMoney(100, CurrencyEnum.MXN)).toStrictEqual('$100');
|
|
46
|
+
expect(formatMoney(1000, CurrencyEnum.MXN)).toStrictEqual('$1,000');
|
|
47
|
+
expect(formatMoney(1000000, CurrencyEnum.MXN)).toStrictEqual('$1,000,000');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should return formatted money right separator and decimal', () => {
|
|
51
|
+
expect.hasAssertions();
|
|
52
|
+
|
|
53
|
+
expect(formatMoney(1434591.1)).toStrictEqual('$1.434.591,1');
|
|
54
|
+
expect(formatMoney(1434591.1, CurrencyEnum.CLP)).toStrictEqual('$1.434.591,1');
|
|
55
|
+
expect(formatMoney(1434591.1, CurrencyEnum.ARS)).toStrictEqual('$1.434.591,1');
|
|
56
|
+
expect(formatMoney(1434591.1, CurrencyEnum.COP)).toStrictEqual('$1.434.591,1');
|
|
57
|
+
|
|
58
|
+
expect(formatMoney(1434591.1, CurrencyEnum.MXN)).toStrictEqual('$1,434,591.1');
|
|
24
59
|
});
|
|
25
60
|
|
|
26
61
|
it('should return date as a formatted CLP Date', () => {
|