Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
refactor(lodash): isEmpty (#2442)
Browse files Browse the repository at this point in the history
* refactor(lodash): isEmpty

reusing objectHasKeys strategy from the helper

IFW-736

* refactor(lodash): remove isEmpty from server

* chore(TS): clearer type

* feedback: don't use objectHasKeys for array

* fix implementation

* refactor(example): remove lodash

* fix(numeric): correct reimplementation of !isEmpty

* chore(size): update expectation

somewhat bigger for now, but in the long run it'll shrink
  • Loading branch information
Haroenv committed Jun 27, 2019
1 parent 527b879 commit a402d29
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions examples/react-native/src/Rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
connectMenu,
} from 'react-instantsearch/connectors';
import Stats from './components/Stats';
import { isEmpty } from 'lodash';

const searchClient = algoliasearch(
'latency',
Expand Down Expand Up @@ -151,7 +150,8 @@ class Rating extends Component {
const { refine, min, max, count, createURL } = this.props;
const items = [];
for (let i = max; i >= min; i--) {
const hasCount = !isEmpty(count.filter(item => Number(item.value) === i));
const hasCount =
count.filter(item => Number(item.value) === i).length > 0;
const lastSelectableItem = count.reduce(
(acc, item) =>
item.value < acc.value || (!acc.value && hasCount) ? item : acc,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEmpty } from 'lodash';
import { objectHasKeys } from '../core/utils';
import createConnector from '../core/createConnector';
import {
getResults,
Expand Down Expand Up @@ -73,7 +73,7 @@ const getCurrentRefinement = (props, searchState, context) => {
{}
);

if (isEmpty(refinement)) {
if (!objectHasKeys(refinement)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isEmpty } from 'lodash';
import PropTypes from 'prop-types';
import createConnector from '../core/createConnector';
import { find } from '../core/utils';
Expand Down Expand Up @@ -157,7 +156,7 @@ export default createConnector({
if (!items.some(item => item.value === '')) {
items.push({
value: '',
isRefined: isEmpty(refinedItem),
isRefined: refinedItem === undefined,
noRefinement: !stats,
label: 'All',
});
Expand Down
8 changes: 5 additions & 3 deletions packages/react-instantsearch-core/src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { isEmpty } from 'lodash';

// From https://github.com/reactjs/react-redux/blob/master/src/utils/shallowEqual.js
export const shallowEqual = (objA, objB) => {
if (objA === objB) {
Expand Down Expand Up @@ -43,7 +41,7 @@ export const removeEmptyKey = (obj: object) => {
return;
}

if (isEmpty(value)) {
if (!objectHasKeys(value)) {
delete obj[key];
} else {
removeEmptyKey(value);
Expand Down Expand Up @@ -86,6 +84,10 @@ export function find<T = any>(
return undefined;
}

export function objectHasKeys(object: object | undefined) {
return object && Object.keys(object).length > 0;
}

// https://github.com/babel/babel/blob/3aaafae053fa75febb3aa45d45b6f00646e30ba4/packages/babel-helpers/src/helpers.js#L604-L620
export function omit(source: { [key: string]: any }, excluded: string[]) {
if (source === null || source === undefined) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isEmpty } from 'lodash';
import React, { Component } from 'react';
import { renderToString } from 'react-dom/server';
import PropTypes from 'prop-types';
Expand Down Expand Up @@ -116,7 +115,7 @@ const createInstantSearchServer = () => {

const helper = algoliasearchHelper(client, sharedParameters.index);

if (isEmpty(derivedParameters)) {
if (Object.keys(derivedParameters).length === 0) {
return singleIndexSearch(helper, sharedParameters);
}

Expand Down

0 comments on commit a402d29

Please sign in to comment.