-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathdata.ts
269 lines (224 loc) · 6.86 KB
/
data.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import * as D from './types/data.js'
import { CRecord, CData } from './module/cdata.js'
import { DataRecord } from './datarecord.js'
import { Mirrored } from './tsutils.js'
type DataTypes = D.DimensionValue | D.MeasureValue
export class Data {
private _cData: CData
constructor(cData: CData) {
this._cData = cData
}
set(obj?: D.TableBySeries | D.TableByRecords): void {
if (!obj) {
return
}
if (obj.series) {
if (!Array.isArray(obj.series)) {
throw new Error('data series field is not an array')
}
for (const series of obj.series) {
this._setSeries(series)
}
}
if ('records' in obj) {
if (!Array.isArray(obj.records)) {
throw new Error('data records field is not an array')
}
const seriesList = this._cData.getMetaInfo().series
for (const record of obj.records) {
this._addRecord(record, seriesList)
}
}
if (obj.filter || obj.filter === null) {
this._setFilter(obj.filter)
}
}
private _addRecord(
record: D.ValueArray | D.Record,
seriesList: Mirrored<D.SeriesInfo[]>
): void {
if (!Array.isArray(record)) {
if (typeof record === 'object' && record !== null) {
record = this._recordObjectToArray(record, seriesList)
} else throw new Error('data record is not an array or object')
}
this._cData.addRecord(record)
}
private _recordObjectToArray(
record: D.Record,
seriesList: Mirrored<D.SeriesInfo[]>
): D.Value[] {
const result = [] as D.Value[]
seriesList.forEach((series) => {
if (series.name in record) {
result.push(record[series.name] || this._default(series.type))
} else {
result.push(this._default(series.type))
}
})
return result
}
private _default(type?: string): D.Value {
return type === 'measure' ? 0 : ''
}
private _setSeries(series: D.Series): void {
if (!series.name) {
throw new Error('missing series name')
}
if (this._isIndexedDimension(series)) {
this._validateIndexedDimension(series)
this._addDimension(
series.name,
series.values ?? [],
series.categories,
series.isContiguous ?? false
)
} else {
const values = series?.values ?? ([] as DataTypes[])
const seriesType = series?.type ?? this._detectType(values)
if (this._isDetectedDimension(series, seriesType)) {
const { indexes, categories } = this._convertDimension(values)
this._addDimension(series.name, indexes, categories, series.isContiguous ?? false)
} else if (this._isMeasure(series, seriesType)) {
if (!series.unit) series.unit = ''
this._addMeasure(series.name, series.unit, values)
} else {
const seriesBase = series as D.SeriesBase
throw new Error('invalid series type: ' + seriesBase.type)
}
}
}
private _isIndexedDimension(series: D.Series): series is D.IndexDimension {
if (!('categories' in series && Array.isArray(series.categories))) {
return false
}
return true
}
private _isDetectedDimension(series: D.Series, type: string | null): series is D.Dimension {
return (
type === 'dimension' ||
('isContiguous' in series && typeof series.isContiguous === 'boolean')
)
}
private _isMeasure(series: D.Series, type: string | null): series is D.Measure {
return type === 'measure' || ('unit' in series && typeof series.unit === 'string')
}
private _validateIndexedDimension(series: D.IndexDimension): void {
if (series.values && Array.isArray(series.values)) {
const max = series.categories.length
for (const value of series.values) {
if (value === null) {
continue
}
const numberValue = Number(value)
if (!Number.isInteger(numberValue)) {
throw new Error('invalid category value (not an int): ' + numberValue)
}
if (numberValue < 0) {
throw new Error('invalid category value (negative): ' + numberValue)
}
if (numberValue >= max) {
throw new Error('invalid category value (out of range): ' + numberValue)
}
}
}
if (new Set(series.categories).size !== series.categories.length) {
throw new Error('duplicate category values')
}
}
private _detectType(values: DataTypes[]): D.SeriesType | null {
if (Array.isArray(values) && values.length) {
if (typeof values[0] === 'number' || values[0] === null) {
return 'measure'
} else if (typeof values[0] === 'string') {
return 'dimension'
}
}
return null
}
private _addDimension(
name: string,
indexes: number[],
categories: string[],
isContiguous: boolean
): void {
if (typeof name !== 'string') {
throw new Error('first parameter should be string')
}
if (!(indexes instanceof Array)) {
throw new Error('second parameter should be an array')
}
if (!(categories instanceof Array)) {
throw new Error('third parameter should be an array')
}
if (typeof isContiguous !== 'boolean') {
throw new Error('fourth parameter should be boolean')
}
if (!this._isStringArray(categories)) {
throw new Error('third parameter should be an array of strings')
}
if (!this._isDimensionIndex(indexes)) {
throw new Error('the measure index array element should be number')
}
this._cData.addDimension(name, indexes, categories, isContiguous)
}
private _isStringArray(values: unknown[]): values is string[] {
for (const value of values) {
if (typeof value !== 'string') {
return false
}
}
return true
}
private _isDimensionIndex(indexes: unknown[]): indexes is number[] {
for (const index of indexes) {
const numberValue = Number(index)
if (!Number.isInteger(numberValue) || numberValue < 0) {
return false
}
}
return true
}
private _convertDimension(values: DataTypes[]): { categories: string[]; indexes: number[] } {
const uniques = new Map()
const categories: string[] = []
const indexes = new Array(values.length)
for (let index = 0; index < values.length; index++) {
const value = values[index]
if (typeof value !== 'string') {
continue
}
let uniqueIndex = uniques.get(value)
if (uniqueIndex === undefined) {
uniqueIndex = categories.length
uniques.set(value, uniqueIndex)
categories.push(value)
}
indexes[index] = uniqueIndex
}
return { categories, indexes }
}
private _addMeasure(name: string, unit: string, values: unknown[]): void {
if (typeof name !== 'string') {
throw new Error("'name' parameter should be string")
}
if (typeof unit !== 'string') {
throw new Error("'unit' parameter should be string")
}
if (!(values instanceof Array)) {
throw new Error("'values' parameter should be an array")
}
const numbers = values.map((value) => Number(value))
this._cData.addMeasure(name, unit, numbers)
}
private _setFilter(filter: D.FilterCallback | null): void {
if (typeof filter === 'function') {
const callback = (cRecord: CRecord): boolean => filter(new DataRecord(cRecord))
this._cData.setFilter(callback)
} else if (filter === null) {
this._cData.setFilter(null)
} else {
throw new Error('data filter is not a function or null')
}
}
}