Skip to content

Commit 05f2851

Browse files
Post processing functions in submodules (#16)
1 parent 1d8bba9 commit 05f2851

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

post-process-functions.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { jsonCopy } from "./general";
2+
3+
export function postProcessRecord(record: any, attributes: any[]) {
4+
const prepareRecord = jsonCopy(record);
5+
if (!prepareRecord.hasOwnProperty('data')) {
6+
if (prepareRecord.hasOwnProperty('fullRecordData')) {
7+
prepareRecord.data = prepareRecord.fullRecordData;
8+
}
9+
else if (prepareRecord.hasOwnProperty('recordData')) {
10+
prepareRecord.data = prepareRecord.recordData;
11+
} else {
12+
throw new Error("Cant find record data in record object");
13+
}
14+
}
15+
attributes.forEach((attribute, index) => {
16+
if (typeof prepareRecord.data[attribute.name] === 'boolean') {
17+
prepareRecord.data[attribute.name] = prepareRecord.data[attribute.name].toString();
18+
}
19+
});
20+
return prepareRecord;
21+
}
22+
23+
export function postProcessAttributes(attributes: any[]) {
24+
const prepareAttributes = jsonCopy(attributes);
25+
if (attributes && attributes.length > 0) {
26+
if (!attributes[0].hasOwnProperty('key')) {
27+
prepareAttributes.forEach((attribute, index) => {
28+
if (attribute.id !== null) {
29+
attribute.key = attribute.id;
30+
} else {
31+
throw new Error("Cant find attribute id in attribute object");
32+
}
33+
});
34+
}
35+
}
36+
return prepareAttributes;
37+
}
38+
39+
export function postProcessUpdateAndSortRecords(prev: any[], newRecords: any[]) {
40+
const merged = [...prev, ...newRecords];
41+
const uniqueRecords = Array.from(
42+
new Map(merged.map((item) => [item.data?.running_id, item])).values()
43+
);
44+
uniqueRecords.sort(
45+
(a, b) => (a.data?.running_id || 0) - (b.data?.running_id || 0)
46+
);
47+
return uniqueRecords;
48+
}

0 commit comments

Comments
 (0)