-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.js
78 lines (67 loc) · 2.2 KB
/
process.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
var rnProcess = require('react-native-dynamic-style-processor').process;
var isArray = Array.isArray || function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
exports.process = function processStyleName(styleName, cssStyles) {
cssStyles = rnProcess(cssStyles);
// Process styleName through the `classnames`-like function.
// This allows to specify styleName as an array or an object,
// not just the string.
styleName = cc(styleName);
var htmlClasses = (styleName || '').split(' ').filter(Boolean);
if (htmlClasses.length > 1) {
htmlClasses = addMultiClasses(htmlClasses, cssStyles);
}
var res = [];
for (var i = 0; i < htmlClasses.length; i++) {
var htmlClass = htmlClasses[i];
if (cssStyles[htmlClass]) res.push(cssStyles[htmlClass]);
}
return res;
};
function addMultiClasses(htmlClasses, cssStyles) {
var selectors = Object.keys(cssStyles);
var resByAmount = {};
var maxAmount = 0;
for (var i = 0; i < selectors.length; i++) {
var selector = selectors[i];
if (!/\./.test(selector)) continue;
var cssClasses = selector.split('.');
if (arrayContainedInArray(cssClasses, htmlClasses)) {
var amount = cssClasses.length;
if (amount > maxAmount) maxAmount = amount;
if (!resByAmount[amount]) resByAmount[amount] = [];
resByAmount[amount].push(selector);
}
}
for (var j = 0; j <= maxAmount; j++) {
if (!resByAmount[j]) continue;
htmlClasses = htmlClasses.concat(resByAmount[j]);
}
return htmlClasses;
};
function arrayContainedInArray(cssClasses, htmlClasses) {
for (var i = 0; i < cssClasses.length; i++) {
if (htmlClasses.indexOf(cssClasses[i]) === -1) return false;
}
return true;
};
// classcat 4.0.2
// https://github.com/jorgebucaran/classcat
function cc(names) {
var i;
var len;
var tmp = typeof names;
var out = "";
if (tmp === "string" || tmp === "number") return names || "";
if (isArray(names) && names.length > 0) {
for (i = 0, len = names.length; i < len; i++) {
if ((tmp = cc(names[i])) !== "") out += (out && " ") + tmp;
}
} else {
for (i in names) {
if (names.hasOwnProperty(i) && names[i]) out += (out && " ") + i;
}
}
return out;
}