Comparing version 0.1.3 to 0.2.0
@@ -6,6 +6,6 @@ # json | ||
``` | ||
import { parseJSON } from 'dx-lib/lib/json' | ||
import { parseJSON, stringifyJSON } from 'dx-lib/lib/json' | ||
// 同JSON.parse,兼容低版本浏览器 | ||
parseJSON('{"a": "a", "b": "b"}') | ||
stringifyJSON({a: "a", b: "b"}) | ||
``` |
@@ -1,54 +0,2 @@ | ||
/** | ||
* https://code.jquery.com/jquery-1.11.3.js | ||
*/ | ||
const rvalidtokens = /(,)|(\[|{)|(}|])|"(?:[^"\\\r\n]|\\["\\\/bfnrt]|\\u[\da-fA-F]{4})*"\s*:?|true|false|null|-?(?!0\d)\d+(?:\.\d+|)(?:[eE][+-]?\d+|)/g | ||
const rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g | ||
function trim(str) { | ||
return str.replace(rtrim, "") | ||
} | ||
export const parseJSON = function(data) { | ||
if (window.JSON && window.JSON.parse) { | ||
return window.JSON.parse(data + "") | ||
} | ||
let requireNonComma | ||
let depth = null | ||
let str = trim(data + "") | ||
// Guard against invalid (and possibly dangerous) input by ensuring that nothing remains | ||
// after removing valid tokens | ||
if ( | ||
str && | ||
!trim( | ||
str.replace(rvalidtokens, function(token, comma, open, close) { | ||
// Force termination if we see a misplaced comma | ||
if (requireNonComma && comma) { | ||
depth = 0 | ||
} | ||
// Perform no more replacements after returning to outermost depth | ||
if (depth === 0) { | ||
return token | ||
} | ||
// Commas must not follow "[", "{", or "," | ||
requireNonComma = open || comma | ||
// Determine new depth | ||
// array/object open ("[" or "{"): depth += true - false (increment) | ||
// array/object close ("]" or "}"): depth += false - true (decrement) | ||
// other cases ("," or primitive): depth += true - true (numeric cast) | ||
depth += !close - !open | ||
// Remove this token | ||
return "" | ||
}) | ||
) | ||
) { | ||
return Function("return " + str)() | ||
} else { | ||
throw new Error("Invalid JSON: " + data) | ||
} | ||
} | ||
export * from './parse.js' | ||
export * from './stringify.js' |
{ | ||
"name": "dx-lib", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "dx fe lib", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -15,2 +15,3 @@ # dx-lib | ||
* [jsonp](doc/jsonp.md) | ||
* [loader](doc/loader.md) | ||
* [domready](doc/domready.md) | ||
@@ -17,0 +18,0 @@ * [cookie](doc/cookie.md) |
@@ -29,2 +29,11 @@ import puppeteer from 'puppeteer' | ||
t.deepEqual(data, { a: "a", b: "b" }) | ||
let strs = await page.evaluate(() => { | ||
return { | ||
str1: stringifyJSON({ a: "a", b: "b" }), | ||
str2: stringifyJSON({ a: "a", b: "b" }, null, 4), | ||
str3: stringifyJSON({ a: "a", b: "b" }, null, '@') | ||
} | ||
}) | ||
t.is(strs.str1, '{"a":"a","b":"b"}') | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
197371
44
1942
40