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

refactor(lodash): isEmpty #2442

Merged
merged 10 commits into from
May 22, 2019
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 && refinedItem.length > 0,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
isRefined: refinedItem && refinedItem.length > 0,
isRefined: refinedItem && refinedItem.length === 0,

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 @@ -85,3 +83,7 @@ export function find<T = any>(
}
return undefined;
}

export function objectHasKeys(object: object | undefined) {
return object && Object.keys(object).length > 0;
}
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 = algoliasearch => {

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

if (isEmpty(derivedParameters)) {
if (Object.keys(derivedParameters).length === 0) {
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
return singleIndexSearch(helper, sharedParameters);
}

Expand Down