Skip to content

Commit f4b9519

Browse files
clarkbwjasonLaster
authored andcommitted
clean out the utils/utils module using lodash utils where possible (firefox-devtools#2114)
* remove truncateStr function from utils/utils `truncateStr` was only referenced in the docs * use flow number to type endTruncateStr * remove unused zip function from utils/utils * replace toObject with lodash.fromPairs src/utils/redux/middleware/promise.js was the only other file using `toObject` * remove unused mapObject function from utils/utils * replace throttle with lodash/throttle * optimize lodash usage to not pull in entire library * use toPairs in middleware/promise * use toPairs in utils/parser and drop entries function utils/utils * remove unused compose function from utils/utils * upgrade to latest lodash 4.17.4 * Revert "replace throttle with lodash/throttle" This reverts commit e23d2e7.
1 parent 2766b2b commit f4b9519

7 files changed

Lines changed: 10 additions & 113 deletions

File tree

bin/getConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const merge = require("lodash").merge;
1+
const merge = require("lodash/merge");
22
const fs = require("fs");
33
const path = require("path");
44

docs/local-development.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,6 @@ index 8c79f4d..6893673 100644
342342
+const Svg = require("./shared/Svg");
343343
const { getSource, getPause, getBreakpoints } = require("../selectors");
344344
const { makeLocationId } = require("../reducers/breakpoints");
345-
const { truncateStr } = require("../utils/utils");
346345
@@ -89,6 +90,7 @@ const Breakpoints = React.createClass({
347346
key: locationId,
348347
onClick: () => this.selectBreakpoint(breakpoint)

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"fuzzaldrin-plus": "^0.4.1",
5656
"glob": "^7.0.3",
5757
"husky": "^0.13.1",
58-
"lodash": "^4.13.1",
58+
"lodash": "^4.17.4",
5959
"md5": "^2.2.1",
6060
"mocha": "^3.1.2",
6161
"mocha-circleci-reporter": "0.0.2",
@@ -89,6 +89,5 @@
8989
"remark-lint": "^6.0.0",
9090
"remark-preset-lint-recommended": "^1.0.0",
9191
"remark-validate-links": "^6.0.0"
92-
9392
}
9493
}

src/utils/parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const babylon = require("babylon");
44
const traverse = require("babel-traverse").default;
55
const t = require("babel-types");
66
const { isDevelopment } = require("devtools-config");
7-
const { entries } = require("./utils");
7+
const toPairs = require("lodash/toPairs");
88
const get = require("lodash/get");
99

1010
import type { SourceText, Source, Location } from "../types";
@@ -111,7 +111,7 @@ function getVariablesInScope(source: Source, location: Location) {
111111
const path = getPathClosestToLocation(source, location);
112112
const bindings = get(path, "scope.bindings", {});
113113

114-
return entries(bindings)
114+
return toPairs(bindings)
115115
.map(([name, binding]) => ({
116116
name,
117117
references: binding.referencePaths

src/utils/redux/middleware/promise.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
66

77
const defer = require("../../defer");
8-
const { entries, toObject } = require("../../utils");
8+
const toPairs = require("lodash/toPairs");
9+
const fromPairs = require("lodash/fromPairs");
910
const { executeSoon } = require("../../DevToolsUtils");
1011

1112
import type { ThunkArgs } from "../../../actions/types";
@@ -18,8 +19,8 @@ function seqIdGen() {
1819
}
1920

2021
function filterAction(action: Object): Object {
21-
return toObject(
22-
entries(action).filter(pair => pair[0] !== PROMISE)
22+
return fromPairs(
23+
toPairs(action).filter(pair => pair[0] !== PROMISE)
2324
);
2425
}
2526

src/utils/utils.js

Lines changed: 1 addition & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,7 @@ function promisify(context: any, method: any, ...args: any) {
3939
* @memberof utils/utils
4040
* @static
4141
*/
42-
function truncateStr(str: any, size: any) {
43-
if (str.length > size) {
44-
return `${str.slice(0, size)}...`;
45-
}
46-
return str;
47-
}
48-
49-
/**
50-
* @memberof utils/utils
51-
* @static
52-
*/
53-
function endTruncateStr(str: any, size: any) {
42+
function endTruncateStr(str: any, size: number) {
5443
if (str.length > size) {
5544
return `...${str.slice(str.length - size)}`;
5645
}
@@ -86,91 +75,6 @@ function workerTask(worker: any, method: string) {
8675
};
8776
}
8877

89-
/**
90-
* Interleaves two arrays element by element, returning the combined array, like
91-
* a zip. In the case of arrays with different sizes, undefined values will be
92-
* interleaved at the end along with the extra values of the larger array.
93-
*
94-
* @param Array a
95-
* @param Array b
96-
* @returns Array
97-
* The combined array, in the form [a1, b1, a2, b2, ...]
98-
* @memberof utils/utils
99-
* @static
100-
*/
101-
function zip(a: any, b: any) {
102-
if (!b) {
103-
return a;
104-
}
105-
if (!a) {
106-
return b;
107-
}
108-
const pairs = [];
109-
for (let i = 0, aLength = a.length, bLength = b.length;
110-
i < aLength || i < bLength;
111-
i++) {
112-
pairs.push([a[i], b[i]]);
113-
}
114-
return pairs;
115-
}
116-
117-
/**
118-
* Converts an object into an array with 2-element arrays as key/value
119-
* pairs of the object. `{ foo: 1, bar: 2}` would become
120-
* `[[foo, 1], [bar 2]]` (order not guaranteed);
121-
*
122-
* @returns array
123-
* @memberof utils/utils
124-
* @static
125-
*/
126-
function entries(obj: any) {
127-
return Object.keys(obj).map(k => [k, obj[k]]);
128-
}
129-
130-
/**
131-
* @memberof utils/utils
132-
* @static
133-
*/
134-
function mapObject(obj: any, iteratee: any) {
135-
return toObject(entries(obj).map(([key, value]) => {
136-
return [key, iteratee(key, value)];
137-
}));
138-
}
139-
140-
/**
141-
* Takes an array of 2-element arrays as key/values pairs and
142-
* constructs an object using them.
143-
* @memberof utils/utils
144-
* @static
145-
*/
146-
function toObject(arr: any) {
147-
const obj = {};
148-
for (let pair of arr) {
149-
obj[pair[0]] = pair[1];
150-
}
151-
return obj;
152-
}
153-
154-
/**
155-
* Composes the given functions into a single function, which will
156-
* apply the results of each function right-to-left, starting with
157-
* applying the given arguments to the right-most function.
158-
* `compose(foo, bar, baz)` === `args => foo(bar(baz(args)`
159-
*
160-
* @param ...function funcs
161-
* @returns function
162-
* @memberof utils/utils
163-
* @static
164-
*/
165-
function compose(...funcs: any) {
166-
return (...args: any) => {
167-
const initialValue = funcs[funcs.length - 1].apply(null, args);
168-
const leftFuncs = funcs.slice(0, -1);
169-
return leftFuncs.reduceRight((composed, f) => f(composed),
170-
initialValue);
171-
};
172-
}
173-
17478
/**
17579
* @memberof utils/utils
17680
* @static
@@ -203,14 +107,8 @@ function waitForMs(ms: number) {
203107
module.exports = {
204108
handleError,
205109
promisify,
206-
truncateStr,
207110
endTruncateStr,
208111
workerTask,
209-
zip,
210-
entries,
211-
toObject,
212-
mapObject,
213-
compose,
214112
updateObj,
215113
throttle,
216114
waitForMs

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3686,7 +3686,7 @@ lodash.uniq@^4.3.0:
36863686
version "4.5.0"
36873687
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
36883688

3689-
lodash@^4.0.0, lodash@^4.1.0, lodash@^4.11.1, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.2, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0, lodash@^4.8.0, lodash@~4.17.2:
3689+
lodash@^4.0.0, lodash@^4.1.0, lodash@^4.11.1, lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.2, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0, lodash@^4.8.0, lodash@~4.17.2:
36903690
version "4.17.4"
36913691
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
36923692

0 commit comments

Comments
 (0)