format.js
4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var override = require('../utils/override');
var Breaks = {
AfterAtRule: 'afterAtRule',
AfterBlockBegins: 'afterBlockBegins',
AfterBlockEnds: 'afterBlockEnds',
AfterComment: 'afterComment',
AfterProperty: 'afterProperty',
AfterRuleBegins: 'afterRuleBegins',
AfterRuleEnds: 'afterRuleEnds',
BeforeBlockEnds: 'beforeBlockEnds',
BetweenSelectors: 'betweenSelectors'
};
var IndentWith = {
Space: ' ',
Tab: '\t'
};
var Spaces = {
AroundSelectorRelation: 'aroundSelectorRelation',
BeforeBlockBegins: 'beforeBlockBegins',
BeforeValue: 'beforeValue'
};
var DEFAULTS = {
breaks: breaks(false),
indentBy: 0,
indentWith: IndentWith.Space,
spaces: spaces(false),
wrapAt: false
};
var BEAUTIFY_ALIAS = 'beautify';
var KEEP_BREAKS_ALIAS = 'keep-breaks';
var OPTION_SEPARATOR = ';';
var OPTION_NAME_VALUE_SEPARATOR = ':';
var HASH_VALUES_OPTION_SEPARATOR = ',';
var HASH_VALUES_NAME_VALUE_SEPARATOR = '=';
var FALSE_KEYWORD_1 = 'false';
var FALSE_KEYWORD_2 = 'off';
var TRUE_KEYWORD_1 = 'true';
var TRUE_KEYWORD_2 = 'on';
function breaks(value) {
var breakOptions = {};
breakOptions[Breaks.AfterAtRule] = value;
breakOptions[Breaks.AfterBlockBegins] = value;
breakOptions[Breaks.AfterBlockEnds] = value;
breakOptions[Breaks.AfterComment] = value;
breakOptions[Breaks.AfterProperty] = value;
breakOptions[Breaks.AfterRuleBegins] = value;
breakOptions[Breaks.AfterRuleEnds] = value;
breakOptions[Breaks.BeforeBlockEnds] = value;
breakOptions[Breaks.BetweenSelectors] = value;
return breakOptions;
}
function spaces(value) {
var spaceOptions = {};
spaceOptions[Spaces.AroundSelectorRelation] = value;
spaceOptions[Spaces.BeforeBlockBegins] = value;
spaceOptions[Spaces.BeforeValue] = value;
return spaceOptions;
}
function formatFrom(source) {
if (source === undefined || source === false) {
return false;
}
if (typeof source == 'object' && 'indentBy' in source) {
source = override(source, { indentBy: parseInt(source.indentBy) });
}
if (typeof source == 'object' && 'indentWith' in source) {
source = override(source, { indentWith: mapIndentWith(source.indentWith) });
}
if (typeof source == 'object') {
return override(DEFAULTS, source);
}
if (typeof source == 'object') {
return override(DEFAULTS, source);
}
if (typeof source == 'string' && source == BEAUTIFY_ALIAS) {
return override(DEFAULTS, {
breaks: breaks(true),
indentBy: 2,
spaces: spaces(true)
});
}
if (typeof source == 'string' && source == KEEP_BREAKS_ALIAS) {
return override(DEFAULTS, {
breaks: {
afterAtRule: true,
afterBlockBegins: true,
afterBlockEnds: true,
afterComment: true,
afterRuleEnds: true,
beforeBlockEnds: true
}
});
}
if (typeof source == 'string') {
return override(DEFAULTS, toHash(source));
}
return DEFAULTS;
}
function toHash(string) {
return string
.split(OPTION_SEPARATOR)
.reduce(function (accumulator, directive) {
var parts = directive.split(OPTION_NAME_VALUE_SEPARATOR);
var name = parts[0];
var value = parts[1];
if (name == 'breaks' || name == 'spaces') {
accumulator[name] = hashValuesToHash(value);
} else if (name == 'indentBy' || name == 'wrapAt') {
accumulator[name] = parseInt(value);
} else if (name == 'indentWith') {
accumulator[name] = mapIndentWith(value);
}
return accumulator;
}, {});
}
function hashValuesToHash(string) {
return string
.split(HASH_VALUES_OPTION_SEPARATOR)
.reduce(function (accumulator, directive) {
var parts = directive.split(HASH_VALUES_NAME_VALUE_SEPARATOR);
var name = parts[0];
var value = parts[1];
accumulator[name] = normalizeValue(value);
return accumulator;
}, {});
}
function normalizeValue(value) {
switch (value) {
case FALSE_KEYWORD_1:
case FALSE_KEYWORD_2:
return false;
case TRUE_KEYWORD_1:
case TRUE_KEYWORD_2:
return true;
default:
return value;
}
}
function mapIndentWith(value) {
switch (value) {
case 'space':
return IndentWith.Space;
case 'tab':
return IndentWith.Tab;
default:
return value;
}
}
module.exports = {
Breaks: Breaks,
Spaces: Spaces,
formatFrom: formatFrom
};