diff --git a/dist/js/index.d.ts b/dist/js/index.d.ts index d49b4aa..c4464d5 100644 --- a/dist/js/index.d.ts +++ b/dist/js/index.d.ts @@ -1,7 +1,7 @@ import * as array from "./shared/array"; +import * as assertion from "./shared/assertion"; import * as cls from "./shared/class"; import * as clone from "./shared/clone"; -import * as compare from "./shared/compare"; import * as constants from "./shared/constants"; import * as hash from "./shared/hash"; import * as math from "./shared/math"; @@ -26,7 +26,7 @@ export declare const sharedUtils: { tree: typeof tree; url: typeof url; uuid: typeof uuid; - compare: typeof compare; + assertion: typeof assertion; }; export declare const Utils: { array: typeof array; @@ -42,7 +42,7 @@ export declare const Utils: { tree: typeof tree; url: typeof url; uuid: typeof uuid; - compare: typeof compare; + assertion: typeof assertion; }; declare const _default: { array: typeof array; @@ -58,6 +58,6 @@ declare const _default: { tree: typeof tree; url: typeof url; uuid: typeof uuid; - compare: typeof compare; + assertion: typeof assertion; }; export default _default; diff --git a/dist/js/index.js b/dist/js/index.js index b4e7084..b192e2c 100644 --- a/dist/js/index.js +++ b/dist/js/index.js @@ -43,9 +43,9 @@ var __importStar = Object.defineProperty(exports, "__esModule", { value: true }); exports.Utils = exports.sharedUtils = void 0; const array = __importStar(require("./shared/array")); +const assertion = __importStar(require("./shared/assertion")); const cls = __importStar(require("./shared/class")); const clone = __importStar(require("./shared/clone")); -const compare = __importStar(require("./shared/compare")); const constants = __importStar(require("./shared/constants")); const hash = __importStar(require("./shared/hash")); const math = __importStar(require("./shared/math")); @@ -70,7 +70,7 @@ exports.sharedUtils = { tree, url, uuid, - compare, + assertion, }; exports.Utils = exports.sharedUtils; exports.default = { ...exports.Utils }; diff --git a/dist/js/index_browser.d.ts b/dist/js/index_browser.d.ts index 62de13b..0344e40 100644 --- a/dist/js/index_browser.d.ts +++ b/dist/js/index_browser.d.ts @@ -17,7 +17,7 @@ export declare const Utils: { tree: typeof import("./shared/tree"); url: typeof import("./shared/url"); uuid: typeof import("./shared/uuid"); - compare: typeof import("./shared/compare"); + assertion: typeof import("./shared/assertion"); }; declare const _default: { codemirror: typeof codemirror; @@ -34,6 +34,6 @@ declare const _default: { tree: typeof import("./shared/tree"); url: typeof import("./shared/url"); uuid: typeof import("./shared/uuid"); - compare: typeof import("./shared/compare"); + assertion: typeof import("./shared/assertion"); }; export default _default; diff --git a/dist/js/index_server.d.ts b/dist/js/index_server.d.ts index f6efc60..57d8632 100644 --- a/dist/js/index_server.d.ts +++ b/dist/js/index_server.d.ts @@ -17,7 +17,7 @@ export declare const Utils: { tree: typeof import("./shared/tree"); url: typeof import("./shared/url"); uuid: typeof import("./shared/uuid"); - compare: typeof import("./shared/compare"); + assertion: typeof import("./shared/assertion"); }; declare const _default: { file: typeof file; @@ -34,6 +34,6 @@ declare const _default: { tree: typeof import("./shared/tree"); url: typeof import("./shared/url"); uuid: typeof import("./shared/uuid"); - compare: typeof import("./shared/compare"); + assertion: typeof import("./shared/assertion"); }; export default _default; diff --git a/dist/js/shared/assertion.d.ts b/dist/js/shared/assertion.d.ts new file mode 100644 index 0000000..bae68b1 --- /dev/null +++ b/dist/js/shared/assertion.d.ts @@ -0,0 +1,6 @@ +export declare function assertShallowDeepAlmostEqual( + expect?: object | boolean | number | null | string | Date, + actual?: object | boolean | number | null | string | Date, + path?: string, + threshold?: number, +): boolean; diff --git a/dist/js/shared/assertion.js b/dist/js/shared/assertion.js new file mode 100644 index 0000000..e9885a9 --- /dev/null +++ b/dist/js/shared/assertion.js @@ -0,0 +1,70 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.assertShallowDeepAlmostEqual = void 0; +function assertShallowDeepAlmostEqual(expect, actual, path = "", threshold = 0.01) { + // null value + if (expect === null) { + if (!(actual === null)) { + throw new Error(`Expected to have null but got "${actual}" at path "${path}".`); + } + return true; + } + // undefined expected value + if (typeof expect === "undefined") { + if (typeof actual !== "undefined") { + throw new Error(`Expected to have undefined but got "${actual}" at path "${path}".`); + } + return true; + } + // scalar description + if (typeof expect === "boolean" || typeof expect === "string") { + if (expect !== actual) { + throw new Error(`Expected to have "${expect}" but got "${actual}" at path "${path}".`); + } + return true; + } + // numbers — here is some important 'almost equal' stuff + // TODO: configurable threshold + if (typeof expect === "number") { + if (typeof actual !== "number") { + throw new Error(`Expected to have number but got "${actual}" at path "${path}".`); + } + if (Math.abs(expect - actual) > threshold) { + throw new Error( + `Expected to have "${expect}+-${threshold}" but got "${actual}" at path "${path}".`, + ); + } + return true; + } + // dates + if (expect instanceof Date) { + if (actual instanceof Date) { + if (expect.getTime() !== actual.getTime()) { + throw new Error( + `Expected to have date "${expect.toISOString()}" but got "${actual.toISOString()}" at path "${path}".`, + ); + } + } else { + throw new Error( + `Expected to have date "${expect.toISOString()}" but got "${actual}" at path "${path}".`, + ); + } + return true; + } + if (actual === null) { + throw new Error(`Expected to have an array/object but got null at path "${path}".`); + } + // array/object description + // eslint-disable-next-line no-restricted-syntax, guard-for-in + for (const prop in expect) { + // @ts-ignore + if (typeof actual[prop] === "undefined" && typeof expect[prop] !== "undefined") { + throw new Error(`Expected "${prop}" field to be defined at path "${path}".`); + } + const newPath = path + (path === "/" ? "" : "/") + prop; + // @ts-ignore + shallowDeepAlmostEqual(expect[prop], actual[prop], newPath, threshold); + } + return true; +} +exports.assertShallowDeepAlmostEqual = assertShallowDeepAlmostEqual;