Skip to content

Commit

Permalink
Implemented iterable/to(String).
Browse files Browse the repository at this point in the history
Fixes #11.
  • Loading branch information
jussi-kalliokoski committed Jun 23, 2015
1 parent 1069f5b commit b972a7d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/iterable/to.js
Expand Up @@ -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
Expand All @@ -37,6 +43,8 @@ export function to <iT, rT> (
} 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]);
}
Expand Down
8 changes: 8 additions & 0 deletions test/spec/iterable/toSpec.js
Expand Up @@ -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");
});
});
});

0 comments on commit b972a7d

Please sign in to comment.