Skip to content

Instantly share code, notes, and snippets.

@thalesmello
Created March 3, 2016 14:14
Show Gist options
  • Save thalesmello/674b79c880b3a20d371d to your computer and use it in GitHub Desktop.
Save thalesmello/674b79c880b3a20d371d to your computer and use it in GitHub Desktop.
convert = (function () {
return {
toString: toString,
toNumber: toNumber,
toArray: toArray,
}
function toString(value) {
if (typeof value === 'undefined' || value === null) return '';
return String(value);
}
function toNumber(value) {
if (typeof value === 'undefined' || value === null) return 0;
return Number(value);
}
function toArray(value) {
if (typeof value === 'undefined' || value === null) return [];
if (value instanceof Array) return value;
if (value.length) {
var arrayLike = true;
for (var i = 0; i < value.length; i++) {
if (typeof value.length[i] === 'undefined') {
arrayLike = false;
break;
}
}
if (arrayLike) return Array.prototype.slice.call(value);
}
return [value];
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment