Skip to content

Commit

Permalink
format styling
Browse files Browse the repository at this point in the history
  • Loading branch information
calebporzio committed Jul 3, 2024
1 parent cf15d91 commit c574bef
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions packages/mask/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,37 @@ let regexes = {
'9': /[0-9]/,
'a': /[a-zA-Z]/,
'*': /[a-zA-Z0-9]/,
};
export function formatInput (template, input) {
}

export function formatInput(template, input) {
let templateMark = 0
let inputMark = 0
let output = ''
let imark = 0
let tmark = 0
while (tmark < template.length && imark < input.length) {
const char = template[tmark]
const ichar = input[imark]
if (char in regexes) {
if (regexes[char].test(ichar)) {
output += ichar
tmark++

// Walk the template and input chars simultaneously one by one...
while (templateMark < template.length && inputMark < input.length) {
let templateChar = template[templateMark]
let inputChar = input[inputMark]

// We've encountered a template placeholder...
if (templateChar in regexes) {
// If the input is "allowed" based on the placeholder...
if (regexes[templateChar].test(inputChar)) {
output += inputChar

templateMark++
}
imark++
} else {
output += char
tmark++
if (char === input[imark]) imark++

inputMark++
} else { // We've encountered a template literal...
output += templateChar

templateMark++

if (templateChar === input[inputMark]) inputMark++
}
}

return output
}

Expand Down

0 comments on commit c574bef

Please sign in to comment.