initial commit taken from gitlab.lrz.de

This commit is contained in:
privatereese
2018-08-24 18:09:42 +02:00
parent ae54ed4c48
commit fc05486403
28494 changed files with 2159823 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* BackAndroid has been moved to BackHandler. This stub calls BackHandler methods
* after generating a warning to remind users to move to the new BackHandler module.
*
* @providesModule BackAndroid
*/
'use strict';
var BackHandler = require('BackHandler');
var warning = require('fbjs/lib/warning');
/**
* Deprecated. Use BackHandler instead.
*/
var BackAndroid = {
exitApp: function() {
warning(false, 'BackAndroid is deprecated. Please use BackHandler instead.');
BackHandler.exitApp();
},
addEventListener: function (
eventName: BackPressEventName,
handler: Function
): {remove: () => void} {
warning(false, 'BackAndroid is deprecated. Please use BackHandler instead.');
return BackHandler.addEventListener(eventName, handler);
},
removeEventListener: function(
eventName: BackPressEventName,
handler: Function
): void {
warning(false, 'BackAndroid is deprecated. Please use BackHandler instead.');
BackHandler.removeEventListener(eventName, handler);
},
};
module.exports = BackAndroid;

View File

@@ -0,0 +1,103 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule BackHandler
*/
'use strict';
var DeviceEventManager = require('NativeModules').DeviceEventManager;
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
var DEVICE_BACK_EVENT = 'hardwareBackPress';
type BackPressEventName = $Enum<{
backPress: string,
}>;
var _backPressSubscriptions = new Set();
RCTDeviceEventEmitter.addListener(DEVICE_BACK_EVENT, function() {
var invokeDefault = true;
var subscriptions = Array.from(_backPressSubscriptions.values()).reverse();
for (var i = 0; i < subscriptions.length; ++i) {
if (subscriptions[i]()) {
invokeDefault = false;
break;
}
}
if (invokeDefault) {
BackHandler.exitApp();
}
});
/**
* Detect hardware button presses for back navigation.
*
* Android: Detect hardware back button presses, and programmatically invoke the default back button
* functionality to exit the app if there are no listeners or if none of the listeners return true.
*
* tvOS: Detect presses of the menu button on the TV remote. (Still to be implemented:
* programmatically disable menu button handling
* functionality to exit the app if there are no listeners or if none of the listeners return true.)
*
* iOS: Not applicable.
*
* The event subscriptions are called in reverse order (i.e. last registered subscription first),
* and if one subscription returns true then subscriptions registered earlier will not be called.
*
* Example:
*
* ```javascript
* BackHandler.addEventListener('hardwareBackPress', function() {
* // this.onMainScreen and this.goBack are just examples, you need to use your own implementation here
* // Typically you would use the navigator here to go to the last state.
*
* if (!this.onMainScreen()) {
* this.goBack();
* return true;
* }
* return false;
* });
* ```
*/
var BackHandler = {
exitApp: function() {
DeviceEventManager.invokeDefaultBackPressHandler();
},
/**
* Adds an event handler. Supported events:
*
* - `hardwareBackPress`: Fires when the Android hardware back button is pressed or when the
* tvOS menu button is pressed.
*/
addEventListener: function (
eventName: BackPressEventName,
handler: Function
): {remove: () => void} {
_backPressSubscriptions.add(handler);
return {
remove: () => BackHandler.removeEventListener(eventName, handler),
};
},
/**
* Removes the event handler.
*/
removeEventListener: function(
eventName: BackPressEventName,
handler: Function
): void {
_backPressSubscriptions.delete(handler);
},
};
module.exports = BackHandler;

View File

@@ -0,0 +1,114 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* On Apple TV, this implements back navigation using the TV remote's menu button.
* On iOS, this just implements a stub.
*
* @providesModule BackHandler
*/
'use strict';
const Platform = require('Platform');
const TVEventHandler = require('TVEventHandler');
type BackPressEventName = $Enum<{
backPress: string,
}>;
function emptyFunction() {}
/**
* Detect hardware button presses for back navigation.
*
* Android: Detect hardware back button presses, and programmatically invoke the default back button
* functionality to exit the app if there are no listeners or if none of the listeners return true.
*
* tvOS: Detect presses of the menu button on the TV remote. (Still to be implemented:
* programmatically disable menu button handling
* functionality to exit the app if there are no listeners or if none of the listeners return true.)
*
* iOS: Not applicable.
*
* The event subscriptions are called in reverse order (i.e. last registered subscription first),
* and if one subscription returns true then subscriptions registered earlier will not be called.
*
* Example:
*
* ```javascript
* BackHandler.addEventListener('hardwareBackPress', function() {
* // this.onMainScreen and this.goBack are just examples, you need to use your own implementation here
* // Typically you would use the navigator here to go to the last state.
*
* if (!this.onMainScreen()) {
* this.goBack();
* return true;
* }
* return false;
* });
* ```
*/
let BackHandler;
if (Platform.isTVOS) {
const _tvEventHandler = new TVEventHandler();
var _backPressSubscriptions = new Set();
_tvEventHandler.enable(this, function(cmp, evt) {
if (evt && evt.eventType === 'menu') {
var invokeDefault = true;
var subscriptions = Array.from(_backPressSubscriptions.values()).reverse();
for (var i = 0; i < subscriptions.length; ++i) {
if (subscriptions[i]()) {
invokeDefault = false;
break;
}
}
if (invokeDefault) {
BackHandler.exitApp();
}
}
});
BackHandler = {
exitApp: emptyFunction,
addEventListener: function (
eventName: BackPressEventName,
handler: Function
): {remove: () => void} {
_backPressSubscriptions.add(handler);
return {
remove: () => BackHandler.removeEventListener(eventName, handler),
};
},
removeEventListener: function(
eventName: BackPressEventName,
handler: Function
): void {
_backPressSubscriptions.delete(handler);
},
};
} else {
BackHandler = {
exitApp: emptyFunction,
addEventListener() {
return {
remove: emptyFunction,
};
},
removeEventListener: emptyFunction,
};
}
module.exports = BackHandler;

View File

@@ -0,0 +1,18 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule DeviceInfo
* @flow
*/
'use strict';
const DeviceInfo = require('NativeModules').DeviceInfo;
const invariant = require('fbjs/lib/invariant');
invariant(DeviceInfo, 'DeviceInfo native module is not installed correctly');
module.exports = DeviceInfo;

View File

@@ -0,0 +1,144 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule Dimensions
* @flow
*/
'use strict';
var EventEmitter = require('EventEmitter');
var Platform = require('Platform');
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
var invariant = require('fbjs/lib/invariant');
var eventEmitter = new EventEmitter();
var dimensionsInitialized = false;
var dimensions = {};
class Dimensions {
/**
* This should only be called from native code by sending the
* didUpdateDimensions event.
*
* @param {object} dims Simple string-keyed object of dimensions to set
*/
static set(dims: {[key:string]: any}): void {
// We calculate the window dimensions in JS so that we don't encounter loss of
// precision in transferring the dimensions (which could be non-integers) over
// the bridge.
if (dims && dims.windowPhysicalPixels) {
// parse/stringify => Clone hack
dims = JSON.parse(JSON.stringify(dims));
const windowPhysicalPixels = dims.windowPhysicalPixels;
dims.window = {
width: windowPhysicalPixels.width / windowPhysicalPixels.scale,
height: windowPhysicalPixels.height / windowPhysicalPixels.scale,
scale: windowPhysicalPixels.scale,
fontScale: windowPhysicalPixels.fontScale,
};
if (Platform.OS === 'android') {
// Screen and window dimensions are different on android
const screenPhysicalPixels = dims.screenPhysicalPixels;
dims.screen = {
width: screenPhysicalPixels.width / screenPhysicalPixels.scale,
height: screenPhysicalPixels.height / screenPhysicalPixels.scale,
scale: screenPhysicalPixels.scale,
fontScale: screenPhysicalPixels.fontScale,
};
// delete so no callers rely on this existing
delete dims.screenPhysicalPixels;
} else {
dims.screen = dims.window;
}
// delete so no callers rely on this existing
delete dims.windowPhysicalPixels;
}
Object.assign(dimensions, dims);
if (dimensionsInitialized) {
// Don't fire 'change' the first time the dimensions are set.
eventEmitter.emit('change', {
window: dimensions.window,
screen: dimensions.screen
});
} else {
dimensionsInitialized = true;
}
}
/**
* Initial dimensions are set before `runApplication` is called so they should
* be available before any other require's are run, but may be updated later.
*
* Note: Although dimensions are available immediately, they may change (e.g
* due to device rotation) so any rendering logic or styles that depend on
* these constants should try to call this function on every render, rather
* than caching the value (for example, using inline styles rather than
* setting a value in a `StyleSheet`).
*
* Example: `var {height, width} = Dimensions.get('window');`
*
* @param {string} dim Name of dimension as defined when calling `set`.
* @returns {Object?} Value for the dimension.
*/
static get(dim: string): Object {
invariant(dimensions[dim], 'No dimension set for key ' + dim);
return dimensions[dim];
}
/**
* Add an event handler. Supported events:
*
* - `change`: Fires when a property within the `Dimensions` object changes. The argument
* to the event handler is an object with `window` and `screen` properties whose values
* are the same as the return values of `Dimensions.get('window')` and
* `Dimensions.get('screen')`, respectively.
*/
static addEventListener(
type: string,
handler: Function
) {
invariant(
type === 'change',
'Trying to subscribe to unknown event: "%s"', type
);
eventEmitter.addListener(type, handler);
}
/**
* Remove an event handler.
*/
static removeEventListener(
type: string,
handler: Function
) {
invariant(
type === 'change',
'Trying to remove listener for unknown event: "%s"', type
);
eventEmitter.removeListener(type, handler);
}
}
let dims: ?{[key: string]: any} = global.nativeExtensions && global.nativeExtensions.DeviceInfo && global.nativeExtensions.DeviceInfo.Dimensions;
let nativeExtensionsEnabled = true;
if (!dims) {
const DeviceInfo = require('DeviceInfo');
dims = DeviceInfo.Dimensions;
nativeExtensionsEnabled = false;
}
invariant(dims, 'Either DeviceInfo native extension or DeviceInfo Native Module must be registered');
Dimensions.set(dims);
if (!nativeExtensionsEnabled) {
RCTDeviceEventEmitter.addListener('didUpdateDimensions', function(update) {
Dimensions.set(update);
});
}
module.exports = Dimensions;

