Created
March 3, 2016 14:14
-
-
Save thalesmello/674b79c880b3a20d371d to your computer and use it in GitHub Desktop.
This file contains 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
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