Skip to content

Commit

Permalink
Switch to Dart Sass
Browse files Browse the repository at this point in the history
  • Loading branch information
niksy committed Jan 14, 2021
1 parent b6e8543 commit 923d69b
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Changed

- Switch to Dart Sass
- Upgrade package

### Removed
Expand Down
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@

[![Build Status][ci-img]][ci]

JSON encode and decode functions for [node-sass][node-sass].
JSON encode and decode functions for [sass][sass].

## Install

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

## Usage

```js
var sass = require('node-sass');
var jsonFns = require('node-sass-json-functions');
import sass from 'sass';
import jsonFns from 'node-sass-json-functions';

sass.render(
{
file: './index.scss',
functions: Object.assign({}, jsonFns)
functions: { ...jsonFns }
},
function (err, res) {
(err, res) => {
// ...
}
);
```

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

Expand Down Expand Up @@ -164,7 +162,7 @@ MIT © [Ivan Nikolić](http://ivannikolic.com)

[ci]: https://travis-ci.org/niksy/node-sass-json-functions
[ci-img]: https://travis-ci.org/niksy/node-sass-json-functions.svg?branch=master
[node-sass]: https://github.com/sass/node-sass
[sass]: https://github.com/sass/dart-sass
[sass-types]: https://github.com/sass/node-sass#functions--v300---experimental
[sass-list]: http://sass-lang.com/documentation/file.SASS_REFERENCE.html#lists
[sass-map]: http://sass-lang.com/documentation/file.SASS_REFERENCE.html#maps
Expand Down
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sass from 'node-sass';
import sass from 'sass';
import getJsonValueFromSassValue from './lib/sass-to-json';
import setJsonValueToSassValue from './lib/json-to-sass';

Expand Down Expand Up @@ -38,5 +38,3 @@ export default {
'json-encode($value, $quotes: true)': encode,
'json-decode($value)': decode
};

export { encode, decode };
31 changes: 12 additions & 19 deletions lib/json-to-sass.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sass from 'node-sass';
import sass from 'sass';
import { isArray, isPlainObject, isString, isNumber, isBoolean } from 'lodash';
import parseColor from 'parse-color';
import parseUnit from 'parse-css-dimension';
Expand All @@ -23,16 +23,13 @@ function isColor(value) {
function parseValueToStringOrNumber(value) {
let resolvedValue, resolvedUnitValue;
try {
resolvedUnitValue = parseUnit(value);
if (unitTypes.indexOf(resolvedUnitValue.type) !== -1) {
resolvedValue = new types.Number(
resolvedUnitValue.value,
resolvedUnitValue.unit
);
} else if (resolvedUnitValue.type === 'percentage') {
resolvedValue = new types.Number(resolvedUnitValue.value, '%');
const { value: parsedValue, unit, type } = parseUnit(value);
if (unitTypes.includes(type)) {
resolvedValue = new types.Number(parsedValue, unit);
} else if (type === 'percentage') {
resolvedValue = new types.Number(parsedValue, '%');
} else {
resolvedValue = new types.String(resolvedUnitValue.value);
resolvedValue = new types.String(value);
}
} catch (error) {
resolvedValue = new types.String(value);
Expand Down Expand Up @@ -80,8 +77,8 @@ function setJsonValueToSassValue(value) {
function arrayToList(array) {
const length = array.length;
const data = new types.List(length);
for (let index = 0; index < length; index++) {
data.setValue(index, setJsonValueToSassValue(array[index]));
for (const [index, item] of array.entries()) {
data.setValue(index, setJsonValueToSassValue(item));
}
return data;
}
Expand All @@ -93,13 +90,9 @@ function arrayToList(array) {
function objectToMap(object) {
const length = Object.keys(object).length;
const data = new types.Map(length);
let index = 0;
for (let property in object) {
if (object.hasOwnProperty(property)) {
data.setKey(index, setJsonValueToSassValue(property));
data.setValue(index, setJsonValueToSassValue(object[property]));
index++;
}
for (const [index, [property, value]] of Object.entries(object).entries()) {
data.setKey(index, setJsonValueToSassValue(property));
data.setValue(index, setJsonValueToSassValue(object[property]));
}
return data;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/sass-to-json.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sass from 'node-sass';
import sass from 'sass';
import { round } from 'lodash';
import rgbHex from 'rgb-hex';
import shortHexColor from 'shorten-css-hex';
Expand Down Expand Up @@ -51,7 +51,7 @@ function getJsonValueFromSassValue(value) {
function listToArray(list) {
const length = list.getLength();
const data = [];
for (let index = 0; index < length; index++) {
for (const index of Array.from({ length }).keys()) {
const value = getJsonValueFromSassValue(list.getValue(index));
data.push(value);
}
Expand All @@ -66,7 +66,7 @@ function listToArray(list) {
function mapToObject(map) {
const length = map.getLength();
const data = {};
for (let index = 0; index < length; index++) {
for (const index of Array.from({ length }).keys()) {
const key = map.getKey(index).getValue();
const value = getJsonValueFromSassValue(map.getValue(index));
data[key] = value;
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
},
"dependencies": {
"lodash": "^4.17.20",
"node-sass": "^5.0.0",
"parse-color": "^1.0.0",
"parse-css-dimension": "^1.1.0",
"rgb-hex": "^3.0.0",
"shorten-css-hex": "^1.1.0"
},
"peerDependencies": {
"sass": ">=1"
},
"devDependencies": {
"changelog-verify": "^1.1.2",
"eslint": "^7.11.0",
Expand All @@ -66,7 +68,8 @@
"nyc": "^15.1.0",
"prettier": "^2.1.2",
"rollup": "^2.32.1",
"sass-true": "^5.0.0",
"sass": "^1.32.4",
"sass-true": "^6.0.1",
"version-changelog": "^3.1.1"
},
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion test/index.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@import 'true';
@use 'true' as *;

@include test-module('json-encode') {

Expand Down

0 comments on commit 923d69b

Please sign in to comment.