View File

@@ -0,0 +1,97 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule HMRClient
* @format
* @flow
*/
'use strict';
const Platform = require('Platform');
const invariant = require('fbjs/lib/invariant');
const MetroHMRClient = require('metro/src/lib/bundle-modules/HMRClient');
/**
* HMR Client that receives from the server HMR updates and propagates them
* runtime to reflects those changes.
*/
const HMRClient = {
enable(platform: string, bundleEntry: string, host: string, port: number) {
invariant(platform, 'Missing required parameter `platform`');
invariant(bundleEntry, 'Missing required paramenter `bundleEntry`');
invariant(host, 'Missing required paramenter `host`');
// Moving to top gives errors due to NativeModules not being initialized
const HMRLoadingView = require('HMRLoadingView');
const wsHostPort = port !== null && port !== '' ? `${host}:${port}` : host;
bundleEntry = bundleEntry.replace(/\.(bundle|delta)/, '.js');
// Build the websocket url
const wsUrl =
`ws://${wsHostPort}/hot?` +
`platform=${platform}&` +
`bundleEntry=${bundleEntry}`;
const hmrClient = new MetroHMRClient(wsUrl);
hmrClient.on('connection-error', e => {
let error = `Hot loading isn't working because it cannot connect to the development server.
Try the following to fix the issue:
- Ensure that the packager server is running and available on the same network`;
if (Platform.OS === 'ios') {
error += `
- Ensure that the Packager server URL is correctly set in AppDelegate`;
} else {
error += `
- Ensure that your device/emulator is connected to your machine and has USB debugging enabled - run 'adb devices' to see a list of connected devices
- If you're on a physical device connected to the same machine, run 'adb reverse tcp:8081 tcp:8081' to forward requests from your device
- If your device is on the same Wi-Fi network, set 'Debug server host & port for device' in 'Dev settings' to your machine's IP address and the port of the local dev server - e.g. 10.0.1.1:8081`;
}
error += `
URL: ${host}:${port}
Error: ${e.message}`;
throw new Error(error);
});
hmrClient.on('update-start', () => {
HMRLoadingView.showMessage('Hot Loading...');
});
hmrClient.on('update', () => {
if (Platform.OS === 'ios') {
const RCTRedBox = require('NativeModules').RedBox;
RCTRedBox && RCTRedBox.dismiss && RCTRedBox.dismiss();
} else {
const RCTExceptionsManager = require('NativeModules').ExceptionsManager;
RCTExceptionsManager &&
RCTExceptionsManager.dismissRedbox &&
RCTExceptionsManager.dismissRedbox();
}
});
hmrClient.on('update-done', () => {
HMRLoadingView.hide();
});
hmrClient.on('error', data => {
HMRLoadingView.hide();
throw new Error(`${data.type} ${data.message}`);
});
hmrClient.enable();
},
};
module.exports = HMRClient;

View File

@@ -0,0 +1,36 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule HMRLoadingView
* @flow
*/
'use strict';
var ToastAndroid = require('ToastAndroid');
const TOAST_SHORT_DELAY = 2000;
class HMRLoadingView {
static _showing: boolean;
static showMessage(message: string) {
if (HMRLoadingView._showing) {
return;
}
ToastAndroid.show(message, ToastAndroid.SHORT);
HMRLoadingView._showing = true;
setTimeout(() => {
HMRLoadingView._showing = false;
}, TOAST_SHORT_DELAY);
}
static hide() {
// noop
}
}
module.exports = HMRLoadingView;

View File

@@ -0,0 +1,30 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule HMRLoadingView
* @flow
*/
'use strict';
const processColor = require('processColor');
const { DevLoadingView } = require('NativeModules');
class HMRLoadingView {
static showMessage(message: string) {
DevLoadingView.showMessage(
message,
processColor('#000000'),
processColor('#aaaaaa'),
);
}
static hide() {
DevLoadingView.hide();
}
}
module.exports = HMRLoadingView;

View File

@@ -0,0 +1,26 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule HeapCapture
* @flow
*/
'use strict';
var HeapCapture = {
captureHeap: function (path: string) {
var error = null;
try {
global.nativeCaptureHeap(path);
console.log('HeapCapture.captureHeap succeeded: ' + path);
} catch (e) {
console.log('HeapCapture.captureHeap error: ' + e.toString());
error = e.toString();
}
require('NativeModules').JSCHeapCapture.captureComplete(path, error);
},
};
module.exports = HeapCapture;

View File

@@ -0,0 +1,25 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule JSDevSupportModule
* @flow
*/
'use strict';
var JSDevSupportModule = {
getJSHierarchy: function (tag: string) {
const hook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
const renderers = hook._renderers;
const keys = Object.keys(renderers);
const renderer = renderers[keys[0]];
var result = renderer.getInspectorDataForViewTag(tag);
var path = result.hierarchy.map( (item) => item.name).join(' -> ');
require('NativeModules').JSDevSupport.setResult(path, null);
},
};
module.exports = JSDevSupportModule;

604
node_modules/react-native/Libraries/Utilities/MatrixMath.js generated vendored Executable file
View File

