Skip to content

Commit

Permalink
Feat: Add date type (elastic#271)
Browse files Browse the repository at this point in the history
* feat: add a date type

* fix: date function doesn't pass input to moment

moment doesn't take generic string input anymore, this relies on the native Date type, unless format is provided

* fix: wrap formatdate's context input in Date type

moment doesn't allow passing arbitrary strings in
  • Loading branch information
w33ble authored Dec 18, 2017
1 parent 7868259 commit 2f37760
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
33 changes: 26 additions & 7 deletions common/functions/date.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
import moment from 'moment';

const getInputDate = (input) => {
// return current date if no input
if (!input) return new Date();

// attempt to cast to a number
const numInput = Number(input);
if (!isNaN(numInput)) return numInput;

// return the input
return input;
};

export const date = {
name: 'date',
type: 'number',
type: 'date',
context: {
types: ['null'],
},
help: 'Returns the current time, or a time parsed from a string, as milliseconds since epoch',
help: 'Returns the current time, or a time parsed from a string, as milliseconds since epoch.',
args: {
_: {
types: ['string', 'null'],
help: 'An optional date string to parse into milliseconds since epoch',
help: 'An optional date string to parse into milliseconds since epoch. ' +
'Can be either a valid Javascript Date input or a string to parse using the format argument. ',
},
format: {
types: ['string', 'null'],
help: 'The momentJS format for parsing the optional date string (See https://momentjs.com/docs/#/displaying/)',
help: 'The momentJS format for parsing the optional date string (See https://momentjs.com/docs/#/displaying/).',
},
},
fn: (context, args) => {
if (!args._) return moment().valueOf();
if (!args.format) return moment(args._).valueOf();
return moment(args._, args.format).valueOf();
const inputDate = getInputDate(args._);
const outputDate = (args._ && args.format) ? moment(inputDate, args.format).toDate() : new Date(inputDate);

if (isNaN(outputDate.getTime())) throw new Error(`Invalid date input: ${args._}`);

return {
type: 'date',
value: outputDate,
};
},
};
2 changes: 1 addition & 1 deletion common/functions/formatdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export const formatdate = {
},
},
fn: (context, args) => {
return moment(context).format(args._);
return moment(new Date(context)).format(args._);
},
};
38 changes: 38 additions & 0 deletions common/types/date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export const date = {
name: 'date',
from: {
number: n => ({
type: 'date',
value: new Date(n),
}),
string: s => ({
type: 'date',
value: new Date(s),
}),
},
to: {
string: d => d.value.toISOString(),
number: d => d.value.getTime(),
render: d => {
return {
type: 'render',
as: 'markdown',
value: {
content: String(d.value.toISOString()),
},
};
},
datatable: (d) => {
return {
type: 'datatable',
columns: [
{
name: 'value',
type: 'date',
},
],
rows: [{ value: d.value.getTime() }],
};
},
},
};
2 changes: 2 additions & 0 deletions common/types/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { boolean } from './boolean';
import { datatable } from './datatable';
import { dataurl } from './dataurl';
import { date } from './date';
import { error } from './error';
import { filter } from './filter';
import { image } from './image';
Expand All @@ -16,6 +17,7 @@ export const typeSpecs = [
boolean,
datatable,
dataurl,
date,
error,
filter,
image,
Expand Down

0 comments on commit 2f37760

Please sign in to comment.