Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
core-text-mask
Advanced tools
Readme
This module contains the core functions that power Text Mask. Currently, Text Mask has a wrapper for React, which can be used directly.
However, Text Mask Core functions could be useful on their own. That's why they are published and documented here as a separate module.
To download the script, use npm.
npm i core-text-mask --save
After installing with npm, you could possibly do something like this from your index.html
:
<script src="./node_modules/core-text-mask/dist/textMaskCore.js"></script>
Including this file in your source code will expose the global object textMaskCore
.
Or if you're using Node.js or a bundler such as webpack or Browserify, you can require
textMaskCore
as such:
var textMaskCore = require('core-text-mask')
textMaskCore
exposes three functions:
The general idea is to take user input, conform it to your desired mask using conformToMask
,
and then apply the output of conformToMask
to the value of the HTML input element.
Once you do that however, the caret position will be lost. You can then use adjustCaretPosition
to restore the caret to its proper position.
To learn more about how to do that, see the API documentation below:
convertMaskToPlaceholder(mask)
This function takes a mask (string), i.e. 11/11/1111
, and returns a placeholder (string).
const placeholder = convertMaskToPlaceholder('11/11/1111')
placeholder // __/__/____
You can use this function to initialize an input
element to a placeholder value.
conformToMask(userInput, mask, config)
This function takes three arguments:
This function returns an object shaped as {output: 'someConformedString', meta: {...}}'
.
output
is gonna contain the conformed string. As for meta
, it contains some meta data
about the operation. This meta data is needed by adjustCaretPosition
function,
which is documented below.
The config
object takes the following values
guide
(boolean) (defaults to true
) (required): this tells conformToMask
whether you want the conformed
string to contain a guide or no. The guide
is basically the placeholder character and the
mask hard characters. For example, with mask (111) 111-1111
, input 123
with guide
set to
true
would return (123) ___-____
. With guide
set to false
, it would return (123)
.
previousConformedInput
(string) (required): this is the previous output
of conformToMask
.
If you're calling conformToMask
for the first time, pass this as an empty string or undefined
.
placeholderCharacter
(string) (optional): for documentation on this key, see this section of the component
documentation page.
validator
(function) (optional): this function will receive one
argument, the conformed string. The function should return a boolean. If the function returned
true
, the output
of conformToMask
will be the newly conformed string. If the function returned
false
, the output
of conformToMask
will be the previousConformedInput
const results = conformToMask('5554833902', '(111) 111-1111')
results // {output: '(555) 483-3902', meta: {...some meta data}}
Whenever the value of the input
element changes, you can pass that value to conformToMask
and it'll make sure that the string looks like the given mask. You can then set that conformed
string as the new value of the input
element.
adjustCaretPosition(argumentsObject)
When you set the value of the input
element, you lose the position of the caret. This function
helps you restore the position.
adjustCaretPosition
takes the following object of arguments:
input
before the last time you set
its value. If you're calling this function for the first time, you can pass an empty string.conformToMask
adjustCaretPosition
will return an integer representing the index of where the caret should be
moved to next.
Public domain - CC0 1.0 Universal
FAQs
Core of https://github.com/msafi/text-mask
The npm package core-text-mask receives a total of 1 weekly downloads. As such, core-text-mask popularity was classified as not popular.
We found that core-text-mask demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.