From 33d4e2024543f6e4650ec06ae0ad1f08b029bc9d Mon Sep 17 00:00:00 2001 From: Benjamin Young Date: Thu, 8 Feb 2024 16:30:07 -0500 Subject: [PATCH] Add diff command. Expands two JSON-LD files and compares the expanded results. This can be extremely useful when moving to a new context file that uses (some or all of) the same term definitions as the previous context file. --- bin/jsonld.js | 21 +++++++++++++++++++++ package.json | 1 + 2 files changed, 22 insertions(+) diff --git a/bin/jsonld.js b/bin/jsonld.js index 98234bf..0f103b9 100755 --- a/bin/jsonld.js +++ b/bin/jsonld.js @@ -8,6 +8,7 @@ * Copyright (c) 2013-2022 Digital Bazaar, Inc. * All rights reserved. */ +import {createDeepComparer} from 'deep-comparer'; import {fileURLToPath} from 'node:url'; import https from 'node:https'; import {inspect} from 'node:util'; @@ -424,6 +425,26 @@ _jsonLdCommand(program.command('canonize [filename|URL|-]')) await _output(result, cmd); }); +program.command('diff ') + .description('compare two JSON-LD documents in expanded form') + .action(async function diff(filenameA, filenameB, cmd) { + const options = _jsonLdOptions(cmd, filenameA); + options.keepFreeFloatingNodes = cmd.keepFreeFloatingNodes; + + const resultA = await jsonld.expand(filenameA, options); + const resultB = await jsonld.expand(filenameB, options); + + const deepCompare = createDeepComparer(); + const differences = await deepCompare(resultA, resultB); + + if(differences.length > 0) { + console.log('Differences found:'); + console.dir(differences, {depth: 5}); + } else { + console.log('No differences found.'); + } + }); + program .parseAsync(process.argv) .catch(e => { diff --git a/package.json b/package.json index caf8c45..4bfe7c5 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "type": "module", "dependencies": { "commander": "^11.1.0", + "deep-comparer": "^2.0.3", "jsonld": "^8.3.2", "jsonld-request": "^2.0.1" },