@@ -0,0 +1,604 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule MatrixMath
* @noflow
*/
/* eslint-disable space-infix-ops */
'use strict';
var invariant = require('fbjs/lib/invariant');
/**
* Memory conservative (mutative) matrix math utilities. Uses "command"
* matrices, which are reusable.
*/
var MatrixMath = {
createIdentityMatrix: function() {
return [
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1
];
},
createCopy: function(m) {
return [
m[0], m[1], m[2], m[3],
m[4], m[5], m[6], m[7],
m[8], m[9], m[10], m[11],
m[12], m[13], m[14], m[15],
];
},
createOrthographic: function(left, right, bottom, top, near, far) {
var a = 2 / (right - left);
var b = 2 / (top - bottom);
var c = -2 / (far - near);
var tx = -(right + left) / (right - left);
var ty = -(top + bottom) / (top - bottom);
var tz = -(far + near) / (far - near);
return [
a, 0, 0, 0,
0, b, 0, 0,
0, 0, c, 0,
tx, ty, tz, 1
];
},
createFrustum: function(left, right, bottom, top, near, far) {
var r_width = 1 / (right - left);
var r_height = 1 / (top - bottom);
var r_depth = 1 / (near - far);
var x = 2 * (near * r_width);
var y = 2 * (near * r_height);
var A = (right + left) * r_width;
var B = (top + bottom) * r_height;
var C = (far + near) * r_depth;
var D = 2 * (far * near * r_depth);
return [
x, 0, 0, 0,
0, y, 0, 0,
A, B, C,-1,
0, 0, D, 0,
];
},
/**
* This create a perspective projection towards negative z
* Clipping the z range of [-near, -far]
*
* @param fovInRadians - field of view in randians
*/
createPerspective: function(fovInRadians, aspect, near, far) {
var h = 1 / Math.tan(fovInRadians / 2);
var r_depth = 1 / (near - far);
var C = (far + near) * r_depth;
var D = 2 * (far * near * r_depth);
return [
h/aspect, 0, 0, 0,
0, h, 0, 0,
0, 0, C,-1,
0, 0, D, 0,
];
},
createTranslate2d: function(x, y) {
var mat = MatrixMath.createIdentityMatrix();
MatrixMath.reuseTranslate2dCommand(mat, x, y);
return mat;
},
reuseTranslate2dCommand: function(matrixCommand, x, y) {
matrixCommand[12] = x;
matrixCommand[13] = y;
},
reuseTranslate3dCommand: function(matrixCommand, x, y, z) {
matrixCommand[12] = x;
matrixCommand[13] = y;
matrixCommand[14] = z;
},
createScale: function(factor) {
var mat = MatrixMath.createIdentityMatrix();
MatrixMath.reuseScaleCommand(mat, factor);
return mat;
},
reuseScaleCommand: function(matrixCommand, factor) {
matrixCommand[0] = factor;
matrixCommand[5] = factor;
},
reuseScale3dCommand: function(matrixCommand, x, y, z) {
matrixCommand[0] = x;
matrixCommand[5] = y;
matrixCommand[10] = z;
},
reusePerspectiveCommand: function(matrixCommand, p) {
matrixCommand[11] = -1 / p;
},
reuseScaleXCommand(matrixCommand, factor) {
matrixCommand[0] = factor;
},
reuseScaleYCommand(matrixCommand, factor) {
matrixCommand[5] = factor;
},
reuseScaleZCommand(matrixCommand, factor) {
matrixCommand[10] = factor;
},
reuseRotateXCommand: function(matrixCommand, radians) {
matrixCommand[5] = Math.cos(radians);
matrixCommand[6] = Math.sin(radians);
matrixCommand[9] = -Math.sin(radians);
matrixCommand[10] = Math.cos(radians);
},
reuseRotateYCommand: function(matrixCommand, amount) {
matrixCommand[0] = Math.cos(amount);
matrixCommand[2] = -Math.sin(amount);
matrixCommand[8] = Math.sin(amount);
matrixCommand[10] = Math.cos(amount);
},
// http://www.w3.org/TR/css3-transforms/#recomposing-to-a-2d-matrix
reuseRotateZCommand: function(matrixCommand, radians) {
matrixCommand[0] = Math.cos(radians);
matrixCommand[1] = Math.sin(radians);
matrixCommand[4] = -Math.sin(radians);
matrixCommand[5] = Math.cos(radians);
},
createRotateZ: function(radians) {
var mat = MatrixMath.createIdentityMatrix();
MatrixMath.reuseRotateZCommand(mat, radians);
return mat;
},
reuseSkewXCommand: function(matrixCommand, radians) {
matrixCommand[4] = Math.tan(radians);
},
reuseSkewYCommand: function(matrixCommand, radians) {
matrixCommand[1] = Math.tan(radians);
},
multiplyInto: function(out, a, b) {
var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
out[0] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
out[1] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
out[2] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
out[3] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
b0 = b[4]; b1 = b[5]; b2 = b[6]; b3 = b[7];
out[4] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
out[5] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
out[6] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
out[7] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
b0 = b[8]; b1 = b[9]; b2 = b[10]; b3 = b[11];
out[8] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
out[9] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
out[10] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
out[11] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
b0 = b[12]; b1 = b[13]; b2 = b[14]; b3 = b[15];
out[12] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
out[13] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
out[14] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
out[15] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
},
determinant(matrix: Array<number>): number {
var [
m00, m01, m02, m03,
m10, m11, m12, m13,
m20, m21, m22, m23,
m30, m31, m32, m33
] = matrix;
return (
m03 * m12 * m21 * m30 - m02 * m13 * m21 * m30 -
m03 * m11 * m22 * m30 + m01 * m13 * m22 * m30 +
m02 * m11 * m23 * m30 - m01 * m12 * m23 * m30 -
m03 * m12 * m20 * m31 + m02 * m13 * m20 * m31 +
m03 * m10 * m22 * m31 - m00 * m13 * m22 * m31 -
m02 * m10 * m23 * m31 + m00 * m12 * m23 * m31 +
m03 * m11 * m20 * m32 - m01 * m13 * m20 * m32 -
m03 * m10 * m21 * m32 + m00 * m13 * m21 * m32 +
m01 * m10 * m23 * m32 - m00 * m11 * m23 * m32 -
m02 * m11 * m20 * m33 + m01 * m12 * m20 * m33 +
m02 * m10 * m21 * m33 - m00 * m12 * m21 * m33 -
m01 * m10 * m22 * m33 + m00 * m11 * m22 * m33
);
},
/**
* Inverse of a matrix. Multiplying by the inverse is used in matrix math
* instead of division.
*
* Formula from:
* http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
*/
inverse(matrix: Array<number>): Array<number> {
var det = MatrixMath.determinant(matrix);
if (!det) {
return matrix;
}
var [
m00, m01, m02, m03,
m10, m11, m12, m13,
m20, m21, m22, m23,
m30, m31, m32, m33
] = matrix;
return [
(m12*m23*m31 - m13*m22*m31 + m13*m21*m32 - m11*m23*m32 - m12*m21*m33 + m11*m22*m33) / det,
(m03*m22*m31 - m02*m23*m31 - m03*m21*m32 + m01*m23*m32 + m02*m21*m33 - m01*m22*m33) / det,
(m02*m13*m31 - m03*m12*m31 + m03*m11*m32 - m01*m13*m32 - m02*m11*m33 + m01*m12*m33) / det,
(m03*m12*m21 - m02*m13*m21 - m03*m11*m22 + m01*m13*m22 + m02*m11*m23 - m01*m12*m23) / det,
(m13*m22*m30 - m12*m23*m30 - m13*m20*m32 + m10*m23*m32 + m12*m20*m33 - m10*m22*m33) / det,
(m02*m23*m30 - m03*m22*m30 + m03*m20*m32 - m00*m23*m32 - m02*m20*m33 + m00*m22*m33) / det,
(m03*m12*m30 - m02*m13*m30 - m03*m10*m32 + m00*m13*m32 + m02*m10*m33 - m00*m12*m33) / det,
(m02*m13*m20 - m03*m12*m20 + m03*m10*m22 - m00*m13*m22 - m02*m10*m23 + m00*m12*m23) / det,
(m11*m23*m30 - m13*m21*m30 + m13*m20*m31 - m10*m23*m31 - m11*m20*m33 + m10*m21*m33) / det,
(m03*m21*m30 - m01*m23*m30 - m03*m20*m31 + m00*m23*m31 + m01*m20*m33 - m00*m21*m33) / det,
(m01*m13*m30 - m03*m11*m30 + m03*m10*m31 - m00*m13*m31 - m01*m10*m33 + m00*m11*m33) / det,
(m03*m11*m20 - m01*m13*m20 - m03*m10*m21 + m00*m13*m21 + m01*m10*m23 - m00*m11*m23) / det,
(m12*m21*m30 - m11*m22*m30 - m12*m20*m31 + m10*m22*m31 + m11*m20*m32 - m10*m21*m32) / det,
(m01*m22*m30 - m02*m21*m30 + m02*m20*m31 - m00*m22*m31 - m01*m20*m32 + m00*m21*m32) / det,
(m02*m11*m30 - m01*m12*m30 - m02*m10*m31 + m00*m12*m31 + m01*m10*m32 - m00*m11*m32) / det,
(m01*m12*m20 - m02*m11*m20 + m02*m10*m21 - m00*m12*m21 - m01*m10*m22 + m00*m11*m22) / det
];
},
/**
* Turns columns into rows and rows into columns.
*/
transpose(m: Array<number>): Array<number> {
return [
m[0], m[4], m[8], m[12],
m[1], m[5], m[9], m[13],
m[2], m[6], m[10], m[14],
m[3], m[7], m[11], m[15]
];
},
/**
* Based on: http://tog.acm.org/resources/GraphicsGems/gemsii/unmatrix.c
*/
multiplyVectorByMatrix(
v: Array<number>,
m: Array<number>
): Array<number> {
var [vx, vy, vz, vw] = v;
return [
vx * m[0] + vy * m[4] + vz * m[8] + vw * m[12],
vx * m[1] + vy * m[5] + vz * m[9] + vw * m[13],
vx * m[2] + vy * m[6] + vz * m[10] + vw * m[14],
vx * m[3] + vy * m[7] + vz * m[11] + vw * m[15]
];
},
/**
* From: https://code.google.com/p/webgl-mjs/source/browse/mjs.js
*/
v3Length(a: Array<number>): number {
return Math.sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
},
/**
* Based on: https://code.google.com/p/webgl-mjs/source/browse/mjs.js
*/
v3Normalize(
vector: Array<number>,
v3Length: number
): Array<number> {
var im = 1 / (v3Length || MatrixMath.v3Length(vector));
return [
vector[0] * im,
vector[1] * im,
vector[2] * im
];
},
/**
* The dot product of a and b, two 3-element vectors.
* From: https://code.google.com/p/webgl-mjs/source/browse/mjs.js
*/
v3Dot(a, b) {
return a[0] * b[0] +
a[1] * b[1] +
a[2] * b[2];
},
/**
* From:
* http://www.opensource.apple.com/source/WebCore/WebCore-514/platform/graphics/transforms/TransformationMatrix.cpp
*/
v3Combine(
a: Array<number>,
b: Array<number>,
aScale: number,
bScale: number
): Array<number> {
return [
aScale * a[0] + bScale * b[0],
aScale * a[1] + bScale * b[1],
aScale * a[2] + bScale * b[2]
];
},
/**
* From:
* http://www.opensource.apple.com/source/WebCore/WebCore-514/platform/graphics/transforms/TransformationMatrix.cpp
*/
v3Cross(a: Array<number>, b: Array<number>): Array<number> {
return [
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0]
];
},
/**
* Based on:
* http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/
* and:
* http://quat.zachbennett.com/
*
* Note that this rounds degrees to the thousandth of a degree, due to
* floating point errors in the creation of the quaternion.
*
* Also note that this expects the qw value to be last, not first.
*
* Also, when researching this, remember that:
* yaw === heading === z-axis
* pitch === elevation/attitude === y-axis
* roll === bank === x-axis
*/
quaternionToDegreesXYZ(q: Array<number>, matrix, row): Array<number> {
var [qx, qy, qz, qw] = q;
var qw2 = qw * qw;
var qx2 = qx * qx;
var qy2 = qy * qy;
var qz2 = qz * qz;
var test = qx * qy + qz * qw;
var unit = qw2 + qx2 + qy2 + qz2;
var conv = 180 / Math.PI;
if (test > 0.49999 * unit) {
return [0, 2 * Math.atan2(qx, qw) * conv, 90];
}
if (test < -0.49999 * unit) {
return [0, -2 * Math.atan2(qx, qw) * conv, -90];
}
return [
MatrixMath.roundTo3Places(
Math.atan2(2*qx*qw-2*qy*qz,1-2*qx2-2*qz2) * conv
),
MatrixMath.roundTo3Places(
Math.atan2(2*qy*qw-2*qx*qz,1-2*qy2-2*qz2) * conv
),
MatrixMath.roundTo3Places(
Math.asin(2*qx*qy+2*qz*qw) * conv
)
];
},
/**
* Based on:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
*/
roundTo3Places(n: number): number {
var arr = n.toString().split('e');
return Math.round(arr[0] + 'e' + (arr[1] ? (+arr[1] - 3) : 3)) * 0.001;
},
/**
* Decompose a matrix into separate transform values, for use on platforms
* where applying a precomposed matrix is not possible, and transforms are
* applied in an inflexible ordering (e.g. Android).
*
* Implementation based on
* http://www.w3.org/TR/css3-transforms/#decomposing-a-2d-matrix
* http://www.w3.org/TR/css3-transforms/#decomposing-a-3d-matrix
* which was based on
* http://tog.acm.org/resources/GraphicsGems/gemsii/unmatrix.c
*/
decomposeMatrix(transformMatrix: Array<number>): ?Object {
invariant(
transformMatrix.length === 16,
'Matrix decomposition needs a list of 3d matrix values, received %s',
transformMatrix
);
// output values
var perspective = [];
var quaternion = [];
var scale = [];
var skew = [];
var translation = [];
// create normalized, 2d array matrix
// and normalized 1d array perspectiveMatrix with redefined 4th column
if (!transformMatrix[15]) {
return;
}
var matrix = [];
var perspectiveMatrix = [];
for (var i = 0; i < 4; i++) {
matrix.push([]);
for (var j = 0; j < 4; j++) {
var value = transformMatrix[(i * 4) + j] / transformMatrix[15];
matrix[i].push(value);
perspectiveMatrix.push(j === 3 ? 0 : value);
}
}
perspectiveMatrix[15] = 1;
// test for singularity of upper 3x3 part of the perspective matrix
if (!MatrixMath.determinant(perspectiveMatrix)) {
return;
}
// isolate perspective
if (matrix[0][3] !== 0 || matrix[1][3] !== 0 || matrix[2][3] !== 0) {
// rightHandSide is the right hand side of the equation.
// rightHandSide is a vector, or point in 3d space relative to the origin.
var rightHandSide = [
matrix[0][3],
matrix[1][3],
matrix[2][3],
matrix[3][3]
];
// Solve the equation by inverting perspectiveMatrix and multiplying
// rightHandSide by the inverse.
var inversePerspectiveMatrix = MatrixMath.inverse(
perspectiveMatrix
);
var transposedInversePerspectiveMatrix = MatrixMath.transpose(
inversePerspectiveMatrix
);
var perspective = MatrixMath.multiplyVectorByMatrix(
rightHandSide,
transposedInversePerspectiveMatrix
);
} else {
// no perspective
perspective[0] = perspective[1] = perspective[2] = 0;
perspective[3] = 1;
}
// translation is simple
for (var i = 0; i < 3; i++) {
translation[i] = matrix[3][i];
}
// Now get scale and shear.
// 'row' is a 3 element array of 3 component vectors
var row = [];
for (i = 0; i < 3; i++) {
row[i] = [
matrix[i][0],
matrix[i][1],
matrix[i][2]
];
}
// Compute X scale factor and normalize first row.
scale[0] = MatrixMath.v3Length(row[0]);
row[0] = MatrixMath.v3Normalize(row[0], scale[0]);
// Compute XY shear factor and make 2nd row orthogonal to 1st.
skew[0] = MatrixMath.v3Dot(row[0], row[1]);
row[1] = MatrixMath.v3Combine(row[1], row[0], 1.0, -skew[0]);
// Compute XY shear factor and make 2nd row orthogonal to 1st.
skew[0] = MatrixMath.v3Dot(row[0], row[1]);
row[1] = MatrixMath.v3Combine(row[1], row[0], 1.0, -skew[0]);
// Now, compute Y scale and normalize 2nd row.
scale[1] = MatrixMath.v3Length(row[1]);
row[1] = MatrixMath.v3Normalize(row[1], scale[1]);
skew[0] /= scale[1];
// Compute XZ and YZ shears, orthogonalize 3rd row
skew[1] = MatrixMath.v3Dot(row[0], row[2]);
row[2] = MatrixMath.v3Combine(row[2], row[0], 1.0, -skew[1]);
skew[2] = MatrixMath.v3Dot(row[1], row[2]);
row[2] = MatrixMath.v3Combine(row[2], row[1], 1.0, -skew[2]);
// Next, get Z scale and normalize 3rd row.
scale[2] = MatrixMath.v3Length(row[2]);
row[2] = MatrixMath.v3Normalize(row[2], scale[2]);
skew[1] /= scale[2];
skew[2] /= scale[2];
// At this point, the matrix (in rows) is orthonormal.
// Check for a coordinate system flip. If the determinant
// is -1, then negate the matrix and the scaling factors.
var pdum3 = MatrixMath.v3Cross(row[1], row[2]);
if (MatrixMath.v3Dot(row[0], pdum3) < 0) {
for (i = 0; i < 3; i++) {
scale[i] *= -1;
row[i][0] *= -1;
row[i][1] *= -1;
row[i][2] *= -1;
}
}
// Now, get the rotations out
quaternion[0] =
0.5 * Math.sqrt(Math.max(1 + row[0][0] - row[1][1] - row[2][2], 0));
quaternion[1] =
0.5 * Math.sqrt(Math.max(1 - row[0][0] + row[1][1] - row[2][2], 0));
quaternion[2] =
0.5 * Math.sqrt(Math.max(1 - row[0][0] - row[1][1] + row[2][2], 0));
quaternion[3] =
0.5 * Math.sqrt(Math.max(1 + row[0][0] + row[1][1] + row[2][2], 0));
if (row[2][1] > row[1][2]) {
quaternion[0] = -quaternion[0];
}
if (row[0][2] > row[2][0]) {
quaternion[1] = -quaternion[1];
}
if (row[1][0] > row[0][1]) {
quaternion[2] = -quaternion[2];
}
// correct for occasional, weird Euler synonyms for 2d rotation
var rotationDegrees;
if (
quaternion[0] < 0.001 && quaternion[0] >= 0 &&
quaternion[1] < 0.001 && quaternion[1] >= 0
) {
// this is a 2d rotation on the z-axis
rotationDegrees = [0, 0, MatrixMath.roundTo3Places(
Math.atan2(row[0][1], row[0][0]) * 180 / Math.PI
)];
} else {
rotationDegrees = MatrixMath.quaternionToDegreesXYZ(quaternion, matrix, row);
}
// expose both base data and convenience names
return {
rotationDegrees,
perspective,
quaternion,
scale,
skew,
translation,
rotate: rotationDegrees[2],
rotateX: rotationDegrees[0],
rotateY: rotationDegrees[1],
scaleX: scale[0],
scaleY: scale[1],
translateX: translation[0],
translateY: translation[1],
};
},
};
module.exports = MatrixMath;

