UNPKG

polyfill-service

Version:
1 lines 13.7 kB
{"aliases":["default","caniuse:json","modernizr:json"],"browsers":{"ie":"6 - 7"},"license":"MIT","repo":"https://github.com/abozhilov/json","docs":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON","spec":"http://people.mozilla.org/~jorendorff/es6-draft.html#sec-json-object","baseDir":"JSON","hasTests":false,"rawSource":"\n// JSON\n(function (global) {\n\tvar\n\ttoString = Object.prototype.toString,\n\thasOwnProperty = Object.prototype.hasOwnProperty,\n\tLEFT_CURLY = '{',\n\tRIGHT_CURLY = '}',\n\tCOLON = ':',\n\tLEFT_BRACE = '[',\n\tRIGHT_BRACE = ']',\n\tCOMMA = ',',\n\ttokenType = {\n\t\tPUNCTUATOR: 1,\n\t\tSTRING: 2,\n\t\tNUMBER: 3,\n\t\tBOOLEAN: 4,\n\t\tNULL: 5\n\t},\n\ttokenMap = {\n\t\t'{': 1, '}': 1, '[': 1, ']': 1, ',': 1, ':': 1,\n\t\t'\"': 2,\n\t\t't': 4, 'f': 4,\n\t\t'n': 5\n\t},\n\tescChars = {\n\t\t'b': '\\b',\n\t\t'f': '\\f',\n\t\t'n': '\\n',\n\t\t'r': '\\r',\n\t\t't': '\\t',\n\t\t'\"': '\"',\n\t\t'\\\\': '\\\\',\n\t\t'/': '/'\n\t},\n\ttokenizer = /^(?:[{}:,\\[\\]]|true|false|null|\"(?:[^\"\\\\\\u0000-\\u001F]|\\\\[\"\\\\\\/bfnrt]|\\\\u[0-9A-F]{4})*\"|-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?)/,\n\twhiteSpace = /^[\\t ]+/,\n\tlineTerminator = /^\\r?\\n/;\n\n\tfunction JSONLexer(JSONStr) {\n\t\tthis.line = 1;\n\t\tthis.col = 1;\n\t\tthis._tokLen = 0;\n\t\tthis._str = JSONStr;\n\t}\n\n\tJSONLexer.prototype = {\n\t\tgetNextToken: function () {\n\t\t\tvar\n\t\t\tstr = this._str,\n\t\t\ttoken, type;\n\n\t\t\tthis.col += this._tokLen;\n\n\t\t\tif (!str.length) {\n\t\t\t\treturn 'END';\n\t\t\t}\n\n\t\t\ttoken = tokenizer.exec(str);\n\n\t\t\tif (token) {\n\t\t\t\ttype = tokenMap[token[0].charAt(0)] || tokenType.NUMBER;\n\t\t\t} else if ((token = whiteSpace.exec(str))) {\n\t\t\t\tthis._tokLen = token[0].length;\n\t\t\t\tthis._str = str.slice(this._tokLen);\n\t\t\t\treturn this.getNextToken();\n\t\t\t} else if ((token = lineTerminator.exec(str))) {\n\t\t\t\tthis._tokLen = 0;\n\t\t\t\tthis._str = str.slice(token[0].length);\n\t\t\t\tthis.line++;\n\t\t\t\tthis.col = 1;\n\t\t\t\treturn this.getNextToken();\n\t\t\t} else {\n\t\t\t\tthis.error('Invalid token');\n\t\t\t}\n\n\t\t\tthis._tokLen = token[0].length;\n\t\t\tthis._str = str.slice(this._tokLen);\n\n\t\t\treturn {\n\t\t\t\ttype: type,\n\t\t\t\tvalue: token[0]\n\t\t\t};\n\t\t},\n\n\t\terror: function (message, line, col) {\n\t\t\tvar err = new SyntaxError(message);\n\n\t\t\terr.line = line || this.line;\n\t\t\terr.col = col || this.col;\n\n\t\t\tthrow err;\n\t\t}\n\t};\n\n\tfunction JSONParser(lexer) {\n\t\tthis.lex = lexer;\n\t}\n\n\tJSONParser.prototype = {\n\t\tparse: function () {\n\t\t\tvar lex = this.lex, jsValue = this.getValue();\n\n\t\t\tif (lex.getNextToken() !== 'END') {\n\t\t\t\tlex.error('Illegal token');\n\t\t\t}\n\n\t\t\treturn jsValue;\n\t\t},\n\t\tgetObject: function () {\n\t\t\tvar\n\t\t\tjsObj = {},\n\t\t\tlex = this.lex,\n\t\t\ttoken, tval, prop,\n\t\t\tline, col,\n\t\t\tpairs = false;\n\n\t\t\twhile (true) {\n\t\t\t\ttoken = lex.getNextToken();\n\t\t\t\ttval = token.value;\n\n\t\t\t\tif (tval === RIGHT_CURLY) {\n\t\t\t\t\treturn jsObj;\n\t\t\t\t}\n\n\t\t\t\tif (pairs) {\n\t\t\t\t\tif (tval === COMMA) {\n\t\t\t\t\t\tline = lex.line;\n\t\t\t\t\t\tcol = lex.col - 1;\n\t\t\t\t\t\ttoken = lex.getNextToken();\n\t\t\t\t\t\ttval = token.value;\n\t\t\t\t\t\tif (tval === RIGHT_CURLY) {\n\t\t\t\t\t\t\tlex.error('Invalid trailing comma', line, col);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tlex.error('Illegal token where expect comma or right curly bracket');\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if (tval === COMMA) {\n\t\t\t\t\tlex.error('Invalid leading comma');\n\t\t\t\t}\n\n\t\t\t\tif (token.type != tokenType.STRING) {\n\t\t\t\t\tlex.error('Illegal token where expect string property name');\n\t\t\t\t}\n\n\t\t\t\tprop = this.getString(tval);\n\n\t\t\t\ttoken = lex.getNextToken();\n\t\t\t\ttval = token.value;\n\n\t\t\t\tif (tval != COLON) {\n\t\t\t\t\tlex.error('Illegal token where expect colon');\n\t\t\t\t}\n\n\t\t\t\tjsObj[prop] = this.getValue();\n\t\t\t\tpairs = true;\n\t\t\t}\n\t\t},\n\t\tgetArray: function () {\n\t\t\tvar\n\t\t\tjsArr = [],\n\t\t\tlex = this.lex,\n\t\t\ttoken, tval,\n\t\t\tline, col,\n\t\t\tvalues = false;\n\n\t\t\twhile (true) {\n\t\t\t\ttoken = lex.getNextToken();\n\t\t\t\ttval = token.value;\n\n\t\t\t\tif (tval === RIGHT_BRACE) {\n\t\t\t\t\treturn jsArr;\n\t\t\t\t}\n\n\t\t\t\tif (values) {\n\t\t\t\t\tif (tval === COMMA) {\n\t\t\t\t\t\tline = lex.line;\n\t\t\t\t\t\tcol = lex.col - 1;\n\t\t\t\t\t\ttoken = lex.getNextToken();\n\t\t\t\t\t\ttval = token.value;\n\n\t\t\t\t\t\tif (tval === RIGHT_BRACE) {\n\t\t\t\t\t\t\tlex.error('Invalid trailing comma', line, col);\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tlex.error('Illegal token where expect comma or right square bracket');\n\t\t\t\t\t}\n\t\t\t\t} else if (tval === COMMA) {\n\t\t\t\t\tlex.error('Invalid leading comma');\n\t\t\t\t}\n\n\t\t\t\tjsArr.push(this.getValue(token));\n\t\t\t\tvalues = true;\n\t\t\t}\n\t\t},\n\t\tgetString: function (strVal) {\n\t\t\treturn strVal.slice(1, -1).replace(/\\\\u?([0-9A-F]{4}|[\"\\\\\\/bfnrt])/g, function (match, escVal) {\n\t\t\t\treturn escChars[escVal] || String.fromCharCode(parseInt(escVal, 16));\n\t\t\t});\n\t\t},\n\t\tgetValue: function(fromToken) {\n\t\t\tvar lex = this.lex,\n\t\t\t\ttoken = fromToken || lex.getNextToken(),\n\t\t\t\ttval = token.value;\n\t\t\tswitch (token.type) {\n\t\t\t\tcase tokenType.PUNCTUATOR:\n\t\t\t\t\tif (tval === LEFT_CURLY) {\n\t\t\t\t\t\treturn this.getObject();\n\t\t\t\t\t} else if (tval === LEFT_BRACE) {\n\t\t\t\t\t\treturn this.getArray();\n\t\t\t\t\t}\n\n\t\t\t\t\tlex.error('Illegal punctoator');\n\n\t\t\t\t\tbreak;\n\t\t\t\tcase tokenType.STRING:\n\t\t\t\t\treturn this.getString(tval);\n\t\t\t\tcase tokenType.NUMBER:\n\t\t\t\t\treturn Number(tval);\n\t\t\t\tcase tokenType.BOOLEAN:\n\t\t\t\t\treturn tval === 'true';\n\t\t\t\tcase tokenType.NULL:\n\t\t\t\t\treturn null;\n\t\t\t\tdefault:\n\t\t\t\t\tlex.error('Invalid value');\n\t\t\t}\n\t\t}\n\t};\n\n\tfunction filter(base, prop, value) {\n\t\tif (typeof value === 'undefined') {\n\t\t\tdelete base[prop];\n\t\t\treturn;\n\t\t}\n\t\tbase[prop] = value;\n\t}\n\n\tfunction walk(holder, name, rev) {\n\t\tvar val = holder[name], i, len;\n\n\t\tif (toString.call(val).slice(8, -1) === 'Array') {\n\t\t\tfor (i = 0, len = val.length; i < len; i++) {\n\t\t\t\tfilter(val, i, walk(val, i, rev));\n\t\t\t}\n\t\t} else if (typeof val === 'object') {\n\t\t\tfor (i in val) {\n\t\t\t\tif (hasOwnProperty.call(val, i)) {\n\t\t\t\t\tfilter(val, i, walk(val, i, rev));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn rev.call(holder, name, val);\n\t}\n\n\tfunction pad(value, length) {\n\t\tvalue = String(value);\n\n\t\treturn value.length >= length ? value : new Array(length - value.length + 1).join('0') + value;\n\t}\n\n\t// <Global>.JSON\n\tglobal.JSON = {\n\t\tparse: function (JSONStr, reviver) {\n\t\t\tvar jsVal = new JSONParser(new JSONLexer(JSONStr)).parse();\n\n\t\t\tif (typeof reviver === 'function') {\n\t\t\t\treturn walk({\n\t\t\t\t\t'': jsVal\n\t\t\t\t}, '', reviver);\n\t\t\t}\n\n\t\t\treturn jsVal;\n\t\t},\n\t\tstringify: function () {\n\t\t\tvar\n\t\t\tvalue = arguments[0],\n\t\t\treplacer = typeof arguments[1] === 'function' ? arguments[1] : null,\n\t\t\tspace = arguments[2] || '',\n\t\t\tspaceSpace = space ? ' ' : '',\n\t\t\tspaceReturn = space ? '\\n' : '',\n\t\t\tclassName = toString.call(value).slice(8, -1),\n\t\t\tarray, key, hasKey, index, length, eachValue;\n\n\t\t\tif (value === null || className === 'Boolean' || className === 'Number') {\n\t\t\t\treturn value;\n\t\t\t}\n\n\t\t\tif (className === 'Array') {\n\t\t\t\tarray = [];\n\n\t\t\t\tfor (length = value.length, index = 0, eachValue; index < length; ++index) {\n\t\t\t\t\teachValue = replacer ? replacer(index, value[index]) : value[index];\n\t\t\t\t\teachValue = this.stringify(eachValue, replacer, space);\n\n\t\t\t\t\tif (eachValue === undefined || eachValue === null) {\n\t\t\t\t\t\teachValue = 'null';\n\t\t\t\t\t}\n\n\t\t\t\t\tarray.push(eachValue);\n\t\t\t\t}\n\n\t\t\t\treturn '[' + spaceReturn + array.join(',' + spaceReturn).replace(/^/mg, space) + spaceReturn + ']';\n\t\t\t}\n\n\t\t\tif (className === 'Date') {\n\t\t\t\treturn '\"' + value.getUTCFullYear() + '-' +\n\t\t\t\tpad(value.getUTCMonth() + 1, 2) + '-' +\n\t\t\t\tpad(value.getUTCDate(), 2) + 'T' +\n\t\t\t\tpad(value.getUTCHours(), 2) + ':' +\n\t\t\t\tpad(value.getUTCMinutes(), 2) + ':' +\n\t\t\t\tpad(value.getUTCSeconds(), 2) + '.' +\n\t\t\t\tpad(value.getUTCMilliseconds(), 3) + 'Z' + '\"';\n\t\t\t}\n\n\t\t\tif (className === 'String') {\n\t\t\t\treturn '\"' + value.replace(/\"/g, '\\\\\"') + '\"';\n\t\t\t}\n\n\t\t\tif (typeof value === 'object') {\n\t\t\t\tarray = [];\n\t\t\t\thasKey = false;\n\n\t\t\t\tfor (key in value) {\n\t\t\t\t\tif (hasOwnProperty.call(value, key)) {\n\t\t\t\t\t\teachValue = replacer ? replacer(key, value[key]) : value[key];\n\t\t\t\t\t\teachValue = this.stringify(eachValue, replacer, space);\n\n\t\t\t\t\t\tif (eachValue !== undefined) {\n\t\t\t\t\t\t\thasKey = true;\n\n\t\t\t\t\t\t\tarray.push('\"' + key + '\":' + spaceSpace + eachValue);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (!hasKey) {\n\t\t\t\t\treturn '{}';\n\t\t\t\t} else {\n\t\t\t\t\treturn '{' + spaceReturn + array.join(',' + spaceReturn).replace(/^/mg, space) + spaceReturn + '}';\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n})(this);\n","minSource":"!function(e){function t(e){this.line=1,this.col=1,this._tokLen=0,this._str=e}function r(e){this.lex=e}function n(e,t,r){return\"undefined\"==typeof r?void delete e[t]:void(e[t]=r)}function i(e,t,r){var o,u,s=e[t];if(\"Array\"===l.call(s).slice(8,-1))for(o=0,u=s.length;u>o;o++)n(s,o,i(s,o,r));else if(\"object\"==typeof s)for(o in s)a.call(s,o)&&n(s,o,i(s,o,r));return r.call(e,t,s)}function o(e,t){return e=String(e),e.length>=t?e:new Array(t-e.length+1).join(\"0\")+e}var l=Object.prototype.toString,a=Object.prototype.hasOwnProperty,u=\"{\",s=\"}\",c=\":\",g=\"[\",h=\"]\",f=\",\",p={PUNCTUATOR:1,STRING:2,NUMBER:3,BOOLEAN:4,NULL:5},v={\"{\":1,\"}\":1,\"[\":1,\"]\":1,\",\":1,\":\":1,'\"':2,t:4,f:4,n:5},N={b:\"\\b\",f:\"\\f\",n:\"\\n\",r:\"\\r\",t:\"\t\",'\"':'\"',\"\\\\\":\"\\\\\",\"/\":\"/\"},k=/^(?:[{}:,\\[\\]]|true|false|null|\"(?:[^\"\\\\\\u0000-\\u001F]|\\\\[\"\\\\\\/bfnrt]|\\\\u[0-9A-F]{4})*\"|-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?)/,T=/^[\\t ]+/,y=/^\\r?\\n/;t.prototype={getNextToken:function(){var e,t,r=this._str;if(this.col+=this._tokLen,!r.length)return\"END\";if(e=k.exec(r))t=v[e[0].charAt(0)]||p.NUMBER;else{if(e=T.exec(r))return this._tokLen=e[0].length,this._str=r.slice(this._tokLen),this.getNextToken();if(e=y.exec(r))return this._tokLen=0,this._str=r.slice(e[0].length),this.line++,this.col=1,this.getNextToken();this.error(\"Invalid token\")}return this._tokLen=e[0].length,this._str=r.slice(this._tokLen),{type:t,value:e[0]}},error:function(e,t,r){var n=new SyntaxError(e);throw n.line=t||this.line,n.col=r||this.col,n}},r.prototype={parse:function(){var e=this.lex,t=this.getValue();return\"END\"!==e.getNextToken()&&e.error(\"Illegal token\"),t},getObject:function(){for(var e,t,r,n,i,o={},l=this.lex,a=!1;;){if(e=l.getNextToken(),t=e.value,t===s)return o;a?t===f?(n=l.line,i=l.col-1,e=l.getNextToken(),t=e.value,t===s&&l.error(\"Invalid trailing comma\",n,i)):l.error(\"Illegal token where expect comma or right curly bracket\"):t===f&&l.error(\"Invalid leading comma\"),e.type!=p.STRING&&l.error(\"Illegal token where expect string property name\"),r=this.getString(t),e=l.getNextToken(),t=e.value,t!=c&&l.error(\"Illegal token where expect colon\"),o[r]=this.getValue(),a=!0}},getArray:function(){for(var e,t,r,n,i=[],o=this.lex,l=!1;;){if(e=o.getNextToken(),t=e.value,t===h)return i;l?t===f?(r=o.line,n=o.col-1,e=o.getNextToken(),t=e.value,t===h&&o.error(\"Invalid trailing comma\",r,n)):o.error(\"Illegal token where expect comma or right square bracket\"):t===f&&o.error(\"Invalid leading comma\"),i.push(this.getValue(e)),l=!0}},getString:function(e){return e.slice(1,-1).replace(/\\\\u?([0-9A-F]{4}|[\"\\\\\\/bfnrt])/g,function(e,t){return N[t]||String.fromCharCode(parseInt(t,16))})},getValue:function(e){var t=this.lex,r=e||t.getNextToken(),n=r.value;switch(r.type){case p.PUNCTUATOR:if(n===u)return this.getObject();if(n===g)return this.getArray();t.error(\"Illegal punctoator\");break;case p.STRING:return this.getString(n);case p.NUMBER:return Number(n);case p.BOOLEAN:return\"true\"===n;case p.NULL:return null;default:t.error(\"Invalid value\")}}},e.JSON={parse:function(e,n){var o=new r(new t(e)).parse();return\"function\"==typeof n?i({\"\":o},\"\",n):o},stringify:function(){var e,t,r,n,i,u,s=arguments[0],c=\"function\"==typeof arguments[1]?arguments[1]:null,g=arguments[2]||\"\",h=g?\" \":\"\",f=g?\"\\n\":\"\",p=l.call(s).slice(8,-1);if(null===s||\"Boolean\"===p||\"Number\"===p)return s;if(\"Array\"===p){for(e=[],i=s.length,n=0,u;i>n;++n)u=c?c(n,s[n]):s[n],u=this.stringify(u,c,g),void 0!==u&&null!==u||(u=\"null\"),e.push(u);return\"[\"+f+e.join(\",\"+f).replace(/^/gm,g)+f+\"]\"}if(\"Date\"===p)return'\"'+s.getUTCFullYear()+\"-\"+o(s.getUTCMonth()+1,2)+\"-\"+o(s.getUTCDate(),2)+\"T\"+o(s.getUTCHours(),2)+\":\"+o(s.getUTCMinutes(),2)+\":\"+o(s.getUTCSeconds(),2)+\".\"+o(s.getUTCMilliseconds(),3)+'Z\"';if(\"String\"===p)return'\"'+s.replace(/\"/g,'\\\\\"')+'\"';if(\"object\"==typeof s){e=[],r=!1;for(t in s)a.call(s,t)&&(u=c?c(t,s[t]):s[t],u=this.stringify(u,c,g),void 0!==u&&(r=!0,e.push('\"'+t+'\":'+h+u)));return r?\"{\"+f+e.join(\",\"+f).replace(/^/gm,g)+f+\"}\":\"{}\"}}}}(this);","detectSource":"'JSON' in this"}