Skip to content

Commit

Permalink
fix mask for ISK and JPY
Browse files Browse the repository at this point in the history
  • Loading branch information
lagden committed Mar 27, 2024
1 parent 4455de1 commit 75dec0d
Show file tree
Hide file tree
Showing 7 changed files with 1,093 additions and 936 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ rules:
# unicorn/prefer-object-from-entries: off
unicorn/prefer-query-selector: off
unicorn/prevent-abbreviations: off

unicorn/prefer-code-point: off
unicorn/no-hex-escape: off
unicorn/escape-case: off
8 changes: 4 additions & 4 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
node-version: [20]

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Install pnpm
run: npm i -g pnpm

- name: Install dependencies
run: pnpm install --no-frozen-lockfile
run: pnpm install

- name: Run test
run: npm test
Expand Down
73 changes: 65 additions & 8 deletions __test__/currency.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test('static mask number', () => {
currency: 'BRL',
},
})
expect(v).toEqual('R$ 1.100,00')
expect(v).toEqual('R$\xa01.100,00')
})

test('static mask number negative', () => {
Expand All @@ -65,7 +65,7 @@ test('static mask number negative', () => {
currency: 'BRL',
},
})
expect(v).toEqual('-R$ 1.100,00')
expect(v).toEqual('-R$\xa01.100,00')
})

test('static mask number 0', () => {
Expand All @@ -75,7 +75,7 @@ test('static mask number 0', () => {
currency: 'BRL',
},
})
expect(v).toEqual('R$ 0,00')
expect(v).toEqual('R$\xa00,00')
})

test('static mask number fraction', () => {
Expand All @@ -85,7 +85,7 @@ test('static mask number fraction', () => {
currency: 'BRL',
},
})
expect(v).toEqual('R$ 5.500,00')
expect(v).toEqual('R$\xa05.500,00')
})

test('static mask number string', () => {
Expand All @@ -95,7 +95,7 @@ test('static mask number string', () => {
currency: 'BRL',
},
})
expect(v).toEqual('R$ 1.111,00')
expect(v).toEqual('R$\xa01.111,00')
})

test('static mask number 0 empty', () => {
Expand All @@ -116,7 +116,7 @@ test('static mask number string fraction', () => {
currency: 'BRL',
},
})
expect(v).toEqual('R$ 1.111,00')
expect(v).toEqual('R$\xa01.111,00')
})

test('static euro', () => {
Expand All @@ -127,7 +127,64 @@ test('static euro', () => {
currency: 'EUR',
},
})
expect(v).toEqual('1.111,00 €')
expect(v).toEqual('1.111,00\xa0€')
})

test('static iceland', () => {
const v = Currency.masking('1111.55', {
locales: 'is',
options: {
style: 'currency',
currency: 'ISK',
},
})
expect(v).toEqual('1.112\xa0kr.')
})

test('input iceland', () => {
const input = document.querySelector('#money')
input.value = '12.99'
const mask = new Currency(input, {
init: true,
maskOpts: {
locales: 'is',
options: {
style: 'currency',
currency: 'ISK',
},
},
})

expect(input.value).toEqual('13\xa0kr.')

for (const char of '92') {
input.value += char
simulant.fire(input, 'input')
}
expect(input.value).toEqual('1.392\xa0kr.')

mask.destroy()
})

test('input iceland negative value', () => {
const input = document.querySelector('#money')
const mask = new Currency(input, {
maskOpts: {
locales: 'is',
options: {
style: 'currency',
currency: 'ISK',
},
},
})

for (const char of '-2468') {
input.value += char
simulant.fire(input, 'input')
}
expect(input.value).toEqual('-2.468\xa0kr.')

mask.destroy()
})

test('input', () => {
Expand Down Expand Up @@ -170,7 +227,7 @@ test('keyup', () => {
simulant.fire(input, 'keyup')
}

expect(input.value).toEqual('1.111,99 €')
expect(input.value).toEqual('1.111,99\xa0€')
mask.destroy()
})

Expand Down
39 changes: 38 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, user-scalable=no">
<title>Example</title>
<style>
body {
display: flex;
flex-direction: column;
gap: 1em;
}
</style>
</head>
<body>
<input id="money" type="text" inputmode="numeric">
<input id="money_real" type="text" inputmode="numeric">
<input id="money_euro" type="text" inputmode="numeric">
<input id="money_ice" type="text" inputmode="numeric">
<input id="money_jp" type="text" inputmode="numeric">
<script type="module">
/* eslint no-undef: 0 */
import Currency from '../src/currency.js'
Expand All @@ -34,7 +43,35 @@
}
})

console.debug(_money, _money_real, _money_euro)
const _money_ice = new Currency(money_ice, {
maskOpts: {
empty: true,
locales: 'is',
options: {
style: 'currency',
currency: 'ISK'
}
}
})

const _money_jp = new Currency(money_jp, {
maskOpts: {
empty: true,
locales: 'ja-JP',
options: {
style: 'currency',
currency: 'JPY'
}
}
})

console.debug({
_money,
_money_real,
_money_euro,
_money_ice,
_money_jp
})
</script>
</body>
</html>
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tadashi/currency",
"version": "3.3.2",
"version": "3.3.3",
"description": "The simple and tiny script for currency input mask",
"keywords": [
"currency",
Expand Down Expand Up @@ -38,11 +38,11 @@
"test": "jest test --coverage --coverageReporters=lcov --coverageReporters=text"
},
"devDependencies": {
"@babel/preset-env": "7.23.2",
"@testing-library/dom": "9.3.3",
"@testing-library/user-event": "14.5.1",
"eslint": "8.52.0",
"eslint-plugin-unicorn": "48.0.1",
"@babel/preset-env": "7.24.3",
"@testing-library/dom": "9.3.4",
"@testing-library/user-event": "14.5.2",
"eslint": "8.57.0",
"eslint-plugin-unicorn": "51.0.1",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"simulant": "0.2.2"
Expand Down
Loading

0 comments on commit 75dec0d

Please sign in to comment.