Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
text-mask-core
Advanced tools
This module contains the core functions that power Text Mask. Text Mask has wrappers for Angular1, Angular2, Ember, React and Vue 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 text-mask-core --save
After installing with npm, you could possibly do something like this from your index.html
:
<script src="./node_modules/text-mask-core/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('text-mask-core')
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.
createTextMaskInputElement(config)
This function takes a configuration and returns an object with an update
method. The update
method is used to conform the raw value to the mask you provide in the config.
// the config requires a `mask` and a reference to an `input` element.
const textMaskConfig = {inputElement, mask}
// initialize text mask
const textMaskInputElement = createTextMaskInputElement(textMaskConfig)
// call `update` to conform the `inputElement.value` to the provided `mask`.
textMaskInputElement.update()
The textMaskConfig
requires a mask
and a reference to the inputElement
. See the documentation here for more information on the properties that the textMaskConfig
accepts.
The default use-case is for the textMaskConfig
to be passed to the createTextMaskInputElement
method when you initialize Text Mask. However, you can also pass the value
and textMaskConfig
to the update
method.
const textMaskConfig = {inputElement, mask}
// initialize text mask without a config (or with a default config)
const textMaskInputElement = createTextMaskInputElement()
// call `update` with the raw value and config
textMaskInputElement.update(inputElement.value, textMaskConfig)
The update
method should be called every time the inputElement.value
changes.
conformToMask(rawValue, mask, config)
This function takes three arguments:
This function returns an object with a property conformedValue
(string).
const results = conformToMask('5554833902', ['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/])
results.conformedValue // '(555) 483-3902'
The config
object takes the following values
guide
(boolean) (defaults to true
): 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 ['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]
, input 123
with guide
set to
true
would return (123) ___-____
. With guide
set to false
, it would return (123)
.
previousConformedValue
(string) (required): this is the previous output
of conformToMask
.
If you're calling conformToMask
for the first time, you don't have to pass this value.
placeholderChar
(string) (optional): for documentation on this key, see this section of the component
documentation page.
const results = conformToMask('5554833902', ['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/])
results.conformedValue // '(555) 483-3902'
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:
previousConformedValue
(string): the string value of the 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.conformedValue
(string): the conformedValue
returned from the last call to conformToMask
currentCaretPosition
(integer): the position of the caret right before you called this
functionrawValue
(string): value of the input elementplaceholderChar
(string): placeholder characterplaceholder
(string): the generated placeholderindexesOfPipedChars
(array): an array of piped characters returned from the last call to the pipe
functioncaretTrapIndexes
(array): an array of caret trap indexesadjustCaretPosition
will return an integer representing the index of where the caret should be
moved to next.
FAQs
Core of https://github.com/text-mask/text-mask
The npm package text-mask-core receives a total of 61,087 weekly downloads. As such, text-mask-core popularity was classified as popular.
We found that text-mask-core demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.