-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
api.js
138 lines (111 loc) · 2.72 KB
/
api.js
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
const test = require('tape')
test('README API', (t) => {
const Table = require('olap-cube').model.Table
// constructor
const table = new Table({
dimensions: ['year', 'month'],
fields: ['revenue'],
points: [[2016, 'Jan']],
data: [[100]]
})
t.deepEqual(table.dimensions, ['year', 'month'], 'table.dimensions')
t.deepEqual(table.fields, ['revenue'], 'table.fields')
t.deepEqual(table.header, ['year', 'month', 'revenue'], 'table.header')
const emptyTable = new Table(table.structure)
// addRows
const table2 = emptyTable.addRows({
header: [ 'year', 'month', 'revenue' ],
rows: [
[ 2015, 'Nov', 80 ],
[ 2015, 'Dec', 90 ],
[ 2016, 'Jan', 100 ],
[ 2016, 'Feb', 170 ],
[ 2016, 'Mar', 280 ],
[ 2017, 'Feb', 177 ],
[ 2017, 'Apr', 410 ]
]
})
t.deepEqual(table2.data, [
[ 80 ],
[ 90 ],
[ 100 ],
[ 170 ],
[ 280 ],
[ 177 ],
[ 410 ]
], 'data')
t.deepEqual(table2.rows, [
[ 2015, 'Nov', 80 ],
[ 2015, 'Dec', 90 ],
[ 2016, 'Jan', 100 ],
[ 2016, 'Feb', 170 ],
[ 2016, 'Mar', 280 ],
[ 2017, 'Feb', 177 ],
[ 2017, 'Apr', 410 ]
], 'rows')
t.deepEqual(table2.points, [
[ 2015, 'Nov' ],
[ 2015, 'Dec' ],
[ 2016, 'Jan' ],
[ 2016, 'Feb' ],
[ 2016, 'Mar' ],
[ 2017, 'Feb' ],
[ 2017, 'Apr' ]
], 'points')
// Slice.
const table3 = table2.slice('year', 2016)
t.deepEqual(table3.points, [
[ 2016, 'Jan' ],
[ 2016, 'Feb' ],
[ 2016, 'Mar' ]
], 'sliced points')
t.deepEqual(table3.data, [
[ 100 ],
[ 170 ],
[ 280 ]
], 'sliced data')
const notFebruary = (point) => point[1] !== 'Feb'
// Dice.
const table4 = table2.dice(notFebruary)
t.deepEqual(table4.points, [
[ 2015, 'Nov' ],
[ 2015, 'Dec' ],
[ 2016, 'Jan' ],
[ 2016, 'Mar' ],
[ 2017, 'Apr' ]
], 'diced points')
t.deepEqual(table4.data, [
[ 80 ],
[ 90 ],
[ 100 ],
[ 280 ],
[ 410 ]
], 'diced data')
// Roll up.
const summation = (sum, value) => {
return [sum[0] + value[0]]
}
const table5 = new Table({
dimensions: ['continent', 'country'],
fields: ['numStores']
})
const table6 = table5.addRows({
header: [ 'continent', 'country', 'numStores' ],
rows: [
[ 'Europe', 'Norway', 20 ],
[ 'Europe', 'Denmark', 48 ],
[ 'Europe', 'Germany', 110 ],
[ 'Europe', 'Portugal', 17 ],
[ 'Asia', 'China', 280 ],
[ 'Asia', 'Russia', 161 ],
[ 'Asia', 'Thailand', 120 ]
]
})
const initialValue = [0]
const table7 = table6.rollup('continent', ['numStores'], summation, initialValue)
t.deepEqual(table7.rows, [
[ 'Europe', 195 ],
[ 'Asia', 561 ]
], 'rolled up points')
t.end()
})