Created
September 16, 2016 03:47
-
-
Save pfgray/c2acdb14e36df848cad0da0bb8a3130b to your computer and use it in GitHub Desktop.
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
| const INVALID = 'INVALID'; | |
| const VALID = 'VALID'; | |
| const Invalid = reasons => ({ | |
| type: INVALID, | |
| reasons: reasons, | |
| and: and, | |
| bifold: bifold | |
| }); | |
| const Valid = values => ({ | |
| type: VALID, | |
| values: values, | |
| and: and, | |
| bifold: bifold | |
| }); | |
| const and = function(validOrInvalid) { | |
| //hm, wish there was a tuple | |
| if (this.type === INVALID) { | |
| if(validOrInvalid.type === INVALID){ | |
| return Invalid([...this.reasons, ...validOrInvalid.reasons]); | |
| } else { | |
| return Invalid(this.reasons); | |
| } | |
| } else { | |
| if(validOrInvalid.type === VALID){ | |
| return Valid([...this.values, ...validOrInvalid.values]) | |
| } else { | |
| return Invalid(validOrInvalid.reasons) | |
| } | |
| } | |
| } | |
| const bifold = function(f, g) { | |
| if (this.type === INVALID) { | |
| return f(this.reasons); | |
| } else { | |
| return g(this.values); | |
| } | |
| } | |
| const validateEmail = email => { | |
| var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | |
| if(re.test(email)){ | |
| return Valid([email]); | |
| } else { | |
| return Invalid([`Uh, ${email} didn't match dat regex`]); | |
| } | |
| } | |
| const assertion1 = validateEmail('[email protected]'); | |
| const assertion2 = validateEmail('[email protected]'); | |
| const assertion3 = validateEmail('[email protected]'); | |
| assertion1.and(assertion2).and(assertion3) | |
| .bifold(reasons => { | |
| console.log("We didn't validate..., because:", reasons); | |
| }, emails => { | |
| console.log("Yay, we have three emails!", emails); | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment