Turns a string into its native typed value. Ex: 'true' -> true, '1' -> 1.
// trim() Polyfill
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
/**
* Turns string to native.
*
* Example:
* 'true' -> true
* '1' -> 1
*
* hat-tip:
* https://github.com/typicode/json-server/blob/master/src/server/utils.js
*
* @param {string} value The string value to transform.
* @return {mixed} The native typed value.
*/
function toNative(value) {
if (typeof value === 'string') {
if (value === '' || value.trim() !== value || (value.length > 1 && value[0] === '0')) {
return value
} else if (value === 'true' || value === 'false') {
return value === 'true'
} else if (!isNaN(+value)) {
return +value
}
}
return value
}