rules-fabric.js
2.47 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
var template = require('lodash.template');
var decls = require('./decls.json');
/*
Rules legend:
- combined - if rule is combined it will be rendered with template
- combined and basic rules are present in basic reset
- combined, basic and inherited rules are present in full reset
*/
function _getRulesMap(inputDecls) {
return inputDecls
.filter(function (decl) {
return !decl.combined;
})
.reduce(function (map, decl) {
map[decl.prop.replace(/\-/g, '')] = decl.initial;
return map;
}, {});
}
function _compileDecls(inputDecls) {
var templateVars = _getRulesMap(inputDecls);
return inputDecls.map(function (decl) {
if (decl.combined && decl.initial) {
var t = template(decl.initial.replace(/\-/g, ''));
decl.initial = t(templateVars);
}
return decl;
});
}
function _getRequirements(inputDecls) {
return inputDecls.reduce(function (map, decl) {
if (!decl.contains) return map;
return decl.contains.reduce(function (mapInner, dependensy) {
mapInner[dependensy] = decl;
return mapInner;
}, map);
}, {});
}
function _expandContainments(inputDecls) {
var requiredMap = _getRequirements(inputDecls);
return inputDecls
.filter(function (decl) {
return !decl.contains;
}).map(function (decl) {
var dependensy = requiredMap[decl.prop];
if (dependensy) {
decl.requiredBy = dependensy.prop;
decl.basic = decl.basic || dependensy.basic;
decl.inherited = decl.inherited || dependensy.inherited;
}
return decl;
});
}
var compiledDecls = _expandContainments(_compileDecls(decls));
function _clearDecls(rules, value) {
return rules.map(function (rule) {
return {
prop: rule.prop,
value: value.replace(/initial/g, rule.initial)
};
});
}
function _allDecls(onlyInherited) {
return compiledDecls.filter(function (decl) {
var allowed = decl.combined || decl.basic;
if (onlyInherited) return allowed && decl.inherited;
return allowed;
});
}
function _concreteDecl(declName) {
return compiledDecls.filter(function (decl) {
return declName === decl.prop || declName === decl.requiredBy;
});
}
function makeFallbackFunction(onlyInherited) {
return function (declName, declValue) {
var result;
if (declName === 'all') {
result = _allDecls(onlyInherited);
} else {
result = _concreteDecl(declName);
}
return _clearDecls(result, declValue);
};
}
module.exports = makeFallbackFunction;