diff --git a/src/utils/__tests__/unique.spec.ts b/src/utils/__tests__/unique.spec.ts index 84823055d..0af1b7e7a 100644 --- a/src/utils/__tests__/unique.spec.ts +++ b/src/utils/__tests__/unique.spec.ts @@ -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); }); }); diff --git a/src/utils/unique.ts b/src/utils/unique.ts index e480107d2..405a201b6 100644 --- a/src/utils/unique.ts +++ b/src/utils/unique.ts @@ -1 +1,3 @@ -export const unique = (value: T, index: number, arr: T[]): boolean => arr.indexOf(value) === index; +export function unique(value: T, index: number, arr: T[]): boolean { + return arr.indexOf(value) === index; +}