View File

@@ -0,0 +1,188 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule PerformanceLogger
* @flow
* @format
*/
'use strict';
const Systrace = require('Systrace');
const infoLog = require('infoLog');
const performanceNow =
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an
* error found when Flow v0.54 was deployed. To see the error delete this
* comment and run Flow. */
global.nativePerformanceNow || require('fbjs/lib/performanceNow');
type Timespan = {
description?: string,
totalTime?: number,
startTime?: number,
endTime?: number,
};
let timespans: {[key: string]: Timespan} = {};
let extras: {[key: string]: any} = {};
const cookies: {[key: string]: number} = {};
const PRINT_TO_CONSOLE = false;
/**
* This is meant to collect and log performance data in production, which means
* it needs to have minimal overhead.
*/
const PerformanceLogger = {
addTimespan(key: string, lengthInMs: number, description?: string) {
if (timespans[key]) {
if (__DEV__) {
infoLog(
'PerformanceLogger: Attempting to add a timespan that already exists ',
key,
);
}
return;
}
timespans[key] = {
description: description,
totalTime: lengthInMs,
};
},
startTimespan(key: string, description?: string) {
if (timespans[key]) {
if (__DEV__) {
infoLog(
'PerformanceLogger: Attempting to start a timespan that already exists ',
key,
);
}
return;
}
timespans[key] = {
description: description,
startTime: performanceNow(),
};
cookies[key] = Systrace.beginAsyncEvent(key);
if (__DEV__ && PRINT_TO_CONSOLE) {
infoLog('PerformanceLogger.js', 'start: ' + key);
}
},
stopTimespan(key: string) {
const timespan = timespans[key];
if (!timespan || !timespan.startTime) {
if (__DEV__) {
infoLog(
'PerformanceLogger: Attempting to end a timespan that has not started ',
key,
);
}
return;
}
if (timespan.endTime) {
if (__DEV__) {
infoLog(
'PerformanceLogger: Attempting to end a timespan that has already ended ',
key,
);
}
return;
}
timespan.endTime = performanceNow();
timespan.totalTime = timespan.endTime - (timespan.startTime || 0);
if (__DEV__ && PRINT_TO_CONSOLE) {
infoLog('PerformanceLogger.js', 'end: ' + key);
}
Systrace.endAsyncEvent(key, cookies[key]);
delete cookies[key];
},
clear() {
timespans = {};
extras = {};
if (__DEV__ && PRINT_TO_CONSOLE) {
infoLog('PerformanceLogger.js', 'clear');
}
},
clearCompleted() {
for (const key in timespans) {
if (timespans[key].totalTime) {
delete timespans[key];
}
}
extras = {};
if (__DEV__ && PRINT_TO_CONSOLE) {
infoLog('PerformanceLogger.js', 'clearCompleted');
}
},
clearExceptTimespans(keys: Array<string>) {
timespans = Object.keys(timespans).reduce(function(previous, key) {
if (keys.indexOf(key) !== -1) {
previous[key] = timespans[key];
}
return previous;
}, {});
extras = {};
},
currentTimestamp() {
return performanceNow();
},
getTimespans() {
return timespans;
},
hasTimespan(key: string) {
return !!timespans[key];
},
logTimespans() {
for (const key in timespans) {
if (timespans[key].totalTime) {
infoLog(key + ': ' + timespans[key].totalTime + 'ms');
}
}
},
addTimespans(newTimespans: Array<number>, labels: Array<string>) {
for (let ii = 0, l = newTimespans.length; ii < l; ii += 2) {
const label = labels[ii / 2];
PerformanceLogger.addTimespan(
label,
newTimespans[ii + 1] - newTimespans[ii],
label,
);
}
},
setExtra(key: string, value: any) {
if (extras[key]) {
if (__DEV__) {
infoLog(
'PerformanceLogger: Attempting to set an extra that already exists ',
{key, currentValue: extras[key], attemptedValue: value},
);
}
return;
}
extras[key] = value;
},
getExtras() {
return extras;
},
};
module.exports = PerformanceLogger;

