Repository navigation
Expand file tree
/
Copy pathcolumn-map.mjs
More file actions
68 lines (61 loc) · 2.33 KB
/
Copy pathcolumn-map.mjs
File metadata and controls
68 lines (61 loc) · 2.33 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
export function createColumnProjector(headers, mapping) {
if (!Array.isArray(headers) || headers.length === 0) {
throw new TypeError('headers must be a nonempty dense array of strings');
}
for (let index = 0; index < headers.length; index += 1) {
if (!Object.hasOwn(headers, index) || typeof headers[index] !== 'string') {
throw new TypeError('headers must be a nonempty dense array of strings');
}
}
const prototype = mapping === null || mapping === undefined
? undefined
: Object.getPrototypeOf(mapping);
if (typeof mapping !== 'object'
|| mapping === null
|| Array.isArray(mapping)
|| (prototype !== Object.prototype && prototype !== null)) {
throw new TypeError('mapping must be a plain object');
}
const entries = Object.entries(mapping);
if (entries.length === 0) {
throw new TypeError('mapping must contain at least one selection');
}
const destinationNames = new Set();
const selections = entries.map(([sourceName, outputName]) => {
if (typeof outputName !== 'string' || outputName.length === 0) {
throw new TypeError(`Destination name for "${sourceName}" must be a nonempty string`);
}
if (destinationNames.has(outputName)) {
throw new TypeError(`Destination name "${outputName}" must be unique`);
}
destinationNames.add(outputName);
const matchingIndices = [];
for (let index = 0; index < headers.length; index += 1) {
if (headers[index] === sourceName) matchingIndices.push(index);
}
if (matchingIndices.length === 0) {
throw new Error(`Selected header "${sourceName}" was not found`);
}
if (matchingIndices.length > 1) {
throw new Error(`Selected header "${sourceName}" is ambiguous (${matchingIndices.length} matches)`);
}
return [outputName, matchingIndices[0]];
});
const width = headers.length;
return (row) => {
if (!Array.isArray(row)) {
throw new TypeError('row must be an array');
}
if (row.length !== width) {
throw new Error(`Row width must be ${width}; received ${row.length}`);
}
for (let index = 0; index < row.length; index += 1) {
if (!Object.hasOwn(row, index)) {
throw new TypeError('row must be a dense array');
}
}
return Object.fromEntries(
selections.map(([outputName, index]) => [outputName, row[index]]),
);
};
}