Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@contember/schema-utils

Package Overview
Dependencies
Maintainers
5
Versions
259
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@contember/schema-utils - npm Package Compare versions

Comparing version 1.3.0-alpha.8 to 1.3.0-alpha.13

dist/tests/cases/unit/deepCompare.test.d.ts

2

dist/src/index.d.ts

@@ -12,5 +12,5 @@ import { Schema } from '@contember/schema';

export * from './schemaFilter';
export { deepCompare } from './utils';
export { deepCompare, compareArraysIgnoreOrder } from './utils';
export declare const emptySchema: Schema;
export declare const schemaType: Typesafe.Type<Schema>;
//# sourceMappingURL=index.d.ts.map

@@ -29,3 +29,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.schemaType = exports.emptySchema = exports.deepCompare = void 0;
exports.schemaType = exports.emptySchema = exports.compareArraysIgnoreOrder = exports.deepCompare = void 0;
const model_1 = require("./model");

@@ -46,2 +46,3 @@ const Typesafe = __importStar(require("@contember/typesafe"));

Object.defineProperty(exports, "deepCompare", { enumerable: true, get: function () { return utils_1.deepCompare; } });
Object.defineProperty(exports, "compareArraysIgnoreOrder", { enumerable: true, get: function () { return utils_1.compareArraysIgnoreOrder; } });
exports.emptySchema = {

@@ -48,0 +49,0 @@ model: model_1.emptyModelSchema,

@@ -0,7 +1,13 @@

declare type Path = (string | number)[];
declare type CompareError = {
path: (string | number)[];
path: Path;
message: string;
};
export declare function deepCompare(a: any, b: any, path: (string | number)[]): CompareError[];
declare type Comparator = (a: unknown, b: unknown) => CompareError[];
export declare const compareArraysIgnoreOrder: (a: unknown, b: unknown, path: Path) => {
path: Path;
message: string;
}[];
export declare function deepCompare(a: any, b: any, path?: Path, getCustomComparator?: (path: Path) => Comparator | null): CompareError[];
export {};
//# sourceMappingURL=deepCompare.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deepCompare = void 0;
exports.deepCompare = exports.compareArraysIgnoreOrder = void 0;
const getType = (val) => {

@@ -23,6 +23,34 @@ if (val === null) {

};
function deepCompare(a, b, path) {
const compareArraysIgnoreOrder = (a, b, path) => {
if (!Array.isArray(a) || !Array.isArray(b)) {
return [{ path, message: 'Invalid type, expected array' }];
}
if (a.length !== b.length) {
return [{ path, message: `Array length: ${a.length} != ${b.length}` }];
}
const haystack = [...b];
for (const needle of a) {
const index = haystack.findIndex(it => {
const errors = deepCompare(needle, it, path);
return errors.length === 0;
});
if (index < 0) {
return [{ path, message: `Array item: ${JSON.stringify(needle)} not found in ${JSON.stringify(haystack)}` }];
}
haystack.splice(index, 1);
}
if (haystack.length > 0) {
return [{ path, message: `Array items: ${JSON.stringify(haystack)} not found in ${JSON.stringify(a)}` }];
}
return [];
};
exports.compareArraysIgnoreOrder = compareArraysIgnoreOrder;
function deepCompare(a, b, path = [], getCustomComparator) {
if (a === b) {
return [];
}
const comparator = getCustomComparator === null || getCustomComparator === void 0 ? void 0 : getCustomComparator(path);
if (comparator) {
return comparator(a, b);
}
const aType = getType(a);

@@ -51,3 +79,3 @@ const bType = getType(b);

for (let i = 0; i < aLength; i++) {
errors.push(...deepCompare(a[i], b[i], [...path, i]));
errors.push(...deepCompare(a[i], b[i], [...path, i], getCustomComparator));
}

@@ -89,3 +117,3 @@ return errors;

else {
errors.push(...deepCompare(a[key], b[key], [...path, key]));
errors.push(...deepCompare(a[key], b[key], [...path, key], getCustomComparator));
}

@@ -92,0 +120,0 @@ }

{
"name": "@contember/schema-utils",
"version": "1.3.0-alpha.8",
"version": "1.3.0-alpha.13",
"license": "Apache-2.0",

@@ -15,9 +15,9 @@ "main": "dist/src/index.js",

"dependencies": {
"@contember/schema": "^1.3.0-alpha.8",
"@contember/typesafe": "^1.3.0-alpha.8"
"@contember/schema": "1.3.0-alpha.13",
"@contember/typesafe": "1.3.0-alpha.13"
},
"devDependencies": {
"@contember/schema-definition": "^1.3.0-alpha.8",
"@contember/schema-definition": "1.3.0-alpha.13",
"@types/node": "^18"
}
}
}

@@ -16,3 +16,3 @@ import { Schema } from '@contember/schema'

export * from './schemaFilter'
export { deepCompare } from './utils'
export { deepCompare, compareArraysIgnoreOrder } from './utils'

@@ -19,0 +19,0 @@ export const emptySchema: Schema = {

@@ -0,3 +1,5 @@

type Path = (string | number)[]
type CompareError = {
path: (string | number)[]
path: Path
message: string

@@ -29,6 +31,37 @@ }

export function deepCompare(a: any, b: any, path: (string | number)[]): CompareError[] {
type Comparator = (a: unknown, b: unknown) => CompareError[]
export const compareArraysIgnoreOrder = (a: unknown, b: unknown, path: Path) => {
if (!Array.isArray(a) || !Array.isArray(b)) {
return [{ path, message: 'Invalid type, expected array' }]
}
if (a.length !== b.length) {
return [{ path, message: `Array length: ${a.length} != ${b.length}` }]
}
const haystack = [...b]
for (const needle of a) {
const index = haystack.findIndex(it => {
const errors = deepCompare(needle, it, path)
return errors.length === 0
})
if (index < 0) {
return [{ path, message: `Array item: ${JSON.stringify(needle)} not found in ${JSON.stringify(haystack)}` }]
}
haystack.splice(index, 1)
}
if (haystack.length > 0) {
return [{ path, message: `Array items: ${JSON.stringify(haystack)} not found in ${JSON.stringify(a)}` }]
}
return []
}
export function deepCompare(a: any, b: any, path: Path = [], getCustomComparator?: (path: Path) => Comparator | null): CompareError[] {
if (a === b) {
return []
}
const comparator = getCustomComparator?.(path)
if (comparator) {
return comparator(a, b)
}
const aType = getType(a)

@@ -57,3 +90,3 @@ const bType = getType(b)

for (let i = 0; i < aLength; i++) {
errors.push(...deepCompare(a[i], b[i], [...path, i]))
errors.push(...deepCompare(a[i], b[i], [...path, i], getCustomComparator))
}

@@ -96,3 +129,3 @@ return errors

} else {
errors.push(...deepCompare(a[key], b[key], [...path, key]))
errors.push(...deepCompare(a[key], b[key], [...path, key], getCustomComparator))
}

@@ -99,0 +132,0 @@ }

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc