-
-
Save ali-master/cb54a3501c0ea29a88a0297cb9533be9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
function jsonToSassVars (obj, indent) { | |
// Make object root properties into sass variables | |
var sass = ""; | |
for (var key in obj) { | |
sass += "$" + key + ":" + JSON.stringify(obj[key], null, indent) + ";\n"; | |
} | |
// Store string values (so they remain unaffected) | |
var storedStrings = []; | |
sass = sass.replace(/(["'])(?:(?=(\\?))\2.)*?\1/g, function (str) { | |
var id = "___JTS" + storedStrings.length; | |
storedStrings.push({id: id, value: str}); | |
return id; | |
}); | |
// Convert js lists and objects into sass lists and maps | |
sass = sass.replace(/[{\[]/g, "(").replace(/[}\]]/g, ")"); | |
// Put string values back (now that we're done converting) | |
storedStrings.forEach(function (str) { | |
sass = sass.replace(str.id, str.value); | |
}); | |
return sass; | |
} | |
module.exports = jsonToSassVars; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment