Skip to content

Commit

Permalink
- hasKeyValue now uses the “SameValueZero" comparison
Browse files Browse the repository at this point in the history
- Updated tests for `hasKeyValue`
  • Loading branch information
ascartabelli committed Aug 29, 2016
1 parent 13b6799 commit 566df89
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
14 changes: 6 additions & 8 deletions dist/lamb.js
Original file line number Diff line number Diff line change
Expand Up @@ -2670,8 +2670,7 @@

/**
* Returns an array of items present only in the first of the given arrays.<br/>
* Note that since version <code>0.13.0</code> this function uses the
* ["SameValueZero" comparison]{@link module:lamb.isSVZ|isSVZ}.
* Note that this function uses the ["SameValueZero" comparison]{@link module:lamb.isSVZ|isSVZ}.
* @example
* var a1 = [1, 2, 3, 4];
* var a2 = [2, 4, 5];
Expand Down Expand Up @@ -2993,8 +2992,7 @@

/**
* Returns an array of every item present in all given arrays.<br/>
* Note that since version <code>0.13.0</code> this function uses the
* ["SameValueZero" comparison]{@link module:lamb.isSVZ|isSVZ}.
* Note that this function uses the ["SameValueZero" comparison]{@link module:lamb.isSVZ|isSVZ}.
* @example
* var a1 = [1, 2, 3, 4];
* var a2 = [2, 5, 4, 6];
Expand Down Expand Up @@ -3383,8 +3381,7 @@
/**
* Returns an array comprised of the unique elements of the given array-like object.<br/>
* Can work with lists of complex objects if supplied with an iteratee.<br/>
* Note that since version <code>0.13.0</code> this function uses the
* ["SameValueZero" comparison]{@link module:lamb.isSVZ|isSVZ}.<br/>
* Note that this function uses the ["SameValueZero" comparison]{@link module:lamb.isSVZ|isSVZ}.<br/>
* When two values are considered equal, the first occurence will be the one included
* in the result array.
* @example <caption>With simple values:</caption>
Expand Down Expand Up @@ -4721,7 +4718,8 @@
}

/**
* Builds a function expecting an object to check against the given key / value pair.
* Builds a predicate expecting an object to check against the given key / value pair.<br/>
* The value check is made with the ["SameValueZero" comparison]{@link module:lamb.isSVZ|isSVZ}.
* @example
* var hasTheCorrectAnswer = _.hasKeyValue("answer", 42);
*
Expand All @@ -4736,7 +4734,7 @@
* @returns {Function}
*/
var hasKeyValue = function (key, value) {
return compose(partial(is, value), getKey(key));
return compose(partial(isSVZ, value), getKey(key));
};

/**
Expand Down
2 changes: 1 addition & 1 deletion 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.

9 changes: 3 additions & 6 deletions src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ function contains (value, fromIndex) {

/**
* Returns an array of items present only in the first of the given arrays.<br/>
* Note that since version <code>0.13.0</code> this function uses the
* ["SameValueZero" comparison]{@link module:lamb.isSVZ|isSVZ}.
* Note that this function uses the ["SameValueZero" comparison]{@link module:lamb.isSVZ|isSVZ}.
* @example
* var a1 = [1, 2, 3, 4];
* var a2 = [2, 4, 5];
Expand Down Expand Up @@ -346,8 +345,7 @@ function insertAt (index, element) {

/**
* Returns an array of every item present in all given arrays.<br/>
* Note that since version <code>0.13.0</code> this function uses the
* ["SameValueZero" comparison]{@link module:lamb.isSVZ|isSVZ}.
* Note that this function uses the ["SameValueZero" comparison]{@link module:lamb.isSVZ|isSVZ}.
* @example
* var a1 = [1, 2, 3, 4];
* var a2 = [2, 5, 4, 6];
Expand Down Expand Up @@ -736,8 +734,7 @@ var union = compose(uniques, flatMapWith(unary(slice)), list);
/**
* Returns an array comprised of the unique elements of the given array-like object.<br/>
* Can work with lists of complex objects if supplied with an iteratee.<br/>
* Note that since version <code>0.13.0</code> this function uses the
* ["SameValueZero" comparison]{@link module:lamb.isSVZ|isSVZ}.<br/>
* Note that this function uses the ["SameValueZero" comparison]{@link module:lamb.isSVZ|isSVZ}.<br/>
* When two values are considered equal, the first occurence will be the one included
* in the result array.
* @example <caption>With simple values:</caption>
Expand Down
5 changes: 3 additions & 2 deletions src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ function hasKey (key) {
}

/**
* Builds a function expecting an object to check against the given key / value pair.
* Builds a predicate expecting an object to check against the given key / value pair.<br/>
* The value check is made with the ["SameValueZero" comparison]{@link module:lamb.isSVZ|isSVZ}.
* @example
* var hasTheCorrectAnswer = _.hasKeyValue("answer", 42);
*
Expand All @@ -161,7 +162,7 @@ function hasKey (key) {
* @returns {Function}
*/
var hasKeyValue = function (key, value) {
return compose(partial(is, value), getKey(key));
return compose(partial(isSVZ, value), getKey(key));
};

/**
Expand Down
17 changes: 17 additions & 0 deletions test/spec/objectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,23 @@ describe("lamb.object", function () {
expect(lamb.hasKeyValue("z", 2)(persons[0])).toBe(false);
});

it("should return `true` for `undefined` values in existing keys", function () {
var obj = {"a": void 0};

expect(lamb.hasKeyValue("a", void 0)(obj)).toBe(true);
expect(lamb.hasKeyValue("a")(obj)).toBe(true);
});

it("should use the \"SameValueZero\" comparison", function () {
var obj = {a: NaN, b: 0, c: -0};

expect(lamb.hasKeyValue("a", NaN)(obj)).toBe(true);
expect(lamb.hasKeyValue("b", 0)(obj)).toBe(true);
expect(lamb.hasKeyValue("b", -0)(obj)).toBe(true);
expect(lamb.hasKeyValue("c", -0)(obj)).toBe(true);
expect(lamb.hasKeyValue("c", 0)(obj)).toBe(true);
});

it("should accept integers as keys and accept array-like objects", function () {
var o = {"1": "a", "2": "b"};
var arr = [1, 2, 3, 4];
Expand Down

0 comments on commit 566df89

Please sign in to comment.