Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Readable dates #695

Merged
merged 3 commits into from
Sep 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/perspective/src/js/perspective.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ export default function(Module) {
const end_row = options.end_row || (viewport.height ? start_row + viewport.height : max_rows);
const start_col = options.start_col || (viewport.left ? viewport.left : 0);
const end_col = Math.min(max_cols, (options.end_col || (viewport.width ? start_col + viewport.width : max_cols)) * (hidden + 1));
let date_format;
if (options.date_format) {
date_format = new Intl.DateTimeFormat(options.date_format);
}

const get_pkeys = !!options.index;

Expand All @@ -401,12 +405,14 @@ export default function(Module) {
const slice = this.get_data_slice(start_row, end_row, start_col, end_col);
const ns = slice.get_column_names();
const col_names = extract_vector_scalar(ns).map(x => x.join(defaults.COLUMN_SEPARATOR_STRING));
const schema = this.schema();

let data = formatter.initDataValue();
for (let ridx = start_row; ridx < end_row; ridx++) {
let row = formatter.initRowValue();
for (let cidx = start_col; cidx < end_col; cidx++) {
const col_name = col_names[cidx];
const col_type = schema[col_name];
if ((cidx - (num_sides > 0 ? 1 : 0)) % (this.config.columns.length + hidden) >= this.config.columns.length) {
// Hidden columns are always at the end, so don't emit these.
continue;
Expand All @@ -421,7 +427,13 @@ export default function(Module) {
row_path.delete();
}
} else {
const value = __MODULE__[`get_from_data_slice_${nidx}`](slice, ridx, cidx);
let value = __MODULE__[`get_from_data_slice_${nidx}`](slice, ridx, cidx);
if ((col_type === "datetime" || col_type === "date") && value !== undefined) {
if (date_format) {
value = new Date(value);
value = date_format.format(value);
}
}
formatter.setColumnValue(data, row, col_name, value);
}
}
Expand Down
18 changes: 18 additions & 0 deletions packages/perspective/test/js/to_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ module.exports = perspective => {
// make sure that number of separators = num of column pivots
expect((name.match(/\|/g) || []).length).toEqual(2);
});

it("should return dates in native form by default", async function() {
let table = perspective.table([{datetime: new Date("2016-06-13")}, {datetime: new Date("2016-06-14")}]);
let view = table.view();
let json = await view.to_json();
expect(json).toEqual([{datetime: 1465776000000}, {datetime: 1465862400000}]);
view.delete();
table.delete();
});

it("should return dates in readable format on passing string in options", async function() {
let table = perspective.table([{datetime: new Date("2016-06-13")}, {datetime: new Date("2016-06-14")}]);
let view = table.view();
let json = await view.to_json({date_format: "en-US"});
expect(json).toEqual([{datetime: "6/13/2016"}, {datetime: "6/14/2016"}]);
view.delete();
table.delete();
});
});

describe("to_arrow()", function() {
Expand Down