UNPKG

4.39 kBJavaScriptView Raw
1import { NUM, STR, OBJ } from '@typen/enum-data-types';
2import { COUNT, INCRE } from '@analys/enum-pivot-mode';
3import { mapper } from '@vect/vector-mapper';
4import { nullish } from '@typen/nullish';
5import { acquire } from '@vect/vector-merge';
6
7const 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
31const defaultCell = defaultField => ({
32 field: defaultField,
33 mode: COUNT
34});
35
36const 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 * @param key
45 * @return {[*,*]}
46 */
47
48const 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 * @param {Object} o
58 * @return {*}
59 */
60
61const getEntryOnce = o => {
62 for (let k in o) return [k, o[k]];
63};
64
65/**
66 *
67 * @param {*} field
68 * @param {str} neglect - default field
69 * @returns {[str,number]|[str,number][]}
70 */
71
72const 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};
87const 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
106function _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
121class TableSpec {
122 /** @type {TableObject} */
123
124 /** @type {str} */
125
126 /** @type {str} */
127
128 /** @type {CubeCell[]|CubeCell} */
129
130 /** @type {Filter[]|Filter} */
131
132 /** @type {function():number} */
133
134 /**
135 * @param {str} side
136 * @param {str} banner
137 * @param {CubeCell[]|CubeCell} [cell]
138 * @param {Filter[]|Filter} [filter]
139 * @param {function():number} formula - formula is valid only when cell is CubeCell array.
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 * @param {str} side
164 * @param {str} banner
165 * @param {CubeCell[]|CubeCell} [cell]
166 * @param {Filter[]|Filter} [filter]
167 * @param {function():number} formula - formula is valid only when cell is CubeCell array.
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
200export { TableSpec, parseCell, parseField, parseKey, parseKeyOnce };