-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): add getFilteredCollection()
- Loading branch information
1 parent
a1aeb59
commit 47eb5ff
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |