Skip to content

Instantly share code, notes, and snippets.

@kasper573
Last active September 18, 2019 08:58
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kasper573/ea4f4861a81e626ea308 to your computer and use it in GitHub Desktop.
Save kasper573/ea4f4861a81e626ea308 to your computer and use it in GitHub Desktop.
"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