Skip to content

Commit

Permalink
Sync latest from FB
Browse files Browse the repository at this point in the history
  • Loading branch information
zpao committed Jan 27, 2016
1 parent 602276d commit 064a484
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 107 deletions.
2 changes: 0 additions & 2 deletions src/core/Deferred.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
* @flow
*/

var Promise = require('Promise');

/**
* Deferred provides a Promise-like API that exposes methods to resolve and
* reject the Promise. It is most useful when converting non-Promise code to use
Expand Down
2 changes: 0 additions & 2 deletions src/core/PromiseMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ var Deferred = require('Deferred');

var invariant = require('invariant');

import type Promise from 'Promise';

/**
* A map of asynchronous values that can be get or set in any order. Unlike a
* normal map, setting the value for a particular key more than once throws.
Expand Down
6 changes: 3 additions & 3 deletions src/core/__tests__/PromiseMap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('PromiseMap', () => {

expect(() => {
map.resolveKey('foo', 1337);
}).toThrow('PromiseMap: Already settled `foo`.');
}).toThrowError('PromiseMap: Already settled `foo`.');

map.get('foo').then(value => {
getValue = value;
Expand All @@ -129,7 +129,7 @@ describe('PromiseMap', () => {

expect(() => {
map.rejectKey('foo', 1337);
}).toThrow('PromiseMap: Already settled `foo`.');
}).toThrowError('PromiseMap: Already settled `foo`.');

map.get('foo').catch(value => {
getValue = value;
Expand All @@ -148,7 +148,7 @@ describe('PromiseMap', () => {

expect(() => {
map.rejectKey('foo', 1337);
}).toThrow('PromiseMap: Already settled `foo`.');
}).toThrowError('PromiseMap: Already settled `foo`.');

map.get('foo').then(value => {
getValue = value;
Expand Down
55 changes: 38 additions & 17 deletions src/core/__tests__/isEmpty-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,43 @@
* @emails oncall+jsinfra
*/

var isEmpty = require('isEmpty');

