|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | + |
| 3 | +import { filterByKeyValue } from '../lib/utils/filter'; |
| 4 | + |
| 5 | +const people = [ |
| 6 | + { |
| 7 | + id: 1, |
| 8 | + name: 'John Doe', |
| 9 | + age: 25, |
| 10 | + parentIds: [1, 2], |
| 11 | + address: { city: 'Houston', zip: '10001' } |
| 12 | + }, |
| 13 | + { |
| 14 | + id: 2, |
| 15 | + name: 'Jane Doe', |
| 16 | + age: 30, |
| 17 | + parentIds: [3, 4], |
| 18 | + address: { city: 'Los Angeles', zip: '90001' } |
| 19 | + }, |
| 20 | + { |
| 21 | + id: 3, |
| 22 | + name: 'Alice Smith', |
| 23 | + age: 22, |
| 24 | + parentIds: [5, 6], |
| 25 | + address: { city: 'Chicago', zip: '60601' } |
| 26 | + }, |
| 27 | + { |
| 28 | + id: 4, |
| 29 | + name: 'Bob Johnson', |
| 30 | + age: 28, |
| 31 | + parentIds: [7, 8], |
| 32 | + address: { city: 'Houston', zip: '77001' } |
| 33 | + } |
| 34 | +]; |
| 35 | + |
| 36 | +describe('filterByKeyValue', () => { |
| 37 | + it('should filter array by filter value', () => { |
| 38 | + expect(filterByKeyValue(people, 'age', 30)).toEqual([ |
| 39 | + { |
| 40 | + id: 2, |
| 41 | + name: 'Jane Doe', |
| 42 | + age: 30, |
| 43 | + parentIds: [3, 4], |
| 44 | + address: { city: 'Los Angeles', zip: '90001' } |
| 45 | + } |
| 46 | + ]); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should filter array by filter value - array strict', () => { |
| 50 | + expect(filterByKeyValue(people, 'age', [22, 25])).toEqual([]); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should filter array by filter value - array loose', () => { |
| 54 | + expect(filterByKeyValue(people, 'age', [22, 25], false)).toEqual([ |
| 55 | + { |
| 56 | + id: 1, |
| 57 | + name: 'John Doe', |
| 58 | + age: 25, |
| 59 | + parentIds: [1, 2], |
| 60 | + address: { city: 'Houston', zip: '10001' } |
| 61 | + }, |
| 62 | + { |
| 63 | + id: 3, |
| 64 | + name: 'Alice Smith', |
| 65 | + age: 22, |
| 66 | + parentIds: [5, 6], |
| 67 | + address: { city: 'Chicago', zip: '60601' } |
| 68 | + } |
| 69 | + ]); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should filter array by filter value - array value strict', () => { |
| 73 | + expect(filterByKeyValue(people, 'parentIds', [1, 8])).toEqual([]); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should filter array by filter value - array value loose', () => { |
| 77 | + expect(filterByKeyValue(people, 'parentIds', [1, 8], false)).toEqual([ |
| 78 | + { |
| 79 | + id: 1, |
| 80 | + name: 'John Doe', |
| 81 | + age: 25, |
| 82 | + parentIds: [1, 2], |
| 83 | + address: { city: 'Houston', zip: '10001' } |
| 84 | + }, |
| 85 | + { |
| 86 | + id: 4, |
| 87 | + name: 'Bob Johnson', |
| 88 | + age: 28, |
| 89 | + parentIds: [7, 8], |
| 90 | + address: { city: 'Houston', zip: '77001' } |
| 91 | + } |
| 92 | + ]); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should filter array by filter value - custom filter', () => { |
| 96 | + expect( |
| 97 | + filterByKeyValue(people, 'age', (value) => Number(value) > 25) |
| 98 | + ).toEqual([ |
| 99 | + { |
| 100 | + id: 2, |
| 101 | + name: 'Jane Doe', |
| 102 | + age: 30, |
| 103 | + parentIds: [3, 4], |
| 104 | + address: { city: 'Los Angeles', zip: '90001' } |
| 105 | + }, |
| 106 | + { |
| 107 | + id: 4, |
| 108 | + name: 'Bob Johnson', |
| 109 | + age: 28, |
| 110 | + parentIds: [7, 8], |
| 111 | + address: { city: 'Houston', zip: '77001' } |
| 112 | + } |
| 113 | + ]); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should filter array by filter value - nested key', () => { |
| 117 | + expect(filterByKeyValue(people, 'address.city', 'Los Angeles')).toEqual( |
| 118 | + [ |
| 119 | + { |
| 120 | + id: 2, |
| 121 | + name: 'Jane Doe', |
| 122 | + age: 30, |
| 123 | + parentIds: [3, 4], |
| 124 | + address: { city: 'Los Angeles', zip: '90001' } |
| 125 | + } |
| 126 | + ] |
| 127 | + ); |
| 128 | + expect(filterByKeyValue(people, 'address.city', 'Houston')).toEqual([ |
| 129 | + { |
| 130 | + id: 1, |
| 131 | + name: 'John Doe', |
| 132 | + age: 25, |
| 133 | + parentIds: [1, 2], |
| 134 | + address: { city: 'Houston', zip: '10001' } |
| 135 | + }, |
| 136 | + { |
| 137 | + id: 4, |
| 138 | + name: 'Bob Johnson', |
| 139 | + age: 28, |
| 140 | + parentIds: [7, 8], |
| 141 | + address: { city: 'Houston', zip: '77001' } |
| 142 | + } |
| 143 | + ]); |
| 144 | + }); |
| 145 | +}); |
0 commit comments