Skip to content

Commit

Permalink
feat(utils): add getFilteredCollection()
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Mar 15, 2024
1 parent a1aeb59 commit 47eb5ff
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export { TableWithSelectableRows } from './tables/TableWithSelectableRows'
export { cleanString } from './utils/cleanString'
export { customDayjs } from './utils/customDayjs'
export { getCoordinates, coordinatesAreDistinct } from './utils/coordinates'
export { getFilteredCollection } from './utils/getFilteredCollection'
export { getHashFromCollection } from './utils/getHashFromCollection'
export { getLocalizedDayjs } from './utils/getLocalizedDayjs'
export { getMaybeBooleanFromRichBoolean } from './utils/getMaybeBooleanFromRichBoolean'
Expand Down
61 changes: 61 additions & 0 deletions src/utils/__tests__/getFilteredCollection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { describe, expect, it } from '@jest/globals'

import { getFilteredCollection } from '../getFilteredCollection'

import type { Filter } from '../../types/definitions'

describe('utils/getFilteredCollection()', () => {
it('should return the collection with 0 filter', () => {
type Item = { id: number; name: string }

const collection: Item[] = [{ id: 1, name: 'Item A' }]
const filters: Array<Filter<Item>> = []

const result = getFilteredCollection(collection, filters)

expect(result).toEqual(collection)
})

it('should filter the collection with 1 filter', () => {
type Item = { id: number; name: string }

const collection: Item[] = [
{ id: 1, name: 'Item A' },
{ id: 2, name: 'Item B' }
]
const filters: Array<Filter<Item>> = [items => items.filter(item => item.id === 1)]

const result = getFilteredCollection(collection, filters)

expect(result).toEqual([{ id: 1, name: 'Item A' }])
})

it('should filter the collection with 2 filters', () => {
type Item = { id: number; isActive: boolean; name: string }

const collection: Item[] = [
{ id: 1, isActive: true, name: 'Item A' },
{ id: 2, isActive: false, name: 'Item B' },
{ id: 3, isActive: true, name: 'Item C' }
]
const filters: Array<Filter<Item>> = [
items => items.filter(item => item.isActive),
items => items.filter(item => item.id > 1)
]

const result = getFilteredCollection(collection, filters)

expect(result).toEqual([{ id: 3, isActive: true, name: 'Item C' }])
})

it('should return `undefined` with an undefined collection', () => {
type Item = { id: number; name: string }

const collection = undefined
const filters: Array<Filter<Item>> = [items => items.filter(item => item.id === 1)]

const result = getFilteredCollection(collection, filters)

expect(result).toBeUndefined()
})
})
11 changes: 11 additions & 0 deletions src/utils/getFilteredCollection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Filter } from '../types/definitions'

export function getFilteredCollection<T>(collection: undefined, filters: Array<Filter<T>>): undefined
export function getFilteredCollection<T>(collection: T[], filters: Array<Filter<T>>): T[]
export function getFilteredCollection<T>(collection: T[] | undefined, filters: Array<Filter<T>>): T[] | undefined {
if (collection === undefined) {
return undefined
}

return filters.reduce((acc, filter) => filter(acc), collection)
}

0 comments on commit 47eb5ff

Please sign in to comment.