UNPKG

1.62 kBJavaScriptView Raw
1import { VLKP } from '@analys/enum-lookups';
2import { coin } from '@analys/table-index';
3import { Columns } from '@vect/column-getter';
4import { wind as wind$1 } from '@vect/entries-init';
5import { wind } from '@vect/object-init';
6
7const lookup = function (valueToFind, key, field) {
8 const table = this,
9 {
10 head,
11 rows
12 } = table,
13 x = head.indexOf(key),
14 y = head.indexOf(field);
15 if (x < 0 || y < 0) return null;
16 const row = rows.find(row => row[x] === valueToFind);
17 return row ? row[y] : null;
18};
19const lookupMany = function (valuesToFind, key, field) {
20 const table = this,
21 {
22 head,
23 rows
24 } = table,
25 x = head.indexOf(key),
26 y = head.indexOf(field);
27 if (x < 0 || y < 0) return valuesToFind.map(() => null);
28 return valuesToFind.map(v => {
29 const row = rows.find(row => row[x] === v);
30 return row ? row[y] : null;
31 });
32};
33
34const lookupTable = function (key, field, objectify) {
35 const table = this,
36 getColumn = Columns(table.rows);
37 const [ki, vi] = [coin.call(table, key), coin.call(table, field)];
38 return ki >= 0 && vi >= 0 ? objectify ? wind(getColumn(ki), getColumn(vi)) : wind$1(getColumn(ki), getColumn(vi)) : objectify ? {} : [];
39};
40
41const lookupCached = function (valueToFind, key, field) {
42 const table = this;
43 let ds, dict;
44 if (!(ds = table[VLKP]) || !(dict = ds.dict) || ds.key !== key || ds.value !== field) table[VLKP] = {
45 dict: dict = lookupTable.call(table, key, field, true),
46 key: key,
47 value: field
48 };
49 return dict[valueToFind];
50};
51
52export { lookup, lookupCached, lookupMany, lookupTable };