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

refactor(lodash): has #2434

Merged
merged 3 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ describe('translatable', () => {
const defaultTranslations = {
sup: 'hey',
thing: n => `${n} things`,
fallbackThing: 'hi',
};
const translations = {
sup: 'hoy',
fallbackThing: undefined,
};
const Translated = translatable(defaultTranslations)(Dummy);
const { translate } = shallow(<Translated translations={translations} />)
.find(Dummy)
.props();
expect(translate('sup')).toBe('hoy');
expect(translate('thing', 20)).toBe('20 things');
expect(translate('fallbackThing')).toBe(undefined);
});
});
3 changes: 1 addition & 2 deletions packages/react-instantsearch-core/src/core/translatable.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { Component } from 'react';
import { has } from 'lodash';

const withKeysPropType = keys => (props, propName, componentName) => {
const prop = props[propName];
Expand All @@ -23,7 +22,7 @@ export default function translatable(defaultTranslations) {
const { translations } = this.props;

const translation =
translations && has(translations, key)
translations && translations.hasOwnProperty(key)
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
? translations[key]
: defaultTranslations[key];

Expand Down
7 changes: 3 additions & 4 deletions packages/react-instantsearch-dom/src/components/LinkList.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { has } from 'lodash';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Link from './Link';
Expand Down Expand Up @@ -35,7 +34,7 @@ export default class LinkList extends Component {
<ul className={cx('list', !canRefine && 'list--noRefinement')}>
{items.map(item => (
<li
key={has(item, 'key') ? item.key : item.value}
key={item.key === undefined ? item.value : item.key}
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
className={cx(
'item',
item.selected && !item.disabled && 'item--selected',
Expand All @@ -45,7 +44,7 @@ export default class LinkList extends Component {
>
{item.disabled ? (
<span className={cx('link')}>
{has(item, 'label') ? item.label : item.value}
{item.label === undefined ? item.value : item.label}
Copy link
Collaborator

Choose a reason for hiding this comment

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

IMO the === undefined is overkill. We can stick with a Boolean check.

</span>
) : (
<Link
Expand All @@ -54,7 +53,7 @@ export default class LinkList extends Component {
href={createURL(item.value)}
onClick={() => onSelect(item.value)}
>
{has(item, 'label') ? item.label : item.value}
{item.label === undefined ? item.value : item.label}
</Link>
)}
</li>
Expand Down
5 changes: 2 additions & 3 deletions packages/react-instantsearch-dom/src/components/Select.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { has } from 'lodash';
import React, { Component } from 'react';
import PropTypes from 'prop-types';

Expand Down Expand Up @@ -36,11 +35,11 @@ export default class Select extends Component {
{items.map(item => (
<option
className={cx('option')}
key={has(item, 'key') ? item.key : item.value}
key={item.key === undefined ? item.value : item.key}
disabled={item.disabled}
value={item.value}
>
{has(item, 'label') ? item.label : item.value}
{item.label === undefined ? item.value : item.label}
</option>
))}
</select>
Expand Down