@walletconnect/safe-json
Advanced tools
Comparing version 1.0.1 to 1.0.2
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.safeJsonStringify = exports.safeJsonParse = void 0; | ||
const JSONStringify = data => JSON.stringify(data, (_, value) => typeof value === "bigint" ? value.toString() + "n" : value); | ||
const JSONParse = json => { | ||
const numbersBiggerThanMaxInt = /([\[:])?(\d{17,}|(?:[9](?:[1-9]07199254740991|0[1-9]7199254740991|00[8-9]199254740991|007[2-9]99254740991|007199[3-9]54740991|0071992[6-9]4740991|00719925[5-9]740991|007199254[8-9]40991|0071992547[5-9]0991|00719925474[1-9]991|00719925474099[2-9])))([,\}\]])/g; | ||
const serializedData = json.replace(numbersBiggerThanMaxInt, "$1\"$2n\"$3"); | ||
return JSON.parse(serializedData, (_, value) => { | ||
const isCustomFormatBigInt = typeof value === "string" && value.match(/^\d+n$/); | ||
if (isCustomFormatBigInt) | ||
return BigInt(value.substring(0, value.length - 1)); | ||
return value; | ||
}); | ||
}; | ||
function safeJsonParse(value) { | ||
@@ -9,3 +20,3 @@ if (typeof value !== "string") { | ||
try { | ||
return JSON.parse(value); | ||
return JSONParse(value); | ||
} | ||
@@ -18,5 +29,5 @@ catch (_a) { | ||
function safeJsonStringify(value) { | ||
return typeof value === "string" ? value : JSON.stringify(value); | ||
return typeof value === "string" ? value : JSONStringify(value) || ""; | ||
} | ||
exports.safeJsonStringify = safeJsonStringify; | ||
//# sourceMappingURL=index.js.map |
@@ -0,1 +1,12 @@ | ||
const JSONStringify = data => JSON.stringify(data, (_, value) => typeof value === "bigint" ? value.toString() + "n" : value); | ||
const JSONParse = json => { | ||
const numbersBiggerThanMaxInt = /([\[:])?(\d{17,}|(?:[9](?:[1-9]07199254740991|0[1-9]7199254740991|00[8-9]199254740991|007[2-9]99254740991|007199[3-9]54740991|0071992[6-9]4740991|00719925[5-9]740991|007199254[8-9]40991|0071992547[5-9]0991|00719925474[1-9]991|00719925474099[2-9])))([,\}\]])/g; | ||
const serializedData = json.replace(numbersBiggerThanMaxInt, "$1\"$2n\"$3"); | ||
return JSON.parse(serializedData, (_, value) => { | ||
const isCustomFormatBigInt = typeof value === "string" && value.match(/^\d+n$/); | ||
if (isCustomFormatBigInt) | ||
return BigInt(value.substring(0, value.length - 1)); | ||
return value; | ||
}); | ||
}; | ||
export function safeJsonParse(value) { | ||
@@ -6,3 +17,3 @@ if (typeof value !== "string") { | ||
try { | ||
return JSON.parse(value); | ||
return JSONParse(value); | ||
} | ||
@@ -14,4 +25,4 @@ catch (_a) { | ||
export function safeJsonStringify(value) { | ||
return typeof value === "string" ? value : JSON.stringify(value); | ||
return typeof value === "string" ? value : JSONStringify(value) || ""; | ||
} | ||
//# sourceMappingURL=index.js.map |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("safeJsonUtils",[],t):"object"==typeof exports?exports.safeJsonUtils=t():e.safeJsonUtils=t()}(this,(function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.safeJsonStringify=t.safeJsonParse=void 0,t.safeJsonParse=function(e){if("string"!=typeof e)throw new Error("Cannot safe json parse value of type "+typeof e);try{return JSON.parse(e)}catch(t){return e}},t.safeJsonStringify=function(e){return"string"==typeof e?e:JSON.stringify(e)}}])})); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("safeJsonUtils",[],t):"object"==typeof exports?exports.safeJsonUtils=t():e.safeJsonUtils=t()}(this,(function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.safeJsonStringify=t.safeJsonParse=void 0;t.safeJsonParse=function(e){if("string"!=typeof e)throw new Error("Cannot safe json parse value of type "+typeof e);try{return(e=>{const t=e.replace(/([\[:])?(\d{17,}|(?:[9](?:[1-9]07199254740991|0[1-9]7199254740991|00[8-9]199254740991|007[2-9]99254740991|007199[3-9]54740991|0071992[6-9]4740991|00719925[5-9]740991|007199254[8-9]40991|0071992547[5-9]0991|00719925474[1-9]991|00719925474099[2-9])))([,\}\]])/g,'$1"$2n"$3');return JSON.parse(t,(e,t)=>"string"==typeof t&&t.match(/^\d+n$/)?BigInt(t.substring(0,t.length-1)):t)})(e)}catch(t){return e}},t.safeJsonStringify=function(e){return"string"==typeof e?e:(t=e,JSON.stringify(t,(e,t)=>"bigint"==typeof t?t.toString()+"n":t)||"");var t}}])})); |
{ | ||
"name": "@walletconnect/safe-json", | ||
"description": "Safe Json Utils", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"author": "WalletConnect, Inc. <walletconnect.com>", | ||
@@ -37,3 +37,6 @@ "license": "MIT", | ||
"test": "env TS_NODE_PROJECT=\"tsconfig.cjs.json\" mocha --timeout 5000 --exit -r ts-node/register ./test/**/*.test.ts", | ||
"lint": "eslint -c '../../.eslintrc' --fix './src/**/*.ts'" | ||
"lint": "eslint -c '../../.eslintrc' --fix './src/**/*.ts'", | ||
"npm-publish:latest": "npm publish --access public --tag latest", | ||
"npm-publish:canary": "npm publish --access public --tag canary", | ||
"prepublishOnly": "npm run test && npm run build" | ||
}, | ||
@@ -40,0 +43,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
215027
61