forked from gbishop/OS-DPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.js
More file actions
60 lines (58 loc) · 1.62 KB
/
data.js
File metadata and controls
60 lines (58 loc) · 1.62 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
export class Data {
/** @param {Rows} rows */
constructor(rows) {
this.allrows = (Array.isArray(rows) && rows) || [];
this.allFields = rows.reduce(
(previous, current) =>
Array.from(
new Set([
...previous,
...Object.keys(current).map((field) => "#" + field),
])
),
[]
);
}
/**
* Extract rows with the given tags
*
* @param {string[]} tags - Tags that must be in each row
* @param {string} match - how to match
* @return {Rows} Rows with the given tags
*/
getTaggedRows(tags, match) {
let result = [];
if (match == "contains") {
// all the tags must be in the row somewhere
result = this.allrows.filter((row) => {
return tags.every((tag) => row.tags.indexOf(tag) >= 0);
});
} else if (match == "sequence") {
// all the tags must match those coming from the row in order
// and any remaining tags in the row must be empty
result = this.allrows.filter((row) => {
return (
tags.every(
(tag, i) => row.tags[i] == tag || row.tags[i] === "*" || tag === "*"
) &&
row.tags
.slice(tags.length)
.every((tag) => tag.length === 0 || tag === "*")
);
});
}
// console.log("gtr result", result);
return result;
}
/**
* Test if tagged rows exist
*
* @param {string[]} tags - Tags that must be in each row
* @return {Boolean} true if tag combination occurs
*/
hasTaggedRows(tags) {
return this.allrows.some((row) =>
tags.every((tag) => row.tags.indexOf(tag) >= 0)
);
}
}