Skip to content

Commit

Permalink
updated reverse fill method
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipak Sarkar committed Apr 1, 2022
1 parent dc03617 commit 92bea16
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 53 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

# vue-number-format

[![](docs/vue-number-format.gif)](https://vue-number-format.netlify.app)

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.

## Installation
Expand Down
Binary file added docs/vue-number-format.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions src/number-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function NumberFormat(config = options) {
this.isClean = false;

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

this.clean = (clean = false) => {
this.isClean = clean;
Expand Down Expand Up @@ -121,7 +121,8 @@ export default function NumberFormat(config = options) {
this.format = (input) => {
if (input === "") return this.options.nullValue;
this.input = input || this.options.nullValue;
if (this.isNull()) return this.options.nullValue;
if (this.isNull() && !this.options.reverseFill)
return this.options.nullValue;
return (
this.sign() +
this.options.prefix +
Expand All @@ -138,7 +139,8 @@ export default function NumberFormat(config = options) {
this.unformat = (input) => {
if (input === "") return this.options.nullValue;
this.input = input || this.options.nullValue;
if (this.isNull()) return this.options.nullValue;
if (this.isNull() && !this.options.reverseFill)
return this.options.nullValue;
return this.sign() + this.realNumber();
};
}
52 changes: 26 additions & 26 deletions tests/unit/number-format.custom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ describe("when the value is invalid with custom config", () => {
});
it("should return as follows", () => {
expect(numberFormat.format("")).toEqual("");
expect(numberFormat.format("foo")).toEqual("$0");
expect(numberFormat.format("-foo")).toEqual("$0");
expect(numberFormat.format("-fo,o-")).toEqual("$0,");
expect(numberFormat.format("-fo.o-")).toEqual("$0");
expect(numberFormat.format("!@#$%^&*()")).toEqual("$0");
expect(numberFormat.format("foo")).toEqual("");
expect(numberFormat.format("-foo")).toEqual("");
expect(numberFormat.format("-fo,o-")).toEqual("");
expect(numberFormat.format("-fo.o-")).toEqual("");
expect(numberFormat.format("!@#$%^&*()")).toEqual("");
});
it("should return as follows", () => {
expect(numberFormat.clean(true).format("")).toEqual("");
expect(numberFormat.clean(true).format("foo")).toEqual("$0");
expect(numberFormat.clean(true).format("-foo")).toEqual("$0");
expect(numberFormat.clean(true).format("-fo.o-")).toEqual("$0");
expect(numberFormat.clean(true).format("-fo,o-")).toEqual("$0");
expect(numberFormat.clean(true).format("!@#$%^&*()")).toEqual("$0");
expect(numberFormat.clean(true).format("foo")).toEqual("");
expect(numberFormat.clean(true).format("-foo")).toEqual("");
expect(numberFormat.clean(true).format("-fo.o-")).toEqual("");
expect(numberFormat.clean(true).format("-fo,o-")).toEqual("");
expect(numberFormat.clean(true).format("!@#$%^&*()")).toEqual("");
});
it("should return as follows", () => {
expect(numberFormat.clean(true).unformat("")).toEqual("");
expect(numberFormat.clean(true).unformat("foo")).toEqual("0");
expect(numberFormat.clean(true).unformat("-foo")).toEqual("0");
expect(numberFormat.clean(true).unformat("-fo.o-")).toEqual("0");
expect(numberFormat.clean(true).unformat("!@#$%^&*()")).toEqual("0");
expect(numberFormat.clean(true).unformat("foo")).toEqual("");
expect(numberFormat.clean(true).unformat("-foo")).toEqual("");
expect(numberFormat.clean(true).unformat("-fo.o-")).toEqual("");
expect(numberFormat.clean(true).unformat("!@#$%^&*()")).toEqual("");
});
});
describe("format when options are custom", () => {
Expand All @@ -64,23 +64,23 @@ describe("format when options are custom", () => {
expect(numberFormat.format("-12.345,,54321-")).toEqual("-$12.345,54321");
});
it("format numerical value", () => {
expect(numberFormat.format(0)).toEqual("$0");
expect(numberFormat.format(0)).toEqual("$0");
expect(numberFormat.format(0.0)).toEqual("$0");
expect(numberFormat.format(0)).toEqual("");
expect(numberFormat.format(0)).toEqual("");
expect(numberFormat.format(0.0)).toEqual("");
expect(numberFormat.format(-0.1)).toEqual("-$0,1");
expect(numberFormat.format(-0.0)).toEqual("$0");
expect(numberFormat.format(-0.0)).toEqual("");
expect(numberFormat.format(0.1)).toEqual("$0,1");
expect(numberFormat.format(12345.54921)).toEqual("$12.345,54921");
expect(numberFormat.format(12345.12345)).toEqual("$12.345,12345");
expect(numberFormat.format(12345.54321)).toEqual("$12.345,54321");
expect(numberFormat.format(12345.54321)).toEqual("$12.345,54321");
});
it("format and clean numerical value", () => {
expect(numberFormat.clean(true).format(0)).toEqual("$0");
expect(numberFormat.clean(true).format(0)).toEqual("$0");
expect(numberFormat.clean(true).format(0.0)).toEqual("$0");
expect(numberFormat.clean(true).format(0)).toEqual("");
expect(numberFormat.clean(true).format(0)).toEqual("");
expect(numberFormat.clean(true).format(0.0)).toEqual("");
expect(numberFormat.clean(true).format(0.1)).toEqual("$0,1");
expect(numberFormat.clean(true).format(-0.0)).toEqual("$0");
expect(numberFormat.clean(true).format(-0.0)).toEqual("");
expect(numberFormat.clean(true).format(-0.1)).toEqual("-$0,1");
expect(numberFormat.clean(true).format(12345.54921)).toEqual("$12.345,55");
expect(numberFormat.clean(true).format(12345.12345)).toEqual("$12.345,12");
Expand Down Expand Up @@ -116,11 +116,11 @@ describe("unformat when options are default", () => {
);
});
it("unformat numerical value", () => {
expect(numberFormat.clean(true).unformat(0)).toEqual("0");
expect(numberFormat.clean(true).unformat(0)).toEqual("0");
expect(numberFormat.clean(true).unformat(0.0)).toEqual("0");
expect(numberFormat.clean(true).unformat(0)).toEqual("");
expect(numberFormat.clean(true).unformat(0)).toEqual("");
expect(numberFormat.clean(true).unformat(0.0)).toEqual("");
expect(numberFormat.clean(true).unformat(-0.1)).toEqual("-0.1");
expect(numberFormat.clean(true).unformat(-0.0)).toEqual("0");
expect(numberFormat.clean(true).unformat(-0.0)).toEqual("");
expect(numberFormat.clean(true).unformat(0.1)).toEqual("0.1");
expect(numberFormat.clean(true).unformat(12345.54921)).toEqual("12345.55");
expect(numberFormat.clean(true).unformat(12345.12345)).toEqual("12345.12");
Expand Down
48 changes: 24 additions & 24 deletions tests/unit/number-format.default.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ describe("when the value is invalid with default config", () => {
const numberFormat = new NumberFormat({});
it("should return as follows", () => {
expect(numberFormat.format("")).toEqual("");
expect(numberFormat.format("foo")).toEqual("0");
expect(numberFormat.format("-foo")).toEqual("0");
expect(numberFormat.format("-fo,o-")).toEqual("0");
expect(numberFormat.format("!@#$%^&*()")).toEqual("0");
expect(numberFormat.format("foo")).toEqual("");
expect(numberFormat.format("-foo")).toEqual("");
expect(numberFormat.format("-fo,o-")).toEqual("");
expect(numberFormat.format("!@#$%^&*()")).toEqual("");
});
it("should return as follows", () => {
expect(numberFormat.clean(true).format("")).toEqual("");
expect(numberFormat.clean(true).format("foo")).toEqual("0");
expect(numberFormat.clean(true).format("-foo")).toEqual("0");
expect(numberFormat.clean(true).format("-fo,o-")).toEqual("0");
expect(numberFormat.clean(true).format("!@#$%^&*()")).toEqual("0");
expect(numberFormat.clean(true).format("foo")).toEqual("");
expect(numberFormat.clean(true).format("-foo")).toEqual("");
expect(numberFormat.clean(true).format("-fo,o-")).toEqual("");
expect(numberFormat.clean(true).format("!@#$%^&*()")).toEqual("");
});
it("should return as follows", () => {
expect(numberFormat.unformat("")).toEqual("");
expect(numberFormat.unformat("foo")).toEqual("0");
expect(numberFormat.unformat("-foo")).toEqual("0");
expect(numberFormat.unformat("-fo,o-")).toEqual("0");
expect(numberFormat.unformat("!@#$%^&*()")).toEqual("0");
expect(numberFormat.unformat("foo")).toEqual("");
expect(numberFormat.unformat("-foo")).toEqual("");
expect(numberFormat.unformat("-fo,o-")).toEqual("");
expect(numberFormat.unformat("!@#$%^&*()")).toEqual("");
});
});
describe("format when options are default", () => {
Expand All @@ -44,23 +44,23 @@ describe("format when options are default", () => {
expect(numberFormat.format("-12,345..54321-")).toEqual("-12,345.54321");
});
it("format numerical value", () => {
expect(numberFormat.format(0)).toEqual("0");
expect(numberFormat.format(0)).toEqual("0");
expect(numberFormat.format(0.0)).toEqual("0");
expect(numberFormat.format(0)).toEqual("");
expect(numberFormat.format(0)).toEqual("");
expect(numberFormat.format(0.0)).toEqual("");
expect(numberFormat.format(-0.1)).toEqual("-0.1");
expect(numberFormat.format(-0.0)).toEqual("0");
expect(numberFormat.format(-0.0)).toEqual("");
expect(numberFormat.format(0.1)).toEqual("0.1");
expect(numberFormat.format(12345.54921)).toEqual("12,345.54921");
expect(numberFormat.format(12345.12345)).toEqual("12,345.12345");
expect(numberFormat.format(12345.54321)).toEqual("12,345.54321");
expect(numberFormat.format(12345.54321)).toEqual("12,345.54321");
});
it("format and clean numerical value", () => {
expect(numberFormat.clean(true).format(0)).toEqual("0");
expect(numberFormat.clean(true).format(0)).toEqual("0");
expect(numberFormat.clean(true).format(0.0)).toEqual("0");
expect(numberFormat.clean(true).format(0)).toEqual("");
expect(numberFormat.clean(true).format(0)).toEqual("");
expect(numberFormat.clean(true).format(0.0)).toEqual("");
expect(numberFormat.clean(true).format(0.1)).toEqual("0.1");
expect(numberFormat.clean(true).format(-0.0)).toEqual("0");
expect(numberFormat.clean(true).format(-0.0)).toEqual("");
expect(numberFormat.clean(true).format(-0.1)).toEqual("-0.1");
expect(numberFormat.clean(true).format(12345.54921)).toEqual("12,345.55");
expect(numberFormat.clean(true).format(12345.12345)).toEqual("12,345.12");
Expand Down Expand Up @@ -91,11 +91,11 @@ describe("unformat when options are default", () => {
);
});
it("unformat numerical value", () => {
expect(numberFormat.clean(true).unformat(0)).toEqual("0");
expect(numberFormat.clean(true).unformat(0)).toEqual("0");
expect(numberFormat.clean(true).unformat(0.0)).toEqual("0");
expect(numberFormat.clean(true).unformat(0)).toEqual("");
expect(numberFormat.clean(true).unformat(0)).toEqual("");
expect(numberFormat.clean(true).unformat(0.0)).toEqual("");
expect(numberFormat.clean(true).unformat(-0.1)).toEqual("-0.1");
expect(numberFormat.clean(true).unformat(-0.0)).toEqual("0");
expect(numberFormat.clean(true).unformat(-0.0)).toEqual("");
expect(numberFormat.clean(true).unformat(0.1)).toEqual("0.1");
expect(numberFormat.clean(true).unformat(12345.54921)).toEqual("12345.55");
expect(numberFormat.clean(true).unformat(12345.12345)).toEqual("12345.12");
Expand Down
1 change: 1 addition & 0 deletions tests/unit/number-format.reverse-fill.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe("when enabled reverse fill", () => {
expect(numberFormat.format("sdfgasd55468.546-")).toEqual("-$554,685.46");
expect(numberFormat.format("-1234.6512")).toEqual("-$123,465.12");
expect(numberFormat.format(0)).toEqual("$0.00");
expect(numberFormat.format(0.0)).toEqual("$0.00");
});
it("should return as follows", () => {
expect(numberFormat.unformat("sdfgasd55468.546")).toEqual("554685.46");
Expand Down

0 comments on commit 92bea16

Please sign in to comment.