Created
December 22, 2015 16:05
-
-
Save brianswisher/e4f24e98c2acd480dab1 to your computer and use it in GitHub Desktop.
Validate ID
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
| ((libs) => { | |
| var promises = []; | |
| libs.forEach((src) => { | |
| promises.push(new Promise(resolve => { | |
| if (!document.querySelector(`[src="${src}"]`)) { | |
| const script = document.createElement("script"); | |
| script.src = src; | |
| script.async = false; | |
| script.onload = () => { | |
| resolve(); | |
| }; | |
| document.head.appendChild(script); | |
| } else { | |
| resolve(); | |
| } | |
| })); | |
| }); | |
| function validateId(id) { | |
| if (id === undefined || id === "") return false; | |
| var num = parseFloat(id); | |
| return num > 0 && num.toString().indexOf(".") === -1; | |
| } | |
| return Promise.all(promises) | |
| .then(() => { | |
| expect(validateId()).toEqual(false); | |
| expect(validateId("")).toEqual(false); | |
| expect(validateId(-1)).toEqual(false); | |
| expect(validateId("1")).toEqual(true); | |
| expect(validateId(1)).toEqual(true); | |
| console.log("All tests passed."); | |
| }); | |
| })([ | |
| "https://wzrd.in/standalone/expect@latest" | |
| ]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment