UNPKG

3.35 kBJavaScriptView Raw
1import { mapper, iterate } from '@vect/vector-mapper';
2import { wind } from '@vect/object-init';
3import { select } from '@vect/columns-select';
4import { unwind } from '@vect/entries-unwind';
5import { transpose } from '@vect/matrix-transpose';
6import { zipper } from '@vect/vector-zipper';
7import { toKeyComparer } from '@analys/util-keyed-vectors';
8import { Columns } from '@vect/column-getter';
9
10/**
11 * @returns {Object[]} - 'this' remains unchanged
12 */
13
14const tabularToSamples = function () {
15 const {
16 head,
17 rows
18 } = this;
19 return mapper(rows, row => wind(head, row));
20};
21
22/**
23 * @param {(str|[*,*])[]} labels
24 * @return {TableObject} - mutated 'this' {head, rows}
25 */
26
27const 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 * @param {(str|[*,*])[]} labels
41 * @returns {[str,number][]}
42 */
43
44const lookupIndexes = function (labels) {
45 return mapper.call(this, labels, lookupIndex);
46};
47/**
48 *
49 * @param {str|[*,*]} [label]
50 * @returns {[str,number]}
51 */
52
53const 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
62const 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 * @param labels
76 * @returns {Object[]} - 'this' remains unchanged
77 */
78
79const selectTabularToSamples = function (labels) {
80 const fieldIndexes = lookupIndexes.call(this, labels);
81 return selectSamples.call(this, fieldIndexes);
82};
83
84/**
85 *
86 * @param comparer
87 * @return {TableObject} - mutated 'this' {head, rows}
88 */
89
90const 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 * If y >= 0 then sort by vector[y] for each vectors, else (e.g. y===undefined) sort by keys.
105 * @param {function(*,*):number} comparer
106 * @param {number} [index]
107 * @returns {TableObject} - mutated 'this' {head, rows}
108 */
109
110const 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 /** [column[i]s, head, columns] */
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
125const voidTabular = () => ({
126 head: [],
127 rows: []
128});
129
130export { selectTabular, selectTabularToSamples, sortTabular, sortTabularByKeys$1 as sortTabularByKeys, tabularToSamples, voidTabular };