Skip to content

Commit

Permalink
Merge pull request #11 from ilteoood/feat/defaultTo
Browse files Browse the repository at this point in the history
feat: defaultTo
  • Loading branch information
ilteoood authored May 31, 2024
2 parents 3a9b018 + 9b7a45c commit ead72cf
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 56 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ concat(
```
</details>
<details>
<summary>defaultTo</summary>
```javascript
import { defaultTo } from '@ilteoood/re-flusso/defaultTo';

await pipeline(
fromIterable([null, undefined]),
defaultTo(1),
toIterable([])
)
```
</details>
<details>
<summary>text</summary>
Expand Down
13 changes: 13 additions & 0 deletions src/defaultTo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { map } from "./map";

export const defaultTo = <T>(
defaultValue: T,
writableStrategy?: QueuingStrategy,
readableStrategy?: QueuingStrategy,
) => {
return map(
(chunk) => chunk ?? defaultValue,
writableStrategy,
readableStrategy,
);
};
12 changes: 8 additions & 4 deletions src/numbers/greaterThan.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { filter } from "../filter";

export const greaterThan = (
value: number,
writableStrategy?: QueuingStrategy,
readableStrategy?: QueuingStrategy,
value: number,
writableStrategy?: QueuingStrategy,
readableStrategy?: QueuingStrategy,
) => {
return filter((chunk: number) => chunk > value, writableStrategy, readableStrategy);
return filter(
(chunk: number) => chunk > value,
writableStrategy,
readableStrategy,
);
};
12 changes: 8 additions & 4 deletions src/numbers/greaterThanEqual.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { filter } from "../filter";

export const greaterThanEqual = (
value: number,
writableStrategy?: QueuingStrategy,
readableStrategy?: QueuingStrategy,
value: number,
writableStrategy?: QueuingStrategy,
readableStrategy?: QueuingStrategy,
) => {
return filter((chunk: number) => chunk >= value, writableStrategy, readableStrategy);
return filter(
(chunk: number) => chunk >= value,
writableStrategy,
readableStrategy,
);
};
12 changes: 8 additions & 4 deletions src/numbers/lessThan.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { filter } from "../filter";

export const lessThan = (
value: number,
writableStrategy?: QueuingStrategy,
readableStrategy?: QueuingStrategy,
value: number,
writableStrategy?: QueuingStrategy,
readableStrategy?: QueuingStrategy,
) => {
return filter((chunk: number) => chunk < value, writableStrategy, readableStrategy);
return filter(
(chunk: number) => chunk < value,
writableStrategy,
readableStrategy,
);
};
12 changes: 8 additions & 4 deletions src/numbers/lessThanEqual.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { filter } from "../filter";

export const lessThanEqual = (
value: number,
writableStrategy?: QueuingStrategy,
readableStrategy?: QueuingStrategy,
value: number,
writableStrategy?: QueuingStrategy,
readableStrategy?: QueuingStrategy,
) => {
return filter((chunk: number) => chunk <= value, writableStrategy, readableStrategy);
return filter(
(chunk: number) => chunk <= value,
writableStrategy,
readableStrategy,
);
};
39 changes: 39 additions & 0 deletions test/defaultTo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, it, expect } from "vitest";
import { defaultTo } from "../src/defaultTo";
import { fromIterable } from "../src/fromIterable";
import { pipeline } from "../src/pipeline";
import { toArray } from "../src/toArray";

describe("defaultTo", () => {
it("should work with empty stream", async () => {
const destinationArray = [];

await pipeline(fromIterable([]), defaultTo(1), toArray(destinationArray));

expect(destinationArray).toEqual([]);
});

it("should work with falsy values", async () => {
const destinationArray = [];

await pipeline(
fromIterable([false, 0]),
defaultTo(1),
toArray(destinationArray),
);

expect(destinationArray).toEqual([false, 0]);
});

it("should work with nullish values", async () => {
const destinationArray = [];

await pipeline(
fromIterable([null, undefined]),
defaultTo(1),
toArray(destinationArray),
);

expect(destinationArray).toEqual([1, 1]);
});
});
20 changes: 10 additions & 10 deletions test/numbers/greaterThan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import { fromIterable } from "../../src/fromIterable";
import { greaterThan } from "../../src/numbers/greaterThan";

describe("greaterThan", () => {
test("should work with empty list", async () => {
const destinationArray = [];
test("should work with empty list", async () => {
const destinationArray = [];

await pipeline(fromIterable([]), greaterThan(0), toArray(destinationArray));
await pipeline(fromIterable([]), greaterThan(0), toArray(destinationArray));

expect(destinationArray).toEqual([]);
});
expect(destinationArray).toEqual([]);
});

test("should correctly filter numbers", async () => {
const destinationArray = [];
test("should correctly filter numbers", async () => {
const destinationArray = [];

await pipeline(fromRange(1, 3), greaterThan(1), toArray(destinationArray));
await pipeline(fromRange(1, 3), greaterThan(1), toArray(destinationArray));

expect(destinationArray).toEqual([2, 3]);
});
expect(destinationArray).toEqual([2, 3]);
});
});
28 changes: 18 additions & 10 deletions test/numbers/greaterThanEqual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ import { fromIterable } from "../../src/fromIterable";
import { greaterThanEqual } from "../../src/numbers/greaterThanEqual";

describe("greaterThanEqual", () => {
test("should work with empty list", async () => {
const destinationArray = [];
test("should work with empty list", async () => {
const destinationArray = [];

await pipeline(fromIterable([]), greaterThanEqual(0), toArray(destinationArray));
await pipeline(
fromIterable([]),
greaterThanEqual(0),
toArray(destinationArray),
);

expect(destinationArray).toEqual([]);
});
expect(destinationArray).toEqual([]);
});

test("should correctly filter numbers", async () => {
const destinationArray = [];
test("should correctly filter numbers", async () => {
const destinationArray = [];

await pipeline(fromRange(1, 3), greaterThanEqual(1), toArray(destinationArray));
await pipeline(
fromRange(1, 3),
greaterThanEqual(1),
toArray(destinationArray),
);

expect(destinationArray).toEqual([1, 2, 3]);
});
expect(destinationArray).toEqual([1, 2, 3]);
});
});
20 changes: 10 additions & 10 deletions test/numbers/lessThan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import { fromIterable } from "../../src/fromIterable";
import { lessThan } from "../../src/numbers/lessThan";

describe("lessThan", () => {
test("should work with empty list", async () => {
const destinationArray = [];
test("should work with empty list", async () => {
const destinationArray = [];

await pipeline(fromIterable([]), lessThan(0), toArray(destinationArray));
await pipeline(fromIterable([]), lessThan(0), toArray(destinationArray));

expect(destinationArray).toEqual([]);
});
expect(destinationArray).toEqual([]);
});

test("should correctly filter numbers", async () => {
const destinationArray = [];
test("should correctly filter numbers", async () => {
const destinationArray = [];

await pipeline(fromRange(1, 3), lessThan(3), toArray(destinationArray));
await pipeline(fromRange(1, 3), lessThan(3), toArray(destinationArray));

expect(destinationArray).toEqual([1, 2]);
});
expect(destinationArray).toEqual([1, 2]);
});
});
28 changes: 18 additions & 10 deletions test/numbers/lessThanEqual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ import { pipeline } from "../../src/pipeline";
import { toArray } from "../../src/toArray";

describe("lessThanEqual", () => {
test("should work with empty list", async () => {
const destinationArray = [];
test("should work with empty list", async () => {
const destinationArray = [];

await pipeline(fromIterable([]), lessThanEqual(0), toArray(destinationArray));
await pipeline(
fromIterable([]),
lessThanEqual(0),
toArray(destinationArray),
);

expect(destinationArray).toEqual([]);
});
expect(destinationArray).toEqual([]);
});

test("should correctly filter numbers", async () => {
const destinationArray = [];
test("should correctly filter numbers", async () => {
const destinationArray = [];

await pipeline(fromRange(1, 3), lessThanEqual(3), toArray(destinationArray));
await pipeline(
fromRange(1, 3),
lessThanEqual(3),
toArray(destinationArray),
);

expect(destinationArray).toEqual([1, 2, 3]);
});
expect(destinationArray).toEqual([1, 2, 3]);
});
});

0 comments on commit ead72cf

Please sign in to comment.