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

Commit

Permalink
refactor(lodash): replace omit (#2466)
Browse files Browse the repository at this point in the history
* refactor(lodash): omit (dom)

In this case, you can simply override a prop, which will take preference, like in any other object.

* refactor(omit): use internal (babel) replacement

* refactor(omit): use destructuring replacement

* refactor(indexUtils): use new omit where possible

* refactor(indexutils): remove omit with path

* chore: fix merge
  • Loading branch information
Haroenv authored May 21, 2019
1 parent 9bc2353 commit 3f7358a
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { omit } from 'lodash';
import { omit } from '../core/utils';
import createConnector from '../core/createConnector';
import {
refineValue,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { omit } from 'lodash';
import PropTypes from 'prop-types';
import createConnector from '../core/createConnector';
import {
getCurrentRefinementValue,
hasMultipleIndices,
getIndexId,
} from '../core/indexUtils';
import { shallowEqual } from '../core/utils';
import { shallowEqual, omit } from '../core/utils';

/**
* connectScrollTo connector provides the logic to build a widget that will
Expand Down Expand Up @@ -67,10 +66,10 @@ export default createConnector({
// using ScrollTo in combination of Pagination. As pagination can be change
// by every widget, we want to scroll only if it cames from the pagination
// widget itself. We also remove the configure key from the search state to
// do this comparaison because for now configure values are not present in the
// do this comparison because for now configure values are not present in the
// search state before a first refinement has been made and will false the results.
// See: https://github.com/algolia/react-instantsearch/issues/164
const cleanedSearchState = omit(omit(searchState, 'configure'), id);
const cleanedSearchState = omit(searchState, ['configure', id]);

const hasNotChanged = shallowEqual(
this._prevSearchState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ describe('utility method for manipulating the search state', () => {
namespace: {},
});
});

it('get results', () => {
const searchResults = { results: { hits: ['some'] } };

Expand All @@ -235,6 +236,7 @@ describe('utility method for manipulating the search state', () => {
expect(results).toEqual({ hits: ['some'] });
});
});

describe('when there are multiple index', () => {
let context = { multiIndexContext: { targetedIndex: 'first' } };
it('refine with no namespace', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { omit } from 'lodash';
import algoliasearchHelper from 'algoliasearch-helper';
import createWidgetsManager from './createWidgetsManager';
import createStore from './createStore';
Expand Down Expand Up @@ -214,18 +213,15 @@ export default function createInstantSearchManager({
nextIsSearchStalled = false;
}

const nextState = omit(
{
...currentState,
results,
isSearchStalled: nextIsSearchStalled,
searching: false,
error: null,
},
'resultsFacetValues'
);
const { resultsFacetValues, ...partialState } = currentState;

store.setState(nextState);
store.setState({
...partialState,
results,
isSearchStalled: nextIsSearchStalled,
searching: false,
error: null,
});
};
}

Expand All @@ -238,31 +234,25 @@ export default function createInstantSearchManager({
nextIsSearchStalled = false;
}

const nextState = omit(
{
...currentState,
isSearchStalled: nextIsSearchStalled,
error,
searching: false,
},
'resultsFacetValues'
);
const { resultsFacetValues, ...partialState } = currentState;

store.setState(nextState);
store.setState({
...partialState,
isSearchStalled: nextIsSearchStalled,
error,
searching: false,
});
}

function handleNewSearch() {
if (!stalledSearchTimer) {
stalledSearchTimer = setTimeout(() => {
const nextState = omit(
{
...store.getState(),
isSearchStalled: true,
},
'resultsFacetValues'
);
const { resultsFacetValues, ...partialState } = store.getState();

store.setState(nextState);
store.setState({
...partialState,
isSearchStalled: true,
});
}, stalledSearchDelay);
}
}
Expand Down
21 changes: 16 additions & 5 deletions packages/react-instantsearch-core/src/core/indexUtils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { has, omit, get } from 'lodash';
import { has, get } from 'lodash';
import { omit } from './utils';

export function getIndexId(context) {
return hasMultipleIndices(context)
Expand Down Expand Up @@ -239,11 +240,11 @@ function cleanUpValueWithSingleIndex({
if (namespace) {
return {
...searchState,
[namespace]: omit(searchState[namespace], attribute),
[namespace]: omit(searchState[namespace], [attribute]),
};
}

return omit(searchState, id);
return omit(searchState, [id]);
}

function cleanUpValueWithMultiIndex({
Expand All @@ -262,11 +263,21 @@ function cleanUpValueWithMultiIndex({
...searchState.indices,
[indexId]: {
...indexSearchState,
[namespace]: omit(indexSearchState[namespace], attribute),
[namespace]: omit(indexSearchState[namespace], [attribute]),
},
},
};
}

return omit(searchState, `indices.${indexId}.${id}`);
if (indexSearchState) {
return {
...searchState,
indices: {
...searchState.indices,
[indexId]: omit(indexSearchState, [id]),
},
};
}

return searchState;
}
17 changes: 17 additions & 0 deletions packages/react-instantsearch-core/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,20 @@ export function find<T = any>(
}
return undefined;
}

// 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) {
return {};
}
const target = {};
const sourceKeys = Object.keys(source);
for (let i = 0; i < sourceKeys.length; i++) {
const key = sourceKeys[i];
if (excluded.indexOf(key) >= 0) {
continue;
}
target[key] = source[key];
}
return target;
}
3 changes: 1 addition & 2 deletions packages/react-instantsearch-dom/src/components/Link.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { omit } from 'lodash';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { isSpecialClick } from '../core/utils';
Expand All @@ -17,6 +16,6 @@ export default class Link extends Component {
};

render() {
return <a {...omit(this.props, 'onClick')} onClick={this.onClick} />;
return <a {...this.props} onClick={this.onClick} />;
}
}

0 comments on commit 3f7358a

Please sign in to comment.