Skip to content

Commit

Permalink
add support to arabic/persian number
Browse files Browse the repository at this point in the history
  • Loading branch information
lagden committed Mar 27, 2024
1 parent 75dec0d commit 9cb10ba
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 108 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
84 changes: 40 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

```
Expand All @@ -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

Expand All @@ -106,6 +99,9 @@ Codepen example: https://codepen.io/lagden/pen/jOrZVjg?editors=1010
</script>
```

## Buy Me a Coffee

BTC: bc1q7famhuj5f25n6qvlm3sssnymk2qpxrfwpyq7g4

## License

Expand Down
38 changes: 38 additions & 0 deletions __test__/currency.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<input id="money_euro" type="text" inputmode="numeric">
<input id="money_ice" type="text" inputmode="numeric">
<input id="money_jp" type="text" inputmode="numeric">
<input id="money_bhd" type="text" inputmode="numeric">
<script type="module">
/* eslint no-undef: 0 */
import Currency from '../src/currency.js'
Expand Down Expand Up @@ -65,12 +66,25 @@
}
})

const _money_bhd = new Currency(money_bhd, {
maskOpts: {
empty: true,
digits: 3,
locales: 'ar-BH',
options: {
style: 'currency',
currency: 'BHD'
}
}
})

console.debug({
_money,
_money_real,
_money_euro,
_money_ice,
_money_jp
_money_jp,
_money_bhd,
})
</script>
</body>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tadashi/currency",
"version": "3.3.3",
"version": "3.4.0",
"description": "The simple and tiny script for currency input mask",
"keywords": [
"currency",
Expand Down
Loading

0 comments on commit 9cb10ba

Please sign in to comment.