-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.js
62 lines (44 loc) · 1.51 KB
/
parser.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
'use strict';
module.exports = parser;
function parser(str, callback) {
const layoutData = require('./data/data.json');
const css = require('css');
const parsed = css.parse(str);
if (parsed.stylesheet) {
identifyMatches(layoutData, parsed.stylesheet, callback);
}
}
function identifyMatches(data, stylesheet, callback) {
const matches = [];
const keyData = Object.keys(data.data);
const dataLength = keyData.length;
const stylesheetData = stylesheet.rules;
const stylesheetLength = stylesheetData.length;
const triggerProps = [];
const declarations = [];
let declarationsLength = 0;
/* Gather all the props from the trigger update data */
for (var i = 0; i < dataLength; i++) {
const triggerProperty = keyData[i];
triggerProps.push(triggerProperty);
}
/* Gather and filter the stylesheet data by rule only */
for (var ruleIndex = 0; ruleIndex < stylesheetLength; ruleIndex++) {
const ruleDeclaration = stylesheetData[ruleIndex].declarations;
if (stylesheetData[ruleIndex].type == 'rule') {
declarations.push(ruleDeclaration);
declarationsLength++;
}
}
/* Iterate through the gathered props and match with trigger data */
for (var decIndex = 0; decIndex < declarationsLength; decIndex++) {
const declarationGroup = declarations[decIndex];
declarationGroup.forEach(function(group) {
const property = group.property;
if (triggerProps.indexOf(property) > -1) {
matches.push(group);
}
});
}
callback(matches);
}