-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (94 loc) · 2.14 KB
/
index.js
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
var resolveProp = require('postcss-resolve-prop');
var parseCssFont = require('parse-css-font');
var unquote = require('unquote');
var t = require('tcomb-postcss');
var helpers = {};
[
'style',
'variant',
'weight',
'stretch',
'size'
].forEach(function(prop) {
helpers[prop] = t.func(t.Rule, t.Any).of(
function(rule) {
return resolveProp(rule, 'font-' + prop, {
parsers: {
font: function(value) {
return parseCssFont(value)[prop];
}
}
});
}
);
});
helpers.family = t.func(t.Rule, t.Any).of(
function(rule) {
return resolveProp(rule, 'font-family', {
parsers: {
font: function(value) {
return parseCssFont(value).family;
},
'font-family': parseFontFamily
}
});
}
);
function parseFontFamily(value) {
return value.split(/\s*,\s*/).map(unquote);
}
helpers.lineHeight = t.func(t.Rule, t.Any).of(
function(rule) {
return resolveProp(rule, 'line-height', {
parsers: {
font: function(value) {
return parseCssFont(value).lineHeight;
},
'line-height': parseLineHeight
}
});
}
);
function parseLineHeight(value) {
var parsed = parseFloat(value);
if (parsed.toString() === value) {
return parsed;
}
return value;
}
module.exports = t.func(t.Rule, t.Object).of(
function(rule) {
return resolveProp(rule, 'font', {
isObjectMode: true,
parsers: {
font: function(value) {
return parseCssFont(value);
},
'font-style': function(value) {
return { style: value };
},
'font-variant': function(value) {
return { variant: value };
},
'font-weight': function(value) {
return { weight: value };
},
'font-stretch': function(value) {
return { stretch: value };
},
'font-size': function(value) {
return { size: value };
},
'font-family': function(value) {
return { family: parseFontFamily(value) };
},
'line-height': function(value) {
return { lineHeight: parseLineHeight(value) };
}
}
});
}
);
Object.keys(helpers).forEach(function(key) {
module.exports[key] = helpers[key];
});