175 lines
5.2 KiB
JavaScript
175 lines
5.2 KiB
JavaScript
export function isNumber(value) {
|
|
return typeof value === 'number' && !Number.isNaN(value);
|
|
}
|
|
export function isFunction(value) {
|
|
return typeof value === 'function';
|
|
}
|
|
export function isObject(value) {
|
|
return typeof value === 'object' && value !== null;
|
|
}
|
|
export function localStorageAvailable() {
|
|
try {
|
|
// Incognito mode might reject access to the localStorage for security reasons.
|
|
// window isn't defined on Node.js
|
|
// https://stackoverflow.com/questions/16427636/check-if-localstorage-is-available
|
|
const key = '__some_random_key_you_are_not_going_to_use__';
|
|
window.localStorage.setItem(key, key);
|
|
window.localStorage.removeItem(key);
|
|
return true;
|
|
} catch (err) {
|
|
return false;
|
|
}
|
|
}
|
|
export function escapeRegExp(value) {
|
|
return value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
|
|
}
|
|
|
|
/**
|
|
* Follows the CSS specification behavior for min and max
|
|
* If min > max, then the min have priority
|
|
*/
|
|
export const clamp = (value, min, max) => Math.max(min, Math.min(max, value));
|
|
|
|
/**
|
|
* Based on `fast-deep-equal`
|
|
*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2017 Evgeny Poberezkin
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
* We only type the public interface to avoid dozens of `as` in the function.
|
|
*/
|
|
|
|
export function isDeepEqual(a, b) {
|
|
if (a === b) {
|
|
return true;
|
|
}
|
|
if (a && b && typeof a === 'object' && typeof b === 'object') {
|
|
if (a.constructor !== b.constructor) {
|
|
return false;
|
|
}
|
|
if (Array.isArray(a)) {
|
|
const length = a.length;
|
|
if (length !== b.length) {
|
|
return false;
|
|
}
|
|
for (let i = 0; i < length; i += 1) {
|
|
if (!isDeepEqual(a[i], b[i])) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
if (a instanceof Map && b instanceof Map) {
|
|
if (a.size !== b.size) {
|
|
return false;
|
|
}
|
|
const entriesA = Array.from(a.entries());
|
|
for (let i = 0; i < entriesA.length; i += 1) {
|
|
if (!b.has(entriesA[i][0])) {
|
|
return false;
|
|
}
|
|
}
|
|
for (let i = 0; i < entriesA.length; i += 1) {
|
|
const entryA = entriesA[i];
|
|
if (!isDeepEqual(entryA[1], b.get(entryA[0]))) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
if (a instanceof Set && b instanceof Set) {
|
|
if (a.size !== b.size) {
|
|
return false;
|
|
}
|
|
const entries = Array.from(a.entries());
|
|
for (let i = 0; i < entries.length; i += 1) {
|
|
if (!b.has(entries[i][0])) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
|
|
const length = a.length;
|
|
if (length !== b.length) {
|
|
return false;
|
|
}
|
|
for (let i = 0; i < length; i += 1) {
|
|
if (a[i] !== b[i]) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
if (a.constructor === RegExp) {
|
|
return a.source === b.source && a.flags === b.flags;
|
|
}
|
|
if (a.valueOf !== Object.prototype.valueOf) {
|
|
return a.valueOf() === b.valueOf();
|
|
}
|
|
if (a.toString !== Object.prototype.toString) {
|
|
return a.toString() === b.toString();
|
|
}
|
|
const keys = Object.keys(a);
|
|
const length = keys.length;
|
|
if (length !== Object.keys(b).length) {
|
|
return false;
|
|
}
|
|
for (let i = 0; i < length; i += 1) {
|
|
if (!Object.prototype.hasOwnProperty.call(b, keys[i])) {
|
|
return false;
|
|
}
|
|
}
|
|
for (let i = 0; i < length; i += 1) {
|
|
const key = keys[i];
|
|
if (!isDeepEqual(a[key], b[key])) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// true if both NaN, false otherwise
|
|
// eslint-disable-next-line no-self-compare
|
|
return a !== a && b !== b;
|
|
}
|
|
|
|
// Pseudo random number. See https://stackoverflow.com/a/47593316
|
|
function mulberry32(a) {
|
|
return () => {
|
|
/* eslint-disable */
|
|
let t = a += 0x6d2b79f5;
|
|
t = Math.imul(t ^ t >>> 15, t | 1);
|
|
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
|
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
/* eslint-enable */
|
|
};
|
|
}
|
|
export function randomNumberBetween(seed, min, max) {
|
|
const random = mulberry32(seed);
|
|
return () => min + (max - min) * random();
|
|
}
|
|
export function deepClone(obj) {
|
|
if (typeof structuredClone === 'function') {
|
|
return structuredClone(obj);
|
|
}
|
|
return JSON.parse(JSON.stringify(obj));
|
|
} |