Created
September 29, 2021 16:32
-
-
Save thoughtsunificator/aabf6820eb5bd7439a5cf1ae72d5fe59 to your computer and use it in GitHub Desktop.
Email tokenizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const STATE_LOCAL_PART = "STATE_LOCAL_PART" | |
export const STATE_DOMAIN = "STATE_DOMAIN" | |
export const ALPHANUMERICS = "abcdefghijklmnopqrstuvwxyz1234567890" | |
export const LOCAL_PARTS_CHARACTERS = [...ALPHANUMERICS, ..."!#$%&'*+-/=?^_`{|}~."] | |
export const LOCAL_DOMAIN_CHARACTERS = [...ALPHANUMERICS, ..."-."] | |
export const tokenize = function(input) { | |
const characters = [...input] | |
let state = STATE_LOCAL_PART | |
const tokens = [] | |
let token = { type: "local_part", buffer: "", bufferIndex: 0 } | |
for(const [index, character] of characters.entries()) { | |
if(state === STATE_LOCAL_PART && LOCAL_PARTS_CHARACTERS.includes(character.toLowerCase())) { | |
if(character === "." && (index === 0 || characters[index + 1] === "@" || characters[index - 1] === ".")) { | |
return false | |
} else { | |
token.buffer += character | |
} | |
} else if(state === STATE_DOMAIN && LOCAL_DOMAIN_CHARACTERS.includes(character.toLowerCase())) { | |
if(character === "-" && (index === 0 || index === characters.length -1) | |
|| character === "." && (index === token.bufferIndex || characters[index - 1] === "." || index === characters.length - 1)) { | |
return false | |
} else { | |
token.buffer += character | |
} | |
if(index === characters.length - 1) { | |
tokens.push(token) | |
} | |
} else if(state === STATE_LOCAL_PART && character === "@") { | |
tokens.push(token) | |
token = { type: "domain", buffer: "", bufferIndex: index + 1 } | |
state = STATE_DOMAIN | |
} else { | |
return false | |
} | |
} | |
if(tokens.length < 2) { | |
return false | |
} | |
return tokens | |
} | |
console.log(tokenize("stackoverflow@.")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment