An open API service indexing awesome lists of open source software.

https://github.com/dawaltconley/sass-cast

Convert Javascript objects to Sass objects and vice versa.
https://github.com/dawaltconley/sass-cast

cast converter json sass

Last synced: 2 months ago
JSON representation

Convert Javascript objects to Sass objects and vice versa.

Awesome Lists containing this project

README

          

# sass-cast

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](https://sasscss.org/blog/new-js-api-release-candidate).

## Usage

#### Table of Contents

* [toSass](#tosass)
* [Parameters](#parameters)
* [Examples](#examples)
* [fromSass](#fromsass)
* [Parameters](#parameters-1)
* [Examples](#examples-1)
* [sassFunctions](#sassfunctions)
* [Examples](#examples-2)
* [require](#require)
* [Examples](#examples-3)
* [Parameters](#parameters-2)
* [legacy](#legacy)
* [toSass](#tosass-1)
* [Parameters](#parameters-3)
* [fromSass](#fromsass-1)
* [Parameters](#parameters-4)
* [sassFunctions](#sassfunctions-1)
* [require](#require-1)
* [Parameters](#parameters-5)

### toSass

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.

#### Parameters

* `value` **any** the value to be converted
* `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)

* `options.parseUnquotedStrings` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** whether to parse unquoted strings for colors or numbers with units (optional, default `false`)
* `options.resolveFunctions` **([boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean) | [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)\)** if true, resolve functions and attempt to cast their return values. if an array, pass as arguments when resolving (optional, default `false`)
* `options.quotes` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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, default `true`)

#### Examples

```javascript
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](https://sass-lang.com/documentation/js-api/classes/Value)

### fromSass

Converts Sass values to their Javascript equivalents.

#### Parameters

* `object` **Value** a [Sass value](https://sass-lang.com/documentation/js-api/classes/Value)
* `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)

* `options.preserveUnits` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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, default `false`)
* `options.rgbColors` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** By default, colors are returned as strings. If true, `fromSass` will return colors as an object with `r`, `g`, `b`, and `a`, properties. (optional, default `false`)
* `options.preserveQuotes` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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, default `false`)

#### Examples

```javascript
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

### sassFunctions

An object defining Sass utility functions.

#### Examples

Pass to sass using the JS API

```javascript
const { sassFunctions } = require('sass-cast');
const sass = require('sass');

sass.compile('main.scss', { functions: sassFunctions });
```

#### require

Sass function for importing data from Javascript or JSON files.
Calls the CommonJS `require` function under the hood.

##### Examples

```scss
// import config info from tailwindcss
$tw: require('./tailwind.config.js', $parseUnquotedStrings: true);
$tw-colors: map.get($tw, theme, extend, colors);
```

##### Parameters

* `$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](#tosass). (optional, default `false`)
* `$resolveFunctions` **SassBoolean** Passed as an option to [toSass](#tosass). (optional, default `false`)
* `$quotes` **SassBoolean** Passed as an option to [toSass](#tosass). (optional, default `true`)

Returns **Value** a [Sass value](https://sass-lang.com/documentation/js-api/classes/Value)

### legacy

Identical methods that interface with Sass's legacy Javascript API for older versions of Sass. Use `require('sass-cast/legacy')`.

#### toSass

Converts any Javascript object to an equivalent legacy Sass object.

##### Parameters

* `value` **any** the value to be converted
* `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)

* `options.parseUnquotedStrings` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** whether to parse unquoted strings for colors or numbers with units (optional, default `false`)
* `options.resolveFunctions` **([boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean) | [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)\)** if true, resolve functions and attempt to cast their return values. if an array, pass as arguments when resolving (optional, default `false`)
* `options.quotes` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | null)** the type of quotes to use when quoting Sass strings (single or double) (optional, default `"'"`)

Returns **LegacyObject** a [legacy Sass object](https://sass-lang.com/documentation/js-api/modules#LegacyValue)

#### fromSass

Converts legacy Sass objects to their Javascript equivalents.

##### Parameters

* `object` **LegacyObject** a [legacy Sass object](https://sass-lang.com/documentation/js-api/modules#LegacyValue)
* `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)

* `options.preserveUnits` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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, default `false`)
* `options.rgbColors` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** By default, colors are returned as strings. If true, `fromSass` will return colors as an object with `r`, `g`, `b`, and `a`, properties. (optional, default `false`)
* `options.preserveQuotes` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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, default `false`)

Returns **any** a Javascript value corresponding to the Sass input

#### sassFunctions

* **See**: [sassFunctions](#sassfunctions)

An object defining legacy Sass utility functions.

##### require

* **See**: [require](https://nodejs.org/api/globals.html#globals_require)

Legacy Sass function for importing data from Javascript or JSON files.

###### Parameters

* `$module`
* `$properties`
* `$parseUnquotedStrings`
* `$resolveFunctions`
* `$quotes`
* `done`