Skip to content

Commit

Permalink
Improved duplicate detection
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Aug 28, 2023
1 parent 486ba13 commit 9650354
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/tooling/piral-cli/src/common/pack.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { tmpdir } from 'os';
import { resolve, relative, join, dirname, basename } from 'path';
import { createTgz } from './archive';
import { onlyUnique } from './utils';
import { onlyUniqueFiles } from './utils';
import { log, progress, fail } from './log';
import { readJson, copy, removeDirectory, checkExists, makeTempDir, createDirectory } from './io';
import { getPossiblePiletMainPaths } from './inspect';
Expand Down Expand Up @@ -62,7 +62,7 @@ export async function createPiletPackage(baseDir: string, source: string, target

const prefix = join(tmpdir(), `${id}-`);
const cwd = await makeTempDir(prefix);
const uniqueFiles = files.filter(onlyUnique);
const uniqueFiles = files.filter(onlyUniqueFiles);
log('generalDebug_0003', `Creating package with content from "${content}" ...`);

await Promise.all(uniqueFiles.map((file) => copy(file, resolve(cwd, relative(root, file)))));
Expand Down
24 changes: 24 additions & 0 deletions src/tooling/piral-cli/src/common/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { onlyUnique, onlyUniqueFiles } from './utils';

describe('only unique', () => {
it('should filter out duplicates', () => {
const arr = [1, 2, 3, 4, 3];
const res = arr.filter(onlyUnique);
expect(res).toEqual([1, 2, 3, 4]);
});

it('should filter out duplicated files', () => {
const res = ['foo/bar', 'bar/foo', 'foo/bar'].filter(onlyUniqueFiles);
expect(res).toEqual(['foo/bar', 'bar/foo']);
});

it('should filter out child directories from front', () => {
const res = ['foo/bar/3', 'bar/foo', 'foo/bar'].filter(onlyUniqueFiles);
expect(res).toEqual(['bar/foo', 'foo/bar']);
});

it('should filter out child directories from back', () => {
const res = ['foo', 'bar/foo', 'foo/bar'].filter(onlyUniqueFiles);
expect(res).toEqual(['foo', 'bar/foo']);
});
});
30 changes: 30 additions & 0 deletions src/tooling/piral-cli/src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
import { sep } from 'path';

export function onlyUnique<T>(value: T, index: number, self: Array<T>) {
return self.indexOf(value) === index;
}

export function onlyUniqueFiles(value: string, index: number, self: Array<string>) {
const valueDir = value + sep;

for (let i = 0; i < index; i++) {
const other = self[i];

if (other === value) {
return false;
}

const otherDir = other + sep;

if (value.startsWith(otherDir)) {
return false;
}
}

for (let i = index + 1; i < self.length; i++) {
const other = self[i];

if (other !== value && valueDir.startsWith(other)) {
return false;
}
}

return true;
}

0 comments on commit 9650354

Please sign in to comment.