Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Extract uniq utility function #22968

Merged
merged 2 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions lib/modules/datasource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as memCache from '../../util/cache/memory';
import * as packageCache from '../../util/cache/package';
import { clone } from '../../util/clone';
import { regEx } from '../../util/regex';
import { uniq } from '../../util/uniq';
import { trimTrailingSlash } from '../../util/url';
import * as allVersioning from '../versioning';
import datasources from './api';
Expand Down Expand Up @@ -394,12 +395,8 @@ export async function getPkgReleases(
.sort((a, b) => version.sortVersions(a.version, b.version));

// Filter versions for uniqueness
res.releases = res.releases.filter(
(filterRelease, filterIndex) =>
res.releases.findIndex(
(findRelease) => findRelease.version === filterRelease.version
) === filterIndex
);
res.releases = uniq(res.releases, (x, y) => x.version === y.version);

if (config?.constraintsFiltering === 'strict') {
// Filter releases for compatibility
for (const [constraintName, constraintValue] of Object.entries(
Expand Down
8 changes: 1 addition & 7 deletions lib/util/exec/docker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { logger } from '../../../logger';
import { getPkgReleases } from '../../../modules/datasource';
import * as versioning from '../../../modules/versioning';
import { newlineRegex, regEx } from '../../regex';
import { uniq } from '../../uniq';
import { ensureTrailingSlash } from '../../url';
import { rawExec } from '../common';
import type { DockerOptions, Opt, VolumeOption, VolumesPair } from '../types';
Expand Down Expand Up @@ -58,13 +59,6 @@ function volumesEql(x: VolumesPair, y: VolumesPair): boolean {
return xFrom === yFrom && xTo === yTo;
}

function uniq<T = unknown>(
array: T[],
eql = (x: T, y: T): boolean => x === y
): T[] {
return array.filter((x, idx, arr) => arr.findIndex((y) => eql(x, y)) === idx);
}

function prepareVolumes(volumes: VolumeOption[] = []): string[] {
const expanded: (VolumesPair | null)[] = volumes.map(expandVolumeOption);
const filtered: VolumesPair[] = expanded.filter(
Expand Down
17 changes: 17 additions & 0 deletions lib/util/uniq.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { uniq } from './uniq';

describe('util/uniq', () => {
it('should return an array with unique elements', () => {
const input = [1, 2, 3, 2, 1, 4];
const expectedOutput = [1, 2, 3, 4];
expect(uniq(input)).toEqual(expectedOutput);
});

it('should use the provided equality function to compare elements', () => {
type T = { id: number };
const input: T[] = [{ id: 1 }, { id: 2 }, { id: 1 }];
const expectedOutput = [{ id: 1 }, { id: 2 }];
const eql = (x: T, y: T) => x.id === y.id;
expect(uniq(input, eql)).toEqual(expectedOutput);
});
});
6 changes: 6 additions & 0 deletions lib/util/uniq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function uniq<T = unknown>(
array: T[],
eql = (x: T, y: T): boolean => x === y
): T[] {
return array.filter((x, idx, arr) => arr.findIndex((y) => eql(x, y)) === idx);
}