From 0d94d0cef9b233ad36f4262e0649acdbaa224e9f Mon Sep 17 00:00:00 2001 From: Ish Date: Fri, 25 Oct 2024 22:25:15 +0530 Subject: [PATCH] Create jsonStringify.js --- JavaScript/Programmes/jsonStringify.js | 54 ++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 JavaScript/Programmes/jsonStringify.js diff --git a/JavaScript/Programmes/jsonStringify.js b/JavaScript/Programmes/jsonStringify.js new file mode 100644 index 0000000..d2a2a86 --- /dev/null +++ b/JavaScript/Programmes/jsonStringify.js @@ -0,0 +1,54 @@ +// Online Javascript Editor for free +// Write, Edit and Run your Javascript code using JS Online Compiler +function isCyclic(input){ + const seen = new Set() + function helper(value = input){ + if(typeof value !== "object" || value === null){ + return false + } + seen.add(value) + return Object.values(value).some((val) => seen.has(val) || helper(val)) + } + return helper() +} + +function jsonStringify(data){ + const typeofData = typeof data + if(isCyclic(data)){ + throw new TypeError('Converting circular structure to JSON'); + } + if (typeof value === 'bigint') { + throw new TypeError('Do not know how to serialize a BigInt'); + } + if(data === '' || data === undefined || typeofData === 'symbol' || typeofData === 'function'){ + return undefined + } + if(data === null || data === Infinity || data === -Infinity || data !== data){ + return 'null' + } + if(typeofData === "string"){ + return `"${data}"` + } + if(typeofData === "number"){ + return `'${data}'` + } + if(Array.isArray(data)){ + const res = data.map(d => jsonStringify(d)) + return `'[${res.join(',')}]'` + } + if(typeofData instanceof Date){ + return `${data.toISOString()}` + } + if(typeofData === "object"){ + const res = Object.entries(data).map(([key,val]) => { + // console.log(key,val) + if(val){ + const d = jsonStringify(val) + return `"${key}":${d}` + } + }).filter(d => d !== undefined) + return `'{${res.join(',')}}'` + } +} + +console.log(jsonStringify(() => {}))