Skip to content

Commit

Permalink
flatten now throws an exception only for nil values, converts ar…
Browse files Browse the repository at this point in the history
…ray-likes to arrays and treats every other value as an empty array
  • Loading branch information
ascartabelli committed Jun 3, 2016
1 parent d5a5fc3 commit 668e17e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
6 changes: 3 additions & 3 deletions dist/lamb.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @overview lamb - A lightweight, and docile, JavaScript library to help embracing functional programming.
* @author Andrea Scartabelli <andrea.scartabelli@gmail.com>
* @version 0.28.0-alpha.14
* @version 0.28.0-alpha.15
* @module lamb
* @license MIT
* @preserve
Expand All @@ -18,7 +18,7 @@
* @category Core
* @type String
*/
lamb._version = "0.28.0-alpha.14";
lamb._version = "0.28.0-alpha.15";

// alias used as a placeholder argument for partial application
var _ = lamb;
Expand Down Expand Up @@ -1919,7 +1919,7 @@
* @returns {Array}
*/
function flatten (array) {
return _flatten(array, []);
return Array.isArray(array) ? _flatten(array, []) : slice(array);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"coveralls": "gulp coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
},
"tonicExample": "var _ = require('lamb');",
"version": "0.28.0-alpha.14",
"version": "0.28.0-alpha.15",
"devDependencies": {
"coveralls": "^2.11.9",
"gulp": "^3.9.1",
Expand Down
2 changes: 1 addition & 1 deletion src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ function flatMapWith (iteratee, iterateeContext) {
* @returns {Array}
*/
function flatten (array) {
return _flatten(array, []);
return Array.isArray(array) ? _flatten(array, []) : slice(array);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions test/spec/arraySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,24 @@ describe("lamb.array", function () {
expect(lamb.flatten(input)).toEqual(["a", "b", {"c" : ["d"]}]);
});

it("should return an array copy of the source object if supplied with an array-like", function () {
expect(lamb.flatten("foo")).toEqual(["f", "o", "o"]);
});

it("should throw an exception if called without arguments", function () {
expect(lamb.flatten).toThrow();
});

it("should throw an exception if supplied with `null` or `undefined` instead of an array-like", function () {
expect(function () { lamb.flatten(null); }).toThrow();
expect(function () { lamb.flatten(void 0); }).toThrow();
});

it("should treat every other value as an empty array", function () {
[{}, /foo/, 1, function () {}, NaN, true, new Date()].forEach(function (value) {
expect(lamb.flatten(value)).toEqual([]);
});
});
});

describe("init", function () {
Expand Down

0 comments on commit 668e17e

Please sign in to comment.