|
| 1 | +import { getUniqueItemsByKeyFromArrrays } from '../utils'; |
| 2 | + |
| 3 | +describe('getUniqueItemsByKeyFromArrrays', () => { |
| 4 | + it('deduplicates items across multiple arrays based on a keyAccessor', () => { |
| 5 | + const arrays = [ |
| 6 | + [ |
| 7 | + { id: 1, name: 'Alice' }, |
| 8 | + { id: 2, name: 'Bob' }, |
| 9 | + ], |
| 10 | + [ |
| 11 | + { id: 1, name: 'Charlie' }, |
| 12 | + { id: 3, name: 'David' }, |
| 13 | + ], |
| 14 | + ]; |
| 15 | + const keyAccessor = (item: { id: number; name: string }) => item.id; |
| 16 | + |
| 17 | + const result = getUniqueItemsByKeyFromArrrays(arrays, keyAccessor); |
| 18 | + |
| 19 | + expect(result).toHaveLength(3); |
| 20 | + expect(result).toEqual([ |
| 21 | + { id: 1, name: 'Charlie' }, |
| 22 | + { id: 2, name: 'Bob' }, |
| 23 | + { id: 3, name: 'David' }, |
| 24 | + ]); |
| 25 | + }); |
| 26 | + |
| 27 | + it('handles empty arrays input', () => { |
| 28 | + const arrays: Array<Array<{ id: number; name: string }>> = [[], []]; |
| 29 | + const keyAccessor = (item: { id: number; name: string }) => item.id; |
| 30 | + |
| 31 | + const result = getUniqueItemsByKeyFromArrrays(arrays, keyAccessor); |
| 32 | + |
| 33 | + expect(result).toHaveLength(0); |
| 34 | + expect(result).toEqual([]); |
| 35 | + }); |
| 36 | + |
| 37 | + it('handles arrays with all unique items', () => { |
| 38 | + const arrays = [ |
| 39 | + [ |
| 40 | + { id: 1, name: 'Alice' }, |
| 41 | + { id: 2, name: 'Bob' }, |
| 42 | + ], |
| 43 | + [ |
| 44 | + { id: 3, name: 'Charlie' }, |
| 45 | + { id: 4, name: 'David' }, |
| 46 | + ], |
| 47 | + ]; |
| 48 | + const keyAccessor = (item: { id: number; name: string }) => item.id; |
| 49 | + |
| 50 | + const result = getUniqueItemsByKeyFromArrrays(arrays, keyAccessor); |
| 51 | + |
| 52 | + expect(result).toHaveLength(4); |
| 53 | + expect(result).toEqual([ |
| 54 | + { id: 1, name: 'Alice' }, |
| 55 | + { id: 2, name: 'Bob' }, |
| 56 | + { id: 3, name: 'Charlie' }, |
| 57 | + { id: 4, name: 'David' }, |
| 58 | + ]); |
| 59 | + }); |
| 60 | + |
| 61 | + it('handles non-primitive keys (e.g., objects)', () => { |
| 62 | + const arrays = [ |
| 63 | + [ |
| 64 | + { id: { subId: 1 }, name: 'Alice' }, |
| 65 | + { id: { subId: 2 }, name: 'Bob' }, |
| 66 | + ], |
| 67 | + [ |
| 68 | + { id: { subId: 1 }, name: 'Charlie' }, |
| 69 | + { id: { subId: 3 }, name: 'David' }, |
| 70 | + ], |
| 71 | + ]; |
| 72 | + const keyAccessor = (item: { id: { subId: number }; name: string }) => item.id.subId; |
| 73 | + |
| 74 | + const result = getUniqueItemsByKeyFromArrrays(arrays, keyAccessor); |
| 75 | + |
| 76 | + expect(result).toHaveLength(3); |
| 77 | + expect(result).toEqual([ |
| 78 | + { id: { subId: 1 }, name: 'Charlie' }, |
| 79 | + { id: { subId: 2 }, name: 'Bob' }, |
| 80 | + { id: { subId: 3 }, name: 'David' }, |
| 81 | + ]); |
| 82 | + }); |
| 83 | + |
| 84 | + it('handles mixed primitive and object keys', () => { |
| 85 | + const arrays = [ |
| 86 | + [ |
| 87 | + { id: 1, name: 'Alice' }, |
| 88 | + { id: 'two', name: 'Bob' }, |
| 89 | + ], |
| 90 | + [ |
| 91 | + { id: 1, name: 'Charlie' }, |
| 92 | + { id: 'three', name: 'David' }, |
| 93 | + ], |
| 94 | + ]; |
| 95 | + const keyAccessor = (item: { id: number | string; name: string }) => item.id; |
| 96 | + |
| 97 | + const result = getUniqueItemsByKeyFromArrrays(arrays, keyAccessor); |
| 98 | + |
| 99 | + expect(result).toHaveLength(3); |
| 100 | + expect(result).toEqual([ |
| 101 | + { id: 1, name: 'Charlie' }, |
| 102 | + { id: 'two', name: 'Bob' }, |
| 103 | + { id: 'three', name: 'David' }, |
| 104 | + ]); |
| 105 | + }); |
| 106 | + |
| 107 | + it('handles null or undefined values in arrays', () => { |
| 108 | + const arrays = [ |
| 109 | + [{ id: 1, name: 'Alice' }, null, undefined], |
| 110 | + [{ id: 1, name: 'Charlie' }, null, undefined], |
| 111 | + ]; |
| 112 | + const keyAccessor = (item: { id: number; name: string } | null | undefined) => |
| 113 | + item?.id !== undefined ? item.id : ''; |
| 114 | + |
| 115 | + const result = getUniqueItemsByKeyFromArrrays(arrays, keyAccessor); |
| 116 | + |
| 117 | + expect(result).toHaveLength(2); |
| 118 | + expect(result).toEqual([{ id: 1, name: 'Charlie' }, undefined]); |
| 119 | + }); |
| 120 | + |
| 121 | + it('handles no keyAccessor (uses item as key)', () => { |
| 122 | + const arrays = [ |
| 123 | + ['a', 'b'], |
| 124 | + ['a', 'c'], |
| 125 | + ]; |
| 126 | + |
| 127 | + const result = getUniqueItemsByKeyFromArrrays(arrays); |
| 128 | + |
| 129 | + expect(result).toHaveLength(3); |
| 130 | + expect(result).toEqual(['a', 'b', 'c']); |
| 131 | + }); |
| 132 | +}); |
0 commit comments