Skip to content

Commit b8a4958

Browse files
committed
feat: add toCss()
1 parent c9580d5 commit b8a4958

7 files changed

+131
-33
lines changed

build/gulpfile.js

-29
This file was deleted.

build/webpack.config.cjs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var pkg = require('../package.json');
44
var join = require('path').join;
55

66
module.exports = {
7-
entry: join(__dirname, '..', 'src', 'index.ts'),
7+
entry: join(__dirname, '..', 'lib', 'index.js'),
88

99
output: {
1010
filename: pkg.name + '.min.js',

lib/__tests__/setup.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
var configure = require('enzyme').configure;
4+
var Adapter = require('enzyme-adapter-react-16');
5+
6+
process.env.NODE_ENV = 'production';
7+
8+
configure({
9+
adapter: new Adapter()
10+
});
11+
12+
global.requestAnimationFrame = window.requestAnimationFrame = function(callback) { setTimeout(callback, 17); };

lib/__tests__/toCss.test.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* eslint-disable */
2+
'use strict';
3+
4+
var toCss = require('../ast').toCss;
5+
6+
describe('toCss', function() {
7+
it('basic example', function() {
8+
var css = toCss({
9+
'.foo': {
10+
color: 'red'
11+
}
12+
});
13+
14+
expect(css).toBe('.foo{color:red;}');
15+
});
16+
17+
it('@-rule', function() {
18+
var css = toCss({
19+
'.foo': {
20+
color: 'red'
21+
},
22+
'@media screen': {
23+
'.foo': {
24+
color: 'red'
25+
}
26+
}
27+
});
28+
29+
expect(css).toBe('.foo{color:red;}@media screen{.foo{color:red;}}');
30+
});
31+
});

lib/ast.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
var interpolateSelectors = require('./interpolateSelectors');
4+
5+
var KEBAB_REGEX = /[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g;
6+
var KEBAB_MATCHER = function(match) { return '-' + match.toLowerCase(); };
7+
8+
function kebabCase(str) {
9+
return str.replace(KEBAB_REGEX, KEBAB_MATCHER);
10+
}
11+
12+
exports.toCss = function toCss (pojso) {
13+
var str = '';
14+
15+
for (var selector in pojso) {
16+
var values = pojso[selector];
17+
18+
// Atrule: @media, @keyframe, ...
19+
if (selector[0] === '@') {
20+
str += selector + '{' + toCss(values) + '}';
21+
} else {
22+
var selectors = selector.split(',');
23+
var declarations = '';
24+
25+
for (var prop in values) {
26+
var value = values[prop];
27+
28+
switch (typeof value) {
29+
case 'string':
30+
case 'number':
31+
prop = kebabCase(prop);
32+
declarations += prop + ':' + value + ';';
33+
break;
34+
case 'object': {
35+
var selectorsInterpolated = interpolateSelectors(selectors, prop);
36+
var obj = {};
37+
obj[selectorsInterpolated] = value;
38+
str += toCss(obj);
39+
break;
40+
}
41+
}
42+
}
43+
str += selectors + '{' + declarations + '}';
44+
}
45+
}
46+
47+
return str;
48+
};

lib/interpolateSelectors.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
module.exports = function(parents, selector) {
4+
var result = [];
5+
var selectors = selector.split(',');
6+
var len1 = parents.length;
7+
var len2 = selectors.length;
8+
var i, j, part1, part2, sel, pos, parent, replacedSelector;
9+
10+
for (i = 0; i < len2; i++) {
11+
sel = selectors[i];
12+
pos = sel.indexOf('&');
13+
14+
if (pos > -1) {
15+
part1 = sel.substr(0, pos);
16+
part2 = sel.substr(pos + 1);
17+
18+
for (j = 0; j < len1; j++) {
19+
parent = parents[j];
20+
replacedSelector = part1 + parent + part2;
21+
22+
result.push(replacedSelector);
23+
}
24+
} else {
25+
for (j = 0; j < len1; j++) {
26+
parent = parents[j];
27+
28+
if (parent) {
29+
result.push(parent + ' ' + sel);
30+
} else {
31+
result.push(sel);
32+
}
33+
}
34+
}
35+
}
36+
37+
return result.join(',');
38+
};

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
"start": "npm run storybook",
1414
"clean": "rimraf dist && npm run test:visual:clean",
1515
"build": "npm run clean && npm run build:cjs && npm run build:umd",
16-
"build:modules": "gulp build-modules --gulpfile build/gulpfile.js",
17-
"build:lib": "gulp build-ts --gulpfile build/gulpfile.js",
1816
"build:cjs": "webpack -p --config build/webpack.config.cjs.js",
1917
"build:umd": "webpack -p --config build/webpack.config.umd.js",
2018
"test": "npm run eslint && npm run test:server && jest",
@@ -91,7 +89,7 @@
9189
"transformIgnorePatterns": [],
9290
"testRegex": ".*/__tests__/.*\\.(test|spec)\\.(jsx?)$",
9391
"setupFiles": [
94-
"./src/__tests__/setup.js"
92+
"./lib/__tests__/setup.js"
9593
],
9694
"moduleFileExtensions": [
9795
"js",

0 commit comments

Comments
 (0)