Skip to content

Commit 92bea16

Browse files
author
Dipak Sarkar
committed
updated reverse fill method
1 parent dc03617 commit 92bea16

File tree

6 files changed

+58
-53
lines changed

6 files changed

+58
-53
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
# vue-number-format
88

9+
[![](docs/vue-number-format.gif)](https://vue-number-format.netlify.app)
10+
911
Vue Number Format is used to format a number using fixed-point notation. It can be used to format a number with a specific number of digits to the right of the decimal.
1012

1113
## Installation

docs/vue-number-format.gif

388 KB
Loading

src/number-format.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function NumberFormat(config = options) {
1111
this.isClean = false;
1212

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

1616
this.clean = (clean = false) => {
1717
this.isClean = clean;
@@ -121,7 +121,8 @@ export default function NumberFormat(config = options) {
121121
this.format = (input) => {
122122
if (input === "") return this.options.nullValue;
123123
this.input = input || this.options.nullValue;
124-
if (this.isNull()) return this.options.nullValue;
124+
if (this.isNull() && !this.options.reverseFill)
125+
return this.options.nullValue;
125126
return (
126127
this.sign() +
127128
this.options.prefix +
@@ -138,7 +139,8 @@ export default function NumberFormat(config = options) {
138139
this.unformat = (input) => {
139140
if (input === "") return this.options.nullValue;
140141
this.input = input || this.options.nullValue;
141-
if (this.isNull()) return this.options.nullValue;
142+
if (this.isNull() && !this.options.reverseFill)
143+
return this.options.nullValue;
142144
return this.sign() + this.realNumber();
143145
};
144146
}

tests/unit/number-format.custom.spec.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@ describe("when the value is invalid with custom config", () => {
2020
});
2121
it("should return as follows", () => {
2222
expect(numberFormat.format("")).toEqual("");
23-
expect(numberFormat.format("foo")).toEqual("$0");
24-
expect(numberFormat.format("-foo")).toEqual("$0");
25-
expect(numberFormat.format("-fo,o-")).toEqual("$0,");
26-
expect(numberFormat.format("-fo.o-")).toEqual("$0");
27-
expect(numberFormat.format("!@#$%^&*()")).toEqual("$0");
23+
expect(numberFormat.format("foo")).toEqual("");
24+
expect(numberFormat.format("-foo")).toEqual("");
25+
expect(numberFormat.format("-fo,o-")).toEqual("");
26+
expect(numberFormat.format("-fo.o-")).toEqual("");
27+
expect(numberFormat.format("!@#$%^&*()")).toEqual("");
2828
});
2929
it("should return as follows", () => {
3030
expect(numberFormat.clean(true).format("")).toEqual("");
31-
expect(numberFormat.clean(true).format("foo")).toEqual("$0");
32-
expect(numberFormat.clean(true).format("-foo")).toEqual("$0");
33-
expect(numberFormat.clean(true).format("-fo.o-")).toEqual("$0");
34-
expect(numberFormat.clean(true).format("-fo,o-")).toEqual("$0");
35-
expect(numberFormat.clean(true).format("!@#$%^&*()")).toEqual("$0");
31+
expect(numberFormat.clean(true).format("foo")).toEqual("");
32+
expect(numberFormat.clean(true).format("-foo")).toEqual("");
33+
expect(numberFormat.clean(true).format("-fo.o-")).toEqual("");
34+
expect(numberFormat.clean(true).format("-fo,o-")).toEqual("");
35+
expect(numberFormat.clean(true).format("!@#$%^&*()")).toEqual("");
3636
});
3737
it("should return as follows", () => {
3838
expect(numberFormat.clean(true).unformat("")).toEqual("");
39-
expect(numberFormat.clean(true).unformat("foo")).toEqual("0");
40-
expect(numberFormat.clean(true).unformat("-foo")).toEqual("0");
41-
expect(numberFormat.clean(true).unformat("-fo.o-")).toEqual("0");
42-
expect(numberFormat.clean(true).unformat("!@#$%^&*()")).toEqual("0");
39+
expect(numberFormat.clean(true).unformat("foo")).toEqual("");
40+
expect(numberFormat.clean(true).unformat("-foo")).toEqual("");
41+
expect(numberFormat.clean(true).unformat("-fo.o-")).toEqual("");
42+
expect(numberFormat.clean(true).unformat("!@#$%^&*()")).toEqual("");
4343
});
4444
});
4545
describe("format when options are custom", () => {
@@ -64,23 +64,23 @@ describe("format when options are custom", () => {
6464
expect(numberFormat.format("-12.345,,54321-")).toEqual("-$12.345,54321");
6565
});
6666
it("format numerical value", () => {
67-
expect(numberFormat.format(0)).toEqual("$0");
68-
expect(numberFormat.format(0)).toEqual("$0");
69-
expect(numberFormat.format(0.0)).toEqual("$0");
67+
expect(numberFormat.format(0)).toEqual("");
68+
expect(numberFormat.format(0)).toEqual("");
69+
expect(numberFormat.format(0.0)).toEqual("");
7070
expect(numberFormat.format(-0.1)).toEqual("-$0,1");
71-
expect(numberFormat.format(-0.0)).toEqual("$0");
71+
expect(numberFormat.format(-0.0)).toEqual("");
7272
expect(numberFormat.format(0.1)).toEqual("$0,1");
7373
expect(numberFormat.format(12345.54921)).toEqual("$12.345,54921");
7474
expect(numberFormat.format(12345.12345)).toEqual("$12.345,12345");
7575
expect(numberFormat.format(12345.54321)).toEqual("$12.345,54321");
7676
expect(numberFormat.format(12345.54321)).toEqual("$12.345,54321");
7777
});
7878
it("format and clean numerical value", () => {
79-
expect(numberFormat.clean(true).format(0)).toEqual("$0");
80-
expect(numberFormat.clean(true).format(0)).toEqual("$0");
81-
expect(numberFormat.clean(true).format(0.0)).toEqual("$0");
79+
expect(numberFormat.clean(true).format(0)).toEqual("");
80+
expect(numberFormat.clean(true).format(0)).toEqual("");
81+
expect(numberFormat.clean(true).format(0.0)).toEqual("");
8282
expect(numberFormat.clean(true).format(0.1)).toEqual("$0,1");
83-
expect(numberFormat.clean(true).format(-0.0)).toEqual("$0");
83+
expect(numberFormat.clean(true).format(-0.0)).toEqual("");
8484
expect(numberFormat.clean(true).format(-0.1)).toEqual("-$0,1");
8585
expect(numberFormat.clean(true).format(12345.54921)).toEqual("$12.345,55");
8686
expect(numberFormat.clean(true).format(12345.12345)).toEqual("$12.345,12");
@@ -116,11 +116,11 @@ describe("unformat when options are default", () => {
116116
);
117117
});
118118
it("unformat numerical value", () => {
119-
expect(numberFormat.clean(true).unformat(0)).toEqual("0");
120-
expect(numberFormat.clean(true).unformat(0)).toEqual("0");
121-
expect(numberFormat.clean(true).unformat(0.0)).toEqual("0");
119+
expect(numberFormat.clean(true).unformat(0)).toEqual("");
120+
expect(numberFormat.clean(true).unformat(0)).toEqual("");
121+
expect(numberFormat.clean(true).unformat(0.0)).toEqual("");
122122
expect(numberFormat.clean(true).unformat(-0.1)).toEqual("-0.1");
123-
expect(numberFormat.clean(true).unformat(-0.0)).toEqual("0");
123+
expect(numberFormat.clean(true).unformat(-0.0)).toEqual("");
124124
expect(numberFormat.clean(true).unformat(0.1)).toEqual("0.1");
125125
expect(numberFormat.clean(true).unformat(12345.54921)).toEqual("12345.55");
126126
expect(numberFormat.clean(true).unformat(12345.12345)).toEqual("12345.12");

tests/unit/number-format.default.spec.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ describe("when the value is invalid with default config", () => {
77
const numberFormat = new NumberFormat({});
88
it("should return as follows", () => {
99
expect(numberFormat.format("")).toEqual("");
10-
expect(numberFormat.format("foo")).toEqual("0");
11-
expect(numberFormat.format("-foo")).toEqual("0");
12-
expect(numberFormat.format("-fo,o-")).toEqual("0");
13-
expect(numberFormat.format("!@#$%^&*()")).toEqual("0");
10+
expect(numberFormat.format("foo")).toEqual("");
11+
expect(numberFormat.format("-foo")).toEqual("");
12+
expect(numberFormat.format("-fo,o-")).toEqual("");
13+
expect(numberFormat.format("!@#$%^&*()")).toEqual("");
1414
});
1515
it("should return as follows", () => {
1616
expect(numberFormat.clean(true).format("")).toEqual("");
17-
expect(numberFormat.clean(true).format("foo")).toEqual("0");
18-
expect(numberFormat.clean(true).format("-foo")).toEqual("0");
19-
expect(numberFormat.clean(true).format("-fo,o-")).toEqual("0");
20-
expect(numberFormat.clean(true).format("!@#$%^&*()")).toEqual("0");
17+
expect(numberFormat.clean(true).format("foo")).toEqual("");
18+
expect(numberFormat.clean(true).format("-foo")).toEqual("");
19+
expect(numberFormat.clean(true).format("-fo,o-")).toEqual("");
20+
expect(numberFormat.clean(true).format("!@#$%^&*()")).toEqual("");
2121
});
2222
it("should return as follows", () => {
2323
expect(numberFormat.unformat("")).toEqual("");
24-
expect(numberFormat.unformat("foo")).toEqual("0");
25-
expect(numberFormat.unformat("-foo")).toEqual("0");
26-
expect(numberFormat.unformat("-fo,o-")).toEqual("0");
27-
expect(numberFormat.unformat("!@#$%^&*()")).toEqual("0");
24+
expect(numberFormat.unformat("foo")).toEqual("");
25+
expect(numberFormat.unformat("-foo")).toEqual("");
26+
expect(numberFormat.unformat("-fo,o-")).toEqual("");
27+
expect(numberFormat.unformat("!@#$%^&*()")).toEqual("");
2828
});
2929
});
3030
describe("format when options are default", () => {
@@ -44,23 +44,23 @@ describe("format when options are default", () => {
4444
expect(numberFormat.format("-12,345..54321-")).toEqual("-12,345.54321");
4545
});
4646
it("format numerical value", () => {
47-
expect(numberFormat.format(0)).toEqual("0");
48-
expect(numberFormat.format(0)).toEqual("0");
49-
expect(numberFormat.format(0.0)).toEqual("0");
47+
expect(numberFormat.format(0)).toEqual("");
48+
expect(numberFormat.format(0)).toEqual("");
49+
expect(numberFormat.format(0.0)).toEqual("");
5050
expect(numberFormat.format(-0.1)).toEqual("-0.1");
51-
expect(numberFormat.format(-0.0)).toEqual("0");
51+
expect(numberFormat.format(-0.0)).toEqual("");
5252
expect(numberFormat.format(0.1)).toEqual("0.1");
5353
expect(numberFormat.format(12345.54921)).toEqual("12,345.54921");
5454
expect(numberFormat.format(12345.12345)).toEqual("12,345.12345");
5555
expect(numberFormat.format(12345.54321)).toEqual("12,345.54321");
5656
expect(numberFormat.format(12345.54321)).toEqual("12,345.54321");
5757
});
5858
it("format and clean numerical value", () => {
59-
expect(numberFormat.clean(true).format(0)).toEqual("0");
60-
expect(numberFormat.clean(true).format(0)).toEqual("0");
61-
expect(numberFormat.clean(true).format(0.0)).toEqual("0");
59+
expect(numberFormat.clean(true).format(0)).toEqual("");
60+
expect(numberFormat.clean(true).format(0)).toEqual("");
61+
expect(numberFormat.clean(true).format(0.0)).toEqual("");
6262
expect(numberFormat.clean(true).format(0.1)).toEqual("0.1");
63-
expect(numberFormat.clean(true).format(-0.0)).toEqual("0");
63+
expect(numberFormat.clean(true).format(-0.0)).toEqual("");
6464
expect(numberFormat.clean(true).format(-0.1)).toEqual("-0.1");
6565
expect(numberFormat.clean(true).format(12345.54921)).toEqual("12,345.55");
6666
expect(numberFormat.clean(true).format(12345.12345)).toEqual("12,345.12");
@@ -91,11 +91,11 @@ describe("unformat when options are default", () => {
9191
);
9292
});
9393
it("unformat numerical value", () => {
94-
expect(numberFormat.clean(true).unformat(0)).toEqual("0");
95-
expect(numberFormat.clean(true).unformat(0)).toEqual("0");
96-
expect(numberFormat.clean(true).unformat(0.0)).toEqual("0");
94+
expect(numberFormat.clean(true).unformat(0)).toEqual("");
95+
expect(numberFormat.clean(true).unformat(0)).toEqual("");
96+
expect(numberFormat.clean(true).unformat(0.0)).toEqual("");
9797
expect(numberFormat.clean(true).unformat(-0.1)).toEqual("-0.1");
98-
expect(numberFormat.clean(true).unformat(-0.0)).toEqual("0");
98+
expect(numberFormat.clean(true).unformat(-0.0)).toEqual("");
9999
expect(numberFormat.clean(true).unformat(0.1)).toEqual("0.1");
100100
expect(numberFormat.clean(true).unformat(12345.54921)).toEqual("12345.55");
101101
expect(numberFormat.clean(true).unformat(12345.12345)).toEqual("12345.12");

tests/unit/number-format.reverse-fill.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe("when enabled reverse fill", () => {
1818
expect(numberFormat.format("sdfgasd55468.546-")).toEqual("-$554,685.46");
1919
expect(numberFormat.format("-1234.6512")).toEqual("-$123,465.12");
2020
expect(numberFormat.format(0)).toEqual("$0.00");
21+
expect(numberFormat.format(0.0)).toEqual("$0.00");
2122
});
2223
it("should return as follows", () => {
2324
expect(numberFormat.unformat("sdfgasd55468.546")).toEqual("554685.46");

0 commit comments

Comments
 (0)