-
Notifications
You must be signed in to change notification settings - Fork 843
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inherit selected value in quick select in EuiSuperDatePicker (#3105)
* Update quick select values on change of input * updated changelog * updated cl * removed console statement * support other commonly used selections * Added function description * renamed file * Added quick select utils unit tests * updated function for a better result for 'now' * updated test * added return * fixed bug in now -> relative case * removed unwanted && and - escape in regex * removed [] * Added test for duration parsing Co-authored-by: Chandler Prall <chandler.prall@gmail.com>
- Loading branch information
1 parent
a4eecae
commit c495aef
Showing
4 changed files
with
134 additions
and
4 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
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
72 changes: 72 additions & 0 deletions
72
src/components/date_picker/super_date_picker/quick_select_popover/quick_select_utils.js
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,72 @@ | ||
/** | ||
* This function returns time value, time unit and time tense for a given time string. | ||
* For example: for `now-40m` it will parse output as time value to `40` | ||
* time unit to `m` and time unit to `last`. | ||
* If given a datetime string it will return a default value. | ||
* If the given string is in the format such as `now/d` it will parse the string to moment object | ||
* and find the time value, time unit and time tense using moment | ||
* This function accepts two strings start and end time. I the start value is now then it uses | ||
* the end value to parse. | ||
* | ||
* @param {string} start start time | ||
* @param {string} start end time | ||
* @returns {object} time value, time unit and time tense | ||
*/ | ||
|
||
import moment from 'moment'; | ||
import dateMath from '@elastic/datemath'; | ||
import { isString } from '../../../../services/predicate'; | ||
import { relativeUnitsFromLargestToSmallest } from '../relative_options'; | ||
import { DATE_MODES } from '../date_modes'; | ||
|
||
const LAST = 'last'; | ||
const NEXT = 'next'; | ||
|
||
const isNow = value => value === DATE_MODES.NOW; | ||
|
||
export const parseTimeParts = (start, end) => { | ||
const results = { | ||
timeValueDefault: 15, | ||
timeUnitsDefault: 'm', | ||
timeTenseDefault: LAST, | ||
}; | ||
|
||
const value = isNow(start) ? end : start; | ||
|
||
const matches = | ||
isString(value) && | ||
value.match(/now(([-+])(\d+)([smhdwMy])(\/[smhdwMy])?)?/); | ||
|
||
if (!matches) { | ||
return results; | ||
} | ||
|
||
const operator = matches[2]; | ||
const timeValue = matches[3]; | ||
const timeUnitsDefault = matches[4]; | ||
|
||
if (timeValue && timeUnitsDefault && operator) { | ||
const timeValueDefault = parseInt(timeValue, 10); | ||
const timeTenseDefault = operator === '+' ? NEXT : LAST; | ||
|
||
return { | ||
timeValueDefault, | ||
timeUnitsDefault, | ||
timeTenseDefault, | ||
}; | ||
} | ||
|
||
const duration = moment.duration(moment().diff(dateMath.parse(value))); | ||
let unitOp = ''; | ||
for (let i = 0; i < relativeUnitsFromLargestToSmallest.length; i++) { | ||
const as = duration.as(relativeUnitsFromLargestToSmallest[i]); | ||
if (as < 0) unitOp = '+'; | ||
if (Math.abs(as) > 1) { | ||
results.timeValueDefault = Math.round(Math.abs(as)); | ||
results.timeUnitsDefault = relativeUnitsFromLargestToSmallest[i]; | ||
results.timeTenseDefault = unitOp === '+' ? NEXT : LAST; | ||
break; | ||
} | ||
} | ||
return results; | ||
}; |
51 changes: 51 additions & 0 deletions
51
src/components/date_picker/super_date_picker/quick_select_popover/quick_select_utils.test.js
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,51 @@ | ||
import moment from 'moment'; | ||
import { parseTimeParts } from './quick_select_utils'; | ||
|
||
describe('parseTimeParts', () => { | ||
it('should parse now', () => { | ||
const out = parseTimeParts('now', 'now+5m'); | ||
expect(out).toEqual({ | ||
timeValueDefault: 5, | ||
timeUnitsDefault: 'm', | ||
timeTenseDefault: 'next', | ||
}); | ||
}); | ||
|
||
it('should parse now-2h', () => { | ||
const out = parseTimeParts('now-2h', 'now+5m'); | ||
expect(out).toEqual({ | ||
timeValueDefault: 2, | ||
timeUnitsDefault: 'h', | ||
timeTenseDefault: 'last', | ||
}); | ||
}); | ||
|
||
it('should parse now+2h', () => { | ||
const out = parseTimeParts('now+2h', 'now+5m'); | ||
expect(out).toEqual({ | ||
timeValueDefault: 2, | ||
timeUnitsDefault: 'h', | ||
timeTenseDefault: 'next', | ||
}); | ||
}); | ||
|
||
describe('duration parsing', () => { | ||
const duration = moment.duration; | ||
beforeEach(() => { | ||
moment.duration = () => duration(6 * 60 * 60 * 1000); | ||
}); | ||
|
||
afterEach(() => { | ||
moment.duration = duration; | ||
}); | ||
|
||
it('should parse now/d', () => { | ||
const out = parseTimeParts('now/d', 'now+5m'); | ||
expect(out).toEqual({ | ||
timeValueDefault: 6, | ||
timeUnitsDefault: 'h', | ||
timeTenseDefault: 'last', | ||
}); | ||
}); | ||
}); | ||
}); |