1 | import { mapper, iterate } from '@vect/vector-mapper';
|
2 | import { wind } from '@vect/object-init';
|
3 | import { select } from '@vect/columns-select';
|
4 | import { unwind } from '@vect/entries-unwind';
|
5 | import { transpose } from '@vect/matrix-transpose';
|
6 | import { zipper } from '@vect/vector-zipper';
|
7 | import { toKeyComparer } from '@analys/util-keyed-vectors';
|
8 | import { Columns } from '@vect/column-getter';
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const tabularToSamples = function () {
|
15 | const {
|
16 | head,
|
17 | rows
|
18 | } = this;
|
19 | return mapper(rows, row => wind(head, row));
|
20 | };
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | const selectTabular = function (labels) {
|
28 | var _lookupIndexes$call;
|
29 |
|
30 | let {
|
31 | rows
|
32 | } = this,
|
33 | indexes;
|
34 | [this.head, indexes] = (_lookupIndexes$call = lookupIndexes.call(this, labels), unwind(_lookupIndexes$call));
|
35 | this.rows = select(rows, indexes);
|
36 | return this;
|
37 | };
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | const lookupIndexes = function (labels) {
|
45 | return mapper.call(this, labels, lookupIndex);
|
46 | };
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | const lookupIndex = function (label) {
|
54 | const {
|
55 | head
|
56 | } = this;
|
57 | if (!Array.isArray(label)) return [label, head.indexOf(label)];
|
58 | const [current, projected] = label;
|
59 | return [projected, head.indexOf(current)];
|
60 | };
|
61 |
|
62 | const selectSamples = function (fieldIndexPairs) {
|
63 | const {
|
64 | rows
|
65 | } = this,
|
66 | depth = fieldIndexPairs === null || fieldIndexPairs === void 0 ? void 0 : fieldIndexPairs.length;
|
67 | return mapper(rows, row => {
|
68 | let o = {};
|
69 | iterate(fieldIndexPairs, ([field, index]) => o[field] = row[index], depth);
|
70 | return o;
|
71 | });
|
72 | };
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | const selectTabularToSamples = function (labels) {
|
80 | const fieldIndexes = lookupIndexes.call(this, labels);
|
81 | return selectSamples.call(this, fieldIndexes);
|
82 | };
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | const sortTabularByKeys$1 = function (comparer) {
|
91 | var _zipper$sort;
|
92 |
|
93 | let {
|
94 | head,
|
95 | rows
|
96 | } = this,
|
97 | columns = transpose(rows);
|
98 | [this.head, columns] = (_zipper$sort = zipper(head, columns, (key, row) => [key, row]).sort(toKeyComparer(comparer)), unwind(_zipper$sort));
|
99 | this.rows = transpose(columns);
|
100 | return this;
|
101 | };
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | const sortTabular = function (comparer, index) {
|
111 | var _zipper$sort;
|
112 |
|
113 | if (index < 0) return sortTabularByKeys.call(this, comparer);
|
114 | let {
|
115 | head,
|
116 | rows
|
117 | } = this,
|
118 | columns = transpose(rows);
|
119 |
|
120 |
|
121 | const Keyed = (_zipper$sort = zipper(head, columns, (key, column) => [column[index], key, column]).sort(toKeyComparer(comparer)), Columns(_zipper$sort));
|
122 | return this.head = Keyed(1), this.rows = transpose(Keyed(2)), this;
|
123 | };
|
124 |
|
125 | const voidTabular = () => ({
|
126 | head: [],
|
127 | rows: []
|
128 | });
|
129 |
|
130 | export { selectTabular, selectTabularToSamples, sortTabular, sortTabularByKeys$1 as sortTabularByKeys, tabularToSamples, voidTabular };
|