Skip to content

Commit ca5f8a9

Browse files
author
Dipak Sarkar
committed
added reverse fill option
1 parent 611cb53 commit ca5f8a9

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

src/number-format.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default function NumberFormat(opt = options) {
99
this.input = ''
1010
this.number = ''
1111
this.isClean = false
12-
this.isNull = (input = this.input) => !input.toString().replace(new RegExp('[^0-9]+', 'gi'), '')
12+
this.isNull = (input = this.input) => !this.numberOnly(input, new RegExp('[^0-9]+', 'gi'))
1313
this.clean = (clean = false) => {
1414
this.isClean = clean
1515
return this
@@ -18,16 +18,32 @@ export default function NumberFormat(opt = options) {
1818
const sign = (this.input.toString().indexOf('-') >= 0 && this.realNumber() > 0) ? '-' : ''
1919
return sign
2020
}
21+
function between(min, n, max) {
22+
return Math.max(min, Math.min(n, max))
23+
}
24+
// Uncaught RangeError: toFixed() digits argument must be between 0 and 20 at Number.toFixed
25+
function fixed(precision) {
26+
return between(0, precision, 20)
27+
}
28+
function toFixed(numbers, precision) {
29+
// eslint-disable-next-line no-restricted-properties
30+
var exp = Math.pow(10, precision)
31+
var float = parseFloat(numbers) / exp
32+
return float.toFixed(fixed(precision))
33+
}
2134
this.toNumber = (string) => Number(string)
35+
this.numberOnly = (string, regExp) => string.toString().replace(regExp, '')
2236
this.isNegative = this.sign() === '-'
2337
this.numbers = () => {
24-
if (typeof this.input === 'number') {
38+
if (this.options.reverseFill) {
39+
this.number = toFixed(this.numberOnly(this.input, /\D+/g), this.options.precision).replace('.', this.options.decimal)
40+
} else if (typeof this.input === 'number') {
2541
this.number = this.toNumber(this.input.toFixed(this.options.precision)).toString().replace('-', '').replace('.', this.options.decimal)
2642
// eslint-disable-next-line no-restricted-globals
2743
} else if (!isNaN(this.toNumber(this.input))) {
2844
this.number = this.input.replace('-', '').replace('.', this.options.decimal)
2945
} else {
30-
this.number = this.input.toString().replace(new RegExp(`[^0-9\\${this.options.decimal}]+`, 'gi'), '')
46+
this.number = this.numberOnly(this.input, new RegExp(`[^0-9\\${this.options.decimal}]+`, 'gi'))
3147
this.number = this.parts(this.number).join(this.options.decimal)
3248
}
3349
return this.number

src/options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
decimal: '.',
66
precision: 2,
77
prefill: true,
8-
reverse: false,
8+
reverseFill: false,
99
min: false,
1010
max: false,
1111
null_value: ''
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import NumberFormat from '../../src/number-format'
2+
3+
describe('when enabled reverse fill', () => {
4+
const numberFormat = new NumberFormat({
5+
reverseFill: true
6+
})
7+
it('should return as follows', () => {
8+
expect(numberFormat.format('sdfgasd55468.546')).toEqual('554,685.46')
9+
expect(numberFormat.format('sdfgasd55468.546')).toEqual('554,685.46')
10+
expect(numberFormat.format('sdfgasd55468.546-')).toEqual('-554,685.46')
11+
expect(numberFormat.format('-1234.6512')).toEqual('-123,465.12')
12+
})
13+
it('should return as follows', () => {
14+
expect(numberFormat.unformat('sdfgasd55468.546')).toEqual(554685.46)
15+
expect(numberFormat.unformat('sdfgasd55468.546')).toEqual(554685.46)
16+
expect(numberFormat.unformat('sdfgasd55468.546-')).toEqual(-554685.46)
17+
expect(numberFormat.unformat('-1234.6512')).toEqual(-123465.12)
18+
})
19+
})
20+
21+

0 commit comments

Comments
 (0)