# node-sass-functions-json

> JSON encode and decode functions for node-sass

Latest version **1.0.0** (published 2017-05-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-sass-functions-json
pnpm add node-sass-functions-json
yarn add node-sass-functions-json
bun add node-sass-functions-json
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2017-05-09 |
| First published | 2016-12-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | itgalaxy |
| Maintainers | itgalaxy |
| Keywords | decode, encode, json-decode, json-encode, functions, json, node-sass, sass, scss |

## Links

- npm: https://www.npmjs.com/package/node-sass-functions-json
- Repository: https://github.com/itgalaxy/node-sass-functions-json
- Homepage: https://github.com/itgalaxy/node-sass-functions-json#readme
- Issues: https://github.com/itgalaxy/node-sass-functions-json/issues
- npm.io page: https://npm.io/package/node-sass-functions-json

## Dependencies (5)

- [rgb-hex](https://npm.io/package/rgb-hex.md) ^2.1.0
- [parse-color](https://npm.io/package/parse-color.md) ^1.0.0
- [lodash.round](https://npm.io/package/lodash.round.md) ^4.0.4
- [shorten-css-hex](https://npm.io/package/shorten-css-hex.md) ^1.1.0
- [parse-css-dimension](https://npm.io/package/parse-css-dimension.md) ^1.1.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.0 (latest) — 2017-05-09
- 1.0.0-alpha.1 — 2016-12-26

## README

# node-sass-functions-json

[![NPM version](https://img.shields.io/npm/v/node-sass-functions-json.svg)](https://www.npmjs.org/package/node-sass-functions-json) 
[![Travis Build Status](https://img.shields.io/travis/itgalaxy/node-sass-functions-json/master.svg?label=build)](https://travis-ci.org/itgalaxy/node-sass-functions-json) 
[![dependencies Status](https://david-dm.org/itgalaxy/node-sass-functions-json/status.svg)](https://david-dm.org/itgalaxy/node-sass-functions-json) 
[![devDependencies Status](https://david-dm.org/itgalaxy/node-sass-functions-json/dev-status.svg)](https://david-dm.org/itgalaxy/node-sass-functions-json?type=dev)
[![Greenkeeper badge](https://badges.greenkeeper.io/itgalaxy/node-sass-functions-json.svg)](https://greenkeeper.io/)

JSON encode and decode functions for [node-sass](https://github.com/sass/node-sass).

## Install

```shell
npm install node-sass-functions-json --save
```

## Usage

```js
const sass = require('node-sass');
const jsonFunctions = require('node-sass-functions-json');

sass.render({
    file: './index.scss',
    functions: Object.assign({}, jsonFunctions)
}, function (error, result) {
    // ...
});
```

Module exports object with prepared functions `json-encode` and `json-decode`. 
If you need functions as separate entities, they are available as static properties `encode` and `decode`.

### Encode

Input:

```scss
$list: 1, 2, "3", (4,5,6), (foo: "bar baz");
$map: (
    foo: 1,
    bar: (2, 3),
    baz: "3 3 3",
    bad: (
        foo: 11,
        bar: 22,
        baz: (
            5, 4, 6, null, 1, 1.23456789px
        ),
        bag: "foo bar"
    ),
    qux: rgba(255,255,255,0.5),
    corgle: red
);

body {
    content: json-encode($list);
    content: json-encode($map);
    content: json-encode($list, $quotes: false);
    content: json-encode($map, $quotes: false);
}
```

Output:

```css
body {
    content: '[1,2,"3",[4,5,6],{"foo":"bar baz"}]';
    content: '{"foo":1,"bar":[2,3],"baz":"3 3 3","bad":{"foo":11,"bar":22,"baz":[5,4,6,null,1,"1.23457px"],"bag":"foo bar"},"qux":"rgba(255,255,255,0.5)","corgle":"#f00"}';
    content: [1,2,"3",[4,5,6],{"foo":"bar baz"}];
    content: {"foo":1,"bar":[2,3],"baz":"3 3 3","bad":{"foo":11,"bar":22,"baz":[5,4,6,null,1,"1.23457px"],"bag":"foo bar"},"qux":"rgba(255,255,255,0.5)","corgle":"#f00"};
}
```

### Decode

Input:

```scss
$array: '[1,2,"3",[4,5,6],{"foo":"bar baz"}]';
$object: '{"foo":1,"bar":[2,3],"baz":"3 3 3","bad":{"foo":11,"bar":22,"baz":[5,4,6,null,1,"1.23456789px"],"bag":"foo bar"},"qux":"rgba(255,255,255,0.5)","corgle":"#f00"}';

@debug json-decode($array);
@debug json-decode($object);
```

Output:

```shell
DEBUG: 1, 2, 3, 4, 5, 6, (foo: bar baz)
DEBUG: (foo: 1, bar: 2, 3, baz: 3 3 3, bad: (foo: 11, bar: 22, baz: 5, 4, 6, null, 1, 1.23456789px, bag: foo bar), 
        qux: rgba(255, 255, 255, 0.5), corgle: red)
```

## API

### json-encode(data, quotes)

Returns: `sass.types.String`

Encodes (`JSON.stringify`) data 
and returns [Sass string](http://sass-lang.com/documentation/file.SASS_REFERENCE.html#sass-script-strings). 
By default, string is quoted with single quotes so that it can be easily used in standard CSS values.

-   [Sass lists](http://sass-lang.com/documentation/file.SASS_REFERENCE.html#lists) are transformed to arrays.

-   [Sass maps](http://sass-lang.com/documentation/file.SASS_REFERENCE.html#maps) are transformed to objects.

-   [Sass colors](http://sass-lang.com/documentation/file.SASS_REFERENCE.html#colors) are transformed 
    to `rgba()` syntax if they have alpha value, otherwise they are transformed 
    to hex value (and it’s shorther version if possible).

-   [Sass strings](http://sass-lang.com/documentation/file.SASS_REFERENCE.html#sass-script-strings) 
    are transformed to strings

-   Sass numbers are transformed to numbers.

-   Sass null values and anything unresolved is transformed to null values.

#### data

Type: `sass.types.*`

Data to encode (stringify).

#### quotes

Type: `Boolean|sass.types.Boolean`  
Default: `true`

Should output string be quoted with single quotes.

### json-decode(data)

Returns: `sass.types.*`

Decodes (`JSON.parse`) string 
and returns one of [available Sass types](https://github.com/sass/node-sass#functions--v300---experimental).

-   Arrays are transformed to [Sass lists](http://sass-lang.com/documentation/file.SASS_REFERENCE.html#lists).

-   Objects are transformed to [Sass maps](http://sass-lang.com/documentation/file.SASS_REFERENCE.html#maps).

-   Anything properly parsed with [parse-color](https://github.com/substack/parse-color) 
    is transformed to [Sass color](http://sass-lang.com/documentation/file.SASS_REFERENCE.html#colors).

-   Strings are transformed to Sass numbers with units if they can be properly parsed 
    with [parse-css-dimension](https://github.com/jedmao/parse-css-dimension), otherwise they are transformed 
    to [Sass strings](http://sass-lang.com/documentation/file.SASS_REFERENCE.html#sass-script-strings).

-   Numbers are transformed to Sass numbers.

-   Null values and anything unresolved is transformed to Sass null values.

#### data

Type: `sass.types.String|sass.types.Number|sass.types.Boolean|sass.types.Null`

String to decode (parse).

## Related

-   [node-sass-json-functions](https://github.com/niksy/node-sass-json-functions) - Thanks for inspiration.

## Contribution

Feel free to push your code if you agree with publishing under the MIT license.

## [Changelog](CHANGELOG.md)

## [License](LICENSE)

---
_Source: https://npm.io/package/node-sass-functions-json · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
