From 83e06986a3cfd4f12b2c1307b0b410cdb249d6d3 Mon Sep 17 00:00:00 2001 From: Restartz Date: Sat, 11 Feb 2023 15:31:45 +0100 Subject: [PATCH 1/6] Allow for custom decimal precision --- packages/mask/src/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/mask/src/index.js b/packages/mask/src/index.js index 57939b8eb..4709efefb 100644 --- a/packages/mask/src/index.js +++ b/packages/mask/src/index.js @@ -163,7 +163,7 @@ export function buildUp(template, input) { return output } -export function formatMoney(input, delimiter = '.', thousands) { +export function formatMoney(input, delimiter = '.', thousands, precision = 2) { if (/^\D+$/.test(input)) return '9' thousands = thousands ?? (delimiter === "," ? "." : ",") @@ -192,7 +192,8 @@ export function formatMoney(input, delimiter = '.', thousands) { template = addThousands(template, thousands) - if (input.includes(delimiter)) template += `${delimiter}99` + if (precision > 0 && input.includes(delimiter)) + template += `${delimiter}` + '9'.repeat(precision) queueMicrotask(() => { if (this.el.value.endsWith(delimiter)) return From 5d8973dea7fcbcf9ca1b928b8565246ec1696f3f Mon Sep 17 00:00:00 2001 From: Restartz Date: Sat, 11 Feb 2023 15:32:46 +0100 Subject: [PATCH 2/6] add test --- tests/cypress/integration/plugins/mask.spec.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/cypress/integration/plugins/mask.spec.js b/tests/cypress/integration/plugins/mask.spec.js index c917f0a29..852cf1365 100644 --- a/tests/cypress/integration/plugins/mask.spec.js +++ b/tests/cypress/integration/plugins/mask.spec.js @@ -178,3 +178,18 @@ test('$money mask should remove letters or non numeric characters', get('input').type('40').should(haveValue('40')) } ) + +test('$money with custom decimal precision', + [html` + + + + + `], + ({ get }) => { + get('#0').type('1234.5678').should(haveValue('12,345,678')) + get('#1').type('1234.5678').should(haveValue('1,234.5')) + get('#2').type('1234.5678').should(haveValue('1,234.56')) + get('#3').type('1234.5678').should(haveValue('1,234.567')) + } +) From c83b80733df683e12d7fadff6e67574bbe2b3a74 Mon Sep 17 00:00:00 2001 From: Restartz Date: Sun, 12 Feb 2023 13:35:04 +0100 Subject: [PATCH 3/6] Changed formatMoney to accept an options object Changed formatMoney to accept an options object as the second parameter while also allowing backwards compatibility for any formatMoney(input, delimiter = '.', thousands) calls updated tests to use options object fixed typos in test add test based on initial value fix initial value test --- packages/mask/src/index.js | 14 +++++++++++--- tests/cypress/integration/plugins/mask.spec.js | 14 ++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/mask/src/index.js b/packages/mask/src/index.js index 4709efefb..c9c50a41f 100644 --- a/packages/mask/src/index.js +++ b/packages/mask/src/index.js @@ -163,10 +163,18 @@ export function buildUp(template, input) { return output } -export function formatMoney(input, delimiter = '.', thousands, precision = 2) { +export function formatMoney(input, options = {}) { if (/^\D+$/.test(input)) return '9' - - thousands = thousands ?? (delimiter === "," ? "." : ",") + + let delimiter = options.delimiter ?? '.' + let thousands = options.thousands ?? (delimiter === ',' ? '.' : ',') + let precision = options.precision ?? 2 + + // backwards compatibility for any formatMoney(input, delimiter = '.', thousands) calls + if (typeof options === 'string') { + delimiter = options + thousands = arguments[2] ?? (delimiter === ',' ? '.' : ',') + } let addThousands = (input, thousands) => { let output = '' diff --git a/tests/cypress/integration/plugins/mask.spec.js b/tests/cypress/integration/plugins/mask.spec.js index 852cf1365..b7370358c 100644 --- a/tests/cypress/integration/plugins/mask.spec.js +++ b/tests/cypress/integration/plugins/mask.spec.js @@ -181,15 +181,17 @@ test('$money mask should remove letters or non numeric characters', test('$money with custom decimal precision', [html` - - - - + + + + + `], ({ get }) => { get('#0').type('1234.5678').should(haveValue('12,345,678')) + get('#10').should(haveValue('1,234')) get('#1').type('1234.5678').should(haveValue('1,234.5')) - get('#2').type('1234.5678').should(haveValue('1,234.56')) - get('#3').type('1234.5678').should(haveValue('1,234.567')) + get('#2').type('1234,5678').should(haveValue('1.234,56')) + get('#3').type('1234,5678').should(haveValue('1 234,567')) } ) From 4ef681c3b6d4193dd43b9cdd3ba41214e8fec4f6 Mon Sep 17 00:00:00 2001 From: Restartz Date: Mon, 6 Mar 2023 21:11:32 +0100 Subject: [PATCH 4/6] Revert "Changed formatMoney to accept an options object" This reverts commit c83b80733df683e12d7fadff6e67574bbe2b3a74. --- packages/mask/src/index.js | 14 +++----------- tests/cypress/integration/plugins/mask.spec.js | 14 ++++++-------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/packages/mask/src/index.js b/packages/mask/src/index.js index c9c50a41f..4709efefb 100644 --- a/packages/mask/src/index.js +++ b/packages/mask/src/index.js @@ -163,18 +163,10 @@ export function buildUp(template, input) { return output } -export function formatMoney(input, options = {}) { +export function formatMoney(input, delimiter = '.', thousands, precision = 2) { if (/^\D+$/.test(input)) return '9' - - let delimiter = options.delimiter ?? '.' - let thousands = options.thousands ?? (delimiter === ',' ? '.' : ',') - let precision = options.precision ?? 2 - - // backwards compatibility for any formatMoney(input, delimiter = '.', thousands) calls - if (typeof options === 'string') { - delimiter = options - thousands = arguments[2] ?? (delimiter === ',' ? '.' : ',') - } + + thousands = thousands ?? (delimiter === "," ? "." : ",") let addThousands = (input, thousands) => { let output = '' diff --git a/tests/cypress/integration/plugins/mask.spec.js b/tests/cypress/integration/plugins/mask.spec.js index b7370358c..852cf1365 100644 --- a/tests/cypress/integration/plugins/mask.spec.js +++ b/tests/cypress/integration/plugins/mask.spec.js @@ -181,17 +181,15 @@ test('$money mask should remove letters or non numeric characters', test('$money with custom decimal precision', [html` - - - - - + + + + `], ({ get }) => { get('#0').type('1234.5678').should(haveValue('12,345,678')) - get('#10').should(haveValue('1,234')) get('#1').type('1234.5678').should(haveValue('1,234.5')) - get('#2').type('1234,5678').should(haveValue('1.234,56')) - get('#3').type('1234,5678').should(haveValue('1 234,567')) + get('#2').type('1234.5678').should(haveValue('1,234.56')) + get('#3').type('1234.5678').should(haveValue('1,234.567')) } ) From 93e936b2df92c30b2fbbac446ab45ec18f834a57 Mon Sep 17 00:00:00 2001 From: Restartz Date: Mon, 6 Mar 2023 21:44:40 +0100 Subject: [PATCH 5/6] Add money format precision documentation --- packages/docs/src/en/plugins/mask.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/docs/src/en/plugins/mask.md b/packages/docs/src/en/plugins/mask.md index 4b21fbac7..a671c7293 100644 --- a/packages/docs/src/en/plugins/mask.md +++ b/packages/docs/src/en/plugins/mask.md @@ -171,3 +171,16 @@ You may also choose to override the thousands separator by supplying a third opt + + +You can also override the default precision of 2 digits by using any desired number digits as the fourth optional argument: + +```alpine + +``` + + +
+ +
+ From 88eee0494aa940bcb3851fa2cc715b7f848f18f2 Mon Sep 17 00:00:00 2001 From: Josh Hanley Date: Tue, 7 Mar 2023 13:17:35 +1100 Subject: [PATCH 6/6] Update mask.md --- packages/docs/src/en/plugins/mask.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/docs/src/en/plugins/mask.md b/packages/docs/src/en/plugins/mask.md index e7d0b3718..e270a7b03 100644 --- a/packages/docs/src/en/plugins/mask.md +++ b/packages/docs/src/en/plugins/mask.md @@ -173,14 +173,14 @@ You may also choose to override the thousands separator by supplying a third opt -You can also override the default precision of 2 digits by using any desired number digits as the fourth optional argument: +You can also override the default precision of 2 digits by using any desired number of digits as the fourth optional argument: ```alpine - + ```
- +