-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
486ba13
commit 9650354
Showing
3 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |