Skip to content

Commit 2d1cafa

Browse files
author
Dipak Sarkar
committed
updated is null methods
1 parent a8cd798 commit 2d1cafa

File tree

4 files changed

+332
-282
lines changed

4 files changed

+332
-282
lines changed

src/number-format.js

Lines changed: 85 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,144 @@
1-
import options from './options'
1+
import options from "./options";
22

33
/**
44
* Number format function
55
* @param {Object} options
66
*/
77
export default function NumberFormat(config = options) {
8-
this.options = Object.assign(options, config)
9-
this.input = ''
10-
this.number = ''
11-
this.isClean = false
8+
this.options = Object.assign(options, config);
9+
this.input = "";
10+
this.number = "";
11+
this.isClean = false;
1212

13-
this.isNull = (input = this.input) => !this.numberOnly(input, new RegExp('[^0-9]+', 'gi'))
13+
this.isNull = (input = this.input) =>
14+
this.numberOnly(input, new RegExp("[^0-9]+", "gi")) === null;
1415

1516
this.clean = (clean = false) => {
16-
this.isClean = clean
17-
return this
18-
}
17+
this.isClean = clean;
18+
return this;
19+
};
1920

2021
this.sign = () => {
21-
const sign = (this.input.toString().indexOf('-') >= 0 && this.realNumber() > 0) ? '-' : ''
22-
return sign
23-
}
22+
const sign =
23+
this.input.toString().indexOf("-") >= 0 && this.realNumber() > 0
24+
? "-"
25+
: "";
26+
return sign;
27+
};
2428

2529
function between(min, n, max) {
26-
return Math.max(min, Math.min(n, max))
30+
return Math.max(min, Math.min(n, max));
2731
}
2832

2933
// Uncaught RangeError: toFixed() digits argument must be between 0 and 20 at Number.toFixed
3034
function fixed(precision) {
31-
return between(0, precision, 20)
35+
return between(0, precision, 20);
3236
}
3337

3438
function toFixed(numbers, precision) {
3539
// eslint-disable-next-line no-restricted-properties
36-
var exp = Math.pow(10, precision)
37-
var float = parseFloat(numbers) / exp
38-
return float.toFixed(fixed(precision))
40+
var exp = Math.pow(10, precision);
41+
var float = parseFloat(numbers) / exp;
42+
return float.toFixed(fixed(precision));
3943
}
4044

41-
this.toNumber = (string) => Number(string)
45+
this.toNumber = (string) => Number(string);
4246

43-
this.numberOnly = (string, regExp) => string.toString().replace(regExp, '')
47+
this.numberOnly = (string, regExp) => string.toString().replace(regExp, "");
4448

45-
this.isNegative = this.sign() === '-'
49+
this.isNegative = this.sign() === "-";
4650

4751
this.numbers = () => {
4852
if (this.options.reverseFill) {
49-
this.number = toFixed(this.numberOnly(this.input, /\D+/g), this.options.precision).replace('.', this.options.decimal)
50-
} else if (typeof this.input === 'number') {
51-
this.number = this.parts(this.input.toString().replace('-', ''), '.').join(this.options.decimal)
53+
this.number = toFixed(
54+
this.numberOnly(this.input, /\D+/g),
55+
this.options.precision
56+
).replace(".", this.options.decimal);
57+
} else if (typeof this.input === "number") {
58+
this.number = this.parts(
59+
this.input.toString().replace("-", ""),
60+
"."
61+
).join(this.options.decimal);
5262
} else {
53-
this.number = this.numberOnly(this.input, new RegExp(`[^0-9\\${this.options.decimal}]+`, 'gi'))
54-
this.number = this.parts(this.number).join(this.options.decimal)
63+
this.number = this.numberOnly(
64+
this.input,
65+
new RegExp(`[^0-9\\${this.options.decimal}]+`, "gi")
66+
);
67+
this.number = this.parts(this.number).join(this.options.decimal);
5568
}
56-
return this.number
57-
}
69+
return this.number;
70+
};
5871

