diff --git a/.gitignore b/.gitignore index c2658d7d..78f2710d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +.idea/ diff --git a/src/dynamic-mask.js b/src/dynamic-mask.js index 713782b2..9b75a7df 100644 --- a/src/dynamic-mask.js +++ b/src/dynamic-mask.js @@ -1,3 +1,5 @@ +import isNextMask from "./is-next-mask"; + export default function dynamicMask (maskit, masks, tokens) { masks = masks.sort((a, b) => a.length - b.length) return function (value, mask, masked = true) { @@ -6,7 +8,7 @@ export default function dynamicMask (maskit, masks, tokens) { var currentMask = masks[i] i++ var nextMask = masks[i] - if (! (nextMask && maskit(value, nextMask, true, tokens).length > currentMask.length) ) { + if (! (nextMask && isNextMask(value, currentMask, nextMask, tokens, maskit)) ) { return maskit(value, currentMask, masked, tokens) } } diff --git a/src/is-next-mask.js b/src/is-next-mask.js new file mode 100644 index 00000000..40a49f8c --- /dev/null +++ b/src/is-next-mask.js @@ -0,0 +1,13 @@ +export default function isNextMask (value, currentMask, nextMask, tokens, maskit) { + const tokensArray = Object.keys(tokens) + const onlyTokenNextMask = nextMask?.split('').reduce((acc, el) => { + if (tokensArray.includes(el)) acc += el + return acc + }, '') + const countNextValue = maskit(value, onlyTokenNextMask, false, tokens).length + const countCurrentMask = currentMask?.split('').reduce((acc, el) => { + if (tokensArray.includes(el)) acc++ + return acc + }, 0) + return countNextValue > countCurrentMask +}