Skip to content

Commit

Permalink
fix: return an empty string if iterable that passed to the 'join' fun…
Browse files Browse the repository at this point in the history
…ction is empty-iterable
  • Loading branch information
shine1594 authored and DoHyeong Lee committed Jan 13, 2022
1 parent 90ce656 commit 0c1f19e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ type ReturnJoinType<T extends Iterable<unknown> | AsyncIterable<unknown>> =
: never;

function sync<A>(sep: string, iterable: Iterable<A>) {
return reduce((a: string, b) => `${a}${sep}${b}`, iterable);
return reduce((a: string, b) => `${a}${sep}${b}`, iterable) ?? "";
}

function async<A>(sep: string, iterable: AsyncIterable<A>) {
return reduce((a: string, b) => `${a}${sep}${b}`, iterable);
return reduce((a: string, b) => `${a}${sep}${b}`, iterable).then(
(a) => a ?? "",
);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/join.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { asyncEmpty, empty } from "../src/_internal/utils";
import { filter, join, map, pipe, toAsync } from "../src/index";

describe("join", function () {
describe("sync", function () {
it("should return an empty string if there are no iterable items", function () {
expect(join(", ", "")).toEqual("");
expect(join(", ", [])).toEqual("");
expect(join(", ", empty())).toEqual("");
});

it("should be joined with separator", function () {
expect(join(", ", "hello")).toEqual("h, e, l, l, o");
});
Expand All @@ -26,6 +33,10 @@ describe("join", function () {
});

describe("async", function () {
it("should return an empty string if there are no iterable items", async function () {
expect(await join(", ", asyncEmpty())).toEqual("");
});

it("should be discarded elements by length", async function () {
const res = await join("-", toAsync([1, 2, 3, 4, 5]));
expect(res).toEqual("1-2-3-4-5");
Expand Down

0 comments on commit 0c1f19e

Please sign in to comment.