forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
4 changed files
with
67 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() }], | ||
}; | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters