lagden / currency

The simple and tiny script for currency input mask

Home Page:https://codepen.io/lagden/pen/jOrZVjg?editors=1010

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Currency

Build Status Coverage Status

The simple and tiny script for currency input mask

Install

$ npm i @tadashi/currency

API

new Currency(input [, opts])

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 Optional configuration options for the instance.

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 Additional options for masking

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()
options Object no {minimumFractionDigits: 2} Additional formatting options - Intl.NumberFormat()
viaInput Boolean no false Whether the value is set via user input.

getUnmasked()

Converts the current masked input value to a numeric value.


Static methods

Currency.data(input)

Retrieves the instance associated with a given input element.

parameter type required default description
input HTMLInputElement yes - 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 Mask 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

Usage

Codepen example: https://codepen.io/lagden/pen/jOrZVjg?editors=1010

<input id="money" type="text" inputmode="numeric">

<script type="module">
  import Currency from 'https://unpkg.com/@tadashi/currency@{version}/src/currency.js'

  // Instance
  const mask = new Currency(money)

  // Static mode
  Currency.masking(1100) // => 1.100,00
</script>

License

MIT © Thiago Lagden

About

The simple and tiny script for currency input mask

https://codepen.io/lagden/pen/jOrZVjg?editors=1010

License:MIT License


Languages

Language:JavaScript 76.1%Language:Shell 15.0%Language:HTML 8.9%