Skip to content

Commit

Permalink
refactor: remove lodash, add fast-deep-equal (#6496)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMagee committed Jun 26, 2020
1 parent 8302fb7 commit 350a70f
Show file tree
Hide file tree
Showing 11 changed files with 619 additions and 514 deletions.
4 changes: 2 additions & 2 deletions lib/datasource/index.ts
@@ -1,5 +1,5 @@
import is from '@sindresorhus/is';
import _ from 'lodash';
import equal from 'fast-deep-equal';
import { logger } from '../logger';
import { ExternalHostError } from '../types/errors/external-host-error';
import * as memCache from '../util/cache/memory';
Expand Down Expand Up @@ -201,7 +201,7 @@ async function fetchReleases(
}
logError(datasource.id, config.lookupName, err);
}
if (!dep || _.isEqual(dep, { releases: [] })) {
if (!dep || equal(dep, { releases: [] })) {
return null;
}
addMetaData(dep, datasourceName, config.lookupName);
Expand Down
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
29 changes: 29 additions & 0 deletions lib/util/index.spec.ts
@@ -0,0 +1,29 @@
import { getName } from '../../test/util';
import { sampleSize } from '.';

describe(getName(__filename), () => {
describe('sampleSize', () => {
const array = ['a', 'b', 'c', 'd'];
it('returns correct sized array', () => {
expect(sampleSize(array, 2)).toHaveLength(2);
});
it('returns full array for undefined number', () => {
expect(sampleSize(array, undefined)).toEqual(array);
});
it('returns full array for null number', () => {
expect(sampleSize(array, null)).toEqual([]);
});
it('returns full array for 0 number', () => {
expect(sampleSize(array, 0)).toEqual([]);
});
it('returns empty array for null array', () => {
expect(sampleSize(null, 1)).toEqual([]);
});
it('returns empty array for undefined array', () => {
expect(sampleSize(undefined, 1)).toEqual([]);
});
it('returns empty array for empty array', () => {
expect(sampleSize([], 1)).toEqual([]);
});
});
});
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);
}
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 @@ -343,8 +342,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_INTEGRATION_UNAUTHORIZED,
Expand All @@ -10,6 +8,7 @@ import { logger } from '../../logger';
import { PlatformPrOptions, Pr, platform } from '../../platform';
import { BranchStatus } from '../../types';
import { ExternalHostError } from '../../types/errors/external-host-error';
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
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): string {
return input
.replace(/(?:^\w|[A-Z]|\b\w)/g, (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

0 comments on commit 350a70f

Please sign in to comment.