View File

@@ -0,0 +1,120 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule PixelRatio
* @flow
*/
'use strict';
var Dimensions = require('Dimensions');
/**
* PixelRatio class gives access to the device pixel density.
*
* ## Fetching a correctly sized image
*
* You should get a higher resolution image if you are on a high pixel density
* device. A good rule of thumb is to multiply the size of the image you display
* by the pixel ratio.
*
* ```
* var image = getImage({
* width: PixelRatio.getPixelSizeForLayoutSize(200),
* height: PixelRatio.getPixelSizeForLayoutSize(100),
* });
* <Image source={image} style={{width: 200, height: 100}} />
* ```
*
* ## Pixel grid snapping
*
* In iOS, you can specify positions and dimensions for elements with arbitrary
* precision, for example 29.674825. But, ultimately the physical display only
* have a fixed number of pixels, for example 640×960 for iPhone 4 or 750×1334
* for iPhone 6. iOS tries to be as faithful as possible to the user value by
* spreading one original pixel into multiple ones to trick the eye. The
* downside of this technique is that it makes the resulting element look
* blurry.
*
* In practice, we found out that developers do not want this feature and they
* have to work around it by doing manual rounding in order to avoid having
* blurry elements. In React Native, we are rounding all the pixels
* automatically.
*
* We have to be careful when to do this rounding. You never want to work with
* rounded and unrounded values at the same time as you're going to accumulate
* rounding errors. Having even one rounding error is deadly because a one
* pixel border may vanish or be twice as big.
*
* In React Native, everything in JavaScript and within the layout engine works
* with arbitrary precision numbers. It's only when we set the position and
* dimensions of the native element on the main thread that we round. Also,
* rounding is done relative to the root rather than the parent, again to avoid
* accumulating rounding errors.
*
*/
class PixelRatio {
/**
* Returns the device pixel density. Some examples:
*
* - PixelRatio.get() === 1
* - mdpi Android devices (160 dpi)
* - PixelRatio.get() === 1.5
* - hdpi Android devices (240 dpi)
* - PixelRatio.get() === 2
* - iPhone 4, 4S
* - iPhone 5, 5c, 5s
* - iPhone 6
* - xhdpi Android devices (320 dpi)
* - PixelRatio.get() === 3
* - iPhone 6 plus
* - xxhdpi Android devices (480 dpi)
* - PixelRatio.get() === 3.5
* - Nexus 6
*/
static get(): number {
return Dimensions.get('window').scale;
}
/**
* Returns the scaling factor for font sizes. This is the ratio that is used to calculate the
* absolute font size, so any elements that heavily depend on that should use this to do
* calculations.
*
* If a font scale is not set, this returns the device pixel ratio.
*
* Currently this is only implemented on Android and reflects the user preference set in
* Settings > Display > Font size, on iOS it will always return the default pixel ratio.
* @platform android
*/
static getFontScale(): number {
return Dimensions.get('window').fontScale || PixelRatio.get();
}
/**
* Converts a layout size (dp) to pixel size (px).
*
* Guaranteed to return an integer number.
*/
static getPixelSizeForLayoutSize(layoutSize: number): number {
return Math.round(layoutSize * PixelRatio.get());
}
/**
* Rounds a layout size (dp) to the nearest layout size that corresponds to
* an integer number of pixels. For example, on a device with a PixelRatio
* of 3, `PixelRatio.roundToNearestPixel(8.4) = 8.33`, which corresponds to
* exactly (8.33 * 3) = 25 pixels.
*/
static roundToNearestPixel(layoutSize: number): number {
var ratio = PixelRatio.get();
return Math.round(layoutSize * ratio) / ratio;
}
// No-op for iOS, but used on the web. Should not be documented.
static startDetecting() {}
}
module.exports = PixelRatio;

View File

@@ -0,0 +1,32 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule Platform
* @flow
*/
'use strict';
const NativeModules = require('NativeModules');
const Platform = {
OS: 'android',
get Version() {
const constants = NativeModules.PlatformConstants;
return constants && constants.Version;
},
get isTesting(): boolean {
const constants = NativeModules.PlatformConstants;
return constants && constants.isTesting;
},
get isTV(): boolean {
const constants = NativeModules.PlatformConstants;
return constants && constants.uiMode === 'tv';
},
select: (obj: Object) => 'android' in obj ? obj.android : obj.default,
};
module.exports = Platform;

View File

@@ -0,0 +1,42 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule Platform
* @flow
*/
'use strict';
const NativeModules = require('NativeModules');
const Platform = {
OS: 'ios',
get Version() {
const constants = NativeModules.PlatformConstants;
return constants && constants.osVersion;
},
get isPad() {
const constants = NativeModules.PlatformConstants;
return constants ? constants.interfaceIdiom === 'pad' : false;
},
/**
* Deprecated, use `isTV` instead.
*/
get isTVOS() {
return Platform.isTV;
},
get isTV() {
const constants = NativeModules.PlatformConstants;
return constants ? constants.interfaceIdiom === 'tv' : false;
},
get isTesting(): boolean {
const constants = NativeModules.PlatformConstants;
return constants && constants.isTesting;
},
select: (obj: Object) => 'ios' in obj ? obj.ios : obj.default,
};
module.exports = Platform;

View File

@@ -0,0 +1,23 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule PlatformOS
* @flow
*/
'use strict';
export type PlatformSelectSpec<A, I> = {|
android: A,
ios: I,
|};
const PlatformOS = {
OS: 'android',
select: <A, I> (spec: PlatformSelectSpec<A, I>): A | I => spec.android,
};
module.exports = PlatformOS;

View File

@@ -0,0 +1,23 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule PlatformOS
* @flow
*/
'use strict';
export type PlatformSelectSpec<A, I> = {|
android: A,
ios: I,
|};
const PlatformOS = {
OS: 'ios',
select: <A, I> (spec: PlatformSelectSpec<A, I>): A | I => spec.ios,
};
module.exports = PlatformOS;

