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" },