2017-09-23 17:25:48 +00:00
|
|
|
// @flow
|
|
|
|
import { Platform } from 'react-native';
|
2017-05-25 22:39:06 +00:00
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
// todo cleanup unused utilities from legacy code
|
2017-09-23 17:25:48 +00:00
|
|
|
|
2017-02-14 15:56:28 +00:00
|
|
|
// modeled after base64 web-safe chars, but ordered by ASCII
|
2018-01-25 18:25:39 +00:00
|
|
|
const PUSH_CHARS =
|
|
|
|
'-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
|
|
|
|
const AUTO_ID_CHARS =
|
|
|
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
2017-11-17 11:07:52 +00:00
|
|
|
const { hasOwnProperty } = Object;
|
2017-09-25 22:19:13 +00:00
|
|
|
|
|
|
|
// const DEFAULT_CHUNK_SIZE = 50;
|
2018-04-16 17:20:08 +00:00
|
|
|
/**
|
|
|
|
* Checks for property existence by name on the specified object
|
|
|
|
* @param object
|
|
|
|
* @param property
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
export function hop(object: Object, property: string): boolean {
|
|
|
|
return hasOwnProperty.call(object, property);
|
|
|
|
}
|
2017-02-14 15:56:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deep get a value from an object.
|
|
|
|
* @website https://github.com/Salakar/deeps
|
|
|
|
* @param object
|
|
|
|
* @param path
|
|
|
|
* @param joiner
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2018-04-13 21:37:48 +00:00
|
|
|
export function deepGet(object: any, path: string, joiner?: string = '/'): any {
|
2018-04-21 23:29:33 +00:00
|
|
|
if (!isObject(object) && !Array.isArray(object)) return undefined;
|
2017-02-14 15:56:28 +00:00
|
|
|
const keys = path.split(joiner);
|
|
|
|
|
|
|
|
let i = 0;
|
|
|
|
let tmp = object;
|
|
|
|
const len = keys.length;
|
|
|
|
|
|
|
|
while (i < len) {
|
2017-03-02 13:09:41 +00:00
|
|
|
const key = keys[i++];
|
2018-04-12 11:30:41 +00:00
|
|
|
if (!tmp || !hasOwnProperty.call(tmp, key)) return undefined;
|
2017-02-14 15:56:28 +00:00
|
|
|
tmp = tmp[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deep check if a key exists.
|
|
|
|
* @website https://github.com/Salakar/deeps
|
|
|
|
* @param object
|
|
|
|
* @param path
|
|
|
|
* @param joiner
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2018-01-25 18:25:39 +00:00
|
|
|
export function deepExists(
|
|
|
|
object: Object,
|
|
|
|
path: string,
|
|
|
|
joiner?: string = '/'
|
|
|
|
): boolean {
|
2017-02-14 15:56:28 +00:00
|
|
|
const keys = path.split(joiner);
|
|
|
|
|
|
|
|
let i = 0;
|
|
|
|
let tmp = object;
|
|
|
|
const len = keys.length;
|
|
|
|
|
|
|
|
while (i < len) {
|
2017-03-02 13:09:41 +00:00
|
|
|
const key = keys[i++];
|
2017-02-14 15:56:28 +00:00
|
|
|
if (!tmp || !hasOwnProperty.call(tmp, key)) return false;
|
|
|
|
tmp = tmp[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp !== undefined;
|
|
|
|
}
|
|
|
|
|
2017-10-09 00:42:57 +00:00
|
|
|
/**
|
|
|
|
* Deep Check if obj1 keys are contained in obj2
|
|
|
|
* @param obj1
|
|
|
|
* @param obj2
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2018-01-25 18:25:39 +00:00
|
|
|
export function areObjectKeysContainedInOther(
|
|
|
|
obj1: Object,
|
|
|
|
obj2: Object
|
|
|
|
): boolean {
|
2017-10-09 00:42:57 +00:00
|
|
|
if (!isObject(obj1) || !isObject(obj2)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const keys1 = Object.keys(obj1);
|
|
|
|
const keys2 = Object.keys(obj2);
|
|
|
|
if (isArrayContainedInOther(keys1, keys2)) {
|
2018-01-25 18:25:39 +00:00
|
|
|
return keys1
|
|
|
|
.filter(key => isObject(obj1[key]))
|
|
|
|
.reduce(
|
|
|
|
(acc, cur) =>
|
|
|
|
acc && areObjectKeysContainedInOther(obj1[cur], obj2[cur]),
|
|
|
|
true
|
|
|
|
);
|
2017-10-09 00:42:57 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if arr1 is contained in arr2
|
|
|
|
* @param arr1
|
|
|
|
* @param arr2
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2018-01-25 18:25:39 +00:00
|
|
|
export function isArrayContainedInOther(
|
|
|
|
arr1: Array<*>,
|
|
|
|
arr2: Array<*>
|
|
|
|
): boolean {
|
2017-10-09 00:42:57 +00:00
|
|
|
if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-25 18:25:39 +00:00
|
|
|
return arr1.reduce((acc, cur) => acc && arr2.includes(cur), true);
|
2017-10-09 00:42:57 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 15:56:28 +00:00
|
|
|
/**
|
|
|
|
* Simple is object check.
|
|
|
|
* @param item
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2017-11-17 11:07:52 +00:00
|
|
|
export function isObject(item: mixed): boolean %checks {
|
2018-01-25 18:25:39 +00:00
|
|
|
return item
|
|
|
|
? typeof item === 'object' && !Array.isArray(item) && item !== null
|
|
|
|
: false;
|
2017-02-14 15:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple is function check
|
|
|
|
* @param item
|
|
|
|
* @returns {*|boolean}
|
|
|
|
*/
|
2017-11-17 11:07:52 +00:00
|
|
|
export function isFunction(item?: mixed): boolean %checks {
|
|
|
|
return item ? typeof item === 'function' : false;
|
2017-02-14 15:56:28 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 16:24:34 +00:00
|
|
|
/**
|
|
|
|
* Simple is string check
|
|
|
|
* @param value
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
2017-11-17 11:07:52 +00:00
|
|
|
export function isString(value: mixed): boolean %checks {
|
2017-06-29 16:24:34 +00:00
|
|
|
return typeof value === 'string';
|
|
|
|
}
|
|
|
|
|
2018-04-13 08:58:53 +00:00
|
|
|
/**
|
|
|
|
* Simple is boolean check
|
|
|
|
* @param value
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
export function isBoolean(value: mixed): boolean %checks {
|
|
|
|
return typeof value === 'boolean';
|
|
|
|
}
|
|
|
|
|
2017-09-24 12:19:24 +00:00
|
|
|
// platform checks
|
|
|
|
export const isIOS = Platform.OS === 'ios';
|
|
|
|
export const isAndroid = Platform.OS === 'android';
|
2017-09-23 17:16:43 +00:00
|
|
|
|
2017-02-14 15:56:28 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2017-04-04 16:58:47 +00:00
|
|
|
export function tryJSONParse(string: string | null): any {
|
2017-02-14 15:56:28 +00:00
|
|
|
try {
|
2017-04-04 16:58:47 +00:00
|
|
|
return string && JSON.parse(string);
|
2017-02-14 15:56:28 +00:00
|
|
|
} catch (jsonError) {
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2017-11-17 11:07:52 +00:00
|
|
|
export function tryJSONStringify(data: mixed): string | null {
|
2017-02-14 15:56:28 +00:00
|
|
|
try {
|
|
|
|
return JSON.stringify(data);
|
|
|
|
} catch (jsonError) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* No operation func
|
|
|
|
*/
|
2018-01-25 18:25:39 +00:00
|
|
|
export function noop(): void {}
|
2017-02-14 15:56:28 +00:00
|
|
|
|
2018-07-07 02:31:26 +00:00
|
|
|
/**
|
|
|
|
* Remove a trailing forward slash from a string
|
|
|
|
* @param str
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
export function stripTrailingSlash(str: string): string {
|
2018-07-10 19:12:01 +00:00
|
|
|
if (!str || !isString(str)) return str;
|
2018-07-07 02:31:26 +00:00
|
|
|
return str.endsWith('/') ? str.slice(0, -1) : str;
|
|
|
|
}
|
2017-02-14 15:56:28 +00:00
|
|
|
|
2017-07-30 06:34:41 +00:00
|
|
|
/**
|
|
|
|
* Returns a string typeof that's valid for Firebase usage
|
|
|
|
* @param value
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2017-04-04 16:58:20 +00:00
|
|
|
export function typeOf(value: any): string {
|
2017-03-02 13:09:41 +00:00
|
|
|
if (value === null) return 'null';
|
2017-02-14 15:56:28 +00:00
|
|
|
if (Array.isArray(value)) return 'array';
|
2017-03-02 13:09:41 +00:00
|
|
|
return typeof value;
|
2017-02-14 15:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// timestamp of last push, used to prevent local collisions if you push twice in one ms.
|
|
|
|
let lastPushTime = 0;
|
|
|
|
|
|
|
|
// we generate 72-bits of randomness which get turned into 12 characters and appended to the
|
|
|
|
// timestamp to prevent collisions with other clients. We store the last characters we
|
|
|
|
// generated because in the event of a collision, we'll use those same characters except
|
|
|
|
// "incremented" by one.
|
|
|
|
const lastRandChars = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a firebase id - for use with ref().push(val, cb) - e.g. -KXMr7k2tXUFQqiaZRY4'
|
|
|
|
* @param serverTimeOffset - pass in server time offset from native side
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
export function generatePushID(serverTimeOffset?: number = 0): string {
|
|
|
|
const timeStampChars = new Array(8);
|
|
|
|
let now = new Date().getTime() + serverTimeOffset;
|
2018-01-25 18:25:39 +00:00
|
|
|
const duplicateTime = now === lastPushTime;
|
2017-02-14 15:56:28 +00:00
|
|
|
|
|
|
|
lastPushTime = now;
|
|
|
|
|
|
|
|
for (let i = 7; i >= 0; i -= 1) {
|
|
|
|
timeStampChars[i] = PUSH_CHARS.charAt(now % 64);
|
|
|
|
now = Math.floor(now / 64);
|
|
|
|
}
|
|
|
|
|
2018-01-25 18:25:39 +00:00
|
|
|
if (now !== 0)
|
|
|
|
throw new Error('We should have converted the entire timestamp.');
|
2017-02-14 15:56:28 +00:00
|
|
|
|
|
|
|
let id = timeStampChars.join('');
|
|
|
|
|
|
|
|
if (!duplicateTime) {
|
|
|
|
for (let i = 0; i < 12; i += 1) {
|
|
|
|
lastRandChars[i] = Math.floor(Math.random() * 64);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if the timestamp hasn't changed since last push,
|
|
|
|
// use the same random number, but increment it by 1.
|
|
|
|
let i;
|
|
|
|
for (i = 11; i >= 0 && lastRandChars[i] === 63; i -= 1) {
|
|
|
|
lastRandChars[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastRandChars[i] += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < 12; i++) {
|
|
|
|
id += PUSH_CHARS.charAt(lastRandChars[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id.length !== 20) throw new Error('Length should be 20.');
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
2017-05-26 16:56:04 +00:00
|
|
|
|
2017-05-27 16:31:00 +00:00
|
|
|
/**
|
|
|
|
* Converts a code and message from a native event to a JS Error
|
|
|
|
* @param code
|
|
|
|
* @param message
|
2017-08-14 17:43:21 +00:00
|
|
|
* @param additionalProps
|
2017-05-27 16:31:00 +00:00
|
|
|
* @returns {Error}
|
|
|
|
*/
|
2018-01-25 18:25:39 +00:00
|
|
|
export function nativeToJSError(
|
|
|
|
code: string,
|
|
|
|
message: string,
|
|
|
|
additionalProps?: Object = {}
|
|
|
|
) {
|
2017-09-25 22:19:13 +00:00
|
|
|
const error: Object = new Error(message);
|
2017-05-26 16:56:04 +00:00
|
|
|
error.code = code;
|
2017-08-14 17:43:21 +00:00
|
|
|
Object.assign(error, additionalProps);
|
2017-08-19 04:18:34 +00:00
|
|
|
// exclude this function from the stack
|
|
|
|
const _stackArray = error.stack.split('\n');
|
|
|
|
error.stack = _stackArray.splice(1, _stackArray.length).join('\n');
|
2017-05-26 16:56:04 +00:00
|
|
|
return error;
|
|
|
|
}
|
2017-06-29 16:24:34 +00:00
|
|
|
|
2017-08-15 20:29:50 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param object
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2017-09-25 22:19:13 +00:00
|
|
|
export function objectToUniqueId(object: Object): string {
|
2017-08-14 10:05:49 +00:00
|
|
|
if (!isObject(object) || object === null) return JSON.stringify(object);
|
|
|
|
|
|
|
|
const keys = Object.keys(object).sort();
|
|
|
|
|
|
|
|
let key = '{';
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
if (i !== 0) key += ',';
|
|
|
|
key += JSON.stringify(keys[i]);
|
|
|
|
key += ':';
|
|
|
|
key += objectToUniqueId(object[keys[i]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
key += '}';
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2017-08-05 20:49:28 +00:00
|
|
|
/**
|
|
|
|
* Return the existing promise if no callback provided or
|
|
|
|
* exec the promise and callback if optionalCallback is valid.
|
|
|
|
*
|
|
|
|
* @param promise
|
|
|
|
* @param optionalCallback
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2018-01-25 18:25:39 +00:00
|
|
|
export function promiseOrCallback(
|
|
|
|
promise: Promise<*>,
|
|
|
|
optionalCallback?: Function
|
|
|
|
): Promise<*> {
|
2017-08-05 20:49:28 +00:00
|
|
|
if (!isFunction(optionalCallback)) return promise;
|
|
|
|
|
2018-01-25 18:25:39 +00:00
|
|
|
return promise
|
|
|
|
.then(result => {
|
|
|
|
// some of firebase internal tests & methods only check/return one arg
|
|
|
|
// see https://github.com/firebase/firebase-js-sdk/blob/master/src/utils/promise.ts#L62
|
|
|
|
if (optionalCallback && optionalCallback.length === 1) {
|
|
|
|
optionalCallback(null);
|
|
|
|
} else if (optionalCallback) {
|
|
|
|
optionalCallback(null, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve(result);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
if (optionalCallback) optionalCallback(error);
|
|
|
|
return Promise.reject(error);
|
|
|
|
});
|
2017-08-05 20:49:28 +00:00
|
|
|
}
|
2017-09-26 13:57:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a firestore auto id for use with collection/document .add()
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
export function firestoreAutoId(): string {
|
|
|
|
let autoId = '';
|
|
|
|
|
|
|
|
for (let i = 0; i < 20; i++) {
|
2018-01-25 18:25:39 +00:00
|
|
|
autoId += AUTO_ID_CHARS.charAt(
|
|
|
|
Math.floor(Math.random() * AUTO_ID_CHARS.length)
|
|
|
|
);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
return autoId;
|
|
|
|
}
|