Skip to content

Commit

Permalink
refactor: remove lodash
Browse files Browse the repository at this point in the history
With the help of [You don't need lodash](https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore). Exceptions are:

- Replaced _.isEqual with [fast-deep-equal](https://github.com/epoberezkin/fast-deep-equal)
- Reimplemented sampleSize
- Reimplemented camelCase

Done as part of my investigation into jest memory leakage. Unfortunately it's brought in via a lot of our dependencies, but it helps a bit
  • Loading branch information
JamieMagee committed Jun 12, 2020
1 parent 1c92405 commit 6ab8ef9
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 18 deletions.
6 changes: 3 additions & 3 deletions lib/manager/npm/update.ts
@@ -1,4 +1,4 @@
import { isEqual } from 'lodash';
import equal from 'fast-deep-equal';
import { ReleaseType, inc } from 'semver';
import { logger } from '../../logger';
import { matchAt, replaceAt } from '../../util/string';
Expand Down Expand Up @@ -117,7 +117,7 @@ export function updateDependency({
newString
);
// Compare the parsed JSON structure of old and new
if (isEqual(parsedContents, JSON.parse(testContent))) {
if (equal(parsedContents, JSON.parse(testContent))) {
newFileContent = testContent;
break;
}
Expand Down Expand Up @@ -172,7 +172,7 @@ export function updateDependency({
newResolution
);
// Compare the parsed JSON structure of old and new
if (isEqual(parsedContents, JSON.parse(testContent))) {
if (equal(parsedContents, JSON.parse(testContent))) {
newFileContent = testContent;
break;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/manager/travis/package.ts
@@ -1,5 +1,5 @@
import is from '@sindresorhus/is';
import { isEqual } from 'lodash';
import equal from 'fast-deep-equal';
import { getPkgReleases } from '../../datasource';
import * as datasourceGithubTags from '../../datasource/github-tags';
import { logger } from '../../logger';
Expand Down Expand Up @@ -124,7 +124,7 @@ export async function getPackageUpdates(

// TODO: `config.currentValue` is a string!
(config.currentValue as any).sort((a, b) => a - b);
if (isEqual(config.currentValue, newValue)) {
if (equal(config.currentValue, newValue)) {
return [];
}
return [
Expand Down
3 changes: 1 addition & 2 deletions lib/versioning/ruby/version.ts
@@ -1,6 +1,5 @@
import { diff, major, minor, patch, prerelease } from '@snyk/ruby-semver';
import { create } from '@snyk/ruby-semver/lib/ruby/gem-version';
import last from 'lodash/last';

interface RubyVersion {
major: number;
Expand All @@ -25,7 +24,7 @@ const floor = (version: string): string =>
// istanbul ignore next
const incrementLastSegment = (version: string): string => {
const segments = create(version).release().getSegments();
const nextLast = parseInt(last(segments), 10) + 1;
const nextLast = parseInt(segments[segments.length - 1], 10) + 1;

return [...segments.slice(0, -1), nextLast].join('.');
};
Expand Down
4 changes: 1 addition & 3 deletions lib/workers/branch/index.ts
@@ -1,5 +1,4 @@
import is from '@sindresorhus/is';
import { concat } from 'lodash';
import { DateTime } from 'luxon';
import minimatch from 'minimatch';
import { RenovateConfig } from '../../config';
Expand Down Expand Up @@ -344,8 +343,7 @@ export async function processBranch(

if (is.nonEmptyArray(commands)) {
// Persist updated files in file system so any executed commands can see them
for (const file of concat(
config.updatedPackageFiles,
for (const file of config.updatedPackageFiles.concat(
config.updatedArtifacts
)) {
if (file.name !== '|delete|') {
Expand Down
22 changes: 18 additions & 4 deletions lib/workers/pr/index.ts
@@ -1,5 +1,3 @@
import sampleSize from 'lodash/sampleSize';
import uniq from 'lodash/uniq';
import { RenovateConfig } from '../../config/common';
import {
PLATFORM_FAILURE,
Expand Down Expand Up @@ -27,7 +25,7 @@ async function addCodeOwners(
assigneesOrReviewers: string[],
pr: Pr
): Promise<string[]> {
return uniq(assigneesOrReviewers.concat(await codeOwnersForPr(pr)));
return [...new Set(assigneesOrReviewers.concat(await codeOwnersForPr(pr)))];
}

export async function addAssigneesReviewers(
Expand Down Expand Up @@ -69,7 +67,7 @@ export async function addAssigneesReviewers(
const additionalReviewers = config.additionalReviewers.map(
noLeadingAtSymbol
);
reviewers = uniq(reviewers.concat(additionalReviewers));
reviewers = [...new Set(reviewers.concat(additionalReviewers))];
}
if (config.reviewersSampleSize !== null) {
reviewers = sampleSize(reviewers, config.reviewersSampleSize);
Expand Down Expand Up @@ -524,3 +522,19 @@ export async function checkAutoMerge(
logger.debug('No automerge');
return false;
}

function sampleSize(array: string[], n: number) {
const length = array == null ? 0 : array.length;
if (!length || n < 1) {
return [];
}
n = n > length ? length : n;
let index = -1;
const lastIndex = length - 1;
const result = [...array];
while (++index < n) {
const rand = index + Math.floor(Math.random() * (lastIndex - index + 1));
[result[rand], result[index]] = [result[index], result[rand]];
}
return result.slice(0, n);
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -124,6 +124,7 @@
"delay": "4.3.0",
"detect-indent": "6.0.0",
"email-addresses": "3.1.0",
"fast-deep-equal": "3.1.3",
"fast-safe-stringify": "2.0.7",
"find-up": "4.1.0",
"fs-extra": "9.0.1",
Expand All @@ -141,7 +142,6 @@
"json5": "2.1.3",
"later": "1.2.0",
"linkify-markdown": "1.0.0",
"lodash": "4.17.15",
"luxon": "1.24.1",
"markdown-it": "10.0.0",
"markdown-table": "1.1.3",
Expand Down
11 changes: 9 additions & 2 deletions tools/generate-imports.ts
@@ -1,5 +1,4 @@
import fs from 'fs-extra';
import _ from 'lodash';
import shell from 'shelljs';

shell.echo('generating imports');
Expand All @@ -26,6 +25,14 @@ async function updateFile(file: string, code: string): Promise<void> {
newFiles.add(file);
}

function camelCase(input: string) {
return input
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (char, index) {
return index === 0 ? char.toLowerCase() : char.toUpperCase();
})
.replace(/-/g, '');
}

async function generate({
path,
types,
Expand All @@ -43,7 +50,7 @@ async function generate({
for (const ds of findModules(`lib/${path}`).filter(
(n) => !excludes?.includes(n)
)) {
const name = _.camelCase(ds);
const name = camelCase(ds);
imports += `import * as ${name} from './${ds}';\n`;
maps += `api.set('${ds}', ${name}${map});\n`;
}
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Expand Up @@ -3991,6 +3991,11 @@ extsprintf@^1.2.0:
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.4.0.tgz#e2689f8f356fad62cca65a3a91c5df5f9551692f"
integrity sha1-4mifjzVvrWLMplo6kcXfX5VRaS8=

fast-deep-equal@3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==

fast-deep-equal@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.1.tgz#545145077c501491e33b15ec408c294376e94ae4"
Expand Down Expand Up @@ -6376,7 +6381,7 @@ lodash.without@~4.4.0:
resolved "https://registry.yarnpkg.com/lodash.without/-/lodash.without-4.4.0.tgz#3cd4574a00b67bae373a94b748772640507b7aac"
integrity sha1-PNRXSgC2e643OpS3SHcmQFB7eqw=

lodash@4.17.15, lodash@^4.17.11, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.4:
lodash@^4.17.11, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.4:
version "4.17.15"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==
Expand Down

0 comments on commit 6ab8ef9

Please sign in to comment.