5972
this.realNumber = () => {
60-
return this.numbers().toString().replace(this.options.decimal, '.')
61-
}
73+
return this.numbers().toString().replace(this.options.decimal, ".");
74+
};
6275

63-
this.parts = (number = '', decimal = this.options.decimal) => {
64-
var parts = number.toString().split(decimal)
65-
parts[0] = this.toNumber(parts[0]) || 0
76+
this.parts = (number = "", decimal = this.options.decimal) => {
77+
var parts = number.toString().split(decimal);
78+
parts[0] = this.toNumber(parts[0]) || 0;
6679

6780
if (parts.length > 1) {
68-
parts[1] = parts.slice(1, parts.length).join('')
69-
parts = parts.slice(0, 2)
81+
parts[1] = parts.slice(1, parts.length).join("");
82+
parts = parts.slice(0, 2);
7083
}
7184

7285
if (this.isClean) {
73-
const newNumber = this.toNumber(parts.join('.')).toFixed(this.options.precision)
74-
const cleanNumber = this.toNumber(newNumber)
75-
const minimumDigits = cleanNumber.toFixed(this.options.minimumFractionDigits)
76-
77-
if (this.options.minimumFractionDigits && this.options.minimumFractionDigits >= 0 && cleanNumber.toString().length < minimumDigits.length) {
78-
parts = minimumDigits.toString().split('.')
86+
const newNumber = this.toNumber(parts.join(".")).toFixed(
87+
this.options.precision
88+
);
89+
const cleanNumber = this.toNumber(newNumber);
90+
const minimumDigits = cleanNumber.toFixed(
91+
this.options.minimumFractionDigits
92+
);
93+
94+
if (
95+
this.options.minimumFractionDigits &&
96+
this.options.minimumFractionDigits >= 0 &&
97+
cleanNumber.toString().length < minimumDigits.length
98+
) {
99+
parts = minimumDigits.toString().split(".");
79100
} else {
80-
parts = cleanNumber.toString().split('.')
101+
parts = cleanNumber.toString().split(".");
81102
}
82103
}
83104

84-
return parts.slice(0, 2)
85-
}
105+
return parts.slice(0, 2);
106+
};
86107

87108
this.addSeparator = () => {
88-
var parts = this.numbers().split(this.options.decimal)
89-
parts[0] = parts[0].toString().replace(/(\d)(?=(?:\d{3})+\b)/gm, `$1${this.options.separator}`)
90-
return parts.join(this.options.decimal)
91-
}
109+
var parts = this.numbers().split(this.options.decimal);
110+
parts[0] = parts[0]
111+
.toString()
112+
.replace(/(\d)(?=(?:\d{3})+\b)/gm, `$1${this.options.separator}`);
113+
return parts.join(this.options.decimal);
114+
};
92115

93116
/**
94117
* Format the input with default config if there is no constructor config
95118
* @param {Number, String} input
96119
* @return {String}
97120
*/
98121
this.format = (input) => {
99-
if (input === '') return this.options.nullValue
100-
this.input = input || this.options.nullValue
101-
if (this.isNull()) return this.options.nullValue
102-
return this.sign() + this.options.prefix + this.addSeparator() + this.options.suffix
103-
}
122+
if (input === "") return this.options.nullValue;
123+
this.input = input || this.options.nullValue;
124+
if (this.isNull()) return this.options.nullValue;
125+
return (
126+
this.sign() +
127+
this.options.prefix +
128+
this.addSeparator() +
129+
this.options.suffix
130+
);
131+
};
104132

105133
/**
106134
* Unformat the input with default config if there is no constructor config
107135
* @param {Number, String} input
108136
* @return {String}
109137
*/
110138
this.unformat = (input) => {
111-
if (input === '') return this.options.nullValue
112-
this.input = input || this.options.nullValue
113-
if (this.isNull()) return this.options.nullValue
114-
return this.sign() + this.realNumber()
115-
}
139+
if (input === "") return this.options.nullValue;
140+
this.input = input || this.options.nullValue;
141+
if (this.isNull()) return this.options.nullValue;
142+
return this.sign() + this.realNumber();
143+
};
116144
}

0 commit comments

Comments
 (0)