node-sass-json-functions 2.0.1 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +46 -0
- package/README.md +74 -40
- package/cjs/index.d.ts +34 -0
- package/cjs/index.d.ts.map +1 -0
- package/cjs/index.js +249 -0
- package/cjs/index.js.map +1 -0
- package/cjs/lib/json-to-sass.d.ts +10 -0
- package/cjs/lib/json-to-sass.d.ts.map +1 -0
- package/cjs/lib/sass-to-json.d.ts +26 -0
- package/cjs/lib/sass-to-json.d.ts.map +1 -0
- package/cjs/package.json +1 -0
- package/cjs/types/lib.d.ts +14 -0
- package/esm/index.d.ts +34 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +237 -0
- package/esm/index.js.map +1 -0
- package/esm/lib/json-to-sass.d.ts +10 -0
- package/esm/lib/json-to-sass.d.ts.map +1 -0
- package/esm/lib/sass-to-json.d.ts +26 -0
- package/esm/lib/sass-to-json.d.ts.map +1 -0
- package/esm/package.json +1 -0
- package/esm/types/lib.d.ts +14 -0
- package/package.json +68 -13
- package/.editorconfig +0 -16
- package/.eslintrc +0 -6
- package/.istanbul.yml +0 -9
- package/.npmignore +0 -4
- package/.travis.yml +0 -3
- package/index.js +0 -44
- package/lib/json-to-sass.js +0 -111
- package/lib/sass-to-json.js +0 -79
- package/test/.eslintrc +0 -5
- package/test/custom-options.js +0 -8
- package/test/custom-options.scss +0 -12
- package/test/index.js +0 -7
- package/test/index.scss +0 -106
package/lib/sass-to-json.js
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const sass = require('node-sass');
|
|
4
|
-
const round = require('lodash').round;
|
|
5
|
-
const rgbHex = require('rgb-hex');
|
|
6
|
-
const shortHexColor = require('shorten-css-hex');
|
|
7
|
-
const types = sass.types;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @param {sass.types.*} value
|
|
11
|
-
* @param {Object} opts
|
|
12
|
-
*
|
|
13
|
-
* @return {Mixed}
|
|
14
|
-
*/
|
|
15
|
-
function getJsonValueFromSassValue ( value, opts ) {
|
|
16
|
-
let rgbValue = [];
|
|
17
|
-
let resolvedValue, alphaValue;
|
|
18
|
-
if ( value instanceof types.List ) {
|
|
19
|
-
resolvedValue = listToArray(value, opts);
|
|
20
|
-
} else if ( value instanceof types.Map ) {
|
|
21
|
-
resolvedValue = mapToObject(value, opts);
|
|
22
|
-
} else if ( value instanceof types.Color ) {
|
|
23
|
-
rgbValue = [value.getR(), value.getG(), value.getB()];
|
|
24
|
-
alphaValue = value.getA();
|
|
25
|
-
if ( alphaValue === 1 ) {
|
|
26
|
-
resolvedValue = shortHexColor(`#${rgbHex.apply(null, rgbValue)}`);
|
|
27
|
-
} else {
|
|
28
|
-
resolvedValue = `rgba(${rgbValue.join(',')},${alphaValue})`;
|
|
29
|
-
}
|
|
30
|
-
} else if ( value instanceof types.Number ) {
|
|
31
|
-
if ( value.getUnit() !== '' ) {
|
|
32
|
-
resolvedValue = String(round(Number(value.getValue()), opts.precision) + value.getUnit());
|
|
33
|
-
} else {
|
|
34
|
-
resolvedValue = round(Number(value.getValue()), opts.precision);
|
|
35
|
-
}
|
|
36
|
-
} else {
|
|
37
|
-
try {
|
|
38
|
-
resolvedValue = value.getValue();
|
|
39
|
-
} catch ( e ) {
|
|
40
|
-
resolvedValue = null;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return resolvedValue;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* @param {sass.types.List} list
|
|
48
|
-
* @param {Object} opts
|
|
49
|
-
*
|
|
50
|
-
* @return {Array}
|
|
51
|
-
*/
|
|
52
|
-
function listToArray ( list, opts ) {
|
|
53
|
-
const length = list.getLength();
|
|
54
|
-
const data = [];
|
|
55
|
-
for ( let i = 0; i < length; i++ ) {
|
|
56
|
-
const value = getJsonValueFromSassValue(list.getValue(i), opts);
|
|
57
|
-
data.push(value);
|
|
58
|
-
}
|
|
59
|
-
return data;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* @param {sass.types.Map} map
|
|
64
|
-
* @param {Object} opts
|
|
65
|
-
*
|
|
66
|
-
* @return {Object}
|
|
67
|
-
*/
|
|
68
|
-
function mapToObject ( map, opts ) {
|
|
69
|
-
const length = map.getLength();
|
|
70
|
-
const data = {};
|
|
71
|
-
for ( let i = 0; i < length; i++ ) {
|
|
72
|
-
const key = map.getKey(i).getValue();
|
|
73
|
-
const value = getJsonValueFromSassValue(map.getValue(i), opts);
|
|
74
|
-
data[key] = value;
|
|
75
|
-
}
|
|
76
|
-
return data;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
module.exports = getJsonValueFromSassValue;
|
package/test/custom-options.js
DELETED
package/test/custom-options.scss
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
@import 'true';
|
|
2
|
-
|
|
3
|
-
@include test-module('Precision') {
|
|
4
|
-
|
|
5
|
-
@include test('should properly apply precision to numbers when using json-encode') {
|
|
6
|
-
@include assert-equal(json-encode(1.23456789), '\'1.23\'');
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
@include test('should ignore precision for numbers when using json-decode') {
|
|
10
|
-
@include assert-equal(json-decode("1.23456789"), 1.23456789);
|
|
11
|
-
}
|
|
12
|
-
}
|
package/test/index.js
DELETED
package/test/index.scss
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
@import 'true';
|
|
2
|
-
|
|
3
|
-
@include test-module('json-encode') {
|
|
4
|
-
|
|
5
|
-
@include test('should convert Sass list to JSON array') {
|
|
6
|
-
|
|
7
|
-
$list: 1, 2, "3", 4px, 42%, 1.23456789px, 1.23456789dppx, 1.23456789deg, (4,5,6), (foo: "bar baz");
|
|
8
|
-
|
|
9
|
-
$actual: json-encode($list);
|
|
10
|
-
$expected: '\'[1,2,"3","4px","42%","1.23457px","1.23457dppx","1.23457deg",[4,5,6],{"foo":"bar baz"}]\'';
|
|
11
|
-
|
|
12
|
-
@include assert-equal($actual, $expected);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
@include test('should convert Sass map to JSON object') {
|
|
16
|
-
|
|
17
|
-
$map: (
|
|
18
|
-
foo: 1,
|
|
19
|
-
bar: (2, 3),
|
|
20
|
-
baz: "3 3 3",
|
|
21
|
-
bad: (
|
|
22
|
-
foo: 11,
|
|
23
|
-
bar: 22,
|
|
24
|
-
baz: (
|
|
25
|
-
5, 4, 6, null, 1, 1.23456789px, 1.23456789dppx, 1.23456789deg
|
|
26
|
-
),
|
|
27
|
-
bag: "foo bar"
|
|
28
|
-
),
|
|
29
|
-
qux: rgba(255,255,255,0.5),
|
|
30
|
-
corgle: red
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
$actual: json-encode($map);
|
|
34
|
-
$expected: '\'{"foo":1,"bar":[2,3],"baz":"3 3 3","bad":{"foo":11,"bar":22,"baz":[5,4,6,null,1,"1.23457px","1.23457dppx","1.23457deg"],"bag":"foo bar"},"qux":"rgba(255,255,255,0.5)","corgle":"#f00"}\'';
|
|
35
|
-
|
|
36
|
-
@include assert-equal($actual, $expected);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
@include test-module('json-decode') {
|
|
41
|
-
|
|
42
|
-
@include test('should convert JSON array to Sass list') {
|
|
43
|
-
|
|
44
|
-
$array: '[1,2,"3","4px","42%","1.23456789px","1.23456789dppx","1.23456789deg",[4,5,6],{"foo":"bar baz"}]';
|
|
45
|
-
|
|
46
|
-
$actual: json-decode($array);
|
|
47
|
-
$expected: 1, 2, "3", 4px, 42%, 1.23456789px, 1.23456789dppx, 1.23456789deg, (4,5,6), (foo: "bar baz");
|
|
48
|
-
|
|
49
|
-
@include assert-equal($actual, $expected);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
@include test('should convert JSON object to Sass map') {
|
|
53
|
-
|
|
54
|
-
$object: '{"foo":1,"bar":[2,3],"baz":"3 3 3","bad":{"foo":11,"bar":22,"baz":[5,4,6,null,1,"1.23456789px","1.23456789dppx","1.23456789deg"],"bag":"foo bar"},"qux":"rgba(255,255,255,0.5)","corgle":"#f00"}';
|
|
55
|
-
|
|
56
|
-
$actual: json-decode($object);
|
|
57
|
-
$expected: (
|
|
58
|
-
foo: 1,
|
|
59
|
-
bar: (2, 3),
|
|
60
|
-
baz: "3 3 3",
|
|
61
|
-
bad: (
|
|
62
|
-
foo: 11,
|
|
63
|
-
bar: 22,
|
|
64
|
-
baz: (
|
|
65
|
-
5, 4, 6, null, 1, 1.23456789px, 1.23456789dppx, 1.23456789deg
|
|
66
|
-
),
|
|
67
|
-
bag: "foo bar"
|
|
68
|
-
),
|
|
69
|
-
qux: rgba(255,255,255,0.5),
|
|
70
|
-
corgle: #f00
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
@include assert-equal($actual, $expected);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
@include test('should convert JSON boolean values to Sass boolean values') {
|
|
77
|
-
@include assert-equal(json-decode('true'), true);
|
|
78
|
-
@include assert-equal(json-decode('false'), false);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
@include test('should convert JSON null value to Sass null value') {
|
|
82
|
-
@include assert-equal(json-decode('null'), null);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
@include test-module('Malformed JSON') {
|
|
87
|
-
|
|
88
|
-
@include test('should treat malformed JSON as Sass null value') {
|
|
89
|
-
@include assert-equal(json-decode('['), null);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
@include test-module('Quoting stringified values') {
|
|
94
|
-
|
|
95
|
-
$list: 1, 2, "3", (4,5,6), (foo: "bar baz");
|
|
96
|
-
$expected: '[1,2,"3",[4,5,6],{"foo":"bar baz"}]';
|
|
97
|
-
|
|
98
|
-
@include test('should render single quotes around stringified values if {quotes: true}') {
|
|
99
|
-
@include assert-equal('body {content:#{json-encode($list)};}', 'body {content:\'#{$expected}\';}');
|
|
100
|
-
@include assert-equal('body {content:#{json-encode($list, $quotes: true)};}', 'body {content:\'#{$expected}\';}');
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
@include test('should omit single quotes around stringified values if {quotes: false}') {
|
|
104
|
-
@include assert-equal('body {content:#{json-encode($list, $quotes: false)};}', 'body {content:#{$expected};}');
|
|
105
|
-
}
|
|
106
|
-
}
|