Skip to content

Commit

Permalink
chore: add more tests to unique function
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanshatford committed Mar 21, 2024
1 parent e9f581d commit 887df73
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
22 changes: 15 additions & 7 deletions src/utils/__tests__/unique.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import { describe, expect, it } from 'vitest';
import { unique } from '../unique';

describe('unique', () => {
it('should return correct index', () => {
expect(unique('a', 0, ['a', 'b', 'c'])).toBeTruthy();
expect(unique('a', 1, ['a', 'b', 'c'])).toBeFalsy();
expect(unique('a', 2, ['a', 'b', 'c'])).toBeFalsy();
expect(unique('a', 0, ['a', 'b', 'c'])).toBeTruthy();
expect(unique('a', 1, ['z', 'a', 'b'])).toBeTruthy();
expect(unique('a', 2, ['y', 'z', 'a'])).toBeTruthy();
it.each([
{ value: 'a', index: 0, arr: ['a', 'b', 'c'], result: true },
{ value: 'a', index: 1, arr: ['a', 'b', 'c'], result: false },
{ value: 'a', index: 2, arr: ['a', 'b', 'c'], result: false },
{ value: 'a', index: 1, arr: ['z', 'a', 'b'], result: true },
{ value: 'a', index: 2, arr: ['y', 'z', 'a'], result: true },
])('unique($value, $index, $arr) === $result', ({ value, index, arr, result }) => {
expect(unique(value, index, arr)).toEqual(result);
});

it.each([
{ input: ['a', 'a', 'b', 'c', 'b', 'b'], expected: ['a', 'b', 'c'] },
{ input: [1, 2, 3, 4, 4, 5, 6, 3], expected: [1, 2, 3, 4, 5, 6] },
])('should filter: $input to the unique array: $expected', ({ input, expected }) => {
expect(input.filter(unique)).toEqual(expected);
});
});
4 changes: 3 additions & 1 deletion src/utils/unique.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const unique = <T>(value: T, index: number, arr: T[]): boolean => arr.indexOf(value) === index;
export function unique<T>(value: T, index: number, arr: T[]): boolean {
return arr.indexOf(value) === index;
}

0 comments on commit 887df73

Please sign in to comment.