Skip to content

Commit

Permalink
fix: listOf now returns List.of<T> (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmichalina authored May 18, 2020
1 parent ab5aa92 commit fbb0e4f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
32 changes: 32 additions & 0 deletions src/list/list.factory.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { listFrom, listOf } from './list.factory'

describe('List Factory', () => {
describe(listFrom.name, () => {
it('should', () => {
const sut = listFrom([1, 2]).filter(a => a > 1)

expect(sut.toArray()).toEqual([2])
})

it('should handle undefined', () => {
const sut = listFrom<number>().filter(a => a > 1)

expect(sut.toArray()).toEqual([])
})
})

describe(listOf.name, () => {
it('should handle nominal', () => {
const sut = listOf(1, 2).filter(a => a > 1)

expect(sut.toArray()).toEqual([2])
})

it('should handle undefined', () => {
const sut = listOf<number>().filter(a => a > 1)

expect(sut.toArray()).toEqual([])
})
})
})

16 changes: 4 additions & 12 deletions src/list/list.factory.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { List } from './list'

export function listOf<T>(...args: T[]) {
return List.of(args)
return List.of<T>(...args)
}

// export function listFrom<T>(value?: T) {
// return List.from<T>()
// }

// export function none<T>() {
// return Maybe.none<T>()
// }

// export function some<T>(value: T) {
// return maybe(value)
// }
export function listFrom<T>(value?: Iterable<T>) {
return List.from<T>(value)
}
10 changes: 6 additions & 4 deletions src/list/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ export class List<T> {
}, args.length)
}

public static from<T>(iterable: Iterable<T>): List<T> {
return new List(function* () {
yield* iterable as any
} as any, (iterable as any).length)
public static from<T>(iterable?: Iterable<T>): List<T> {
return iterable
? new List(function* () {
yield* iterable as any
} as any, (iterable as any).length)
: List.empty()
}

public static range(start: number, end: number, step = 1): List<number> {
Expand Down

0 comments on commit fbb0e4f

Please sign in to comment.