From b972a7d262864148b28d599f358b3d2979f9d593 Mon Sep 17 00:00:00 2001 From: Jussi Kalliokoski Date: Tue, 23 Jun 2015 09:56:41 +0300 Subject: [PATCH] Implemented `iterable/to(String)`. Fixes #11. --- src/iterable/to.js | 8 ++++++++ test/spec/iterable/toSpec.js | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/iterable/to.js b/src/iterable/to.js index e2a6f01..07041f7 100644 --- a/src/iterable/to.js +++ b/src/iterable/to.js @@ -21,6 +21,12 @@ * })::to(Object); // returns { a: { id: "a" }, b: { id: "b"} } * ``` * + * @example Strings + * + * ```javascript + * "foo".split("").reverse()::to(String) // "oof" + * + * * @example Other Collections * * ```javascript @@ -37,6 +43,8 @@ export function to ( } else if ( Type === Object ) { return [...this].reduce((accumulation, entry) => ({ ...accumulation, [entry[0]]: entry[1] }), {}); + } else if ( Type === String ) { + return [...this].join(""); } else { return new Type([...this]); } diff --git a/test/spec/iterable/toSpec.js b/test/spec/iterable/toSpec.js index 208905f..41f7a30 100644 --- a/test/spec/iterable/toSpec.js +++ b/test/spec/iterable/toSpec.js @@ -36,4 +36,12 @@ describe("to()", function () { [...set].should.deep.equal([1,2,3,4]); }); }); + + describe("when called with String constructor", function () { + it("should return the values as a String", function () { + const string = ["a", "b", "c", "d"][Symbol.iterator]()::to(String); + string.should.be.a("string"); + string.should.equal("abcd"); + }); + }); });