Skip to content

Commit 9f6fc3b

Browse files
committed
Fix handling zero
1 parent c9685fa commit 9f6fc3b

File tree

15 files changed

+72
-16
lines changed

15 files changed

+72
-16
lines changed

src/async/_private/firstOrDefault.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const firstOrDefault = <TSource>(
1111
const firstOrDefault1 = async <T>(source: AsyncIterable<T>) => {
1212
const first = await source[Symbol.asyncIterator]().next()
1313
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
14-
return first.value || null
14+
return first.value ?? null
1515
}
1616

1717
const firstOrDefault2 = async <T>(

src/async/_private/max.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const max: MaxFunc = <TSource>(
1818
const max1 = async (source: AsyncIterable<number>) => {
1919
let maxItem: number | null = null
2020
for await (const item of source) {
21-
maxItem = Math.max(maxItem || Number.NEGATIVE_INFINITY, item)
21+
maxItem = Math.max(maxItem ?? Number.NEGATIVE_INFINITY, item)
2222
}
2323

2424
if (maxItem === null) {
@@ -32,7 +32,7 @@ const max2 = async <TSource>(
3232
source: AsyncIterable<TSource>, selector: (x: TSource) => number) => {
3333
let maxItem: number | null = null
3434
for await (const item of source) {
35-
maxItem = Math.max(maxItem || Number.NEGATIVE_INFINITY, selector(item))
35+
maxItem = Math.max(maxItem ?? Number.NEGATIVE_INFINITY, selector(item))
3636
}
3737

3838
if (maxItem === null) {

src/async/_private/maxAsync.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const maxAsync = async <TSource>(
44
source: AsyncIterable<TSource>, selector: (x: TSource) => Promise<number>): Promise<number> => {
55
let max: number | null = null
66
for await (const item of source) {
7-
max = Math.max(max || Number.NEGATIVE_INFINITY, await selector(item))
7+
max = Math.max(max ?? Number.NEGATIVE_INFINITY, await selector(item))
88
}
99

1010
if (max === null) {

src/async/_private/min.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const min: MinAsync = (source: AsyncIterable<number>, selector?: (x: numb
1616
const min1 = async (source: AsyncIterable<number>) => {
1717
let minValue: number | null = null
1818
for await (const item of source) {
19-
minValue = Math.min(minValue || Number.POSITIVE_INFINITY, item)
19+
minValue = Math.min(minValue ?? Number.POSITIVE_INFINITY, item)
2020
}
2121

2222
if (minValue === null) {
@@ -29,7 +29,7 @@ const min1 = async (source: AsyncIterable<number>) => {
2929
const min2 = async (source: AsyncIterable<number>, selector: (x: number) => number) => {
3030
let minValue: number | null = null
3131
for await (const item of source) {
32-
minValue = Math.min(minValue || Number.POSITIVE_INFINITY, selector(item))
32+
minValue = Math.min(minValue ?? Number.POSITIVE_INFINITY, selector(item))
3333
}
3434

3535
if (minValue === null) {

src/async/_private/minAsync.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const minAsync = async <TSource>(
55
selector: (x: TSource) => Promise<number>): Promise<number> => {
66
let min: number | null = null
77
for await (const item of source) {
8-
min = Math.min(min || Number.POSITIVE_INFINITY, await selector(item))
8+
min = Math.min(min ?? Number.POSITIVE_INFINITY, await selector(item))
99
}
1010

1111
if (min === null) {

src/initializer/bindArrayEnumerable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const bindArrayEnumerable = <T, TKey extends keyof IEnumerable<T>>() => {
4747
return this[index] as T
4848
}
4949
prototype.elementAtOrDefault = function(index: number): T | null {
50-
return (this[index] as T | undefined) || null
50+
return (this[index] as T | undefined) ?? null
5151
}
5252
prototype.first = function(predicate?: (x: T) => boolean): T {
5353
if (predicate) {

src/sync/_private/firstOrDefault.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const firstOrDefault = <TSource>(
1010
const firstOrDefault1 = <T>(source: Iterable<T>): T | null => {
1111
const first = source[Symbol.iterator]().next()
1212
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
13-
return first.value || null
13+
return first.value ?? null
1414
}
1515

1616
const firstOrDefault2 = <T>(source: Iterable<T>, predicate: (x: T) => boolean): T | null => {

src/sync/_private/max.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const max: MaxFunc = <TSource>(
1818
const max1 = (source: Iterable<number>): number => {
1919
let maxItem: number | null = null
2020
for (const item of source) {
21-
maxItem = Math.max(maxItem || Number.NEGATIVE_INFINITY, item)
21+
maxItem = Math.max(maxItem ?? Number.NEGATIVE_INFINITY, item)
2222
}
2323

2424
if (maxItem === null) {
@@ -31,7 +31,7 @@ const max1 = (source: Iterable<number>): number => {
3131
const max2 = <TSource>(source: Iterable<TSource>, selector: (x: TSource) => number): number => {
3232
let maxItem: number | null = null
3333
for (const item of source) {
34-
maxItem = Math.max(maxItem || Number.NEGATIVE_INFINITY, selector(item))
34+
maxItem = Math.max(maxItem ?? Number.NEGATIVE_INFINITY, selector(item))
3535
}
3636

3737
if (maxItem === null) {

src/sync/_private/maxAsync.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const maxAsync = async <TSource>(
44
source: Iterable<TSource>, selector: (x: TSource) => Promise<number>): Promise<number> => {
55
let max: number | null = null
66
for (const item of source) {
7-
max = Math.max(max || Number.NEGATIVE_INFINITY, await selector(item))
7+
max = Math.max(max ?? Number.NEGATIVE_INFINITY, await selector(item))
88
}
99

1010
if (max === null) {

src/sync/_private/min.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const min: MinFunc = <TSource>(source: Iterable<TSource> | Iterable<numbe
1717
const min1 = (source: Iterable<number>) => {
1818
let minItem: number | null = null
1919
for (const item of source) {
20-
minItem = Math.min(minItem || Number.POSITIVE_INFINITY, item)
20+
minItem = Math.min(minItem ?? Number.POSITIVE_INFINITY, item)
2121
}
2222

2323
if (minItem === null) {
@@ -30,7 +30,7 @@ const min1 = (source: Iterable<number>) => {
3030
const min2 = <TSource>(source: Iterable<TSource>, selector: (x: TSource) => number) => {
3131
let minItem: number | null = null
3232
for (const item of source) {
33-
minItem = Math.min(minItem || Number.POSITIVE_INFINITY, selector(item))
33+
minItem = Math.min(minItem ?? Number.POSITIVE_INFINITY, selector(item))
3434
}
3535

3636
if (minItem === null) {

src/sync/_private/minAsync.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const minAsync = async <TSource>(
44
source: Iterable<TSource>, selector: (x: TSource) => Promise<number>): Promise<number> => {
55
let min: number | null = null
66
for (const item of source) {
7-
min = Math.min(min || Number.POSITIVE_INFINITY, await selector(item))
7+
min = Math.min(min ?? Number.POSITIVE_INFINITY, await selector(item))
88
}
99

1010
if (min === null) {

tests/unittests/tests/ElementAtOrDefault.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { asAsync, expectAsync, itAsync, itEnumerable, itParallel } from "../Test
33
describe("elementAtOrDefault", () => {
44

55
it("String", () => {
6+
expect("0".elementAtOrDefault(0)).toBe("0")
7+
expect("".elementAtOrDefault(0)).toBeNull()
8+
69
expect("abc".elementAtOrDefault(0)).toBe("a")
710
expect("abc".elementAtOrDefault(1)).toBe("b")
811
expect("abc".elementAtOrDefault(2)).toBe("c")
@@ -25,6 +28,21 @@ describe("elementAtOrDefault", () => {
2528
expect(await asParallel([1, 2]).elementAtOrDefault(1)).toBe(2)
2629
})
2730

31+
itEnumerable("Starting with Zero", (asEnumerable) => {
32+
expect(asEnumerable([0]).elementAtOrDefault(0)).toBe(0)
33+
expect(asEnumerable([0, 2]).elementAtOrDefault(1)).toBe(2)
34+
})
35+
36+
itAsync("Starting with Zero", async () => {
37+
expect(await asAsync([0]).elementAtOrDefault(0)).toBe(0)
38+
expect(await asAsync([0, 2]).elementAtOrDefault(1)).toBe(2)
39+
})
40+
41+
itParallel("Starting with Zero", async (asParallel) => {
42+
expect(await asParallel([0]).elementAtOrDefault(0)).toBe(0)
43+
expect(await asParallel([0, 2]).elementAtOrDefault(1)).toBe(2)
44+
})
45+
2846
itEnumerable("Empty to be null", (asEnumerable) =>
2947
expect(asEnumerable([]).elementAtOrDefault(0)).toBeNull())
3048

tests/unittests/tests/Max.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ describe("max", () => {
3535

3636
itParallel("Basic", async (asParallel) => expect(await asParallel([1, 2, 3]).max()).toBe(3))
3737

38+
itEnumerable("Max Zero", (asEnumerable) => {
39+
expect(asEnumerable([0, -1]).max()).toBe(0)
40+
})
41+
42+
itAsync("Max Zero", async () => {
43+
expect(await asAsync([0, -1]).max()).toBe(0)
44+
})
45+
46+
itParallel("Max Zero", async (asParallel) => {
47+
expect(await asParallel([0, -1]).max()).toBe(0)
48+
})
49+
3850
itEnumerable("empty array throws exception", (asEnumerable) =>
3951
expect(() => asEnumerable([]).max()).toThrowError(InvalidOperationException))
4052

tests/unittests/tests/Min.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ describe("min", () => {
1414
expect(await asParallel([1, 2, 3, -7]).min()).toBe(-7)
1515
})
1616

17+
itEnumerable("Min Zero", (asEnumerable) => {
18+
expect(asEnumerable([1, 2, 0, 1, 3, 4, 9, 8, 5]).min()).toBe(0)
19+
})
20+
21+
itAsync("Min Zero", async () => {
22+
expect(await asAsync([1, 2, 0, 1, 3, 4, 9, 8, 5]).min()).toBe(0)
23+
})
24+
25+
itParallel("Min Zero", async (asParallel) => {
26+
expect(await asParallel([1, 2, 0, 1, 3, 4, 9, 8, 5]).min()).toBe(0)
27+
})
28+
1729
itEnumerable("MinEmptyError", (asEnumerable) => {
1830
expect(() => asEnumerable([]).min()).toThrowError(InvalidOperationException)
1931
})
@@ -56,6 +68,20 @@ describe("min", () => {
5668
expectMin.toBe(1)
5769
})
5870

71+
itEnumerable("Min Predicate Zero", (asEnumerable) => {
72+
expect(asEnumerable([1, 2, 0, 1, 3, 4, 9, 8, 5]).min(Math.abs)).toBe(0)
73+
})
74+
75+
itAsync("Min Predicate Zero", async () => {
76+
const expectMin = await expectAsync(asAsync([1, 2, 0, 1, 3, 4, 9, 8, 5]).min(Math.abs))
77+
expectMin.toBe(0)
78+
})
79+
80+
itParallel("Min Predicate Zero", async (asParallel) => {
81+
const expectMin = await expectAsync(asParallel([1, 2, 0, 1, 3, 4, 9, 8, 5]).min(Math.abs))
82+
expectMin.toBe(0)
83+
})
84+
5985
itEnumerable("empty exception", (asEnumerable) => {
6086
expect(() => asEnumerable([]).min()).toThrowError(InvalidOperationException)
6187
})

tests/unittests/tests/MinAsync.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe("minAsync", () => {
2424
})
2525

2626
itAsync("Min Predicate", async () => {
27-
const expectMin = await expectAsync(asAsync([1, 2, 3, -7]).min(Math.abs))
27+
const expectMin = await expectAsync(asAsync([1, 2, 3, -7]).minAsync(async (x) => Math.abs(x)))
2828
expectMin.toBe(1)
2929
})
3030

0 commit comments

Comments
 (0)