@turbox3d/shared
Version:
turbox shared internal utility
167 lines • 4.9 kB
JavaScript
import _typeof from "@babel/runtime/helpers/esm/typeof";
import { isSymbol } from './lang';
import { generateUUID } from './uuid';
export function isObject(value) {
return value !== null && _typeof(value) === 'object';
}
export function isPromise(value) {
return value && typeof value.then === 'function';
}
// {}
export function isPlainObject(value) {
if (value === null || _typeof(value) !== 'object') return false;
var proto = Object.getPrototypeOf(value);
return proto === Object.prototype || proto === null;
}
/**
* 对数组从小到大排序
*/
export function sortBy(arr, func) {
var length = arr.length;
for (var i = 0; i < length; i++) {
for (var j = i + 1; j < length; j++) {
var itemI = arr[i];
var itemJ = arr[j];
var indicatorI = func ? func(itemI) : itemI;
var indicatorJ = func ? func(itemJ) : itemJ;
if (indicatorJ < indicatorI) {
arr[i] = itemJ;
arr[j] = itemI;
}
}
}
return arr;
}
export function includes(array, item) {
return array.indexOf(item) > -1;
}
export function remove(array, item) {
var index = array.indexOf(item);
if (index > -1) {
array.splice(index, 1);
}
}
export function batchRemove(array, items) {
items.forEach(function (item) {
var index = array.indexOf(item);
if (index > -1) {
array.splice(index, 1);
}
});
}
export function batchRemoveFromSet(set, items) {
items.forEach(function (item) {
set["delete"](item);
});
}
// boolean, string, number, undefined, null
export function isPrimitive(value) {
return value === null || _typeof(value) !== 'object' && typeof value !== 'function';
}
export function convert2UniqueString(key) {
if (!isSymbol(key)) {
return key.toString();
}
return key.toString() + generateUUID();
}
export var deduplicate = function deduplicate(array) {
return Array.from(new Set(array));
};
var promise = Promise.resolve();
/**
* nextTick would flush promise micro task
*/
export function nextTick(fn) {
return fn ? promise.then(fn) : promise;
}
export var hasOwn = function hasOwn(val, key) {
return Object.prototype.hasOwnProperty.call(val, key);
};
// From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
export function is(x, y) {
if (x === y) {
return x !== 0 || 1 / x === 1 / y;
}
// eslint-disable-next-line no-self-compare
return x !== x && y !== y;
}
export function bind(fn, ctx) {
function boundFn(a) {
var l = arguments.length;
// eslint-disable-next-line no-nested-ternary
return l ? l > 1
// eslint-disable-next-line prefer-rest-params
? fn.apply(ctx, arguments) : fn.call(ctx, a) : fn.call(ctx);
}
return boundFn;
}
// From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
export function shallowEqual(objA, objB) {
var ignoreKeys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
if (is(objA, objB)) return true;
if (_typeof(objA) !== 'object' || objA === null || _typeof(objB) !== 'object' || objB === null) {
return false;
}
var keysA = [];
var keysB = [];
if (ignoreKeys && ignoreKeys.length > 0) {
var ignoreSet = new Set(ignoreKeys);
keysA = Object.keys(objA).filter(function (key) {
return !ignoreSet.has(key);
});
keysB = Object.keys(objB).filter(function (key) {
return !ignoreSet.has(key);
});
} else {
keysA = Object.keys(objA);
keysB = Object.keys(objB);
}
if (keysA.length !== keysB.length) return false;
for (var i = 0; i < keysA.length; i++) {
if (!objB.hasOwnProperty(keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}
return true;
}
export var dummy = function dummy() {
//
};
export function loadJSON(url) {
return new Promise(function (resolve) {
var xhr = new XMLHttpRequest();
xhr.overrideMimeType('application/json');
xhr.open('GET', url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
resolve(JSON.parse(xhr.responseText || '{}'));
}
};
xhr.send(null);
});
}
export function updateQueryStringParameter(url, key, value) {
if (!value) {
return url;
}
var re = new RegExp("([?&])".concat(key, "=.*?(&|$)"), 'i');
var separator = url.indexOf('?') !== -1 ? '&' : '?';
if (url.match(re)) {
return url.replace(re, "$1".concat(key, "=").concat(value, "$2"));
}
return "".concat(url).concat(separator).concat(key, "=").concat(value);
}
export function getContextEnv() {
return typeof window !== 'undefined' ? 'browser' : 'vm';
}
export function getContextParam(key) {
var value;
var env = getContextEnv();
if (env === 'browser') {
value = window[key];
}
if (env === 'vm') {
value = global[key];
}
return value;
}