Skip to content

Commit

Permalink
Moved to lodash 3.10.1 (to align with Kibana) (elastic#359)
Browse files Browse the repository at this point in the history
- created a `predicate` module that exports all predicate function (e.g. `isString`, `isFunction`,...)
- created `isNil` predicate
- `sortable_properties` no longer uses lodash `sortBy` and uses our comparators instaed

this change should enable using tables in Kibana.
  • Loading branch information
uboness authored and bevacqua committed Feb 2, 2018
1 parent 96391bd commit 1356e08
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

No public interface changes since `0.0.16`.

**Bug fixes**

- downgraded `lodash` version to `3.10.0` to align it with Kibana

# [`0.0.16`](https://github.com/elastic/eui/tree/v0.0.16)

- `EuiRadio` now supports the `input` tag's `name` attribute. `EuiRadioGroup` accepts a `name` prop that will propagate to its `EuiRadio`s. ([#348](https://github.com/elastic/eui/pull/348))
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"html": "^1.0.0",
"jquery": "^3.2.1",
"keymirror": "^0.1.1",
"lodash": "^4.17.4",
"lodash": "^3.10.1",
"numeral": "^2.0.6",
"prop-types": "^15.6.0",
"react-ace": "^5.5.0",
Expand Down Expand Up @@ -75,7 +75,6 @@
"html-webpack-plugin": "^2.30.1",
"jest": "^22.0.6",
"jest-cli": "^22.0.6",
"lodash": "^4.17.4",
"moment": "2.13.0",
"node-sass": "^4.5.3",
"npm-run": "^4.1.2",
Expand Down
2 changes: 1 addition & 1 deletion src/components/table_of_records/default_record_action.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { isString } from 'lodash';
import { isString } from '../../services/predicate';
import { EuiButton, EuiButtonIcon } from '../button';

const defaults = {
Expand Down
3 changes: 2 additions & 1 deletion src/components/table_of_records/table_of_records.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import _ from 'lodash';
import { isString } from '../../services/predicate';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import {
Expand Down Expand Up @@ -162,7 +163,7 @@ export class EuiTableOfRecords extends React.Component {

recordId(record) {
const id = this.props.config.recordId;
return _.isString(id) ? record[id] : id(record);
return isString(id) ? record[id] : id(record);
}

changeSelection(selection) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/format/format_auto.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray, isBoolean, isDate, isNaN, isNil, isNumber, isString } from 'lodash';
import { isNil, isArray, isBoolean, isDate, isNaN, isNumber, isString } from '../predicate';
import { formatBoolean } from './format_boolean';
import { formatDate } from './format_date';
import { formatNumber } from './format_number';
Expand Down
2 changes: 1 addition & 1 deletion src/services/format/format_boolean.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNil } from 'lodash';
import { isNil } from '../predicate';

export const formatBoolean = (value, { yes = 'Yes', no = 'No', nil = '' } = {}) => {
if (isNil(value)) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/format/format_date.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNil, isFunction, isString } from 'lodash';
import { isNil, isFunction, isString } from '../predicate';
import moment from 'moment';

const calendar = (value, options = {}) => {
Expand Down
2 changes: 1 addition & 1 deletion src/services/format/format_number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numeral from 'numeral';
import { isNil, isString } from 'lodash';
import { isNil, isString } from '../predicate';

const numberFormatAliases = {
decimal1: '0,0.0',
Expand Down
2 changes: 1 addition & 1 deletion src/services/format/format_text.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNil } from 'lodash';
import { isNil } from '../predicate';

export const formatText = (value, { nil = '' } = {}) => {
return isNil(value) ? nil : value.toString();
Expand Down
15 changes: 15 additions & 0 deletions src/services/predicate/common_predicates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const always = () => true;

export const never = () => false;

export const isUndefined = (value) => {
return value === undefined;
};

export const isNull = (value) => {
return value === null;
};

export const isNil = (value) => {
return isUndefined(value) || isNull(value);
};
44 changes: 44 additions & 0 deletions src/services/predicate/common_predicates.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { always, never, isNil, isUndefined, isNull } from './common_predicates';

describe('common predicates', () => {

test('always', () => {
[ undefined, null, 'a', 1, true, false, Date.now(), {}, [], /.*/ ].forEach(value => {
expect(always(value)).toBe(true);
});
});

test('never', () => {
[ undefined, null, 'a', 1, true, false, Date.now(), {}, [], /.*/ ].forEach(value => {
expect(never(value)).toBe(false);
});
});

test('isUndefined', () => {
[ undefined ].forEach(value => {
expect(isUndefined(value)).toBe(true);
});
[ null, 'a', 1, true, false, Date.now(), {}, [], /.*/ ].forEach(value => {
expect(isUndefined(value)).toBe(false);
});
});

test('isNull', () => {
[ null ].forEach(value => {
expect(isNull(value)).toBe(true);
});
[ undefined, 'a', 1, true, false, Date.now(), {}, [], /.*/ ].forEach(value => {
expect(isNull(value)).toBe(false);
});
});

test('isNil', () => {
[ undefined, null ].forEach(value => {
expect(isNil(value)).toBe(true);
});
[ 'a', 1, true, false, Date.now(), {}, [], /.*/ ].forEach(value => {
expect(isNil(value)).toBe(false);
});
});

});
2 changes: 2 additions & 0 deletions src/services/predicate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './common_predicates';
export * from './lodash_predicates';
9 changes: 9 additions & 0 deletions src/services/predicate/lodash_predicates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export {
isFunction,
isArray,
isString,
isBoolean,
isDate,
isNumber,
isNaN,
} from 'lodash';
2 changes: 1 addition & 1 deletion src/services/random.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNil } from 'lodash';
import { isNil } from './predicate';

const defaultRand = Math.random;

Expand Down
10 changes: 7 additions & 3 deletions src/services/sort/comparators.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ export const Comparators = Object.freeze({
return (v1, v2) => comparator(v2, v1);
},

property(prop, comparator = undefined) {
value(valueCallback, comparator = undefined) {
if (!comparator) {
comparator = this.default(SortDirection.ASC);
}
return (o1, o2) => {
return comparator(o1[prop], o2[prop]);
return comparator(valueCallback(o1), valueCallback(o2));
};
}
},

property(prop, comparator = undefined) {
return this.value(value => value[prop], comparator);
},

});
12 changes: 8 additions & 4 deletions src/services/sort/sortable_properties.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sortBy from 'lodash/sortBy';
import { Comparators } from './comparators';

/**
* @typedef {Object} SortableProperty
Expand Down Expand Up @@ -43,9 +43,13 @@ export class SortableProperties {
* @returns {Array.<Object>} sorted array of items, based off the sort properties.
*/
sortItems(items) {
return this.isCurrentSortAscending()
? sortBy(items, this.getSortedProperty().getValue)
: sortBy(items, this.getSortedProperty().getValue).reverse();
const copy = [...items];
let comparator = Comparators.value(this.getSortedProperty().getValue);
if (!this.isCurrentSortAscending()) {
comparator = Comparators.reverse(comparator);
}
copy.sort(comparator);
return copy;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/prop_types/is.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNil } from 'lodash';
import { isNil } from '../../services/predicate';

export const is = (expectedValue) => {

Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5299,7 +5299,7 @@ lodash.uniq@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"

lodash@^3.3.1:
lodash@^3.10.1, lodash@^3.3.1:
version "3.10.1"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"

Expand Down

0 comments on commit 1356e08

Please sign in to comment.