mirror of
https://github.com/tenrok/maska.git
synced 2026-05-30 15:24:06 +03:00
@@ -0,0 +1,5 @@
|
||||
const config = {
|
||||
testEnvironment: "jsdom",
|
||||
};
|
||||
|
||||
module.exports = config;
|
||||
+2
-2
@@ -7,7 +7,7 @@ function getOpts (mask) {
|
||||
if (mask.mask) {
|
||||
opts.mask = Array.isArray(mask.mask) ? JSON.stringify(mask.mask) : mask.mask
|
||||
opts.tokens = mask.tokens ? { ...mask.tokens } : {}
|
||||
opts.preprocessor = mask.preprocessor;
|
||||
opts.preprocessor = mask.preprocessor
|
||||
} else {
|
||||
opts.mask = Array.isArray(mask) ? JSON.stringify(mask) : mask
|
||||
}
|
||||
@@ -32,7 +32,7 @@ const directive = () => {
|
||||
if (state.has(el) && !needUpdate(mask)) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
state.set(el, new Maska(el, getOpts(mask.value)))
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -21,7 +21,7 @@ function dynamic (mask) {
|
||||
const processed = masks.map(m => process(value, m, tokens, false))
|
||||
const last = processed.pop()
|
||||
|
||||
for (let i in masks) {
|
||||
for (const i in masks) {
|
||||
if (checkMask(last, masks[i], tokens)) {
|
||||
return process(value, masks[i], tokens, masked)
|
||||
}
|
||||
@@ -31,7 +31,7 @@ function dynamic (mask) {
|
||||
}
|
||||
|
||||
function checkMask (variant, mask, tokens) {
|
||||
for (let tok in tokens) {
|
||||
for (const tok in tokens) {
|
||||
if (tokens[tok].escape) {
|
||||
mask = mask.replace(new RegExp(tok + '.{1}', 'g'), '')
|
||||
}
|
||||
@@ -62,8 +62,8 @@ function process (value, mask, tokens, masked = true) {
|
||||
ret += mask[im]
|
||||
im++
|
||||
} else if (tokens[mask[im]] && tokens[mask[im]].escape) {
|
||||
ret += mask[im+1]
|
||||
im = im+2
|
||||
ret += mask[im + 1]
|
||||
im = im + 2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-5
@@ -6,7 +6,7 @@ export default class Maska {
|
||||
constructor (el, opts = {}) {
|
||||
if (!el) throw new Error('Maska: no element for mask')
|
||||
|
||||
if (opts.preprocessor != null && typeof opts.preprocessor != 'function') {
|
||||
if (opts.preprocessor != null && typeof opts.preprocessor !== 'function') {
|
||||
throw new Error('Maska: preprocessor must be a function')
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ export default class Maska {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
this._opts = {
|
||||
mask: opts.mask,
|
||||
tokens: { ...tokens, ...opts.tokens },
|
||||
@@ -71,10 +70,10 @@ export default class Maska {
|
||||
const digit = oldValue[position - 1]
|
||||
|
||||
el.dataset.maskRawValue = mask(el.value, el.dataset.mask, this._opts.tokens, false)
|
||||
let elValue = el.value;
|
||||
|
||||
let elValue = el.value
|
||||
|
||||
if (this._opts.preprocessor) {
|
||||
elValue = this._opts.preprocessor(elValue);
|
||||
elValue = this._opts.preprocessor(elValue)
|
||||
}
|
||||
|
||||
el.value = mask(elValue, el.dataset.mask, this._opts.tokens)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import mask from './../src/mask'
|
||||
import tokens from './../src/tokens'
|
||||
import Maska from './../src/maska'
|
||||
|
||||
test('12 #.#', () => {
|
||||
expect(mask('12', '#.#', tokens)).toBe('1.2')
|
||||
@@ -205,3 +206,19 @@ test('Custom transform with `uppercase` and `lowercase` enabled: abkTYX -> АВ
|
||||
}
|
||||
})).toBe('авктух')
|
||||
})
|
||||
|
||||
test('Custom value preprocessing', () => {
|
||||
const elem = document.createElement('input')
|
||||
document.body.appendChild(elem)
|
||||
|
||||
const mask = new Maska(elem, {
|
||||
mask: 'S*',
|
||||
preprocessor: function(val) {
|
||||
return val.toLocaleUpperCase()
|
||||
}
|
||||
})
|
||||
elem.value = "abkTYX"
|
||||
elem.dispatchEvent(new Event('input', {bubbles: true}))
|
||||
|
||||
expect(elem.value).toBe("ABKTYX")
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user