@tadashi/mask
Advanced tools
Comparing version 1.6.0 to 1.6.1
@@ -0,1 +1,3 @@ | ||
import hexID from '@tadashi/hex-id'; | ||
const map = new Map(); | ||
@@ -9,3 +11,2 @@ map.set('9', /\d/); | ||
const GUID = Symbol('GUID'); | ||
const EVENT = Symbol('EVENT'); | ||
@@ -18,4 +19,4 @@ class Mask { | ||
static masking(_value, _mask) { | ||
const mask = String(_mask); | ||
const value = String(_value).replace(/[^\da-zA-Z]/g, ''); | ||
const mask = String(_mask); | ||
@@ -27,13 +28,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 | ||
} | ||
} | ||
@@ -44,3 +44,10 @@ | ||
constructor(input, mask = '', keyEvent = 'input') { | ||
constructor(...args) { | ||
const [ | ||
input, | ||
mask = '', | ||
keyEvent = 'input', | ||
triggerOnBlur = false | ||
] = args; | ||
if (input instanceof HTMLInputElement === false) { | ||
@@ -56,2 +63,4 @@ throw new TypeError('The input should be a HTMLInputElement') | ||
this.events = new Set(); | ||
this.input = input; | ||
@@ -66,7 +75,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); | ||
@@ -85,3 +99,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])) { | ||
@@ -88,0 +105,0 @@ instances.delete(this.input[GUID]); |
(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()); | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Mask = factory()); | ||
}(this, (function () { | ||
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; | ||
function createCommonjsModule(fn, basedir, module) { | ||
return module = { | ||
path: basedir, | ||
exports: {}, | ||
require: function (path, base) { | ||
return commonjsRequire(path, (base === undefined || base === null) ? module.path : base); | ||
} | ||
}, fn(module, module.exports), module.exports; | ||
} | ||
function commonjsRequire () { | ||
throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs'); | ||
} | ||
var index_umd = createCommonjsModule(function (module, exports) { | ||
(function (global, factory) { | ||
module.exports = factory() ; | ||
}(commonjsGlobal, (function () { | ||
/* globals globalThis */ | ||
const PROCESS_UNIQUE = (typeof globalThis.process === 'object' && globalThis.process.pid) || globalThis.crypto.getRandomValues(new Uint8Array(5)); | ||
let cc = Math.floor(Math.random() * 0xFFFFFF); | ||
function _next() { | ||
cc += 1 % 0xFFFFFF; | ||
return cc | ||
} | ||
function _toHex(view) { | ||
const arr = []; | ||
const len = view.byteLength; | ||
for (let i = 0; i < len; i++) { | ||
arr.push(view.getUint8(i).toString(16).padStart(2, '0')); | ||
} | ||
return arr.join('') | ||
} | ||
/** | ||
* Generate an ID (24 character hex string) | ||
* | ||
* @returns {string} returns a valid 24 character ObjectID hex string. | ||
*/ | ||
function hexID() { | ||
const time = ~~(Date.now() / 1000); | ||
const inc = _next(); | ||
const buffer = new ArrayBuffer(12); | ||
const view = new DataView(buffer); | ||
// 4-byte timestamp | ||
view.setUint8(3, time & 0xFF); | ||
view.setUint8(2, (time >> 8) & 0xFF); | ||
view.setUint8(1, (time >> 16) & 0xFF); | ||
view.setUint8(0, (time >> 24) & 0xFF); | ||
// 5-byte process unique | ||
view.setUint8(4, PROCESS_UNIQUE[0]); | ||
view.setUint8(5, PROCESS_UNIQUE[1]); | ||
view.setUint8(6, PROCESS_UNIQUE[2]); | ||
view.setUint8(7, PROCESS_UNIQUE[3]); | ||
view.setUint8(8, PROCESS_UNIQUE[4]); | ||
// 3-byte counter | ||
view.setUint8(11, inc & 0xFF); | ||
view.setUint8(10, (inc >> 8) & 0xFF); | ||
view.setUint8(9, (inc >> 16) & 0xFF); | ||
return _toHex(view) | ||
} | ||
return hexID; | ||
}))); | ||
}); | ||
const map = new Map(); | ||
@@ -14,3 +92,2 @@ map.set('9', /\d/); | ||
const GUID = Symbol('GUID'); | ||
const EVENT = Symbol('EVENT'); | ||
@@ -23,4 +100,4 @@ class Mask { | ||
static masking(_value, _mask) { | ||
const mask = String(_mask); | ||
const value = String(_value).replace(/[^\da-zA-Z]/g, ''); | ||
const mask = String(_mask); | ||
@@ -32,13 +109,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 | ||
} | ||
} | ||
@@ -49,3 +125,10 @@ | ||
constructor(input, mask = '', keyEvent = 'input') { | ||
constructor(...args) { | ||
const [ | ||
input, | ||
mask = '', | ||
keyEvent = 'input', | ||
triggerOnBlur = false | ||
] = args; | ||
if (input instanceof HTMLInputElement === false) { | ||
@@ -61,2 +144,4 @@ throw new TypeError('The input should be a HTMLInputElement') | ||
this.events = new Set(); | ||
this.input = input; | ||
@@ -71,7 +156,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] = index_umd(); | ||
instances.set(this.input[GUID], this); | ||
@@ -90,3 +180,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])) { | ||
@@ -93,0 +186,0 @@ instances.delete(this.input[GUID]); |
{ | ||
"name": "@tadashi/mask", | ||
"version": "1.6.0", | ||
"version": "1.6.1", | ||
"description": "The simple and tiny script for input mask", | ||
@@ -71,2 +71,4 @@ "keywords": [ | ||
"devDependencies": { | ||
"@rollup/plugin-commonjs": "15.1.0", | ||
"@rollup/plugin-node-resolve": "9.0.0", | ||
"ava": "3.13.0", | ||
@@ -73,0 +75,0 @@ "esm": "3.2.25", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
28080
319
8