Skip to content

Commit

Permalink
[enzyme] [Refactor] remove most uses of lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 20, 2018
1 parent a47f2ba commit 89b39b6
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 18 deletions.
3 changes: 2 additions & 1 deletion packages/enzyme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"is-number-object": "^1.0.3",
"is-string": "^1.0.4",
"is-subset": "^0.1.1",
"lodash": "^4.17.4",
"lodash.escape": "^4.0.1",
"lodash.isequal": "^4.5.0",
"object-inspect": "^1.6.0",
"object-is": "^1.0.1",
"object.assign": "^4.1.0",
Expand Down
10 changes: 5 additions & 5 deletions packages/enzyme/src/Debug.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import without from 'lodash/without';
import escape from 'lodash/escape';
import compact from 'lodash/compact';
import escape from 'lodash.escape';
import functionName from 'function.prototype.name';
import isString from 'is-string';
import isNumber from 'is-number-object';
Expand Down Expand Up @@ -59,7 +57,7 @@ function propString(prop, options) {

function propsString(node, options) {
const props = propsOfNode(node);
const keys = without(Object.keys(props), 'children');
const keys = Object.keys(props).filter(x => x !== 'children');
return keys.map(key => `${key}=${propString(props[key], options)}`).join(' ');
}

Expand All @@ -77,7 +75,9 @@ export function debugNode(node, indentLength = 2, options = {}) {
}
if (!node) return '';

const childrenStrs = compact(childrenOfNode(node).map(n => debugNode(n, indentLength, options)));
const childrenStrs = childrenOfNode(node)
.map(n => debugNode(n, indentLength, options))
.filter(Boolean);
const type = typeName(node);

const props = options.ignoreProps ? '' : propsString(node, options);
Expand Down
6 changes: 2 additions & 4 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import cheerio from 'cheerio';
import flat from 'array.prototype.flat';
import compact from 'lodash/compact';

import {
containsChildrenSubArray,
Expand Down Expand Up @@ -57,7 +56,7 @@ function findWhereUnwrapped(wrapper, predicate, filter = treeFilter) {
* @returns {ReactWrapper}
*/
function filterWhereUnwrapped(wrapper, predicate) {
return wrapper.wrap(compact(wrapper.getNodesInternal().filter(predicate)));
return wrapper.wrap(wrapper.getNodesInternal().filter(predicate).filter(Boolean));
}

function privateSetNodes(wrapper, nodes) {
Expand Down Expand Up @@ -925,8 +924,7 @@ class ReactWrapper {
flatMap(fn) {
const nodes = this.getNodesInternal().map((n, i) => fn.call(this, this.wrap(n), i));
const flattened = flat(nodes, 1);
const compacted = compact(flattened);
return this.wrap(compacted);
return this.wrap(flattened.filter(Boolean));
}

/**
Expand Down
6 changes: 2 additions & 4 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import flat from 'array.prototype.flat';
import compact from 'lodash/compact';
import cheerio from 'cheerio';

import {
Expand Down Expand Up @@ -61,7 +60,7 @@ function findWhereUnwrapped(wrapper, predicate, filter = treeFilter) {
* @returns {ShallowWrapper}
*/
function filterWhereUnwrapped(wrapper, predicate) {
return wrapper.wrap(compact(wrapper.getNodesInternal().filter(predicate)));
return wrapper.wrap(wrapper.getNodesInternal().filter(predicate).filter(Boolean));
}

/**
Expand Down Expand Up @@ -1129,8 +1128,7 @@ class ShallowWrapper {
flatMap(fn) {
const nodes = this.getNodesInternal().map((n, i) => fn.call(this, this.wrap(n), i));
const flattened = flat(nodes, 1);
const compacted = compact(flattened);
return this.wrap(compacted);
return this.wrap(flattened.filter(Boolean));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/Utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint no-use-before-define: 0 */
import isEqual from 'lodash/isEqual';
import isEqual from 'lodash.isequal';
import is from 'object-is';
import entries from 'object.entries';
import functionName from 'function.prototype.name';
Expand Down
8 changes: 5 additions & 3 deletions packages/enzyme/src/selectors.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { createParser } from 'rst-selector-parser';
import values from 'object.values';
import isEmpty from 'lodash/isEmpty';
import flat from 'array.prototype.flat';
import unique from 'lodash/uniq';
import is from 'object-is';
import has from 'has';
import {
Expand Down Expand Up @@ -42,6 +40,10 @@ const PREFIX_ATTRIBUTE_OPERATOR = '^=';
const SUFFIX_ATTRIBUTE_OPERATOR = '$=';
const SUBSTRING_ATTRIBUTE_OPERATOR = '*=';

function unique(arr) {
return [...new Set(arr)];
}

/**
* Calls reduce on a array of nodes with the passed
* function, returning only unique results.
Expand Down Expand Up @@ -260,7 +262,7 @@ export function buildPredicate(selector) {
}
// If the selector is an non-empty object, treat the keys/values as props
if (typeof selector === 'object') {
if (!Array.isArray(selector) && selector !== null && !isEmpty(selector)) {
if (!Array.isArray(selector) && selector !== null && Object.keys(selector).length > 0) {
const hasUndefinedValues = values(selector).some(value => typeof value === 'undefined');
if (hasUndefinedValues) {
throw new TypeError('Enzyme::Props can’t have `undefined` values. Try using ‘findWhere()’ instead.');
Expand Down

0 comments on commit 89b39b6

Please sign in to comment.