From c9ce79ef8042327c4dda775a9befb3d5005361fd Mon Sep 17 00:00:00 2001 From: Jamie Magee Date: Fri, 12 Jun 2020 12:14:00 +0200 Subject: [PATCH] refactor: remove lodash 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 --- lib/manager/npm/update.ts | 6 +++--- lib/manager/travis/package.ts | 4 ++-- lib/versioning/ruby/version.ts | 3 +-- lib/workers/branch/index.ts | 4 +--- lib/workers/pr/index.ts | 22 ++++++++++++++++++---- package.json | 2 +- tools/generate-imports.ts | 11 +++++++++-- yarn.lock | 7 ++++++- 8 files changed, 41 insertions(+), 18 deletions(-) diff --git a/lib/manager/npm/update.ts b/lib/manager/npm/update.ts index b4c824958fe14d..9d81890e3400fc 100644 --- a/lib/manager/npm/update.ts +++ b/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'; @@ -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; } @@ -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; } diff --git a/lib/manager/travis/package.ts b/lib/manager/travis/package.ts index 76952d263cf946..f39a3eb05a24a3 100644 --- a/lib/manager/travis/package.ts +++ b/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'; @@ -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 [ diff --git a/lib/versioning/ruby/version.ts b/lib/versioning/ruby/version.ts index 1beb2b916bd11a..4a5a1445260aca 100644 --- a/lib/versioning/ruby/version.ts +++ b/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; @@ -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('.'); }; diff --git a/lib/workers/branch/index.ts b/lib/workers/branch/index.ts index 31963c57719a7c..cf6a333a7ad668 100644 --- a/lib/workers/branch/index.ts +++ b/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'; @@ -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|') { diff --git a/lib/workers/pr/index.ts b/lib/workers/pr/index.ts index 9fc7e586e968ca..98f19864bc9a0c 100644 --- a/lib/workers/pr/index.ts +++ b/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, @@ -27,7 +25,7 @@ async function addCodeOwners( assigneesOrReviewers: string[], pr: Pr ): Promise { - return uniq(assigneesOrReviewers.concat(await codeOwnersForPr(pr))); + return [...new Set(assigneesOrReviewers.concat(await codeOwnersForPr(pr)))]; } export async function addAssigneesReviewers( @@ -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); @@ -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); +} diff --git a/package.json b/package.json index 7646f555066ab3..f6eca35ad58a8a 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/tools/generate-imports.ts b/tools/generate-imports.ts index d1fed87ddba63a..60f612cdb70b6d 100644 --- a/tools/generate-imports.ts +++ b/tools/generate-imports.ts @@ -1,5 +1,4 @@ import fs from 'fs-extra'; -import _ from 'lodash'; import shell from 'shelljs'; shell.echo('generating imports'); @@ -26,6 +25,14 @@ async function updateFile(file: string, code: string): Promise { 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, @@ -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`; } diff --git a/yarn.lock b/yarn.lock index acfb3d135c502e..4ab321dd0ea164 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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==