Skip to content

Commit

Permalink
Using Object.is implementation when compare values inside React.PropT…
Browse files Browse the repository at this point in the history
…ypes.oneOf
  • Loading branch information
chicoxyzzy committed Feb 29, 2016
1 parent 1387188 commit 03925f4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/isomorphic/classic/types/ReactPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ var ReactPropTypes = {
shape: createShapeTypeChecker,
};

/**
* inlined Object.is polyfill to avoid requiring consumers ship their own
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
*/
/*eslint-disable no-self-compare*/
function is(x, y) {
// SameValue algorithm
if (x === y) { // Steps 1-5, 7-10
// Steps 6.b-6.e: +0 != -0
return x !== 0 || 1 / x === 1 / y;
} else {
// Step 6.a: NaN == NaN
return x !== x && y !== y;
}
}
/*eslint-enable no-self-compare*/

function createChainableTypeChecker(validate) {
function checkType(
isRequired,
Expand Down Expand Up @@ -218,7 +235,7 @@ function createEnumTypeChecker(expectedValues) {
function validate(props, propName, componentName, location, propFullName) {
var propValue = props[propName];
for (var i = 0; i < expectedValues.length; i++) {
if (propValue === expectedValues[i]) {
if (is(propValue, expectedValues[i])) {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ describe('ReactPropTypes', function() {
);
});

it('should warn for invalid strings', function() {
it('should warn for invalid values', function() {
typeCheckFail(
PropTypes.oneOf(['red', 'blue']),
true,
Expand Down Expand Up @@ -606,6 +606,7 @@ describe('ReactPropTypes', function() {
it('should not warn for valid values', function() {
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'red');
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'blue');
typeCheckPass(PropTypes.oneOf(['red', 'blue', NaN]), NaN);
});

it('should be implicitly optional and not warn without values', function() {
Expand Down

0 comments on commit 03925f4

Please sign in to comment.