Skip to content

Commit

Permalink
fix(utils): remove conditional types from getFilteredCollection()
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Mar 15, 2024
1 parent 82a59ee commit 472e11f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/utils/__tests__/getFilteredCollection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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 collection = [{ id: 1, name: 'Item A' }] as Item[] | undefined
const filters: Array<Filter<Item>> = []

const result = getFilteredCollection(collection, filters)
Expand All @@ -19,10 +19,10 @@ describe('utils/getFilteredCollection()', () => {
it('should filter the collection with 1 filter', () => {
type Item = { id: number; name: string }

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

const result = getFilteredCollection(collection, filters)
Expand All @@ -33,11 +33,11 @@ describe('utils/getFilteredCollection()', () => {
it('should filter the collection with 2 filters', () => {
type Item = { id: number; isActive: boolean; name: string }

const collection: Item[] = [
const collection = [
{ id: 1, isActive: true, name: 'Item A' },
{ id: 2, isActive: false, name: 'Item B' },
{ id: 3, isActive: true, name: 'Item C' }
]
] as Item[] | undefined
const filters: Array<Filter<Item>> = [
items => items.filter(item => item.isActive),
items => items.filter(item => item.id > 1)
Expand All @@ -51,7 +51,7 @@ describe('utils/getFilteredCollection()', () => {
it('should return `undefined` with an undefined collection', () => {
type Item = { id: number; name: string }

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

const result = getFilteredCollection(collection, filters)
Expand Down
2 changes: 0 additions & 2 deletions src/utils/getFilteredCollection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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
Expand Down

0 comments on commit 472e11f

Please sign in to comment.