1.1.3 • Published 5 years ago

thisisjustatest v1.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

Table of Contents

Installing

npm install mardigras

Template system

Before to show how to use MdG, it is important to show how its template system works. During the creation of a pattern for a single or multiple mask, you need to create a sequence of templating characters that represents each mask´s character type.

Templating characters

CharacterMeaningRange
#NumberAny number (0-9)
&LetterAny letter (a-z)
@AlphanumericNumber or letter
$Special charactersNot a number or letter

Separators

Separators are literal characters that separates each templating character to shape the mask. The allowed separators are:

Some Examples

Here are some examples of masks, using the template system;

  • Brazilian Zip Code (CEP) - #####-###
  • Canadian Zip Code - &#&#&#
  • French Car Plate - &&-###-&&
  • Spanish Car Plate - ####-&&&
  • ISBN Code - ###-##-#####-##-#
  • Phone Number - (##)#####-####
  • Serial Number - ####@@@@@####
  • Californian Driver License - @########

Usage

First step: import the Module

First, what you need to do is import MdG.

import mdg from 'mardigras';

Second step: Initialize MdG

Every mask in Mdg needs an initialization.

const someMask = mdg(options);

Options

The Mdg initial options is a Javascript Object with the following values:

ValueTypeDescriptionrequired
patternArray | StringSequence of templating characters and separators.Yes
onErrorFunctionMethod that will be called when the mask is NOT COMPLETED or INVALID. *No
onSucessFunctionMethod that will be called when the mask is COMPLETED or VALID. **No
validationFunctionMethod that check if the mask´s value is valid.No

* If validation is set, will be called only when the mask is INVALID. ** If validation is set, will be called only when the mask is VALID.

Example - Phone mask

In the following example, we are going to create a multi mask for a phone number. The sequence accepts 10 or 11 digits.

const error = () => {
  console.log('Almost There.');
}
const success = () => {
  console.log('Yayyy! The mask is completed.')
}
const phoneMask = mdg({
  pattern: [
  '(##)####-####', // 10 digits
  '(##)#####-####' // 11 digits
  ],
  onError: error,
  onSuccess: success
});

Third step: Choose your Parade

Still following the example above, in MdG, when it is intialized, you can choose the best way to use it.

Option 1 - HTML Input Field (checkField method)

The checkField method inserts the result of a mask directly on a HTML input field during its manipulation.

someMask.checkfield(inputField, eventType)
ParameterTypeDescriptionrequired
inputFieldDOM ElementElement that will be changed.Yes
eventTypeStringEvent name to change DOM Element. *No

* Default: input ...continuing the example:

/*
  HTML file ->
  <input type="text" class="js-mdg" value=""/>
*/
const inputField = document.querySelector('.js-mdg');
phoneMask.checkField(inputField);

Option 2 - Internally (checkValue method)

With checkValue method you can get the mask output internally and expose it the way you want. Best choice for Front-end Frameworks and another tools.

someMask.checkValue(inputValue)
ParameterTypeDescriptionrequired
inputValueStringValue to apply mask.Yes

When the method checkValue is called, it expose a Javascript Object with the following values:

ValueTypeDescription
inputStringThe initial value.
outputStringThe best final value.
completedBooleanIf the output value completes the mask.
isValidBoolean | NullIf the output value is valid. *

* Null if validation is not defined. ...continuing the example:

phoneMask.checkValue('12345'); // 5 digits long
/*
{input: "12345", output: "(12)345", completed: false, isValid: null}
*/
phoneMask.checkValue('1234512345'); // 10 digits long
/*
{input: "1234512345", output: "(12)3451-2345", completed: true, isValid: null}
*/
phoneMask.checkValue('12-34576-1234576') // 14 digits long
/*
{input: "12-34576-1234576", output: "(12)34576-1234", completed: true, isValid: null}
*/

What about validation

If the mask needs to pass through validation, the validation value must be set during the MdG initialization. It´s a function that MUST receive a single String and return a Boolean value. Taking the example above, let´s assume that the two first digits represent the country calling code and the only acceptable codes are from Argentina(54), Chile (56) and Colombia (57).

const countryValidation = ( output ) =>{
  const rule = /\(5[4|6|7]/;
  return rule.test(output)
}

License

This project is licensed under the MIT License - see the LICENSE.md file for details. Enjoy. Have yourself a lot of beads.

1.1.3

5 years ago

1.1.2

5 years ago