UNPKG

545 BJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var numStrict = require('@typen/num-strict');
6
7class Mag {
8 constructor(digit = 2, sep = 3) {
9 this.dg = digit;
10 this.reg = new RegExp(`\\d(?=(\\d{${sep || 3}})+${digit > 0 ? '\\.' : '$'})`, 'g');
11 }
12
13 static build(digit, sep) {
14 return new Mag(digit, sep);
15 }
16
17 parse(any) {
18 return numStrict.isNumeric(any) ? this.format(any) : String(any);
19 }
20
21 format(num) {
22 return num.toFixed(this.dg).replace(this.reg, '$&,');
23 }
24
25}
26
27exports.Mag = Mag;