Skip to content

Commit

Permalink
Merge pull request #695 from finos/readable-dates
Browse files Browse the repository at this point in the history
Readable dates
  • Loading branch information
texodus authored Sep 21, 2019
2 parents b4897c3 + 389d57e commit a056023
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
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 = Math.min(max_rows, 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;
const leaves_only = !!options.leaves_only;
Expand All @@ -402,6 +406,7 @@ 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();

Expand All @@ -414,6 +419,7 @@ export default function(Module) {
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 @@ -426,7 +432,13 @@ export default function(Module) {
}
}
} 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 @@ -185,6 +185,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("leaves_only flag", function() {
Expand Down

0 comments on commit a056023

Please sign in to comment.