@tadashi/mask
Advanced tools
Comparing version 1.5.0 to 1.6.0
{ | ||
"name": "@tadashi/mask", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "The simple and tiny script for input mask", | ||
@@ -71,9 +71,12 @@ "keywords": [ | ||
"devDependencies": { | ||
"ava": "3.7.0", | ||
"ava": "3.13.0", | ||
"esm": "3.2.25", | ||
"jsdom": "16.2.2", | ||
"rollup": "2.6.1", | ||
"jsdom": "16.4.0", | ||
"rollup": "2.32.1", | ||
"simulant": "0.2.2", | ||
"xo": "0.29.1" | ||
"xo": "0.34.1" | ||
}, | ||
"dependencies": { | ||
"@tadashi/hex-id": "2.0.0" | ||
} | ||
} |
'use strict' | ||
import hexID from '@tadashi/hex-id' | ||
const map = new Map() | ||
@@ -11,3 +13,2 @@ map.set('9', /\d/) | ||
const GUID = Symbol('GUID') | ||
const EVENT = Symbol('EVENT') | ||
@@ -20,4 +21,4 @@ class Mask { | ||
static masking(_value, _mask) { | ||
const mask = String(_mask) | ||
const value = String(_value).replace(/[^\da-zA-Z]/g, '') | ||
const mask = String(_mask) | ||
@@ -29,13 +30,12 @@ const res = [] | ||
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 | ||
} | ||
} else { | ||
res.push(char) | ||
} | ||
if (map.has(char) === false) { | ||
res.push(char) | ||
continue | ||
} | ||
if (value.length > cc && map.get(char).test(value.charAt(cc))) { | ||
res.push(value.charAt(cc++)) | ||
} else { | ||
break | ||
} | ||
} | ||
@@ -46,3 +46,10 @@ | ||
constructor(input, mask = '', keyEvent = 'input') { | ||
constructor(...args) { | ||
const [ | ||
input, | ||
mask = '', | ||
keyEvent = 'input', | ||
triggerOnBlur = false | ||
] = args | ||
if (input instanceof HTMLInputElement === false) { | ||
@@ -58,2 +65,4 @@ throw new TypeError('The input should be a HTMLInputElement') | ||
this.events = new Set() | ||
this.input = input | ||
@@ -68,7 +77,12 @@ this.mask = input.dataset.mask || mask | ||
// Listener | ||
this[EVENT] = keyEvent | ||
this.input.addEventListener(this[EVENT], this) | ||
this.input.addEventListener(keyEvent, this) | ||
this.events.add(keyEvent) | ||
if (triggerOnBlur) { | ||
this.input.addEventListener('blur', this) | ||
this.events.add('blur') | ||
} | ||
// Storage instance | ||
this.input[GUID] = `${Math.random()}_${Date.now()}` | ||
this.input[GUID] = hexID() | ||
instances.set(this.input[GUID], this) | ||
@@ -87,3 +101,6 @@ } | ||
destroy() { | ||
this.input.removeEventListener(this[EVENT], this) | ||
for (const _event of this.events) { | ||
this.input.removeEventListener(_event, this) | ||
} | ||
if (instances.has(this.input[GUID])) { | ||
@@ -90,0 +107,0 @@ instances.delete(this.input[GUID]) |
20384
236
1
+ Added@tadashi/hex-id@2.0.0
+ Added@tadashi/hex-id@2.0.0(transitive)