Skip to content

Commit 89b39b6

Browse files
committed
[enzyme] [Refactor] remove most uses of lodash
1 parent a47f2ba commit 89b39b6

File tree

6 files changed

+17
-18
lines changed

6 files changed

+17
-18
lines changed

packages/enzyme/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"is-number-object": "^1.0.3",
4444
"is-string": "^1.0.4",
4545
"is-subset": "^0.1.1",
46-
"lodash": "^4.17.4",
46+
"lodash.escape": "^4.0.1",
47+
"lodash.isequal": "^4.5.0",
4748
"object-inspect": "^1.6.0",
4849
"object-is": "^1.0.1",
4950
"object.assign": "^4.1.0",

packages/enzyme/src/Debug.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import without from 'lodash/without';
2-
import escape from 'lodash/escape';
3-
import compact from 'lodash/compact';
1+
import escape from 'lodash.escape';
42
import functionName from 'function.prototype.name';
53
import isString from 'is-string';
64
import isNumber from 'is-number-object';
@@ -59,7 +57,7 @@ function propString(prop, options) {
5957

6058
function propsString(node, options) {
6159
const props = propsOfNode(node);
62-
const keys = without(Object.keys(props), 'children');
60+
const keys = Object.keys(props).filter(x => x !== 'children');
6361
return keys.map(key => `${key}=${propString(props[key], options)}`).join(' ');
6462
}
6563

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

80-
const childrenStrs = compact(childrenOfNode(node).map(n => debugNode(n, indentLength, options)));
78+
const childrenStrs = childrenOfNode(node)
79+
.map(n => debugNode(n, indentLength, options))
80+
.filter(Boolean);
8181
const type = typeName(node);
8282

8383
const props = options.ignoreProps ? '' : propsString(node, options);

packages/enzyme/src/ReactWrapper.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import cheerio from 'cheerio';
22
import flat from 'array.prototype.flat';
3-
import compact from 'lodash/compact';
43

54
import {
65
containsChildrenSubArray,
@@ -57,7 +56,7 @@ function findWhereUnwrapped(wrapper, predicate, filter = treeFilter) {
5756
* @returns {ReactWrapper}
5857
*/
5958
function filterWhereUnwrapped(wrapper, predicate) {
60-
return wrapper.wrap(compact(wrapper.getNodesInternal().filter(predicate)));
59+
return wrapper.wrap(wrapper.getNodesInternal().filter(predicate).filter(Boolean));
6160
}
6261

6362
function privateSetNodes(wrapper, nodes) {
@@ -925,8 +924,7 @@ class ReactWrapper {
925924
flatMap(fn) {
926925
const nodes = this.getNodesInternal().map((n, i) => fn.call(this, this.wrap(n), i));
927926
const flattened = flat(nodes, 1);
928-
const compacted = compact(flattened);
929-
return this.wrap(compacted);
927+
return this.wrap(flattened.filter(Boolean));
930928
}
931929

932930
/**

packages/enzyme/src/ShallowWrapper.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import flat from 'array.prototype.flat';
2-
import compact from 'lodash/compact';
32
import cheerio from 'cheerio';
43

54
import {
@@ -61,7 +60,7 @@ function findWhereUnwrapped(wrapper, predicate, filter = treeFilter) {
6160
* @returns {ShallowWrapper}
6261
*/
6362
function filterWhereUnwrapped(wrapper, predicate) {
64-
return wrapper.wrap(compact(wrapper.getNodesInternal().filter(predicate)));
63+
return wrapper.wrap(wrapper.getNodesInternal().filter(predicate).filter(Boolean));
6564
}
6665

6766
/**
@@ -1129,8 +1128,7 @@ class ShallowWrapper {
11291128
flatMap(fn) {
11301129
const nodes = this.getNodesInternal().map((n, i) => fn.call(this, this.wrap(n), i));
11311130
const flattened = flat(nodes, 1);
1132-
const compacted = compact(flattened);
1133-
return this.wrap(compacted);
1131+
return this.wrap(flattened.filter(Boolean));
11341132
}
11351133

11361134
/**

packages/enzyme/src/Utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint no-use-before-define: 0 */
2-
import isEqual from 'lodash/isEqual';
2+
import isEqual from 'lodash.isequal';
33
import is from 'object-is';
44
import entries from 'object.entries';
55
import functionName from 'function.prototype.name';

packages/enzyme/src/selectors.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { createParser } from 'rst-selector-parser';
22
import values from 'object.values';
3-
import isEmpty from 'lodash/isEmpty';
43
import flat from 'array.prototype.flat';
5-
import unique from 'lodash/uniq';
64
import is from 'object-is';
75
import has from 'has';
86
import {
@@ -42,6 +40,10 @@ const PREFIX_ATTRIBUTE_OPERATOR = '^=';
4240
const SUFFIX_ATTRIBUTE_OPERATOR = '$=';
4341
const SUBSTRING_ATTRIBUTE_OPERATOR = '*=';
4442

43+
function unique(arr) {
44+
return [...new Set(arr)];
45+
}
46+
4547
/**
4648
* Calls reduce on a array of nodes with the passed
4749
* function, returning only unique results.
@@ -260,7 +262,7 @@ export function buildPredicate(selector) {
260262
}
261263
// If the selector is an non-empty object, treat the keys/values as props
262264
if (typeof selector === 'object') {
263-
if (!Array.isArray(selector) && selector !== null && !isEmpty(selector)) {
265+
if (!Array.isArray(selector) && selector !== null && Object.keys(selector).length > 0) {
264266
const hasUndefinedValues = values(selector).some(value => typeof value === 'undefined');
265267
if (hasUndefinedValues) {
266268
throw new TypeError('Enzyme::Props can’t have `undefined` values. Try using ‘findWhere()’ instead.');

0 commit comments

Comments
 (0)