Skip to content

Commit

Permalink
Array setters now throw exceptions only if they receive nil argumen…
Browse files Browse the repository at this point in the history
…ts in place of an array-like object and they return an empty array for any other non-array-like object
  • Loading branch information
ascartabelli committed Apr 27, 2016
1 parent c115468 commit d723cfc
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 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.23.0-rc.1
* @version 0.23.0-rc.2
* @module lamb
* @license MIT
* @preserve
Expand All @@ -18,7 +18,7 @@
* @category Core
* @type String
*/
lamb._version = "0.23.0-rc.1";
lamb._version = "0.23.0-rc.2";

// alias used as a placeholder argument for partial application
var _ = lamb;
Expand Down Expand Up @@ -304,7 +304,7 @@

function _setIndex (arrayLike, index, value, updater) {
var result = slice(arrayLike);
var idx = _getNaturalIndex(index, arrayLike.length);
var idx = _getNaturalIndex(index, result.length);

if (!isUndefined(idx)) {
result[idx] = updater ? updater(arrayLike[idx]) : value;
Expand Down
4 changes: 2 additions & 2 deletions dist/lamb.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lamb.min.js.map

Large diffs are not rendered by default.

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.23.0-rc.1",
"version": "0.23.0-rc.2",
"devDependencies": {
"coveralls": "^2.11.8",
"gulp": "^3.9.1",
Expand Down
2 changes: 1 addition & 1 deletion src/accessors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function _isEnumerable (obj, key) {

function _setIndex (arrayLike, index, value, updater) {
var result = slice(arrayLike);
var idx = _getNaturalIndex(index, arrayLike.length);
var idx = _getNaturalIndex(index, result.length);

if (!isUndefined(idx)) {
result[idx] = updater ? updater(arrayLike[idx]) : value;
Expand Down
14 changes: 14 additions & 0 deletions test/spec/accessorsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ describe("lamb.accessors", function () {
expect(lamb.setAt(0, 99)).toThrow();
expect(function () { lamb.setIndex(null, 0, 99); }).toThrow();
});

it("should return an empty array if a non-array-like object is supplied", function () {
[/foo/, 1, function () {}, NaN, true, new Date()].forEach(function (v) {
expect(lamb.setIndex(v, 2, 99)).toEqual([]);
expect(lamb.setAt(2, 99)(v)).toEqual([]);
});
});
});

describe("updateIndex / updateAt", function () {
Expand Down Expand Up @@ -210,6 +217,13 @@ describe("lamb.accessors", function () {
expect(function () { lamb.updateIndex(null, 0, fn99); }).toThrow();
expect(lamb.updateAt(0, fn99)).toThrow();
});

it("should return an empty array if a non-array-like object is supplied", function () {
[/foo/, 1, function () {}, NaN, true, new Date()].forEach(function (v) {
expect(lamb.updateIndex(v, 2, fn99)).toEqual([]);
expect(lamb.updateAt(2, fn99)(v)).toEqual([]);
});
});
});
});

Expand Down

0 comments on commit d723cfc

Please sign in to comment.