161 lines
4.3 KiB
Plaintext
161 lines
4.3 KiB
Plaintext
function last(arr) {
|
|
return arr[arr.length - 1];
|
|
}
|
|
function isFunction(d) {
|
|
return typeof d === "function";
|
|
}
|
|
function functionalUpdate(updater, previous) {
|
|
if (isFunction(updater)) {
|
|
return updater(previous);
|
|
}
|
|
return updater;
|
|
}
|
|
function pick(parent, keys) {
|
|
return keys.reduce((obj, key) => {
|
|
obj[key] = parent[key];
|
|
return obj;
|
|
}, {});
|
|
}
|
|
function replaceEqualDeep(prev, _next) {
|
|
if (prev === _next) {
|
|
return prev;
|
|
}
|
|
const next = _next;
|
|
const array = isPlainArray(prev) && isPlainArray(next);
|
|
if (array || isPlainObject(prev) && isPlainObject(next)) {
|
|
const prevItems = array ? prev : Object.keys(prev);
|
|
const prevSize = prevItems.length;
|
|
const nextItems = array ? next : Object.keys(next);
|
|
const nextSize = nextItems.length;
|
|
const copy = array ? [] : {};
|
|
let equalItems = 0;
|
|
for (let i = 0; i < nextSize; i++) {
|
|
const key = array ? i : nextItems[i];
|
|
if ((!array && prevItems.includes(key) || array) && prev[key] === void 0 && next[key] === void 0) {
|
|
copy[key] = void 0;
|
|
equalItems++;
|
|
} else {
|
|
copy[key] = replaceEqualDeep(prev[key], next[key]);
|
|
if (copy[key] === prev[key] && prev[key] !== void 0) {
|
|
equalItems++;
|
|
}
|
|
}
|
|
}
|
|
return prevSize === nextSize && equalItems === prevSize ? prev : copy;
|
|
}
|
|
return next;
|
|
}
|
|
function isPlainObject(o) {
|
|
if (!hasObjectPrototype(o)) {
|
|
return false;
|
|
}
|
|
const ctor = o.constructor;
|
|
if (typeof ctor === "undefined") {
|
|
return true;
|
|
}
|
|
const prot = ctor.prototype;
|
|
if (!hasObjectPrototype(prot)) {
|
|
return false;
|
|
}
|
|
if (!prot.hasOwnProperty("isPrototypeOf")) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
function hasObjectPrototype(o) {
|
|
return Object.prototype.toString.call(o) === "[object Object]";
|
|
}
|
|
function isPlainArray(value) {
|
|
return Array.isArray(value) && value.length === Object.keys(value).length;
|
|
}
|
|
function getObjectKeys(obj, ignoreUndefined) {
|
|
let keys = Object.keys(obj);
|
|
if (ignoreUndefined) {
|
|
keys = keys.filter((key) => obj[key] !== void 0);
|
|
}
|
|
return keys;
|
|
}
|
|
function deepEqual(a, b, opts) {
|
|
if (a === b) {
|
|
return true;
|
|
}
|
|
if (typeof a !== typeof b) {
|
|
return false;
|
|
}
|
|
if (isPlainObject(a) && isPlainObject(b)) {
|
|
const ignoreUndefined = (opts == null ? void 0 : opts.ignoreUndefined) ?? true;
|
|
const aKeys = getObjectKeys(a, ignoreUndefined);
|
|
const bKeys = getObjectKeys(b, ignoreUndefined);
|
|
if (!(opts == null ? void 0 : opts.partial) && aKeys.length !== bKeys.length) {
|
|
return false;
|
|
}
|
|
return bKeys.every((key) => deepEqual(a[key], b[key], opts));
|
|
}
|
|
if (Array.isArray(a) && Array.isArray(b)) {
|
|
if (a.length !== b.length) {
|
|
return false;
|
|
}
|
|
return !a.some((item, index) => !deepEqual(item, b[index], opts));
|
|
}
|
|
return false;
|
|
}
|
|
function createControlledPromise(onResolve) {
|
|
let resolveLoadPromise;
|
|
let rejectLoadPromise;
|
|
const controlledPromise = new Promise((resolve, reject) => {
|
|
resolveLoadPromise = resolve;
|
|
rejectLoadPromise = reject;
|
|
});
|
|
controlledPromise.status = "pending";
|
|
controlledPromise.resolve = (value) => {
|
|
controlledPromise.status = "resolved";
|
|
controlledPromise.value = value;
|
|
resolveLoadPromise(value);
|
|
onResolve == null ? void 0 : onResolve(value);
|
|
};
|
|
controlledPromise.reject = (e) => {
|
|
controlledPromise.status = "rejected";
|
|
rejectLoadPromise(e);
|
|
};
|
|
return controlledPromise;
|
|
}
|
|
function escapeJSON(jsonString) {
|
|
return jsonString.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/"/g, '\\"');
|
|
}
|
|
function shallow(objA, objB) {
|
|
if (Object.is(objA, objB)) {
|
|
return true;
|
|
}
|
|
if (typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null) {
|
|
return false;
|
|
}
|
|
const keysA = Object.keys(objA);
|
|
if (keysA.length !== Object.keys(objB).length) {
|
|
return false;
|
|
}
|
|
for (const item of keysA) {
|
|
if (!Object.prototype.hasOwnProperty.call(objB, item) || !Object.is(objA[item], objB[item])) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
function hasUriEncodedChars(inputString) {
|
|
const pattern = /%[0-9A-Fa-f]{2}/;
|
|
return pattern.test(inputString);
|
|
}
|
|
export {
|
|
createControlledPromise,
|
|
deepEqual,
|
|
escapeJSON,
|
|
functionalUpdate,
|
|
hasUriEncodedChars,
|
|
isPlainArray,
|
|
isPlainObject,
|
|
last,
|
|
pick,
|
|
replaceEqualDeep,
|
|
shallow
|
|
};
|
|
//# sourceMappingURL=utils.js.map
|