Skip to content

Commit ead72cf

Browse files
authored
Merge pull request #11 from ilteoood/feat/defaultTo
feat: defaultTo
2 parents 3a9b018 + 9b7a45c commit ead72cf

11 files changed

+154
-56
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ concat(
6666
```
6767
</details>
6868
69+
<details>
70+
<summary>defaultTo</summary>
71+
72+
```javascript
73+
import { defaultTo } from '@ilteoood/re-flusso/defaultTo';
74+
75+
await pipeline(
76+
fromIterable([null, undefined]),
77+
defaultTo(1),
78+
toIterable([])
79+
)
80+
```
81+
</details>
82+
6983
<details>
7084
<summary>text</summary>
7185

src/defaultTo.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { map } from "./map";
2+
3+
export const defaultTo = <T>(
4+
defaultValue: T,
5+
writableStrategy?: QueuingStrategy,
6+
readableStrategy?: QueuingStrategy,
7+
) => {
8+
return map(
9+
(chunk) => chunk ?? defaultValue,
10+
writableStrategy,
11+
readableStrategy,
12+
);
13+
};

src/numbers/greaterThan.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { filter } from "../filter";
22

33
export const greaterThan = (
4-
value: number,
5-
writableStrategy?: QueuingStrategy,
6-
readableStrategy?: QueuingStrategy,
4+
value: number,
5+
writableStrategy?: QueuingStrategy,
6+
readableStrategy?: QueuingStrategy,
77
) => {
8-
return filter((chunk: number) => chunk > value, writableStrategy, readableStrategy);
8+
return filter(
9+
(chunk: number) => chunk > value,
10+
writableStrategy,
11+
readableStrategy,
12+
);
913
};

src/numbers/greaterThanEqual.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { filter } from "../filter";
22

33
export const greaterThanEqual = (
4-
value: number,
5-
writableStrategy?: QueuingStrategy,
6-
readableStrategy?: QueuingStrategy,
4+
value: number,
5+
writableStrategy?: QueuingStrategy,
6+
readableStrategy?: QueuingStrategy,
77
) => {
8-
return filter((chunk: number) => chunk >= value, writableStrategy, readableStrategy);
8+
return filter(
9+
(chunk: number) => chunk >= value,
10+
writableStrategy,
11+
readableStrategy,
12+
);
913
};

src/numbers/lessThan.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { filter } from "../filter";
22

33
export const lessThan = (
4-
value: number,
5-
writableStrategy?: QueuingStrategy,
6-
readableStrategy?: QueuingStrategy,
4+
value: number,
5+
writableStrategy?: QueuingStrategy,
6+
readableStrategy?: QueuingStrategy,
77
) => {
8-
return filter((chunk: number) => chunk < value, writableStrategy, readableStrategy);
8+
return filter(
9+
(chunk: number) => chunk < value,
10+
writableStrategy,
11+
readableStrategy,
12+
);
913
};

src/numbers/lessThanEqual.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { filter } from "../filter";
22

33
export const lessThanEqual = (
4-
value: number,
5-
writableStrategy?: QueuingStrategy,
6-
readableStrategy?: QueuingStrategy,
4+
value: number,
5+
writableStrategy?: QueuingStrategy,
6+
readableStrategy?: QueuingStrategy,
77
) => {
8-
return filter((chunk: number) => chunk <= value, writableStrategy, readableStrategy);
8+
return filter(
9+
(chunk: number) => chunk <= value,
10+
writableStrategy,
11+
readableStrategy,
12+
);
913
};

test/defaultTo.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { describe, it, expect } from "vitest";
2+
import { defaultTo } from "../src/defaultTo";
3+
import { fromIterable } from "../src/fromIterable";
4+
import { pipeline } from "../src/pipeline";
5+
import { toArray } from "../src/toArray";
6+
7+
describe("defaultTo", () => {
8+
it("should work with empty stream", async () => {
9+
const destinationArray = [];
10+
11+
await pipeline(fromIterable([]), defaultTo(1), toArray(destinationArray));
12+
13+
expect(destinationArray).toEqual([]);
14+
});
15+
16+
it("should work with falsy values", async () => {
17+
const destinationArray = [];
18+
19+
await pipeline(
20+
fromIterable([false, 0]),
21+
defaultTo(1),
22+
toArray(destinationArray),
23+
);
24+
25+
expect(destinationArray).toEqual([false, 0]);
26+
});
27+
28+
it("should work with nullish values", async () => {
29+
const destinationArray = [];
30+
31+
await pipeline(
32+
fromIterable([null, undefined]),
33+
defaultTo(1),
34+
toArray(destinationArray),
35+
);
36+
37+
expect(destinationArray).toEqual([1, 1]);
38+
});
39+
});

test/numbers/greaterThan.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ import { fromIterable } from "../../src/fromIterable";
66
import { greaterThan } from "../../src/numbers/greaterThan";
77

88
describe("greaterThan", () => {
9-
test("should work with empty list", async () => {
10-
const destinationArray = [];
9+
test("should work with empty list", async () => {
10+
const destinationArray = [];
1111

12-
await pipeline(fromIterable([]), greaterThan(0), toArray(destinationArray));
12+
await pipeline(fromIterable([]), greaterThan(0), toArray(destinationArray));
1313

14-
expect(destinationArray).toEqual([]);
15-
});
14+
expect(destinationArray).toEqual([]);
15+
});
1616

17-
test("should correctly filter numbers", async () => {
18-
const destinationArray = [];
17+
test("should correctly filter numbers", async () => {
18+
const destinationArray = [];
1919

20-
await pipeline(fromRange(1, 3), greaterThan(1), toArray(destinationArray));
20+
await pipeline(fromRange(1, 3), greaterThan(1), toArray(destinationArray));
2121

22-
expect(destinationArray).toEqual([2, 3]);
23-
});
22+
expect(destinationArray).toEqual([2, 3]);
23+
});
2424
});

test/numbers/greaterThanEqual.test.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,27 @@ import { fromIterable } from "../../src/fromIterable";
66
import { greaterThanEqual } from "../../src/numbers/greaterThanEqual";
77

88
describe("greaterThanEqual", () => {
9-
test("should work with empty list", async () => {
10-
const destinationArray = [];
9+
test("should work with empty list", async () => {
10+
const destinationArray = [];
1111

12-
await pipeline(fromIterable([]), greaterThanEqual(0), toArray(destinationArray));
12+
await pipeline(
13+
fromIterable([]),
14+
greaterThanEqual(0),
15+
toArray(destinationArray),
16+
);
1317

14-
expect(destinationArray).toEqual([]);
15-
});
18+
expect(destinationArray).toEqual([]);
19+
});
1620

17-
test("should correctly filter numbers", async () => {
18-
const destinationArray = [];
21+
test("should correctly filter numbers", async () => {
22+
const destinationArray = [];
1923

20-
await pipeline(fromRange(1, 3), greaterThanEqual(1), toArray(destinationArray));
24+
await pipeline(
25+
fromRange(1, 3),
26+
greaterThanEqual(1),
27+
toArray(destinationArray),
28+
);
2129

22-
expect(destinationArray).toEqual([1, 2, 3]);
23-
});
30+
expect(destinationArray).toEqual([1, 2, 3]);
31+
});
2432
});

test/numbers/lessThan.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ import { fromIterable } from "../../src/fromIterable";
66
import { lessThan } from "../../src/numbers/lessThan";
77

88
describe("lessThan", () => {
9-
test("should work with empty list", async () => {
10-
const destinationArray = [];
9+
test("should work with empty list", async () => {
10+
const destinationArray = [];
1111

12-
await pipeline(fromIterable([]), lessThan(0), toArray(destinationArray));
12+
await pipeline(fromIterable([]), lessThan(0), toArray(destinationArray));
1313

14-
expect(destinationArray).toEqual([]);
15-
});
14+
expect(destinationArray).toEqual([]);
15+
});
1616

17-
test("should correctly filter numbers", async () => {
18-
const destinationArray = [];
17+
test("should correctly filter numbers", async () => {
18+
const destinationArray = [];
1919

20-
await pipeline(fromRange(1, 3), lessThan(3), toArray(destinationArray));
20+
await pipeline(fromRange(1, 3), lessThan(3), toArray(destinationArray));
2121

22-
expect(destinationArray).toEqual([1, 2]);
23-
});
22+
expect(destinationArray).toEqual([1, 2]);
23+
});
2424
});

test/numbers/lessThanEqual.test.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,27 @@ import { pipeline } from "../../src/pipeline";
66
import { toArray } from "../../src/toArray";
77

88
describe("lessThanEqual", () => {
9-
test("should work with empty list", async () => {
10-
const destinationArray = [];
9+
test("should work with empty list", async () => {
10+
const destinationArray = [];
1111

12-
await pipeline(fromIterable([]), lessThanEqual(0), toArray(destinationArray));
12+
await pipeline(
13+
fromIterable([]),
14+
lessThanEqual(0),
15+
toArray(destinationArray),
16+
);
1317

14-
expect(destinationArray).toEqual([]);
15-
});
18+
expect(destinationArray).toEqual([]);
19+
});
1620

17-
test("should correctly filter numbers", async () => {
18-
const destinationArray = [];
21+
test("should correctly filter numbers", async () => {
22+
const destinationArray = [];
1923

20-
await pipeline(fromRange(1, 3), lessThanEqual(3), toArray(destinationArray));
24+
await pipeline(
25+
fromRange(1, 3),
26+
lessThanEqual(3),
27+
toArray(destinationArray),
28+
);
2129

22-
expect(destinationArray).toEqual([1, 2, 3]);
23-
});
30+
expect(destinationArray).toEqual([1, 2, 3]);
31+
});
2432
});

0 commit comments

Comments
 (0)