babel-plugin-object-to-json-parse
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -24,3 +24,26 @@ "use strict"; | ||
}; | ||
const createSafeStringForJsonParse = (value) => { | ||
if (/\\/.test(value)) { | ||
value = value.replace(/\\/g, '\\\\'); | ||
} | ||
if (/"/.test(value)) { | ||
value = value.replace(/"/g, '\\"'); | ||
} | ||
if (/[\t\f\r\n\b]/g.test(value)) { | ||
const codes = ['\f', '\r', '\n', '\t', '\b']; | ||
const replaceCodes = ['\\f', '\\r', '\\n', '\\t', '\\b']; | ||
for (let i = 0; i < codes.length; i++) { | ||
value = value.replace(new RegExp(codes[i]), replaceCodes[i]); | ||
} | ||
} | ||
return value; | ||
}; | ||
function converter(node) { | ||
// for negative number, ex) -10 | ||
if (types_1.isUnaryExpression(node)) { | ||
const { operator, argument } = node; | ||
if (operator === '-' && types_1.isNumericLiteral(argument)) { | ||
return -argument.value; | ||
} | ||
} | ||
if (!isValidJsonValue(node)) { | ||
@@ -30,17 +53,5 @@ throw new Error('Invalid value is included.'); | ||
if (types_1.isStringLiteral(node)) { | ||
let { value } = node; | ||
if (/\\/.test(value)) { | ||
value = value.replace(/\\/g, '\\\\'); | ||
} | ||
if (/"/.test(value)) { | ||
value = value.replace(/"/g, '\\"'); | ||
} | ||
if (/[\t\f\r\n\b]/g.test(value)) { | ||
const codes = ['\f', '\r', '\n', '\t', '\b']; | ||
const replaceCodes = ['\\f', '\\r', '\\n', '\\t', '\\b']; | ||
for (let i = 0; i < codes.length; i++) { | ||
value = value.replace(new RegExp(codes[i]), replaceCodes[i]); | ||
} | ||
} | ||
return value; | ||
const { value } = node; | ||
const safeValue = createSafeStringForJsonParse(value); | ||
return safeValue; | ||
} | ||
@@ -63,3 +74,6 @@ if (types_1.isNullLiteral(node)) { | ||
return properties.reduce((acc, cur) => { | ||
const key = cur.key.name || cur.key.value; | ||
let key = cur.key.name || cur.key.value; | ||
if (typeof key === 'string') { | ||
key = createSafeStringForJsonParse(key); | ||
} | ||
// see issues#10 | ||
@@ -66,0 +80,0 @@ if (typeof key === 'number' && !Number.isSafeInteger(key)) { |
{ | ||
"name": "babel-plugin-object-to-json-parse", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
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
11572
164