1 |
|
2 |
|
3 |
|
4 |
|
5 | var slice = Array.prototype.slice;
|
6 |
|
7 | module.exports = function(selectors, props) {
|
8 | var children = slice.call(arguments, 2);
|
9 | if (typeof selectors === 'function') {
|
10 | props = props || {};
|
11 | props.children = children;
|
12 | return selectors(props);
|
13 | }
|
14 |
|
15 | return '\n' + formatSelectors(selectors) + ' {\n' +
|
16 | formatProps(props) +
|
17 | formatChildren(children) +
|
18 | '}\n';
|
19 | };
|
20 |
|
21 | function formatSelectors(selectors) {
|
22 | return Array.isArray(selectors) ?
|
23 | selectors.join(',\n') :
|
24 | selectors;
|
25 | }
|
26 |
|
27 | function formatProps(props) {
|
28 | if (!props) return '';
|
29 | var parts = [];
|
30 | for (var k in props) {
|
31 | if (k === 'key') continue;
|
32 | parts.push(' ' + k + ': ' + props[k] + ';');
|
33 | }
|
34 | return parts.join('\n') + '\n';
|
35 | }
|
36 |
|
37 | function formatChildren(children) {
|
38 | if (!children || children.length === 0) return '';
|
39 | return children.map(function(child) {
|
40 | if (Array.isArray(child)) return formatChildren(child);
|
41 | if (!child) return '';
|
42 | return child.split('\n').map(function(line) {
|
43 | return ' ' + line;
|
44 | }).join('\n');
|
45 | }).join('\n') + '\n';
|
46 | }
|