Last active
June 21, 2016 01:56
-
-
Save nbogie/c3f7a8e73190778a611e520b7c30e35b to your computer and use it in GitHub Desktop.
trim as regex in js
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
| function trim(str){ | |
| return str.replace(/^\s*((?:.|\n)*?)\s*$/, "$1"); | |
| } | |
| var testCases = [ | |
| [" a" , "a"], | |
| ["A " , "A"], | |
| [" a " , "a"], | |
| ["a b! " , "a b!"], | |
| [" .- " , ".-"], | |
| ["\nfoo" , "foo"], | |
| ["\t\t\n You \njust \t ", "You \njust"], | |
| ]; | |
| var testReport = testCases.map(function(test) { | |
| var [input, expected] = test; | |
| var output = trim(input); | |
| var successStr = output === expected ? "PASS" : "FAIL"; | |
| return { | |
| result: successStr, | |
| input: input, | |
| expected: expected, | |
| was: output | |
| }; | |
| }); | |
| testReport.filter(r => r.result === 'FAIL') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment