Skip to content

Instantly share code, notes, and snippets.

@brianswisher
Created December 22, 2015 16:05
Show Gist options
  • Select an option

  • Save brianswisher/e4f24e98c2acd480dab1 to your computer and use it in GitHub Desktop.

Select an option

Save brianswisher/e4f24e98c2acd480dab1 to your computer and use it in GitHub Desktop.
Validate ID
((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