Skip to content

Commit 75e9748

Browse files
authored
Merge pull request #55 from timkraut/comma-in-object
Convert values containing commas inside of an object into strings
2 parents 15dddf8 + c1490ef commit 75e9748

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 3.1.4
4+
* Convert values containing commas inside of an object into strings
5+
36
## 3.1.3
47
* Extend key filtering to nested maps
58

src/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export function parseValue(value) {
5858
return parseMap(value);
5959
} else if (value === '') {
6060
return '""'; // Return explicitly an empty string (Sass would otherwise throw an error as the variable is set to nothing)
61+
} else if (_.isString(value) && shouldWrapInStrings(value)) {
62+
return '"' + value + '"'; // Sass throws an error if a value contains a comma and is not treated as a string
6163
} else {
6264
return value;
6365
}
@@ -76,6 +78,22 @@ export function parseMap(map) {
7678
.join(',')})`;
7779
}
7880

81+
/**
82+
* There are multiple SASS functions (e.g. hsl() oder rgb()) which receive comma-separated values
83+
* (e.g. hsl(270, 60%, 70%)). These should not be wrapped in quotes. A string like "Tomorrow, I will
84+
* write SASS" should be wrapped in quotes. This function checks if commas are used outside of SASS
85+
* functions.
86+
*
87+
* @param input Input to check.
88+
*
89+
* @return {boolean} True = Should be wrapped in quotes.
90+
*/
91+
export function shouldWrapInStrings(input) {
92+
const inputWithoutFunctions = input.replace(/[a-zA-Z]+\([^)]*\)/, "") // Remove functions
93+
94+
return inputWithoutFunctions.includes(',');
95+
}
96+
7997
// Super-hacky: Override Babel's transpiled export to provide both
8098
// a default CommonJS export and named exports.
8199
// Fixes: https://github.com/Updater/node-sass-json-importer/issues/32

test/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,21 @@ describe('parseValue', function() {
226226
});
227227

228228
it('returns the raw value if not an array, object or empty string', function() {
229-
expect(123).to.eql(123);
229+
expect(parseValue(123)).to.eql(123);
230230
});
231+
232+
it('wraps the value in an object in quotes if the value contains a comma outside of a function', function() {
233+
expect(parseValue({
234+
"key": "value with , somewhere",
235+
})).to.eql('(key: "value with , somewhere")');
236+
});
237+
238+
it('does not wrap the value in an object in quotes if the value contains a function', function() {
239+
expect(parseValue({
240+
"key": "hsl(270, 60%, 70%)",
241+
})).to.eql('(key: hsl(270, 60%, 70%))');
242+
});
243+
231244
it('can parse nested maps with invalid keys', function() {
232245
const nestedWithInvalid = {
233246
inner: {

0 commit comments

Comments
 (0)