diff --git a/lib/util/index.ts b/lib/util/index.ts index 017f4fa8fa7445..58b1c8631c6788 100644 --- a/lib/util/index.ts +++ b/lib/util/index.ts @@ -23,3 +23,21 @@ export async function resolveFile(file: string): Promise { } 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); +} diff --git a/lib/util/string.ts b/lib/util/string.ts index 2cad78d692bc9c..a52501fb99c7de 100644 --- a/lib/util/string.ts +++ b/lib/util/string.ts @@ -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, ''); +} diff --git a/lib/versioning/ruby/version.ts b/lib/versioning/ruby/version.ts index 4a5a1445260aca..7999090829b17b 100644 --- a/lib/versioning/ruby/version.ts +++ b/lib/versioning/ruby/version.ts @@ -24,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(segments[segments.length - 1], 10) + 1; + const nextLast = parseInt(segments.pop(), 10) + 1; - return [...segments.slice(0, -1), nextLast].join('.'); + return [...segments, nextLast].join('.'); }; // istanbul ignore next diff --git a/lib/workers/pr/index.ts b/lib/workers/pr/index.ts index 98f19864bc9a0c..31e9ebd8c7f390 100644 --- a/lib/workers/pr/index.ts +++ b/lib/workers/pr/index.ts @@ -8,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'; @@ -522,19 +523,3 @@ 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/test/globals.ts b/test/globals.ts index d8c3db8ff91a22..bb1baa4077aa02 100644 --- a/test/globals.ts +++ b/test/globals.ts @@ -11,3 +11,7 @@ jest.mock('../lib/logger'); beforeAll(() => { nock.disableNetConnect(); }); + +afterAll(() => { + global?.gc(); +}); diff --git a/tools/generate-imports.ts b/tools/generate-imports.ts index 60f612cdb70b6d..36598390dd06cb 100644 --- a/tools/generate-imports.ts +++ b/tools/generate-imports.ts @@ -1,6 +1,8 @@ import fs from 'fs-extra'; import shell from 'shelljs'; +import { camelCase } from '../lib/util/string'; + shell.echo('generating imports'); const newFiles = new Set(); @@ -25,14 +27,6 @@ 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,