UNPKG

2.23 kBJavaScriptView Raw
1import { slice, matchSlice } from '@analys/table-init';
2import { selectTabular, selectTabularToSamples, tabularToSamples } from '@analys/tabular';
3import { randBetw } from '@aryth/rand';
4import { shuffle } from '@vect/vector-select';
5import { coin } from '@analys/table-index';
6import { nullish } from '@typen/nullish';
7import { column } from '@vect/column-getter';
8import { wind as wind$1 } from '@vect/entries-init';
9import { wind } from '@vect/object-init';
10
11/**
12 *
13 * @param {TableObject} table
14 * @param {(str|[str,str])[]} [fields]
15 * @param {boolean=true} [mutate]
16 * @returns {TableObject}
17 */
18
19const tableSelect = function (table, fields, {
20 mutate = false
21} = {}) {
22 var _table;
23
24 let o = mutate ? table : (_table = table, slice(_table));
25 if (fields !== null && fields !== void 0 && fields.length) selectTabular.call(o, fields);
26 return o;
27};
28
29const MEAN = 5;
30function tableShuffle(table, {
31 h,
32 w,
33 oscillate
34} = {}) {
35 var _table;
36
37 let {
38 head,
39 rows
40 } = (_table = table, matchSlice(_table));
41 if (!h || oscillate) h = randBetw(MEAN - 1, MEAN + 5);
42 if (!w || oscillate) w = randBetw(MEAN - 2, MEAN + 1);
43 const headSelection = shuffle(head.slice(), w);
44 rows = shuffle(rows.slice(), h);
45 return selectTabular.call({
46 head,
47 rows
48 }, headSelection);
49}
50
51/**
52 *
53 * @param {string} key
54 * @param {string|string[]|[string,string][]} [field]
55 * @param objectify
56 * @return {Object|Array}
57 */
58
59const tableToObject = function (key, field, objectify = true) {
60 var _table$rows;
61
62 const table = this;
63 const hi = table === null || table === void 0 ? void 0 : (_table$rows = table.rows) === null || _table$rows === void 0 ? void 0 : _table$rows.length;
64 let x, y;
65 const keys = (x = coin.call(table, key)) >= 0 ? column(table.rows, x, hi) : null;
66 const values = nullish(field) || Array.isArray(field) ? field !== null && field !== void 0 && field.length ? selectTabularToSamples.call(matchSlice(table), field) : tabularToSamples.call(matchSlice(table)) : (y = coin.call(table, field)) >= 0 ? column(table.rows, y, hi) : null;
67 return keys && values ? objectify ? wind(keys, values) : wind$1(keys, values) : objectify ? {} : [];
68};
69
70export { tableSelect, tableShuffle, tableToObject };