1 | import { NUM, STR, OBJ } from '@typen/enum-data-types';
|
2 | import { COUNT, INCRE } from '@analys/enum-pivot-mode';
|
3 | import { mapper } from '@vect/vector-mapper';
|
4 | import { nullish } from '@typen/nullish';
|
5 | import { acquire } from '@vect/vector-merge';
|
6 |
|
7 | const parseCell = (cell, defaultField) => {
|
8 | var _cell$field, _cell$mode;
|
9 |
|
10 | if (cell === void 0 || cell === null) return defaultCell(defaultField);
|
11 |
|
12 | switch (typeof cell) {
|
13 | case OBJ:
|
14 | if (Array.isArray(cell)) return cell.length ? mapper(cell, cell => parseCell(cell, defaultField)) : defaultCell(defaultField);
|
15 | cell.field = (_cell$field = cell.field) !== null && _cell$field !== void 0 ? _cell$field : defaultField;
|
16 | cell.mode = (_cell$mode = cell.mode) !== null && _cell$mode !== void 0 ? _cell$mode : COUNT;
|
17 | return cell;
|
18 |
|
19 | case STR:
|
20 | case NUM:
|
21 | return {
|
22 | field: cell,
|
23 | mode: INCRE
|
24 | };
|
25 |
|
26 | default:
|
27 | return defaultCell(defaultField);
|
28 | }
|
29 | };
|
30 |
|
31 | const defaultCell = defaultField => ({
|
32 | field: defaultField,
|
33 | mode: COUNT
|
34 | });
|
35 |
|
36 | const parseKey = key => {
|
37 | if (nullish(key)) return [key];
|
38 | let t = typeof key;
|
39 | if (t === STR || t === NUM) return [key];
|
40 | if (t === OBJ) return Array.isArray(key) ? key : Object.entries(key);
|
41 | return key;
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | const parseKeyOnce = key => {
|
49 | if (nullish(key)) return [key];
|
50 | let t = typeof key;
|
51 | if (t === STR || t === NUM) return [key];
|
52 | if (t === OBJ) return Array.isArray(key) ? key : getEntryOnce(key);
|
53 | return [key];
|
54 | };
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | const getEntryOnce = o => {
|
62 | for (let k in o) return [k, o[k]];
|
63 | };
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | const parseField = (field, neglect) => {
|
73 | let t = typeof field,
|
74 | ents;
|
75 | if (nullish(field)) return [neglect, COUNT];
|
76 |
|
77 | if (t === OBJ) {
|
78 | ents = Array.isArray(field) ? parseFields(field, neglect) : Object.entries(field);
|
79 | if (ents.length === 0) return [neglect, COUNT];
|
80 | if (ents.length === 1) return ents[0];
|
81 | return ents;
|
82 | }
|
83 |
|
84 | if (t === STR || t === NUM) return [field, INCRE];
|
85 | return [neglect, COUNT];
|
86 | };
|
87 | const parseFields = (fields, neglect) => {
|
88 | let ents = [],
|
89 | t;
|
90 |
|
91 | for (let field of fields) if (nullish(field)) {
|
92 | ents.push([neglect, COUNT]);
|
93 | } else if (Array.isArray(field)) {
|
94 | ents.push(field);
|
95 | } else if ((t = typeof field) && (t === STR || t === NUM)) {
|
96 | ents.push([field, INCRE]);
|
97 | } else if (t === OBJ) {
|
98 | acquire(ents, Object.entries(field));
|
99 | } else {
|
100 | ents.push([neglect, COUNT]);
|
101 | }
|
102 |
|
103 | return ents;
|
104 | };
|
105 |
|
106 | function _defineProperty(obj, key, value) {
|
107 | if (key in obj) {
|
108 | Object.defineProperty(obj, key, {
|
109 | value: value,
|
110 | enumerable: true,
|
111 | configurable: true,
|
112 | writable: true
|
113 | });
|
114 | } else {
|
115 | obj[key] = value;
|
116 | }
|
117 |
|
118 | return obj;
|
119 | }
|
120 |
|
121 | class TableSpec {
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | constructor(side, banner, cell, filter, formula) {
|
142 | _defineProperty(this, "table", void 0);
|
143 |
|
144 | _defineProperty(this, "side", void 0);
|
145 |
|
146 | _defineProperty(this, "banner", void 0);
|
147 |
|
148 | _defineProperty(this, "cell", void 0);
|
149 |
|
150 | _defineProperty(this, "filter", void 0);
|
151 |
|
152 | _defineProperty(this, "formula", void 0);
|
153 |
|
154 | Object.assign(this, {
|
155 | side,
|
156 | banner,
|
157 | cell,
|
158 | filter,
|
159 | formula
|
160 | });
|
161 | }
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | static build({
|
172 | side,
|
173 | banner,
|
174 | cell,
|
175 | filter,
|
176 | formula
|
177 | }) {
|
178 | return new TableSpec(side, banner, cell, filter, formula);
|
179 | }
|
180 |
|
181 | toObject() {
|
182 | const {
|
183 | side,
|
184 | banner,
|
185 | cell,
|
186 | filter,
|
187 | formula
|
188 | } = this;
|
189 | return {
|
190 | side,
|
191 | banner,
|
192 | cell,
|
193 | filter,
|
194 | formula
|
195 | };
|
196 | }
|
197 |
|
198 | }
|
199 |
|
200 | export { TableSpec, parseCell, parseField, parseKey, parseKeyOnce };
|