Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
"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.value);
return sass;
module.exports = jsonToSassVars;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.