View File

@@ -0,0 +1,60 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule PolyfillFunctions
* @flow
* @format
*/
'use strict';
const defineLazyObjectProperty = require('defineLazyObjectProperty');
/**
* Sets an object's property. If a property with the same name exists, this will
* replace it but maintain its descriptor configuration. The property will be
* replaced with a lazy getter.
*
* In DEV mode the original property value will be preserved as `original[PropertyName]`
* so that, if necessary, it can be restored. For example, if you want to route
* network requests through DevTools (to trace them):
*
* global.XMLHttpRequest = global.originalXMLHttpRequest;
*
* @see https://github.com/facebook/react-native/issues/934
*/
function polyfillObjectProperty<T>(
object: Object,
name: string,
getValue: () => T,
): void {
const descriptor = Object.getOwnPropertyDescriptor(object, name);
if (__DEV__ && descriptor) {
const backupName = `original${name[0].toUpperCase()}${name.substr(1)}`;
Object.defineProperty(object, backupName, {
...descriptor,
value: object[name],
});
}
const {enumerable, writable, configurable} = descriptor || {};
if (descriptor && !configurable) {
console.error('Failed to set polyfill. ' + name + ' is not configurable.');
return;
}
defineLazyObjectProperty(object, name, {
get: getValue,
enumerable: enumerable !== false,
writable: writable !== false,
});
}
function polyfillGlobal<T>(name: string, getValue: () => T): void {
polyfillObjectProperty(global, name, getValue);
}
module.exports = {polyfillObjectProperty, polyfillGlobal};

View File

@@ -0,0 +1,54 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule RCTLog
* @flow
*/
'use strict';
const invariant = require('fbjs/lib/invariant');
const levelsMap = {
log: 'log',
info: 'info',
warn: 'warn',
error: 'error',
fatal: 'error',
};
let warningHandler: ?(Array<any> => void) = null;
const RCTLog = {
// level one of log, info, warn, error, mustfix
logIfNoNativeHook(level: string, ...args: Array<any>): void {
// We already printed in the native console, so only log here if using a js debugger
if (typeof global.nativeLoggingHook === 'undefined') {
RCTLog.logToConsole(level, ...args);
} else {
// Report native warnings to YellowBox
if (warningHandler && level === 'warn') {
warningHandler(...args);
}
}
},
// Log to console regardless of nativeLoggingHook
logToConsole(level: string, ...args: Array<any>): void {
const logFn = levelsMap[level];
invariant(
logFn,
'Level "' + level + '" not one of ' + Object.keys(levelsMap).toString()
);
console[logFn](...args);
},
setWarningHandler(handler: typeof warningHandler): void {
warningHandler = handler;
}
};
module.exports = RCTLog;

View File

@@ -0,0 +1,39 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule SceneTracker
* @flow
*/
'use strict';
type Scene = {name: string};
let _listeners: Array<(scene: Scene) => void> = [];
let _activeScene = {name: 'default'};
const SceneTracker = {
setActiveScene(scene: Scene) {
_activeScene = scene;
_listeners.forEach((listener) => listener(_activeScene));
},
getActiveScene(): Scene {
return _activeScene;
},
addActiveSceneChangedListener(callback: (scene: Scene) => void): {remove: () => void} {
_listeners.push(callback);
return {
remove: () => {
_listeners = _listeners.filter((listener) => callback !== listener);
},
};
},
};
module.exports = SceneTracker;

View File

@@ -0,0 +1,50 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
'use strict';
const _backPressSubscriptions = new Set();
const BackHandler = {
exitApp: jest.fn(),
addEventListener: function (
eventName: BackPressEventName,
handler: Function
): {remove: () => void} {
_backPressSubscriptions.add(handler);
return {
remove: () => BackHandler.removeEventListener(eventName, handler),
};
},
removeEventListener: function(
eventName: BackPressEventName,
handler: Function
): void {
_backPressSubscriptions.delete(handler);
},
mockPressBack: function() {
let invokeDefault = true;
const subscriptions = [..._backPressSubscriptions].reverse();
for (let i = 0; i < subscriptions.length; ++i) {
if (subscriptions[i]()) {
invokeDefault = false;
break;
}
}
if (invokeDefault) {
BackHandler.exitApp();
}
},
};
module.exports = BackHandler;

View File

@@ -0,0 +1,21 @@
/**
* Copyright (c) 2004-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const PixelRatio = {
get: jest.fn().mockReturnValue(2),
getFontScale: jest.fn(() => PixelRatio.get()),
getPixelSizeForLayoutSize: jest.fn(layoutSize => Math.round(layoutSize * PixelRatio.get())),
roundToNearestPixel: jest.fn(layoutSize => {
const ratio = PixelRatio.get();
return Math.round(layoutSize * ratio) / ratio;
}),
startDetecting: jest.fn(),
};
module.exports = PixelRatio;

View File

@@ -0,0 +1,31 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule binaryToBase64
* @flow
*/
'use strict';
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
* found when Flow v0.54 was deployed. To see the error delete this comment and
* run Flow. */
const base64 = require('base64-js');
function binaryToBase64(data: ArrayBuffer | $ArrayBufferView) {
if (data instanceof ArrayBuffer) {
data = new Uint8Array(data);
}
if (data instanceof Uint8Array) {
return base64.fromByteArray(data);
}
if (!ArrayBuffer.isView(data)) {
throw new Error('data must be ArrayBuffer or typed array');
}
const {buffer, byteOffset, byteLength} = data;
return base64.fromByteArray(new Uint8Array(buffer, byteOffset, byteLength));
}
module.exports = binaryToBase64;

View File

@@ -0,0 +1,179 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule buildStyleInterpolator
*/
'use strict';
var keyOf = require('fbjs/lib/keyOf');
var X_DIM = keyOf({x: null});
var Y_DIM = keyOf({y: null});
var Z_DIM = keyOf({z: null});
var InitialOperationField = {
transformTranslate: [0, 0, 0],
transformScale: [1, 1, 1],
};
var InterpolateMatrix = {
transformScale: function(mat, x, y, z) {
mat[0] = mat[0] * x;
mat[1] = mat[1] * x;
mat[2] = mat[2] * x;
mat[3] = mat[3] * x;
mat[4] = mat[4] * y;
mat[5] = mat[5] * y;
mat[6] = mat[6] * y;
mat[7] = mat[7] * y;
mat[8] = mat[8] * z;
mat[9] = mat[9] * z;
mat[10] = mat[10] * z;
mat[11] = mat[11] * z;
},
transformTranslate: function(mat, x, y, z) {
mat[12] = mat[0] * x + mat[4] * y + mat[8] * z + mat[12];
mat[13] = mat[1] * x + mat[5] * y + mat[9] * z + mat[13];
mat[14] = mat[2] * x + mat[6] * y + mat[10] * z + mat[14];
mat[15] = mat[3] * x + mat[7] * y + mat[11] * z + mat[15];
}
};
var computeNextValLinear = function(anim, from, to, value) {
var hasRoundRatio = 'round' in anim;
var roundRatio = anim.round;
var ratio = (value - anim.min) / (anim.max - anim.min);
if (!anim.extrapolate) {
ratio = ratio > 1 ? 1 : (ratio < 0 ? 0 : ratio);
}
var nextVal = from * (1 - ratio) + to * ratio;
if (hasRoundRatio) {
nextVal = Math.round(roundRatio * nextVal) / roundRatio;
}
return nextVal;
};
var computeNextValLinearScalar = function(anim, value) {
return computeNextValLinear(anim, anim.from, anim.to, value);
};
var setNextValAndDetectChange = function(result, name, nextVal, didChange) {
if (!didChange) {
var prevVal = result[name];
result[name] = nextVal;
didChange = didChange || (nextVal !== prevVal);
} else {
result[name] = nextVal;
}
return didChange;
};
var initIdentity = function(mat) {
mat[0] = 1;
mat[1] = 0;
mat[2] = 0;
mat[3] = 0;
mat[4] = 0;
mat[5] = 1;
mat[6] = 0;
mat[7] = 0;
mat[8] = 0;
mat[9] = 0;
mat[10] = 1;
mat[11] = 0;
mat[12] = 0;
mat[13] = 0;
mat[14] = 0;
mat[15] = 1;
};
var computeNextMatrixOperationField = function(anim, name, dim, index, value) {
if (anim.from[dim] !== undefined && anim.to[dim] !== undefined) {
return computeNextValLinear(anim, anim.from[dim], anim.to[dim], value);
} else {
return InitialOperationField[name][index];
}
};
var computeTransform = function(anim, name, value, result,
didChange, didMatrix) {
var transform = result.transform !== undefined ?
result.transform : (result.transform = [{ matrix: [] }]);
var mat = transform[0].matrix;
var m0 = mat[0];
var m1 = mat[1];
var m2 = mat[2];
var m3 = mat[3];
var m4 = mat[4];
var m5 = mat[5];
var m6 = mat[6];
var m7 = mat[7];
var m8 = mat[8];
var m9 = mat[9];
var m10 = mat[10];
var m11 = mat[11];
var m12 = mat[12];
var m13 = mat[13];
var m14 = mat[14];
var m15 = mat[15];
if (!didMatrix) {
initIdentity(mat); // This will be the first transform.
}
var x = computeNextMatrixOperationField(anim, name, X_DIM, 0, value);
var y = computeNextMatrixOperationField(anim, name, Y_DIM, 1, value);
var z = computeNextMatrixOperationField(anim, name, Z_DIM, 2, value);
InterpolateMatrix[name](mat, x, y, z);
if (!didChange) {
didChange = m0 !== mat[0] || m1 !== mat[1] ||
m2 !== mat[2] || m3 !== mat[3] ||
m4 !== mat[4] || m5 !== mat[5] ||
m6 !== mat[6] || m7 !== mat[7] ||
m8 !== mat[8] || m9 !== mat[9] ||
m10 !== mat[10] || m11 !== mat[11] ||
m12 !== mat[12] || m13 !== mat[13] ||
m14 !== mat[14] || m15 !== mat[15];
}
return didChange;
};
/**
* @param {object} anims Animation configuration by style property name.
* @return {function} Function accepting style object, that mutates that style
* object and returns a boolean describing if any update was actually applied.
*/
var buildStyleInterpolator = function(anims) {
function styleInterpolator(result, value) {
var didChange = false;
var didMatrix = false;
for (var name in anims) {
var anim = anims[name];
if (anim.type === 'linear') {
if (name in InterpolateMatrix) {
didChange = computeTransform(anim, name, value, result,
didChange, didMatrix);
didMatrix = true;
} else {
var next = computeNextValLinearScalar(anim, value);
didChange = setNextValAndDetectChange(result, name, next, didChange);
}
} else if (anim.type === 'constant') {
var next = anim.value;
didChange = setNextValAndDetectChange(result, name, next, didChange);
} else if (anim.type === 'step') {
var next = value >= anim.threshold ? anim.to : anim.from;
didChange = setNextValAndDetectChange(result, name, next, didChange);
} else if (anim.type === 'identity') {
var next = value;
didChange = setNextValAndDetectChange(result, name, next, didChange);
}
}
return didChange;
}
return styleInterpolator;
};
module.exports = buildStyleInterpolator;

