Skip to content

Commit fbb0e4f

Browse files
fix: listOf now returns List.of<T> (#144)
1 parent ab5aa92 commit fbb0e4f

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

src/list/list.factory.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { listFrom, listOf } from './list.factory'
2+
3+
describe('List Factory', () => {
4+
describe(listFrom.name, () => {
5+
it('should', () => {
6+
const sut = listFrom([1, 2]).filter(a => a > 1)
7+
8+
expect(sut.toArray()).toEqual([2])
9+
})
10+
11+
it('should handle undefined', () => {
12+
const sut = listFrom<number>().filter(a => a > 1)
13+
14+
expect(sut.toArray()).toEqual([])
15+
})
16+
})
17+
18+
describe(listOf.name, () => {
19+
it('should handle nominal', () => {
20+
const sut = listOf(1, 2).filter(a => a > 1)
21+
22+
expect(sut.toArray()).toEqual([2])
23+
})
24+
25+
it('should handle undefined', () => {
26+
const sut = listOf<number>().filter(a => a > 1)
27+
28+
expect(sut.toArray()).toEqual([])
29+
})
30+
})
31+
})
32+

src/list/list.factory.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import { List } from './list'
22

33
export function listOf<T>(...args: T[]) {
4-
return List.of(args)
4+
return List.of<T>(...args)
55
}
66

7-
// export function listFrom<T>(value?: T) {
8-
// return List.from<T>()
9-
// }
10-
11-
// export function none<T>() {
12-
// return Maybe.none<T>()
13-
// }
14-
15-
// export function some<T>(value: T) {
16-
// return maybe(value)
17-
// }
7+
export function listFrom<T>(value?: Iterable<T>) {
8+
return List.from<T>(value)
9+
}

src/list/list.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ export class List<T> {
3030
}, args.length)
3131
}
3232

33-
public static from<T>(iterable: Iterable<T>): List<T> {
34-
return new List(function* () {
35-
yield* iterable as any
36-
} as any, (iterable as any).length)
33+
public static from<T>(iterable?: Iterable<T>): List<T> {
34+
return iterable
35+
? new List(function* () {
36+
yield* iterable as any
37+
} as any, (iterable as any).length)
38+
: List.empty()
3739
}
3840

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

0 commit comments

Comments
 (0)