|
1 | 1 | 'use strict'; |
2 | 2 | (function (module) { |
3 | | - var _ = require('lodash'); |
4 | | - var fs = require('graceful-fs'); |
| 3 | + const fs = require('graceful-fs'); |
5 | 4 |
|
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; |
10 | 9 | }; |
11 | 10 |
|
12 | 11 | 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 | + } |
16 | 15 |
|
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) { |
20 | 19 | 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)); |
24 | 23 | } |
25 | 24 | break; |
26 | 25 | 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); |
30 | 29 | } |
31 | 30 | break; |
32 | 31 | 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()); |
36 | 35 | } |
37 | 36 | 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()) + ')'; |
39 | 38 | } |
40 | 39 | } |
41 | 40 | 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() + ')'; |
43 | 42 | } |
44 | 43 | break; |
45 | 44 | 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(); |
49 | 48 | } |
50 | 49 | break; |
51 | 50 | default: |
52 | | - value = a.getValue(); |
| 51 | + output = value.getValue(); |
53 | 52 | } |
54 | | - return value; |
55 | | - }; |
| 53 | + return output; |
| 54 | + } |
56 | 55 |
|
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({ |
60 | 59 | prefix: '', |
61 | 60 | suffix: '', |
62 | 61 | extend: false, |
63 | 62 | hex_color: false |
64 | | - }); |
65 | | - var output = exporter.get_value(value, opt); |
| 63 | + }, get_value(options)); |
| 64 | + let output = get_value(value, opt); |
66 | 65 | if (opt.extend && 'SassMap' === value.constructor.name) { |
67 | 66 | try { |
68 | | - _.defaults(output, JSON.parse(fs.readFileSync(path + '/' + file.getValue()))); |
| 67 | + output = Object.assign({}, JSON.parse(fs.readFileSync(path + '/' + file.getValue())), output); |
69 | 68 | } |
70 | 69 | catch (e) { |
71 | 70 | console.log(e); |
|
74 | 73 | fs.writeFileSync(path + '/' + file.getValue(), opt.prefix + JSON.stringify(output, null, ' ') + opt.suffix); |
75 | 74 | return value; |
76 | 75 | } |
77 | | - }; |
| 76 | + } |
78 | 77 |
|
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 | + } |
83 | 81 | })(module); |
0 commit comments