28
node_modules/react-native/Libraries/Utilities/clamp.js generated vendored Normal file
View File

@@ -0,0 +1,28 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule clamp
* @typechecks
*/
'use strict';
/**
* @param {number} min
* @param {number} value
* @param {number} max
* @return {number}
*/
function clamp(min, value, max) {
if (value < min) {
return min;
}
if (value > max) {
return max;
}
return value;
}
module.exports = clamp;

View File

@@ -0,0 +1,75 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule createStrictShapeTypeChecker
* @flow
*/
'use strict';
var invariant = require('fbjs/lib/invariant');
var merge = require('merge');
function createStrictShapeTypeChecker(
shapeTypes: {[key: string]: ReactPropsCheckType}
): ReactPropsChainableTypeChecker {
function checkType(isRequired, props, propName, componentName, location?, ...rest) {
if (!props[propName]) {
if (isRequired) {
invariant(
false,
`Required object \`${propName}\` was not specified in ` +
`\`${componentName}\`.`
);
}
return;
}
var propValue = props[propName];
var propType = typeof propValue;
var locationName = location || '(unknown)';
if (propType !== 'object') {
invariant(
false,
`Invalid ${locationName} \`${propName}\` of type \`${propType}\` ` +
`supplied to \`${componentName}\`, expected \`object\`.`
);
}
// We need to check all keys in case some are required but missing from
// props.
var allKeys = merge(props[propName], shapeTypes);
for (var key in allKeys) {
var checker = shapeTypes[key];
if (!checker) {
invariant(
false,
`Invalid props.${propName} key \`${key}\` supplied to \`${componentName}\`.` +
'\nBad object: ' + JSON.stringify(props[propName], null, ' ') +
'\nValid keys: ' + JSON.stringify(Object.keys(shapeTypes), null, ' ')
);
}
var error = checker(propValue, key, componentName, location, ...rest);
if (error) {
invariant(
false,
error.message +
'\nBad object: ' + JSON.stringify(props[propName], null, ' ')
);
}
}
}
function chainedCheckType(
props: {[key: string]: any},
propName: string,
componentName: string,
location?: string,
...rest
): ?Error {
return checkType(false, props, propName, componentName, location, ...rest);
}
chainedCheckType.isRequired = checkType.bind(null, true);
return chainedCheckType;
}
module.exports = createStrictShapeTypeChecker;

View File

@@ -0,0 +1,74 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule deepFreezeAndThrowOnMutationInDev
* @flow
*/
'use strict';
/**
* If your application is accepting different values for the same field over
* time and is doing a diff on them, you can either (1) create a copy or
* (2) ensure that those values are not mutated behind two passes.
* This function helps you with (2) by freezing the object and throwing if
* the user subsequently modifies the value.
*
* There are two caveats with this function:
* - If the call site is not in strict mode, it will only throw when
* mutating existing fields, adding a new one
* will unfortunately fail silently :(
* - If the object is already frozen or sealed, it will not continue the
* deep traversal and will leave leaf nodes unfrozen.
*
* Freezing the object and adding the throw mechanism is expensive and will
* only be used in DEV.
*/
function deepFreezeAndThrowOnMutationInDev<T: Object>(object: T): T {
if (__DEV__) {
if (typeof object !== 'object' ||
object === null ||
Object.isFrozen(object) ||
Object.isSealed(object)) {
return object;
}
var keys = Object.keys(object);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (object.hasOwnProperty(key)) {
object.__defineGetter__(key, identity.bind(null, object[key]));
object.__defineSetter__(key, throwOnImmutableMutation.bind(null, key));
}
}
Object.freeze(object);
Object.seal(object);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (object.hasOwnProperty(key)) {
deepFreezeAndThrowOnMutationInDev(object[key]);
}
}
}
return object;
}
function throwOnImmutableMutation(key, value) {
throw Error(
'You attempted to set the key `' + key + '` with the value `' +
JSON.stringify(value) + '` on an object that is meant to be immutable ' +
'and has been frozen.'
);
}
function identity(value) {
return value;
}
module.exports = deepFreezeAndThrowOnMutationInDev;

View File

@@ -0,0 +1,65 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule defineLazyObjectProperty
* @flow
*/
'use strict';
/**
* Defines a lazily evaluated property on the supplied `object`.
*/
function defineLazyObjectProperty<T>(
object: Object,
name: string,
descriptor: {
get: () => T,
enumerable?: boolean,
writable?: boolean,
},
): void {
const {get} = descriptor;
const enumerable = descriptor.enumerable !== false;
const writable = descriptor.writable !== false;
let value;
let valueSet = false;
function getValue(): T {
// WORKAROUND: A weird infinite loop occurs where calling `getValue` calls
// `setValue` which calls `Object.defineProperty` which somehow triggers
// `getValue` again. Adding `valueSet` breaks this loop.
if (!valueSet) {
// Calling `get()` here can trigger an infinite loop if it fails to
// remove the getter on the property, which can happen when executing
// JS in a V8 context. `valueSet = true` will break this loop, and
// sets the value of the property to undefined, until the code in `get()`
// finishes, at which point the property is set to the correct value.
valueSet = true;
setValue(get());
}
return value;
}
function setValue(newValue: T): void {
value = newValue;
valueSet = true;
Object.defineProperty(object, name, {
value: newValue,
configurable: true,
enumerable,
writable,
});
}
Object.defineProperty(object, name, {
get: getValue,
set: setValue,
configurable: true,
enumerable,
});
}
module.exports = defineLazyObjectProperty;

View File

@@ -0,0 +1,36 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule deprecatedPropType
* @flow
*/
'use strict';
const UIManager = require('UIManager');
/**
* Adds a deprecation warning when the prop is used.
*/
function deprecatedPropType(
propType: ReactPropsCheckType,
explanation: string
): ReactPropsCheckType {
return function validate(props, propName, componentName, ...rest) {
// Don't warn for native components.
if (!UIManager[componentName] && props[propName] !== undefined) {
console.warn(`\`${propName}\` supplied to \`${componentName}\` has been deprecated. ${explanation}`);
}
return propType(
props,
propName,
componentName,
...rest
);
};
}
module.exports = deprecatedPropType;

View File

@@ -0,0 +1,64 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule deepDiffer
* @flow
*/
'use strict';
/*
* @returns {bool} true if different, false if equal
*/
var deepDiffer = function(one: any, two: any): bool {
if (one === two) {
// Short circuit on identical object references instead of traversing them.
return false;
}
if ((typeof one === 'function') && (typeof two === 'function')) {
// We consider all functions equal
return false;
}
if ((typeof one !== 'object') || (one === null)) {
// Primitives can be directly compared
return one !== two;
}
if ((typeof two !== 'object') || (two === null)) {
// We know they are different because the previous case would have triggered
// otherwise.
return true;
}
if (one.constructor !== two.constructor) {
return true;
}
if (Array.isArray(one)) {
// We know two is also an array because the constructors are equal
var len = one.length;
if (two.length !== len) {
return true;
}
for (var ii = 0; ii < len; ii++) {
if (deepDiffer(one[ii], two[ii])) {
return true;
}
}
} else {
for (var key in one) {
if (deepDiffer(one[key], two[key])) {
return true;
}
}
for (var twoKey in two) {
// The only case we haven't checked yet is keys that are in two but aren't
// in one, which means they are different.
if (one[twoKey] === undefined && two[twoKey] !== undefined) {
return true;
}
}
}
return false;
};
module.exports = deepDiffer;

