UNPKG

@irysius/grid-math

Version:

Tools to assist with grid math and algorithms

66 lines (65 loc) 2.26 kB
(function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function create(x, y, width, height, type) { let result = { x, y, width, height }; if (type != null) { result.type = type; } return result; } exports.create = create; function empty(type) { let result = { x: 0, y: 0, width: 0, height: 0 }; if (type != null) { result.type = type; } return result; } exports.empty = empty; function fromClientBounds(v) { return { x: v.left, y: v.top, width: v.width, height: v.height }; } exports.fromClientBounds = fromClientBounds; function isRect(v, typeToCheck) { return !!(v && typeof v.x === 'number' && typeof v.y === 'number' && typeof v.width === 'number' && typeof v.height === 'number') && (v.type == null || typeof v.type === 'string') && (typeof typeToCheck !== 'string' || v.type === typeToCheck); } exports.isRect = isRect; function areEqual(a, b, ignoreType) { return (ignoreType || a.type === b.type) && (a.x === b.x && a.y === b.y && a.width == b.width && a.height === b.height); } exports.areEqual = areEqual; function closeEnough(e) { return function areCloseEnough(a, b, ignoreType) { return (ignoreType || a.type === b.type) && (Math.abs(a.x - b.x) < e && Math.abs(a.y - b.y) < e && Math.abs(a.width - b.width) < e && Math.abs(a.height - b.height) < e); }; } exports.closeEnough = closeEnough; function clone(v) { return Object.assign({}, v); } exports.clone = clone; });