describe('isEmpty', function() {
it('should work', function() {
expect(isEmpty(null)).toEqual(true);
expect(isEmpty(false)).toEqual(true);
expect(isEmpty(undefined)).toEqual(true);
expect(isEmpty(0)).toEqual(true);
expect(isEmpty('')).toEqual(true);
expect(isEmpty([])).toEqual(true);
expect(isEmpty({})).toEqual(true);
expect(isEmpty(Object.create(null))).toEqual(true);

expect(isEmpty(1)).toEqual(false);
expect(isEmpty('abc')).toEqual(false);
expect(isEmpty([1])).toEqual(false);
expect(isEmpty({a : 1})).toEqual(false);
jest.mock('_shouldPolyfillES6Collection');

const Map = require('Map');
const Set = require('Set');

const isEmpty = require('isEmpty');
const _shouldPolyfillES6Collection = require('_shouldPolyfillES6Collection');

describe('isEmpty', () => {
it('should return true for empty supported types', () => {
expect(isEmpty(undefined)).toBe(true);
expect(isEmpty(null)).toBe(true);
expect(isEmpty(false)).toBe(true);
expect(isEmpty(0)).toBe(true);
expect(isEmpty('')).toBe(true);
expect(isEmpty([])).toBe(true);
expect(isEmpty({})).toBe(true);
expect(isEmpty(Object.create(null))).toBe(true);
});

it('should return false for non-empty supported types', () => {
expect(isEmpty(1)).toBe(false);
expect(isEmpty('0')).toBe(false);
expect(isEmpty([1])).toBe(false);
expect(isEmpty({a: 1})).toBe(false);
});

it('should not allow maps and sets', () => {
// Ensure that `Map` and `Set` use non-native polyfills
_shouldPolyfillES6Collection.mockReturnValue(true);

// Polyfilled
expect(() => isEmpty(new Map())).toThrow();
expect(() => isEmpty(new Set())).toThrow();

// Native
expect(() => isEmpty(new global.Map())).toThrow();
expect(() => isEmpty(new global.Set())).toThrow();
});
});
15 changes: 15 additions & 0 deletions src/core/__tests__/shallowEqual-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ describe('shallowEqual', () => {
expect(shallowEqual(undefined, undefined)).toBe(true);
});

it('returns true if arguments are not objects and are equal', () => {
expect(shallowEqual(1, 1)).toBe(true);
});

it('returns true if arguments are shallow equal', () => {
expect(
shallowEqual(
Expand All @@ -35,6 +39,17 @@ describe('shallowEqual', () => {
).toBe(true);
});

it('returns true when comparing NaN', () => {
expect(shallowEqual(NaN, NaN)).toBe(true);

expect(
shallowEqual(
{a: 1, b: 2, c: 3, d: NaN},
{a: 1, b: 2, c: 3, d: NaN}
)
).toBe(true);
});

it('returns false if arguments are not objects and not equal', () => {
expect(
shallowEqual(
Expand Down
59 changes: 58 additions & 1 deletion src/core/createArrayFromMixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,64 @@
* @typechecks
*/

var toArray = require('toArray');
var invariant = require('invariant');

/**
* Convert array-like objects to arrays.
*
* This API assumes the caller knows the contents of the data type. For less
* well defined inputs use createArrayFromMixed.
*
* @param {object|function|filelist} obj
* @return {array}
*/
function toArray(obj) {
var length = obj.length;

// Some browsers builtin objects can report typeof 'function' (e.g. NodeList
// in old versions of Safari).
invariant(
!Array.isArray(obj) &&
(typeof obj === 'object' || typeof obj === 'function'),
'toArray: Array-like object expected'
);

invariant(
typeof length === 'number',
'toArray: Object needs a length property'
);

invariant(
length === 0 ||
(length - 1) in obj,
'toArray: Object should have keys for indices'
);

invariant(
typeof obj.callee !== 'function',
'toArray: Object can\'t be `arguments`. Use rest params ' +
'(function(...args) {}) or Array.from() instead.'
);

// Old IE doesn't give collections access to hasOwnProperty. Assume inputs
// without method will throw during the slice call and skip straight to the
// fallback.
if (obj.hasOwnProperty) {
try {
return Array.prototype.slice.call(obj);
} catch (e) {
// IE < 9 does not support Array#slice on collections objects
}
}

// Fall back to copying key by key. This assumes all keys have a value,
// so will not preserve sparsely populated inputs.
var ret = Array(length);
for (var ii = 0; ii < length; ii++) {
ret[ii] = obj[ii];
}
return ret;
}

/**
* Perform a heuristic test to determine if an object is "array-like".
Expand Down
2 changes: 1 addition & 1 deletion src/core/createNodesFromMarkup.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function createNodesFromMarkup(markup, handleScript) {
createArrayFromMixed(scripts).forEach(handleScript);
}

var nodes = createArrayFromMixed(node.childNodes);
var nodes = Array.from(node.childNodes);
while (node.lastChild) {
node.removeChild(node.lastChild);
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/isEmpty.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@

/*eslint-disable no-unused-vars */

const invariant = require('invariant');

/**
* Mimics empty from PHP.
*/
function isEmpty(obj) {
invariant(
!(obj && obj[Symbol.iterator] && obj.size !== undefined),
'isEmpty does not support Map or Set',
);

if (Array.isArray(obj)) {
return obj.length === 0;
} else if (typeof obj === 'object') {
Expand Down
2 changes: 0 additions & 2 deletions src/core/resolveImmediate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
* @flow
*/

var Promise = require('Promise');

var resolvedPromise = Promise.resolve();

/**
Expand Down
24 changes: 22 additions & 2 deletions src/core/shallowEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,34 @@
* @flow
*/

/*eslint-disable no-self-compare */

'use strict';

var hasOwnProperty = Object.prototype.hasOwnProperty;

/**
* 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
*/
function is(x: mixed, y: mixed): boolean {
// SameValue algorithm
if (x === y) { // Steps 1-5, 7-10
// Steps 6.b-6.e: +0 != -0
return x !== 0 || 1 / (x: $FlowIssue) === 1 / (y: $FlowIssue);
} else {
// Step 6.a: NaN == NaN
return x !== x && y !== y;
}
}

/**
* Performs equality by iterating through keys on an object and returning false
* when any key has values which are not strictly equal between the arguments.
* Returns true when the values of all keys are strictly equal.
*/
function shallowEqual(objA: mixed, objB: mixed): boolean {
if (objA === objB) {
if (is(objA, objB)) {
return true;
}

Expand All @@ -39,7 +56,10 @@ function shallowEqual(objA: mixed, objB: mixed): boolean {

// Test for A's keys different from B.
for (var i = 0; i < keysA.length; i++) {
if (!hasOwnProperty.call(objB, keysA[i]) || objA[keysA[i]] !== objB[keysA[i]]) {
if (
!hasOwnProperty.call(objB, keysA[i]) ||
!is(objA[keysA[i]], objB[keysA[i]])
) {
return false;
}
}
Expand Down
66 changes: 0 additions & 66 deletions src/core/toArray.js

This file was deleted.

10 changes: 5 additions & 5 deletions src/fetch/__tests__/fetchWithRetries-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('fetchWithRetries', () => {

handleNext = jest.genMockFunction();

spyOn(console, 'error').andCallFake(message => {
spyOn(console, 'error').and.callFake(message => {
expect(message).toBe(
'Warning: fetchWithRetries: HTTP timeout, retrying.'
);
Expand Down Expand Up @@ -135,10 +135,10 @@ describe('fetchWithRetries', () => {
expect(fetch.mock.calls.length).toBe(retries + 1);
// Timeout last request.
jest.runAllTimers();
expect(handleNext.mock.calls[0][0]).toEqual(new Error(
'fetchWithRetries',
'Failed to get response from server, tried ' + retries + ' times',
));
expect(handleNext.mock.calls[0][0].message).toEqual(
'fetchWithRetries(): ' +
'Failed to get response from server, tried ' + (retries + 1) + ' times.'
);
});

// Test fails when used with npm `promise` due to Jest timing issues.
Expand Down
1 change: 0 additions & 1 deletion src/fetch/fetchWithRetries.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
'use strict';

var ExecutionEnvironment = require('ExecutionEnvironment');
var Promise = require('Promise');

var sprintf = require('sprintf');
var fetch = require('fetch');
Expand Down
5 changes: 0 additions & 5 deletions src/package.json

This file was deleted.

0 comments on commit 064a484

Please sign in to comment.