Skip to content

Commit 61d6204

Browse files
committed
Refactors regex while loop to process sass maps
1 parent 9d721da commit 61d6204

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

index.js

+29-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var gutil = require('gulp-util');
44

55
module.exports = function() {
66
return through(function (file) {
7-
var regex = /\$(.*?):(.*?);/g;
7+
var regex = /\$(.*?):([\s\S]*?);/g;
88
var variables = {};
99
var stringifiedContent;
1010
var jsonVariables;
@@ -24,8 +24,35 @@ module.exports = function() {
2424
if (m.index === regex.lastIndex) {
2525
regex.lastIndex++;
2626
}
27+
// test for sass maps
28+
if (m[2].indexOf(':') > -1 &&
29+
m[2].indexOf('(') > -1 &&
30+
m[2].indexOf(')') > -1 &&
31+
m[2].indexOf(',') > -1) {
32+
// Split of the SASS map property
33+
var sassMapRule = m[2].split(',');
34+
// Empty object that will be the JSON property
35+
var outputObj = {};
2736

28-
variables[m[1].trim()] = m[2].trim();
37+
for (var prop in sassMapRule) {
38+
var sassMapRuleObj = sassMapRule[prop].split(':');
39+
40+
// ignore sass map property it it is null or undefined
41+
if (!sassMapRuleObj[0] || !sassMapRuleObj[1]) {
42+
continue;
43+
}
44+
45+
// time newlines, whitespaces, and parentheses
46+
var sassMapKey = sassMapRuleObj[0].replace(/[\(\n]/, '').trim();
47+
var sassMapVal = sassMapRuleObj[1].trim();
48+
49+
outputObj[sassMapKey] = sassMapVal;
50+
}
51+
variables[m[1].trim()] = outputObj;
52+
// non sass maps rules
53+
} else {
54+
variables[m[1].trim()] = m[2].trim();
55+
}
2956
}
3057

3158
jsonVariables = JSON.stringify(variables, null, '\t');

0 commit comments

Comments
 (0)