input-format
Formatting user's text input on-the-fly
See Demo
GitHub Ban
On March 9th, 2020, GitHub, Inc. silently banned my account (and all my libraries) without any notice for an unknown reason. I opened a support ticked but they didn't answer. Because of that, I had to move all my libraries to GitLab.
Installation
npm install input-format --save
Usage
Start with defining parse()
and format()
functions:
parse()
function parses a single character from an input string (will be called for each character in the input string). After parsing each subsequent character, it returns the entire parsed value: string
so far.format()
function formats the entire parsed value back to a stringified representation. Returns an object of shape: { value: string, template: string }
.
Because "masked input" is a common use case, this library also provides parse()
and format()
function creators from a template:
templateParser()
creates a parse()
function from a template
.templateFormatter()
creates a format()
function from a template
.
An example for parsing and formatting a US phone number:
import { templateParser, templateFormatter, parseDigit } from 'input-format'
const TEMPLATE = '(xxx) xxx-xxxx'
const parse = templateParser(TEMPLATE, parseDigit)
const format = templateFormatter(TEMPLATE)
When parse()
and format()
functions have been defined, they should be used either in a DOM environment or in React.
React
import ReactInput from 'input-format/react'
const [phone, setPhone] = useState()
<ReactInput
value={phone}
onChange={setPhone}
parse={parse}
format={format}
/>
Phone: {phone}
DOM
import {
onChange,
onKeyDown
} from 'input-format'
const input = document.querySelector('input')
const optionalOnChangeListener = (value) => {
console.log('Value has changed:', value)
}
input.addEventListener('change', (event) => {
onChange(event, input, parse, format, optionalOnChangeListener)
})
input.addEventListener('keydown', (event) => {
onKeyDown(event, input, parse, format, optionalOnChangeListener)
})
Low-level API
This is an example of using the low-level API — the exported parse()
and format()
functions.
import { parse, format } from 'input-format'
function _parse(character, value) {
if (value.length < 10) {
if (character >= '0' && character <= '9') {
return character
}
}
}
function _format(value) {
...
return {
text: '(800) 555-3535',
template: '(xxx) xxx-xxxx'
}
}
let value
let text = '(800) 555-3535'
let caret = 4
{ value, caret } = parse(text, caret, _parse)
value === '8005553535'
caret === 2
{ text, caret } = format(value, caret, _format)
value === '(800) 555-3535'
caret === 4
Contributing
After cloning this repo, ensure dependencies are installed by running:
npm install
This module is written in ES6 and uses Babel for ES5
transpilation. Widely consumable JavaScript can be produced by running:
npm run build
Once npm run build
has run, you may import
or require()
directly from
node.
After developing, the full test suite can be evaluated by running:
npm test
When you're ready to test your new functionality on a real project, you can run
npm pack
It will build
, test
and then create a .tgz
archive which you can then install in your project folder
npm install [module name with version].tar.gz
License
MIT