diff --git a/README.md b/README.md index c2a0fe1..a277f5e 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,20 @@ concat( ``` +
+defaultTo + +```javascript +import { defaultTo } from '@ilteoood/re-flusso/defaultTo'; + +await pipeline( + fromIterable([null, undefined]), + defaultTo(1), + toIterable([]) +) +``` +
+
text diff --git a/src/defaultTo.ts b/src/defaultTo.ts new file mode 100644 index 0000000..5d594d2 --- /dev/null +++ b/src/defaultTo.ts @@ -0,0 +1,13 @@ +import { map } from "./map"; + +export const defaultTo = ( + defaultValue: T, + writableStrategy?: QueuingStrategy, + readableStrategy?: QueuingStrategy, +) => { + return map( + (chunk) => chunk ?? defaultValue, + writableStrategy, + readableStrategy, + ); +}; diff --git a/src/numbers/greaterThan.ts b/src/numbers/greaterThan.ts index ed9ed4d..c305c59 100644 --- a/src/numbers/greaterThan.ts +++ b/src/numbers/greaterThan.ts @@ -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, + ); }; diff --git a/src/numbers/greaterThanEqual.ts b/src/numbers/greaterThanEqual.ts index d3cc9c8..827b4e2 100644 --- a/src/numbers/greaterThanEqual.ts +++ b/src/numbers/greaterThanEqual.ts @@ -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, + ); }; diff --git a/src/numbers/lessThan.ts b/src/numbers/lessThan.ts index d3ae352..4263d58 100644 --- a/src/numbers/lessThan.ts +++ b/src/numbers/lessThan.ts @@ -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, + ); }; diff --git a/src/numbers/lessThanEqual.ts b/src/numbers/lessThanEqual.ts index 2d75f31..02d4bfe 100644 --- a/src/numbers/lessThanEqual.ts +++ b/src/numbers/lessThanEqual.ts @@ -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, + ); }; diff --git a/test/defaultTo.test.ts b/test/defaultTo.test.ts new file mode 100644 index 0000000..ed02531 --- /dev/null +++ b/test/defaultTo.test.ts @@ -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]); + }); +}); diff --git a/test/numbers/greaterThan.test.ts b/test/numbers/greaterThan.test.ts index af2871d..10af3e0 100644 --- a/test/numbers/greaterThan.test.ts +++ b/test/numbers/greaterThan.test.ts @@ -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]); + }); }); diff --git a/test/numbers/greaterThanEqual.test.ts b/test/numbers/greaterThanEqual.test.ts index 3cdd900..7f2951b 100644 --- a/test/numbers/greaterThanEqual.test.ts +++ b/test/numbers/greaterThanEqual.test.ts @@ -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]); + }); }); diff --git a/test/numbers/lessThan.test.ts b/test/numbers/lessThan.test.ts index d081bd8..68b2b04 100644 --- a/test/numbers/lessThan.test.ts +++ b/test/numbers/lessThan.test.ts @@ -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]); + }); }); diff --git a/test/numbers/lessThanEqual.test.ts b/test/numbers/lessThanEqual.test.ts index 31a0abc..fd4faf5 100644 --- a/test/numbers/lessThanEqual.test.ts +++ b/test/numbers/lessThanEqual.test.ts @@ -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]); + }); });