Skip to content

Commit 6dca3d2

Browse files
committed
refactoring
1 parent dfd5f26 commit 6dca3d2

2 files changed

Lines changed: 40 additions & 43 deletions

File tree

index.js

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,70 @@
11
'use strict';
22
(function (module) {
3-
var _ = require('lodash');
4-
var fs = require('graceful-fs');
3+
const fs = require('graceful-fs');
54

6-
var exporter = module.exports = function (path, name) {
7-
var out = {};
8-
out[exporter.interface(name)] = exporter.function(path);
9-
return out;
5+
module.exports = function (path, name) {
6+
const exports = {};
7+
exports[declaration(name)] = implementation(path);
8+
return exports;
109
};
1110

1211
function toHex(c) {
13-
var hex = Math.round(c).toString(16).toUpperCase();
14-
return hex.length == 1 ? "0" + hex : hex;
15-
};
12+
const hex = Math.round(c).toString(16).toUpperCase();
13+
return hex.length === 1 ? "0" + hex : hex;
14+
}
1615

17-
exporter.get_value = function get_value(a, opt) {
18-
var value, i;
19-
switch (a.constructor.name) {
16+
function get_value(value, options) {
17+
let output;
18+
switch (value.constructor.name) {
2019
case 'SassList':
21-
value = [];
22-
for (i = 0; i < a.getLength(); i++) {
23-
value.push(get_value(a.getValue(i), opt));
20+
output = [];
21+
for (let i = 0; i < value.getLength(); i++) {
22+
output.push(get_value(value.getValue(i), options));
2423
}
2524
break;
2625
case 'SassMap':
27-
value = {};
28-
for (i = 0; i < a.getLength(); i++) {
29-
value[a.getKey(i).getValue()] = get_value(a.getValue(i), opt);
26+
output = {};
27+
for (let i = 0; i < value.getLength(); i++) {
28+
output[value.getKey(i).getValue()] = get_value(value.getValue(i), options);
3029
}
3130
break;
3231
case 'SassColor':
33-
if (1 === a.getA()) {
34-
if (opt.hex_color) {
35-
value = '#' + toHex(a.getR()) + toHex(a.getG()) + toHex(a.getB());
32+
if (1 === value.getA()) {
33+
if (options.hex_color) {
34+
output = '#' + toHex(value.getR()) + toHex(value.getG()) + toHex(value.getB());
3635
}
3736
else {
38-
value = 'rgb(' + Math.round(a.getR()) + ', ' + Math.round(a.getG()) + ', ' + Math.round(a.getB()) + ')';
37+
output = 'rgb(' + Math.round(value.getR()) + ', ' + Math.round(value.getG()) + ', ' + Math.round(value.getB()) + ')';
3938
}
4039
}
4140
else {
42-
value = 'rgba(' + Math.round(a.getR()) + ', ' + Math.round(a.getG()) + ', ' + Math.round(a.getB()) + ', ' + a.getA() + ')';
41+
output = 'rgba(' + Math.round(value.getR()) + ', ' + Math.round(value.getG()) + ', ' + Math.round(value.getB()) + ', ' + value.getA() + ')';
4342
}
4443
break;
4544
case 'SassNumber':
46-
value = a.getValue();
47-
if (a.getUnit()) {
48-
value += a.getUnit();
45+
output = value.getValue();
46+
if (value.getUnit()) {
47+
output += value.getUnit();
4948
}
5049
break;
5150
default:
52-
value = a.getValue();
51+
output = value.getValue();
5352
}
54-
return value;
55-
};
53+
return output;
54+
}
5655

57-
exporter.function = function (path) {
58-
return function (file, value, options) {
59-
var opt = _.defaults(exporter.get_value(options), {
56+
function implementation(path) {
57+
return (file, value, options) => {
58+
const opt = Object.assign({
6059
prefix: '',
6160
suffix: '',
6261
extend: false,
6362
hex_color: false
64-
});
65-
var output = exporter.get_value(value, opt);
63+
}, get_value(options));
64+
let output = get_value(value, opt);
6665
if (opt.extend && 'SassMap' === value.constructor.name) {
6766
try {
68-
_.defaults(output, JSON.parse(fs.readFileSync(path + '/' + file.getValue())));
67+
output = Object.assign({}, JSON.parse(fs.readFileSync(path + '/' + file.getValue())), output);
6968
}
7069
catch (e) {
7170
console.log(e);
@@ -74,10 +73,9 @@
7473
fs.writeFileSync(path + '/' + file.getValue(), opt.prefix + JSON.stringify(output, null, ' ') + opt.suffix);
7574
return value;
7675
}
77-
};
76+
}
7877

79-
exporter.interface = function (name) {
80-
name = name || 'export';
81-
return name + '($file, $value, $options:())';
82-
};
78+
function declaration(name) {
79+
return (name || 'export') + '($file, $value, $options:())';
80+
}
8381
})(module);

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-sass-export",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "lightweight extension that allows you to export an encoded Sass variables (map, list, etc) into an external JSON file",
55
"main": "index.js",
66
"scripts": {
@@ -14,8 +14,7 @@
1414
"author": "punkundead",
1515
"license": "BSD-2-Clause",
1616
"dependencies": {
17-
"graceful-fs": "^4.1",
18-
"lodash": "^4.17"
17+
"graceful-fs": "^4.1"
1918
},
2019
"devDependencies": {},
2120
"repository": {

0 commit comments

Comments
 (0)