mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 01:13:23 -04:00
Fix: #36501
This commit is contained in:
parent
4f7fc12f6b
commit
ea7faad921
2 changed files with 15 additions and 26 deletions
|
@ -17,20 +17,15 @@
|
|||
* under the License.
|
||||
*/
|
||||
|
||||
import { isArray, findLast } from 'lodash';
|
||||
import { isArray, last } from 'lodash';
|
||||
|
||||
const DEFAULT_VALUE = 0;
|
||||
const extractValue = data => data && data[1] || null;
|
||||
|
||||
export default (data, defaultValue = DEFAULT_VALUE) => {
|
||||
if (!isArray(data)) {
|
||||
return data;
|
||||
return data || defaultValue;
|
||||
}
|
||||
|
||||
const extractValue = data => data && data[1] || null;
|
||||
|
||||
// If the last value is zero or null because of a partial bucket or
|
||||
// some kind of timeshift weirdness we will show the second to last.
|
||||
const lastValid = findLast(data, item => extractValue(item));
|
||||
|
||||
return extractValue(lastValid) || defaultValue;
|
||||
return extractValue(last(data)) || defaultValue;
|
||||
};
|
||||
|
|
|
@ -17,33 +17,27 @@
|
|||
* under the License.
|
||||
*/
|
||||
|
||||
import { expect } from 'chai';
|
||||
import getLastValue from '../get_last_value';
|
||||
import getLastValue from './get_last_value';
|
||||
|
||||
describe('getLastValue(data)', () => {
|
||||
|
||||
it('returns data if data is not array', () => {
|
||||
expect(getLastValue('foo')).to.equal('foo');
|
||||
test('should returns data if data is not array', () => {
|
||||
expect(getLastValue('foo')).toBe('foo');
|
||||
});
|
||||
|
||||
it('returns the last value', () => {
|
||||
const data = [[1, 1]];
|
||||
expect(getLastValue(data)).to.equal(1);
|
||||
test('should returns the last value', () => {
|
||||
expect(getLastValue([[1, 2]])).toBe(2);
|
||||
});
|
||||
|
||||
it('returns the second to last value if the last value is null (default)', () => {
|
||||
const data = [[1, 4], [2, null]];
|
||||
expect(getLastValue(data)).to.equal(4);
|
||||
test('should returns the default value ', () => {
|
||||
expect(getLastValue()).toBe(0);
|
||||
});
|
||||
|
||||
it('returns 0 if second to last is not defined (default)', () => {
|
||||
const data = [[1, null], [2, null]];
|
||||
expect(getLastValue(data)).to.equal(0);
|
||||
test('should returns 0 if second to last is not defined (default)', () => {
|
||||
expect(getLastValue([[1, null], [2, null]])).toBe(0);
|
||||
});
|
||||
|
||||
it('returns the N to last value if the last N-1 values are null (default)', () => {
|
||||
const data = [[1, 4], [2, null], [3, null]];
|
||||
expect(getLastValue(data, 3)).to.equal(4);
|
||||
test('should allows to override the default value', () => {
|
||||
expect(getLastValue(null, '-')).toBe('-');
|
||||
});
|
||||
});
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue