json-alexander
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "json-alexander", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Serenity Now! Forgiving JSON parser", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
const { isBalanced, trimQuotes, isNull } = require('./utils') | ||
const DEBUG = false | ||
const log = (DEBUG) ? log : () => {} | ||
const log = (DEBUG) ? console.log : () => {} | ||
@@ -21,3 +21,3 @@ module.exports.safeParse = function simpleParse(data, defaultValue) { | ||
module.exports.parseJSON = function parseJSON(input, defaultValue) { | ||
let error | ||
if (isNull(input) || input === '' || input === undefined) { | ||
@@ -37,2 +37,3 @@ return defaultValue || input | ||
const [err, first ] = parse(value) | ||
error = err | ||
// log('trimmed', trimmed) | ||
@@ -49,2 +50,3 @@ if (first) { | ||
const [errTwo, second ] = parse(trimmed) | ||
error = errTwo | ||
if (second) { | ||
@@ -58,4 +60,7 @@ log('second', second) | ||
const fixString = convertStringObjectToJsonString(trimmed) | ||
// log('fixString', fixString) | ||
//* | ||
log('fixString', fixString) | ||
/**/ | ||
const [errThree, third ] = parse(fixString) | ||
error = errThree | ||
if (third) { | ||
@@ -73,3 +78,3 @@ log('third', third) | ||
const [errFour, four ] = parse(what) | ||
error = errFour | ||
if (four) { | ||
@@ -83,3 +88,5 @@ log('four', four) | ||
const final = trimQuotes(what.replace(/'/g, '"')) | ||
// console.log('final', final) | ||
const [errFive, five ] = parse(final) | ||
error = errFive | ||
if (five) { | ||
@@ -99,5 +106,23 @@ log('five', five) | ||
// .replace(/\[\s?(false+?)\s?\]/, 'FALSE_PLACEHOLDER') | ||
.replace(/\[\s?([A-Za-z]+?)\s?\]/, '[ "$1" ]') | ||
// [ xyz ] | ||
.replace(/\[\s?([A-Za-z.@_]+?)\s?\]/g, '[ "$1" ]') | ||
// [ xyz, | ||
// .replace(/\[\s?([_@.A-Za-z]+?),\s?/g, '[ "$1",') | ||
// .replace(/,\s?([_@.A-Za-z]+?)\s?\]/g, ', "$1" ]') | ||
.replace(/:\s?([A-Za-z]+?)\s}/g, ': "$1" }') | ||
// log('newer', newer) | ||
var pattern = /([^[]+(?=]))/gm | ||
let updated = newer | ||
while((result = pattern.exec(newer)) !== null) { | ||
// console.log(result); | ||
if (result[0]) { | ||
const newText = result[0].replace(/\b([A-Za-z.@_]+?)\b/g, '"$1"') | ||
updated = updated.replace(result[0], newText) | ||
} | ||
} | ||
// console.log('xupdated', updated) | ||
log('newer', newer) | ||
// Wrap values missing quotes | ||
@@ -113,5 +138,6 @@ const newerStill = newer | ||
// log('newerStill', newerStill) | ||
log('newerStill', newerStill) | ||
const [errSeven, six ] = parse(newerStill) | ||
error = errSeven | ||
if (six) { | ||
@@ -127,3 +153,4 @@ log('six', six) | ||
const [errSix, seven ] = parse(balance) | ||
const [errEight, seven ] = parse(balance) | ||
error = errEight | ||
if (seven) { | ||
@@ -134,3 +161,12 @@ log('seven', seven) | ||
throw new Error('Unable to parse JSON') | ||
if (newerStill.match(/^"?\[{/) && !newerStill.match(/\}\]$/)) { | ||
const [errNine, eight ] = parse(`${newerStill} }]`) | ||
error = errNine | ||
if (eight) { | ||
log('eight', eight) | ||
return eight | ||
} | ||
} | ||
throw new Error(`Unable to parse JSON\n${error}\n\n${input}`) | ||
} | ||
@@ -153,6 +189,15 @@ | ||
// function convertStringObjectToJsonString(str) { | ||
// return str.replace(/(\w+:)|(\w+ :)/g, (matchedStr) => { | ||
// return '"' + matchedStr.substring(0, matchedStr.length - 1) + '":' | ||
// }) | ||
// } | ||
function convertStringObjectToJsonString(str) { | ||
return str.replace(/(\w+:)|(\w+ :)/g, (matchedStr) => { | ||
return '"' + matchedStr.substring(0, matchedStr.length - 1) + '":' | ||
return str.replace(/(\w+\s*(?::))[^:/]/g, (matchedStr) => { | ||
const x = matchedStr.substring(0, matchedStr.length - 2) | ||
const y = x.trim().replace(/:/, '') | ||
return '"' + y + '": ' | ||
}) | ||
.replace(/""/g, '"').replace(/"'/g, "'") | ||
} | ||
@@ -159,0 +204,0 @@ |
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
14324
376