diff --git a/CHANGELOG.md b/CHANGELOG.md index 0edb499..84937e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 3.1.4 +* Convert values containing commas inside of an object into strings + ## 3.1.3 * Extend key filtering to nested maps diff --git a/src/index.js b/src/index.js index d322112..b087545 100644 --- a/src/index.js +++ b/src/index.js @@ -58,6 +58,8 @@ export function parseValue(value) { return parseMap(value); } else if (value === '') { return '""'; // Return explicitly an empty string (Sass would otherwise throw an error as the variable is set to nothing) + } else if (_.isString(value) && shouldWrapInStrings(value)) { + return '"' + value + '"'; // Sass throws an error if a value contains a comma and is not treated as a string } else { return value; } @@ -76,6 +78,22 @@ export function parseMap(map) { .join(',')})`; } +/** + * There are multiple SASS functions (e.g. hsl() oder rgb()) which receive comma-separated values + * (e.g. hsl(270, 60%, 70%)). These should not be wrapped in quotes. A string like "Tomorrow, I will + * write SASS" should be wrapped in quotes. This function checks if commas are used outside of SASS + * functions. + * + * @param input Input to check. + * + * @return {boolean} True = Should be wrapped in quotes. + */ +export function shouldWrapInStrings(input) { + const inputWithoutFunctions = input.replace(/[a-zA-Z]+\([^)]*\)/, "") // Remove functions + + return inputWithoutFunctions.includes(','); +} + // Super-hacky: Override Babel's transpiled export to provide both // a default CommonJS export and named exports. // Fixes: https://github.com/Updater/node-sass-json-importer/issues/32 diff --git a/test/index.js b/test/index.js index dd23c1a..3c3c3d4 100644 --- a/test/index.js +++ b/test/index.js @@ -226,8 +226,21 @@ describe('parseValue', function() { }); it('returns the raw value if not an array, object or empty string', function() { - expect(123).to.eql(123); + expect(parseValue(123)).to.eql(123); }); + + it('wraps the value in an object in quotes if the value contains a comma outside of a function', function() { + expect(parseValue({ + "key": "value with , somewhere", + })).to.eql('(key: "value with , somewhere")'); + }); + + it('does not wrap the value in an object in quotes if the value contains a function', function() { + expect(parseValue({ + "key": "hsl(270, 60%, 70%)", + })).to.eql('(key: hsl(270, 60%, 70%))'); + }); + it('can parse nested maps with invalid keys', function() { const nestedWithInvalid = { inner: {