comunicationLeap/node_modules/parsejson/index.js
node_modules/parsejson/index.jsRaw
1/**
2 * JSON parse.
3 *
4 * @see Based on jQuery#parseJSON (MIT) and JSON2
5 * @api private
6 */
7
8var rvalidchars = /^[\],:{}\s]*$/;
9var rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g;
10var rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
11var rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g;
12var rtrimLeft = /^\s+/;
13var rtrimRight = /\s+$/;
14
15module.exports = function parsejson(data) {
16 if ('string' != typeof data || !data) {
17 return null;
18 }
19
20 data = data.replace(rtrimLeft, '').replace(rtrimRight, '');
21
22 // Attempt to parse using the native JSON parser first
23 if (global.JSON && JSON.parse) {
24 return JSON.parse(data);
25 }
26
27 if (rvalidchars.test(data.replace(rvalidescape, '@')
28 .replace(rvalidtokens, ']')
29 .replace(rvalidbraces, ''))) {
30 return (new Function('return ' + data))();
31 }
32};