diff --git a/.eslintrc.yml b/.eslintrc.yml
index 49442ca..a482370 100644
--- a/.eslintrc.yml
+++ b/.eslintrc.yml
@@ -63,6 +63,7 @@ rules:
unicorn/prefer-query-selector: off
unicorn/prevent-abbreviations: off
- unicorn/prefer-code-point: off
+ # unicorn/prefer-code-point: off
unicorn/no-hex-escape: off
unicorn/escape-case: off
+ unicorn/prefer-negative-index: off
diff --git a/README.md b/README.md
index 348c10b..54c6869 100644
--- a/README.md
+++ b/README.md
@@ -1,17 +1,15 @@
# Currency
-[![Build Status][ci-img]][ci]
-[![Coverage Status][coveralls-img]][coveralls]
+[![Build Status][ci-img]][ci] [![Coverage Status][coveralls-img]][coveralls]
-[ci-img]: https://github.com/lagden/currency/actions/workflows/nodejs.yml/badge.svg
-[ci]: https://github.com/lagden/currency/actions/workflows/nodejs.yml
+[ci-img]: https://github.com/lagden/currency/actions/workflows/nodejs.yml/badge.svg
+[ci]: https://github.com/lagden/currency/actions/workflows/nodejs.yml
[coveralls-img]: https://coveralls.io/repos/github/lagden/currency/badge.svg?branch=main
-[coveralls]: https://coveralls.io/github/lagden/currency?branch=main
-
+[coveralls]: https://coveralls.io/github/lagden/currency?branch=main
+[number-format]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#syntax
The simple and tiny script for currency input mask
-
## Install
```
@@ -24,69 +22,64 @@ $ npm i @tadashi/currency
Constructs a new Currency instance for a given input element.
-parameter | type | required | default | description
------------ | -------------------- | ----------- | ------------------- | ------------
-input | HTMLInputElement | yes | - | The input element to associate with the instance.
-opts | Object | no | [see below](#opts) | Optional configuration options for the instance.
-
+| parameter | type | required | default | description |
+| --------- | ----------- | -------- | ------------------ | ----------------- |
+| input | HTMLElement | yes | - | The input element |
+| opts | Object | no | [see below](#opts) | Optional settings |
#### opts
-parameter | type | required | default | description
------------ | -------------------- | ----------- | ------------------- | ------------
-keyEvent | String | no | input | The type of key event to listen to
-triggerOnBlur | Boolean | no | false | Whether to trigger masking on blur
-init | Boolean | no | false | Whether to initialize masking on instance creation
-backspace | Boolean | no | false | Whether to handle backspace key input
-maskOpts | Object | no | [see below](#maskOpts) | Additional options for masking
-
+| parameter | type | required | default | description |
+| ------------- | ------- | -------- | ---------------------- | --------------------------------------- |
+| keyEvent | String | no | input | The event type for input |
+| triggerOnBlur | Boolean | no | false | Trigger event on blur |
+| init | Boolean | no | false | Initialize masking on instance creation |
+| backspace | Boolean | no | false | Handle backspace |
+| maskOpts | Object | no | [see below](#maskOpts) | Masking options |
#### maskOpts
-parameter | type | required | default | description
------------ | -------------------- | ----------- | ------------------- | ------------
-empty | Boolean | no | false | Whether to return an empty string for zero values
-locales | String or Array | no | pt-BR | The locale to use for formatting - [Intl.NumberFormat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#syntax)
-options | Object | no | {minimumFractionDigits: 2} | Additional formatting options - [Intl.NumberFormat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#syntax)
-viaInput | Boolean | no | false | Whether the value is set via user input.
-
+| parameter | type | required | default | description |
+| --------- | --------------- | -------- | ------- | ------------------------------------------------------------------------ |
+| digits | Number | no | 2 | The number of digits after the decimal point |
+| empty | Boolean | no | false | Allow empty value |
+| locales | String or Array | no | pt-BR | The locales to use for formatting - [Intl.NumberFormat()][number-format] |
+| options | Object | no | - | Additional options for formatting - [Intl.NumberFormat()][number-format] |
+| viaInput | Boolean | no | false | Specify if the value is coming directly from an input |
### getUnmasked()
-Converts the current masked input value to a numeric value.
+Get the unmasked value of the input.
---
-
### Static methods
#### Currency.data(input)
-Retrieves the instance associated with a given input element.
-
-parameter | type | required | default | description
------------ | -------------------- | ----------- | ------------------- | ------------
-input | HTMLInputElement | yes | - | Input element
+Check if input has a Currency instance.
+| parameter | type | required | default | description |
+| --------- | ---------------- | -------- | ------- | ----------------- |
+| input | HTMLInputElement | yes | - | The input element |
#### Currency.masking(v \[, opts\])
Formats a numeric value as a currency string with masking.
-parameter | type | required | default | description
------------ | -------------------- | ----------- | ------------------- | ------------
-v | String | yes | - | Numeric value or string to format
-opts | Object | no | [see above](#maskOpts) | Mask Options
-
+| parameter | type | required | default | description |
+| --------- | ------ | -------- | ---------------------- | --------------- |
+| v | String | Number | yes | - |
+| opts | Object | no | [see above](#maskOpts) | Masking options |
#### Currency.unmasking(v)
-Converts a masked currency value to a numeric value.
-
-parameter | type | required | default | description
------------ | -------------------- | ----------- | ------------------- | ------------
-v | String | yes | - | The masked currency value
+Convert a masked value into an unmasked numeric value.
+| parameter | type | required | default | description |
+| --------- | ------ | -------- | ------- | --------------------------------------------- |
+| v | String | yes | - | The masked input value. |
+| digits | Number | no | 2 | The number of digits after the decimal point. |
## Usage
@@ -106,6 +99,9 @@ Codepen example: https://codepen.io/lagden/pen/jOrZVjg?editors=1010
```
+## Buy Me a Coffee
+
+BTC: bc1q7famhuj5f25n6qvlm3sssnymk2qpxrfwpyq7g4
## License
diff --git a/__test__/currency.spec.js b/__test__/currency.spec.js
index 72cc446..708cde1 100644
--- a/__test__/currency.spec.js
+++ b/__test__/currency.spec.js
@@ -187,6 +187,44 @@ test('input iceland negative value', () => {
mask.destroy()
})
+test('static arabic', () => {
+ const v = Currency.masking('1911.55', {
+ digits: 3,
+ locales: 'ar-BH',
+ options: {
+ style: 'currency',
+ currency: 'BHD',
+ },
+ })
+ expect(v).toEqual('١٬٩١١٫٥٥٠ د.ب.')
+})
+
+test('input arabic', () => {
+ const input = document.querySelector('#money')
+ input.value = '12.99'
+ const mask = new Currency(input, {
+ init: true,
+ maskOpts: {
+ digits: 3,
+ locales: 'ar-BH',
+ options: {
+ style: 'currency',
+ currency: 'BHD',
+ },
+ },
+ })
+
+ expect(input.value).toEqual('١٢٫٩٩٠ د.ب.')
+
+ for (const char of '92') {
+ input.value += char
+ simulant.fire(input, 'input')
+ }
+ expect(input.value).toEqual('١٬٢٩٩٫٠٩٢ د.ب.')
+
+ mask.destroy()
+})
+
test('input', () => {
const input = document.querySelector('#money')
const mask = new Currency(input)
diff --git a/example/index.html b/example/index.html
index bbf3162..c1be613 100644
--- a/example/index.html
+++ b/example/index.html
@@ -19,6 +19,7 @@
+