@tadashi/mask
Advanced tools
Comparing version 1.4.1 to 1.5.0
@@ -1,99 +0,91 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global = global || self, global.Mask = factory()); | ||
}(this, function () { 'use strict'; | ||
const map = new Map(); | ||
map.set('9', /\d/); | ||
map.set('A', /[\da-zA-Z]/); | ||
map.set('S', /[a-zA-Z]/); | ||
const map = new Map(); | ||
map.set('9', /[\d]/); | ||
map.set('A', /[0-9a-zA-Z]/); | ||
map.set('S', /[a-zA-Z]/); | ||
const instances = new Map(); | ||
const instances = new Map(); | ||
const GUID = Symbol('GUID'); | ||
const EVENT = Symbol('EVENT'); | ||
const GUID = Symbol('GUID'); | ||
const EVENT = Symbol('EVENT'); | ||
class Mask { | ||
static data(input) { | ||
return instances.has(input[GUID]) && instances.get(input[GUID]) | ||
} | ||
class Mask { | ||
static data(input) { | ||
return instances.has(input[GUID]) && instances.get(input[GUID]) | ||
} | ||
static masking(_value, _mask) { | ||
const value = String(_value).replace(/[^\da-zA-Z]/g, ''); | ||
const mask = String(_mask); | ||
static masking(_value, _mask) { | ||
const value = String(_value).replace(/[^0-9a-zA-Z]/g, ''); | ||
const mask = String(_mask); | ||
const res = []; | ||
let cc = 0; | ||
const res = []; | ||
let cc = 0; | ||
for (let i = 0; i < mask.length; i++) { | ||
const char = mask.charAt(i); | ||
if (value.length > cc) { | ||
if (map.has(char)) { | ||
if (map.get(char).test(value.charAt(cc))) { | ||
res.push(value.charAt(cc++)); | ||
} else { | ||
break | ||
} | ||
for (let i = 0; i < mask.length; i++) { | ||
const char = mask.charAt(i); | ||
if (value.length > cc) { | ||
if (map.has(char)) { | ||
if (map.get(char).test(value.charAt(cc))) { | ||
res.push(value.charAt(cc++)); | ||
} else { | ||
res.push(char); | ||
break | ||
} | ||
} else { | ||
res.push(char); | ||
} | ||
} | ||
return res.join('') | ||
} | ||
constructor(input, mask = '', keyEvent = 'input') { | ||
if (input instanceof HTMLInputElement === false) { | ||
throw new TypeError('The input should be a HTMLInputElement') | ||
} | ||
return res.join('') | ||
} | ||
// Check if element has an instance | ||
const instance = Mask.data(input); | ||
if (instance instanceof Mask) { | ||
return instance | ||
} | ||
constructor(input, mask = '', keyEvent = 'input') { | ||
if (input instanceof HTMLInputElement === false) { | ||
throw new TypeError('The input should be a HTMLInputElement') | ||
} | ||
this.input = input; | ||
this.mask = input.dataset.mask || mask; | ||
// Check if element has an instance | ||
const instance = Mask.data(input); | ||
if (instance instanceof Mask) { | ||
return instance | ||
} | ||
// Check if has mask | ||
if (this.mask.length === 0) { | ||
throw new Error('The mask can not be empty') | ||
} | ||
this.input = input; | ||
this.mask = input.dataset.mask || mask; | ||
// Listener | ||
this[EVENT] = keyEvent; | ||
this.input.addEventListener(this[EVENT], this); | ||
// Storage instance | ||
this.input[GUID] = `${Math.random()}_${Date.now()}`; | ||
instances.set(this.input[GUID], this); | ||
// Check if has mask | ||
if (this.mask.length === 0) { | ||
throw new Error('The mask can not be empty') | ||
} | ||
masking(event) { | ||
/* istanbul ignore next */ | ||
if (event && event.inputType === 'deleteContentBackward') { | ||
return false | ||
} | ||
// Listener | ||
this[EVENT] = keyEvent; | ||
this.input.addEventListener(this[EVENT], this); | ||
this.input.value = Mask.masking(this.input.value, this.mask); | ||
} | ||
// Storage instance | ||
this.input[GUID] = `${Math.random()}_${Date.now()}`; | ||
instances.set(this.input[GUID], this); | ||
} | ||
destroy() { | ||
this.input.removeEventListener(this[EVENT], this); | ||
if (instances.has(this.input[GUID])) { | ||
instances.delete(this.input[GUID]); | ||
} | ||
masking(event) { | ||
/* istanbul ignore next */ | ||
if (event && event.inputType === 'deleteContentBackward') { | ||
return false | ||
} | ||
handleEvent(event) { | ||
this.masking(event); | ||
this.input.value = Mask.masking(this.input.value, this.mask); | ||
} | ||
destroy() { | ||
this.input.removeEventListener(this[EVENT], this); | ||
if (instances.has(this.input[GUID])) { | ||
instances.delete(this.input[GUID]); | ||
} | ||
} | ||
return Mask; | ||
handleEvent(event) { | ||
this.masking(event); | ||
} | ||
} | ||
})); | ||
export default Mask; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@tadashi/mask", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"description": "The simple and tiny script for input mask", | ||
@@ -20,3 +20,3 @@ "keywords": [ | ||
}, | ||
"main": "dist/index.js", | ||
"main": "dist/index.umd.js", | ||
"repository": "lagden/mask", | ||
@@ -28,3 +28,3 @@ "scripts": { | ||
"rollup": "rollup -c", | ||
"prebuild": "npm test", | ||
"prebuild": "npm test && rm -rf dist", | ||
"build": "npm run rollup", | ||
@@ -52,14 +52,17 @@ "start": "npm run build" | ||
"rules": { | ||
"capitalized-comments": 0 | ||
"capitalized-comments": 0, | ||
"unicorn/prevent-abbreviations": 0, | ||
"no-constructor-return": 0 | ||
} | ||
}, | ||
"esm": { | ||
"cjs": { | ||
"dedefault": true | ||
} | ||
}, | ||
"ava": { | ||
"babel": false, | ||
"extensions": [ | ||
"js", | ||
"mjs" | ||
"files": [ | ||
"test/**/*", | ||
"!test/helpers/**/*" | ||
], | ||
"helpers": [ | ||
"test/helpers/**/*" | ||
], | ||
"require": [ | ||
@@ -71,9 +74,9 @@ "esm", | ||
"devDependencies": { | ||
"ava": "2.4.0", | ||
"ava": "3.7.0", | ||
"esm": "3.2.25", | ||
"jsdom": "15.2.0", | ||
"rollup": "1.25.0", | ||
"jsdom": "16.2.2", | ||
"rollup": "2.6.1", | ||
"simulant": "0.2.2", | ||
"xo": "0.25.3" | ||
"xo": "0.29.1" | ||
} | ||
} |
@@ -41,3 +41,3 @@ # Mask | ||
<script type="module"> | ||
import Mask from './node_modules/@tadashi/mask/src/index.js' | ||
import Mask from './node_modules/@tadashi/mask/dist/index.js' | ||
const el = document.getElementById('telefone') | ||
@@ -44,0 +44,0 @@ const mask = new Mask(el) |
'use strict' | ||
const map = new Map() | ||
map.set('9', /[\d]/) | ||
map.set('A', /[0-9a-zA-Z]/) | ||
map.set('9', /\d/) | ||
map.set('A', /[\da-zA-Z]/) | ||
map.set('S', /[a-zA-Z]/) | ||
@@ -19,3 +19,3 @@ | ||
static masking(_value, _mask) { | ||
const value = String(_value).replace(/[^0-9a-zA-Z]/g, '') | ||
const value = String(_value).replace(/[^\da-zA-Z]/g, '') | ||
const mask = String(_mask) | ||
@@ -22,0 +22,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20127
8
225