auto-parse
Version:
Automatically convert any value to its best matching JavaScript type. Supports numbers, booleans, objects, arrays, BigInt, Symbol, comma-separated numbers, prefix stripping, allowed type enforcement and a plugin API.
2 lines (1 loc) • 3.26 kB
JavaScript
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).autoParse=e()}}(function(){return function u(i,f,c){function a(n,e){if(!f[n]){if(!i[n]){var t="function"==typeof require&&require;if(!e&&t)return t(n,!0);if(s)return s(n,!0);var r=new Error("Cannot find module '"+n+"'");throw r.code="MODULE_NOT_FOUND",r}var o=f[n]={exports:{}};i[n][0].call(o.exports,function(e){return a(i[n][1][e]||e)},o,o.exports,u,i,f,c)}return f[n].exports}for(var s="function"==typeof require&&require,e=0;e<c.length;e++)a(c[e]);return a}({1:[function(e,n,t){n.exports=p;var i=e("typpy");function f(e){return e.replace(/[""'']/gi,"").trim().toLowerCase()}function c(e){return!!e&&("number"==typeof e||"boolean"==typeof e?!!e:"true"===(e=f(e))||"1"===e||"false"!==e&&"0"!==e&&null)}function a(e){if(i(e,Array))return e.map(function(e,n){return p(e)});if(i(e,Object)||void 0===e.constructor){for(var n in e)e[n]=p(e[n]);return e}return{}}function s(n,e){if(n&&n.constructor===e||i(n,e))return n;var t=e;switch(e&&e.name&&(t=e.name.toLowerCase()),t=f(t)){case"string":return"object"==typeof n?JSON.stringify(n):String(n);case"function":return i(n,Function)?n:function(e){return"function"==typeof e&&e(n),n};case"date":return new Date(n);case"object":var r;try{r=JSON.parse(n)}catch(e){}return i(r,Object)||i(r,Array)?p(r):i(r,"undefined")?a(n):{};case"boolean":return c(n)||!1;case"number":return Number(n);case"undefined":return;case"null":return null;case"array":return[n];default:if("function"==typeof e)return new e(n);throw new Error("Unsupported type.")}}function p(n,e){if(e)return s(n,e);var t=n;if(null===n)return null;if(void 0!==n){if(n instanceof Date||n instanceof RegExp)return n;if("number"==typeof n||"boolean"==typeof n)return n;if("function"==typeof n)return p(n());if("object"==typeof n)return a(n);if("NaN"===n)return NaN;var r=null;try{r=JSON.parse(n)}catch(e){try{r=JSON.parse(n.trim().replace(/(\\\\")|(\\")/gi,'"').replace(/(\\n|\\\\n)/gi,"").replace(/(^"|"$)|(^'|'$)/gi,""))}catch(e){try{r=JSON.parse(n.trim().replace(/'/gi,'"'))}catch(e){}}}if(r&&"object"==typeof r)return p(r);if("undefined"!==(n=f(n))&&""!==n){if("null"===n)return null;var o=Number(n);if(i(o,Number))return o;var u=c(n);return i(u,Boolean)?u:String(t)}}}},{typpy:4}],2:[function(e,n,t){"use strict";var r=e("noop6");!function(){if("string"!=typeof r.name)try{Object.defineProperty(Function.prototype,"name",{get:function(){var e=this.toString().trim().match(/^function\s*([^\s(]+)/),n=e?e[1]:"";return Object.defineProperty(this,"name",{value:n}),n}})}catch(e){}}(),n.exports=function(e){return e.name}},{noop6:3}],3:[function(e,n,t){"use strict";n.exports=function(){}},{}],4:[function(e,n,t){"use strict";function r(e,n){return 2===arguments.length?r.is(e,n):r.get(e,!0)}e("function.name"),r.is=function(e,n){return r.get(e,"string"==typeof n)===n},r.get=function(e,n){return"string"==typeof e?n?"string":String:null===e?n?"null":null:void 0===e?n?"undefined":void 0:e!=e?n?"nan":NaN:n?e.constructor.name.toLowerCase():e.constructor},n.exports=r},{"function.name":2}]},{},[1])(1)});