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 18, 2020
1 parent bd98334 commit 64af4ca
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 144 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
18 changes: 18 additions & 0 deletions lib/util/index.ts
Expand Up @@ -23,3 +23,21 @@ export async function resolveFile(file: string): Promise<string> {
}
return join(pkg, '../', file);
}

export function sampleSize(array: string[], n: number): string[] {
const length = array == null ? 0 : array.length;
if (!length || n < 1) {
return [];
}
// eslint-disable-next-line no-param-reassign
n = n > length ? length : n;
let index = 0;
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]];
index += 1;
}
return result.slice(0, n);
}
8 changes: 8 additions & 0 deletions lib/util/string.ts
Expand Up @@ -23,3 +23,11 @@ export function replaceAt(
content.substr(index + oldString.length)
);
}

export function camelCase(input: string): string {
return input
.replace(/(?:^\w|[A-Z]|\b\w)/g, (char, index) => {
return index === 0 ? char.toLowerCase() : char.toUpperCase();
})
.replace(/-/g, '');
}
5 changes: 2 additions & 3 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,9 +24,9 @@ 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.pop(), 10) + 1;

return [...segments.slice(0, -1), nextLast].join('.');
return [...segments, nextLast].join('.');
};

// istanbul ignore next
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
7 changes: 3 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 All @@ -10,6 +8,7 @@ import {
import { logger } from '../../logger';
import { PlatformPrOptions, Pr, platform } from '../../platform';
import { BranchStatus } from '../../types';
import { sampleSize } from '../../util';
import { BranchConfig, PrResult } from '../common';
import { getPrBody } from './body';
import { ChangeLogError } from './changelog';
Expand All @@ -27,7 +26,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 +68,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
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
5 changes: 3 additions & 2 deletions tools/generate-imports.ts
@@ -1,7 +1,8 @@
import fs from 'fs-extra';
import _ from 'lodash';
import shell from 'shelljs';

import { camelCase } from '../lib/util/string';

shell.echo('generating imports');
const newFiles = new Set();

Expand Down Expand Up @@ -43,7 +44,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

0 comments on commit 64af4ca

Please sign in to comment.