From 0cf4d7c7b86d8eb05604a64037e725a8d9bd7b1e Mon Sep 17 00:00:00 2001 From: Yury Mamedov Date: Wed, 10 Nov 2021 01:28:55 +0300 Subject: [PATCH] #51 Added test, switched jest env to jsdom, lint fixed --- jest.config.js | 5 +++++ src/directive.js | 4 ++-- src/mask.js | 8 ++++---- src/maska.js | 9 ++++----- test/mask.test.js | 17 +++++++++++++++++ 5 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 jest.config.js diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..02d1287 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,5 @@ +const config = { + testEnvironment: "jsdom", + }; + +module.exports = config; \ No newline at end of file diff --git a/src/directive.js b/src/directive.js index 1c5b936..e1ec613 100644 --- a/src/directive.js +++ b/src/directive.js @@ -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))) } } diff --git a/src/mask.js b/src/mask.js index 75f9c73..e383f30 100644 --- a/src/mask.js +++ b/src/mask.js @@ -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 } } } diff --git a/src/maska.js b/src/maska.js index 0b6daff..a91d7b6 100644 --- a/src/maska.js +++ b/src/maska.js @@ -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) diff --git a/test/mask.test.js b/test/mask.test.js index e72b225..273f5e7 100644 --- a/test/mask.test.js +++ b/test/mask.test.js @@ -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") +})