Last active
September 18, 2019 08:58
-
-
Save kasper573/ea4f4861a81e626ea308 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