Convert Javascript objects to Sass objects and vice versa. Based on sassport
and node-sass-utils
, updated to work with newer sass versions and the modern
Sass API.
Converts any Javascript object to an equivalent Sass value.
This method is recursive and will convert the values of any array or object, as well as the array or object itself.
-
value
any the value to be converted -
options
Object (optional, default{}
)-
options.parseUnquotedStrings
boolean whether to parse unquoted strings for colors or numbers with units (optional, defaultfalse
) -
options.resolveFunctions
(boolean | Array<any>) if true, resolve functions and attempt to cast their return values. if an array, pass as arguments when resolving (optional, defaultfalse
) -
options.quotes
boolean controls whether returned SassStrings are quoted. input strings that contain quotes will always return a quoted SassString even if this flag is false. (optional, defaulttrue
)
-
const { toSass } = require('sass-cast');
const string = toSass('a simple string');
// quoted SassString => '"a simple string"'
const map = toSass({
key: 'value',
nested: {
'complex//:key': [ null, 4 ],
}
});
// SassMap => '("key": "value", "nested": ("complex//:key": (null, 4)))'
Returns Value a Sass value
Converts Sass values to their Javascript equivalents.
-
object
Value a Sass value -
options
Object (optional, default{}
)-
options.preserveUnits
boolean By default, only the values of numbers are returned, not their units. If true,fromSass
will return numbers as a two-item Array, i.e. [ value, unit ] (optional, defaultfalse
) -
options.rgbColors
boolean By default, colors are returned as strings. If true,fromSass
will return colors as an object withr
,g
,b
, anda
, properties. (optional, defaultfalse
) -
options.preserveQuotes
boolean By default, quoted Sass strings return their inner text as a string. If true,fromSass
will preserve the quotes in the returned string value. (optional, defaultfalse
)
-
const { fromSass, toSass } = require('sass-cast');
const sassString = toSass('a sass string object');
const string = fromSass(sassString);
// 'a sass string object'
Returns any a Javascript value corresponding to the Sass input
An object defining Sass utility functions.
Pass to sass using the JS API
const { sassFunctions } = require('sass-cast');
const sass = require('sass');
sass.compile('main.scss', { functions: sassFunctions });
Sass function for importing data from Javascript or JSON files.
Calls the CommonJS require
function under the hood.
// import config info from tailwindcss
$tw: require('./tailwind.config.js', $parseUnquotedStrings: true);
$tw-colors: map.get($tw, theme, extend, colors);
-
$module
SassString Path to the file or module. Relative paths are relative to the Node process running Sass compilation. -
$properties
SassList List of properties, if you only want to parse part of the module data. (optional, default()
) -
$parseUnquotedStrings
SassBoolean Passed as an option to toSass. (optional, defaultfalse
) -
$resolveFunctions
SassBoolean Passed as an option to toSass. (optional, defaultfalse
) -
$quotes
SassBoolean Passed as an option to toSass. (optional, defaulttrue
)
Returns Value a Sass value
Identical methods that interface with Sass's legacy Javascript API for older versions of Sass. Use require('sass-cast/legacy')
.
Converts any Javascript object to an equivalent legacy Sass object.
-
value
any the value to be converted -
options
Object (optional, default{}
)-
options.parseUnquotedStrings
boolean whether to parse unquoted strings for colors or numbers with units (optional, defaultfalse
) -
options.resolveFunctions
(boolean | Array<any>) if true, resolve functions and attempt to cast their return values. if an array, pass as arguments when resolving (optional, defaultfalse
) -
options.quotes
(string | null) the type of quotes to use when quoting Sass strings (single or double) (optional, default"'"
)
-
Returns LegacyObject a legacy Sass object
Converts legacy Sass objects to their Javascript equivalents.
-
object
LegacyObject a legacy Sass object -
options
Object (optional, default{}
)-
options.preserveUnits
boolean By default, only the values of numbers are returned, not their units. If true,fromSass
will return numbers as a two-item Array, i.e. [ value, unit ] (optional, defaultfalse
) -
options.rgbColors
boolean By default, colors are returned as strings. If true,fromSass
will return colors as an object withr
,g
,b
, anda
, properties. (optional, defaultfalse
) -
options.preserveQuotes
boolean By default, quoted Sass strings return their inner text as a string. If true,fromSass
will preserve the quotes in the returned string value. (optional, defaultfalse
)
-
Returns any a Javascript value corresponding to the Sass input
- See: sassFunctions
An object defining legacy Sass utility functions.
- See: require
Legacy Sass function for importing data from Javascript or JSON files.
$module
$properties
$parseUnquotedStrings
$resolveFunctions
$quotes
done