forked from ArtemySinitsa/aws-textract-json-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetTableDataFunc.js
More file actions
84 lines (76 loc) · 2.6 KB
/
getTableDataFunc.js
File metadata and controls
84 lines (76 loc) · 2.6 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
const utilFuncs = require('./utilityFuncs');
module.exports = data => {
const utils = utilFuncs(data)();
return options => {
try {
// GET ALL TABLE BLOCKS
const tables = data.Blocks.filter(
table =>
table.BlockType === 'TABLE' &&
table.Confidence >
(options && options.minConfidence ? options.minConfidence : 0)
);
// GET ALL CELL BLOCKS
const getCells = id => {
return data.Blocks.filter(cells => cells.BlockType === 'CELL').filter(
cell => cell.Id === id
);
};
const allTables = [];
tables.forEach(table => {
const [tableRelationshipIds] = table.Relationships.map(rel => rel.Ids);
const cellArray = [];
let tableCells = null;
tableRelationshipIds.forEach(tableRelationshipId => {
tableCells = getCells(tableRelationshipId);
tableCells.forEach(tableCell => {
const cellIds = tableCell.Relationships;
// For each id in the child relationships go get the words
if (cellIds) {
cellIds.forEach(child => {
const words = utils.getWords(child);
// Using reduce to turn the list of words into one line
const [[selects]] = utils.getSelects(child);
if (selects) {
cellArray.push(selects.SelectionStatus);
} else {
const completedWord = utils.buildWords(words);
cellArray.push(completedWord);
}
});
} else {
cellArray.push('NA');
}
});
});
const NumberOfColumnsInTable = Math.max(
...tableCells.map(tableCell => tableCell.ColumnIndex)
);
const tableArray = [];
[cellArray].forEach(cell => {
while (cell.length) {
tableArray.push(cell.splice(0, NumberOfColumnsInTable));
}
});
allTables.push(tableArray);
});
const tableRowsAsobjects = [];
allTables.forEach(table => {
const headers = table.shift(); // takes first element of array, which is also an array
const objects = table.map(tableRows => {
return headers.reduce(
(accumulator, currentHeaderValue, initialValue) => {
accumulator[currentHeaderValue] = tableRows[initialValue];
return accumulator;
},
{}
);
});
tableRowsAsobjects.push(objects);
});
return tableRowsAsobjects;
} catch (error) {
return error;
}
};
};