Skip to content

Commit

Permalink
feat: Including format and type in tableColumns (#587) Thanks to @dan…
Browse files Browse the repository at this point in the history
…panaite!

* Including format and type in tableColumns

* Updating docs

* Updating JSDoc

Fixes #585
  • Loading branch information
danpanaite authored Apr 14, 2020
1 parent ec2750e commit 3f7d74f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/Cube.js-Frontend/@cubejs-client-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ For example
// ResultSet.tableColumns() will return
[
{ key: "Stories.time", title: "Stories Time", shortTitle: "Time" },
{ key: "Stories.count", title: "Stories Count", shortTitle: "Count" },
{ key: "Stories.time", title: "Stories Time", shortTitle: "Time", type: "time", format: undefined },
{ key: "Stories.count", title: "Stories Count", shortTitle: "Count", type: "count", format: undefined },
//...
]
```
Expand Down
16 changes: 13 additions & 3 deletions packages/cubejs-client-core/src/ResultSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,8 @@ class ResultSet {
*
* // ResultSet.tableColumns() will return
* [
* { key: "Stories.time", title: "Stories Time", shortTitle: "Time" },
* { key: "Stories.count", title: "Stories Count", shortTitle: "Count" },
* { key: "Stories.time", title: "Stories Time", shortTitle: "Time", type: "time", format: undefined },
* { key: "Stories.count", title: "Stories Count", shortTitle: "Count", type: "count", format: undefined },
* //...
* ]
* ```
Expand All @@ -377,6 +377,8 @@ class ResultSet {
key: m,
title: this.loadResponse.annotation.measures[m].title,
shortTitle: this.loadResponse.annotation.measures[m].shortTitle,
format: this.loadResponse.annotation.measures[m].format,
type: this.loadResponse.annotation.measures[m].type,
})) :
[{
key: field,
Expand All @@ -387,7 +389,15 @@ class ResultSet {
shortTitle: (
this.loadResponse.annotation.dimensions[field] ||
this.loadResponse.annotation.timeDimensions[field]
).shortTitle
).shortTitle,
format: (
this.loadResponse.annotation.dimensions[field] ||
this.loadResponse.annotation.timeDimensions[field]
).format,
type: (
this.loadResponse.annotation.dimensions[field] ||
this.loadResponse.annotation.timeDimensions[field]
).type
}]
);
return normalizedPivotConfig.x.map(column)
Expand Down
80 changes: 80 additions & 0 deletions packages/cubejs-client-core/src/ResultSet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,4 +597,84 @@ describe('ResultSet', () => {
]);
});
});

describe('tableColumns', () => {
test('returns array of column definitions for tablePivot', () => {
const resultSet = new ResultSet({
"query": {
"measures": ["Orders.count", "Orders.totalAmount"],
"timeDimensions": [{
"dimension": "Orders.createdAt",
"granularity": "day",
"dateRange": ["2020-01-08T00:00:00.000", "2020-01-14T23:59:59.999"]
}],
"dimensions": ["Orders.createdAt"],
"filters": [],
"timezone": "UTC"
},
"data": [],
"annotation": {
"measures": {
"Orders.count": {
"title": "Orders Count",
"shortTitle": "Count",
"type": "count",
},
"Orders.totalAmount": {
"title": "Orders Total Amount",
"shortTitle": "Total Amount",
"type": "number",
"format": "currency"
}
},
"dimensions": {
"Orders.createdAt": {
"title": "Orders Created at",
"shortTitle": "Created at",
"type": "time"
}
},
"segments": {},
"timeDimensions": {
"Orders.createdAt.day": {
"title": "Orders Created at",
"shortTitle": "Created at",
"type": "time"
}
}
}
});

expect(resultSet.tableColumns()).toEqual([
{
"format": undefined,
"key": "Orders.createdAt.day",
"shortTitle": "Created at",
"title": "Orders Created at",
"type": "time",
},
{
"format": undefined,
"key": "Orders.createdAt",
"shortTitle": "Created at",
"title": "Orders Created at",
"type": "time",
},
{
"format": undefined,
"key": "Orders.count",
"shortTitle": "Count",
"title": "Orders Count",
"type": "count",
},
{
"format": "currency",
"key": "Orders.totalAmount",
"shortTitle": "Total Amount",
"title": "Orders Total Amount",
"type": "number",
}
]);
});
});
});

0 comments on commit 3f7d74f

Please sign in to comment.