@tldraw/utils
Version:
tldraw infinite canvas SDK (private utilities).
140 lines (139 loc) • 4.62 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var object_exports = {};
__export(object_exports, {
areObjectsShallowEqual: () => areObjectsShallowEqual,
filterEntries: () => filterEntries,
getChangedKeys: () => getChangedKeys,
getOwnProperty: () => getOwnProperty,
groupBy: () => groupBy,
hasOwnProperty: () => hasOwnProperty,
isEqualAllowingForFloatingPointErrors: () => isEqualAllowingForFloatingPointErrors,
mapObjectMapValues: () => mapObjectMapValues,
objectMapEntries: () => objectMapEntries,
objectMapEntriesIterable: () => objectMapEntriesIterable,
objectMapFromEntries: () => objectMapFromEntries,
objectMapKeys: () => objectMapKeys,
objectMapValues: () => objectMapValues,
omit: () => omit
});
module.exports = __toCommonJS(object_exports);
var import_lodash = __toESM(require("lodash.isequalwith"));
function hasOwnProperty(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
}
function getOwnProperty(obj, key) {
if (!hasOwnProperty(obj, key)) {
return void 0;
}
return obj[key];
}
function objectMapKeys(object) {
return Object.keys(object);
}
function objectMapValues(object) {
return Object.values(object);
}
function objectMapEntries(object) {
return Object.entries(object);
}
function* objectMapEntriesIterable(object) {
for (const key in object) {
if (!Object.prototype.hasOwnProperty.call(object, key)) continue;
yield [key, object[key]];
}
}
function objectMapFromEntries(entries) {
return Object.fromEntries(entries);
}
function filterEntries(object, predicate) {
const result = {};
let didChange = false;
for (const [key, value] of objectMapEntries(object)) {
if (predicate(key, value)) {
result[key] = value;
} else {
didChange = true;
}
}
return didChange ? result : object;
}
function mapObjectMapValues(object, mapper) {
const result = {};
for (const key in object) {
if (!Object.prototype.hasOwnProperty.call(object, key)) continue;
result[key] = mapper(key, object[key]);
}
return result;
}
function areObjectsShallowEqual(obj1, obj2) {
if (obj1 === obj2) return true;
const keys1 = new Set(Object.keys(obj1));
const keys2 = new Set(Object.keys(obj2));
if (keys1.size !== keys2.size) return false;
for (const key of keys1) {
if (!keys2.has(key)) return false;
if (!Object.is(obj1[key], obj2[key])) return false;
}
return true;
}
function groupBy(array, keySelector) {
const result = {};
for (const value of array) {
const key = keySelector(value);
if (!result[key]) result[key] = [];
result[key].push(value);
}
return result;
}
function omit(obj, keys) {
const result = { ...obj };
for (const key of keys) {
delete result[key];
}
return result;
}
function getChangedKeys(obj1, obj2) {
const result = [];
for (const key in obj1) {
if (!Object.is(obj1[key], obj2[key])) {
result.push(key);
}
}
return result;
}
function isEqualAllowingForFloatingPointErrors(obj1, obj2, threshold = 1e-6) {
return (0, import_lodash.default)(obj1, obj2, (value1, value2) => {
if (typeof value1 === "number" && typeof value2 === "number") {
return Math.abs(value1 - value2) < threshold;
}
return void 0;
});
}
//# sourceMappingURL=object.js.map