View File

@@ -0,0 +1,40 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule insetsDiffer
* @flow
*/
'use strict';
type Inset = {
top: ?number,
left: ?number,
right: ?number,
bottom: ?number,
}
var dummyInsets = {
top: undefined,
left: undefined,
right: undefined,
bottom: undefined,
};
var insetsDiffer = function(
one: ?Inset,
two: ?Inset
): bool {
one = one || dummyInsets;
two = two || dummyInsets;
return one !== two && (
one.top !== two.top ||
one.left !== two.left ||
one.right !== two.right ||
one.bottom !== two.bottom
);
};
module.exports = insetsDiffer;

View File

@@ -0,0 +1,42 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule matricesDiffer
*/
'use strict';
/**
* Unrolls an array comparison specially for matrices. Prioritizes
* checking of indices that are most likely to change so that the comparison
* bails as early as possible.
*
* @param {MatrixMath.Matrix} one First matrix.
* @param {MatrixMath.Matrix} two Second matrix.
* @return {boolean} Whether or not the two matrices differ.
*/
var matricesDiffer = function(one, two) {
if (one === two) {
return false;
}
return !one || !two ||
one[12] !== two[12] ||
one[13] !== two[13] ||
one[14] !== two[14] ||
one[5] !== two[5] ||
one[10] !== two[10] ||
one[1] !== two[1] ||
one[2] !== two[2] ||
one[3] !== two[3] ||
one[4] !== two[4] ||
one[6] !== two[6] ||
one[7] !== two[7] ||
one[8] !== two[8] ||
one[9] !== two[9] ||
one[11] !== two[11] ||
one[15] !== two[15];
};
module.exports = matricesDiffer;

View File

@@ -0,0 +1,28 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule pointsDiffer
* @flow
*/
'use strict';
type Point = {
x: ?number,
y: ?number,
}
var dummyPoint = {x: undefined, y: undefined};
var pointsDiffer = function(one: ?Point, two: ?Point): bool {
one = one || dummyPoint;
two = two || dummyPoint;
return one !== two && (
one.x !== two.x ||
one.y !== two.y
);
};
module.exports = pointsDiffer;

View File

@@ -0,0 +1,22 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule sizesDiffer
*/
'use strict';
var dummySize = {width: undefined, height: undefined};
var sizesDiffer = function(one, two) {
one = one || dummySize;
two = two || dummySize;
return one !== two && (
one.width !== two.width ||
one.height !== two.height
);
};
module.exports = sizesDiffer;

View File

@@ -0,0 +1,19 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule dismissKeyboard
*
* This function dismisses the currently-open keyboard, if any
*/
'use strict';
var TextInputState = require('TextInputState');
function dismissKeyboard() {
TextInputState.blurTextInput(TextInputState.currentlyFocusedField());
}
module.exports = dismissKeyboard;

View File

@@ -0,0 +1,50 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule groupByEveryN
* @flow
*/
/**
* Useful method to split an array into groups of the same number of elements.
* You can use it to generate grids, rows, pages...
*
* If the input length is not a multiple of the count, it'll fill the last
* array with null so you can display a placeholder.
*
* Example:
* groupByEveryN([1, 2, 3, 4, 5], 3)
* => [[1, 2, 3], [4, 5, null]]
*
* groupByEveryN([1, 2, 3], 2).map(elems => {
* return <Row>{elems.map(elem => <Elem>{elem}</Elem>)}</Row>;
* })
*/
'use strict';
function groupByEveryN<T>(array: Array<T>, n: number): Array<Array<?T>> {
var result = [];
var temp = [];
for (var i = 0; i < array.length; ++i) {
if (i > 0 && i % n === 0) {
result.push(temp);
temp = [];
}
temp.push(array[i]);
}
if (temp.length > 0) {
while (temp.length !== n) {
temp.push(null);
}
result.push(temp);
}
return result;
}
module.exports = groupByEveryN;

View File

@@ -0,0 +1,18 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule infoLog
*/
'use strict';
/**
* Intentional info-level logging for clear separation from ad-hoc console debug logging.
*/
function infoLog(...args) {
return console.log(...args);
}
module.exports = infoLog;

View File

@@ -0,0 +1,26 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule logError
* @flow
*/
'use strict';
/**
* Small utility that can be used as an error handler. You cannot just pass
* `console.error` as a failure callback - it's not properly bound. If passes an
* `Error` object, it will print the message and stack.
*/
var logError = function(...args: $ReadOnlyArray<mixed>) {
if (args.length === 1 && args[0] instanceof Error) {
var err = args[0];
console.error('Error: "' + err.message + '". Stack:\n' + err.stack);
} else {
console.error.apply(console, args);
}
};
module.exports = logError;

View File

@@ -0,0 +1,27 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule mapWithSeparator
* @flow
*/
'use strict';
function mapWithSeparator<TFrom, TTo>(
items: Array<TFrom>,
itemRenderer: (item: TFrom, index: number, items: Array<TFrom>) => TTo,
spacerRenderer: (index: number) => TTo,
): Array<TTo> {
const mapped = [];
if (items.length > 0) {
mapped.push(itemRenderer(items[0], 0, items));
for (let ii = 1; ii < items.length; ii++) {
mapped.push(spacerRenderer(ii - 1), itemRenderer(items[ii], ii, items));
}
}
return mapped;
}
module.exports = mapWithSeparator;

View File

@@ -0,0 +1,32 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule mergeFast
* @flow
*/
'use strict';
/**
* Faster version of `merge` that doesn't check its arguments and
* also merges prototype inherited properties.
*
* @param {object} one Any non-null object.
* @param {object} two Any non-null object.
* @return {object} Merging of two objects, including prototype
* inherited properties.
*/
var mergeFast = function(one: Object, two: Object): Object {
var ret = {};
for (var keyOne in one) {
ret[keyOne] = one[keyOne];
}
for (var keyTwo in two) {
ret[keyTwo] = two[keyTwo];
}
return ret;
};
module.exports = mergeFast;

View File

@@ -0,0 +1,25 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule mergeIntoFast
* @flow
*/
'use strict';
/**
* Faster version of `mergeInto` that doesn't check its arguments and
* also copies over prototype inherited properties.
*
* @param {object} one Object to assign to.
* @param {object} two Object to assign from.
*/
var mergeIntoFast = function(one: Object, two: Object): void {
for (var keyTwo in two) {
one[keyTwo] = two[keyTwo];
}
};
module.exports = mergeIntoFast;

View File

@@ -0,0 +1,47 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule stringifySafe
* @flow
*/
'use strict';
/**
* Tries to stringify with JSON.stringify and toString, but catches exceptions
* (e.g. from circular objects) and always returns a string and never throws.
*/
function stringifySafe(arg: any): string {
var ret;
var type = typeof arg;
if (arg === undefined) {
ret = 'undefined';
} else if (arg === null) {
ret = 'null';
} else if (type === 'string') {
ret = '"' + arg + '"';
} else if (type === 'function') {
try {
ret = arg.toString();
} catch (e) {
ret = '[function unknown]';
}
} else {
// Perform a try catch, just in case the object has a circular
// reference or stringify throws for some other reason.
try {
ret = JSON.stringify(arg);
} catch (e) {
if (typeof arg.toString === 'function') {
try {
ret = arg.toString();
} catch (E) {}
}
}
}
return ret || '["' + type + '" failed to stringify]';
}
module.exports = stringifySafe;

View File

@@ -0,0 +1,47 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule truncate
* @flow
*/
'use strict';
type truncateOptions = {
breakOnWords: boolean,
minDelta: number,
elipsis: string,
}
const defaultOptions = {
breakOnWords: true,
minDelta: 10, // Prevents truncating a tiny bit off the end
elipsis: '...',
};
// maxChars (including ellipsis)
const truncate = function(
str: ?string,
maxChars: number,
options?: truncateOptions
): ?string {
options = Object.assign({}, defaultOptions, options);
if (str && str.length &&
str.length - options.minDelta + options.elipsis.length >= maxChars) {
// If the slice is happening in the middle of a wide char, add one more char
var extraChar = str.charCodeAt(maxChars - options.elipsis.length) > 255
? 1
: 0;
str = str.slice(0, maxChars - options.elipsis.length + 1 + extraChar);
if (options.breakOnWords) {
var ii = Math.max(str.lastIndexOf(' '), str.lastIndexOf('\n'));
str = str.slice(0, ii);
}
str = str.trim() + options.elipsis;
}
return str;
};
module.exports = truncate;