Skip to content

Commit 93e35ac

Browse files
committed
fix: bin format
1 parent 4b76581 commit 93e35ac

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

packages/jsondiffpatch/bin/jsondiffpatch.js

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ import { create } from "../lib/with-text-diffs.js";
88
const allowedFlags = [
99
"--help",
1010
"--format",
11-
"--omit-removed-values",
12-
"--no-moves",
13-
"--no-text-diff",
14-
"--object-keys",
11+
"--omit-removed-values",
12+
"--no-moves",
13+
"--no-text-diff",
14+
"--object-keys",
1515
];
1616

1717
const args = process.argv.slice(2);
1818
const flags = {};
1919
const files = [];
2020
for (const arg of args) {
2121
if (arg.startsWith("--")) {
22-
const argParts = arg.split("=");
22+
const argParts = arg.split("=");
2323
if (allowedFlags.indexOf(argParts[0]) === -1) {
2424
console.error(`unrecognized option: ${argParts[0]}`);
2525
process.exit(2);
@@ -49,8 +49,10 @@ example:`;
4949
};
5050

5151
function createInstance() {
52-
const format = typeof flags["--format"] === "string" ? flags["--format"] : "console";
53-
const objectKeys = (flags["--object-keys="] ?? "id,key").split(",")
52+
const format =
53+
typeof flags["--format"] === "string" ? flags["--format"] : "console";
54+
const objectKeys = (flags["--object-keys="] ?? "id,key")
55+
.split(",")
5456
.map((key) => key.trim());
5557

5658
const jsondiffpatch = create({
@@ -77,19 +79,19 @@ function createInstance() {
7779
: {}),
7880
},
7981
});
80-
return jsondiffpatch;
82+
return jsondiffpatch;
8183
}
8284

8385
function printDiff(delta) {
84-
if (flags["--format"] ==="json") {
85-
console.log(JSON.stringify(delta, null, 2));
86-
} else if (flags["--format"] ==="json-compact") {
87-
console.log(JSON.stringify(delta));
88-
} else if (flags["--format"] === "jsonpatch") {
89-
jsonpatchFormatter.log(delta);
90-
} else {
91-
consoleFormatter.log(delta);
92-
}
86+
if (flags["--format"] === "json") {
87+
console.log(JSON.stringify(delta, null, 2));
88+
} else if (flags["--format"] === "json-compact") {
89+
console.log(JSON.stringify(delta));
90+
} else if (flags["--format"] === "jsonpatch") {
91+
jsonpatchFormatter.log(delta);
92+
} else {
93+
consoleFormatter.log(delta);
94+
}
9395
}
9496

9597
function getJson(path) {
@@ -104,32 +106,29 @@ const jsondiffpatch = createInstance();
104106

105107
if (files.length !== 2 || flags.includes("--help")) {
106108
console.log(usage());
107-
const delta = jsondiffpatch.diff({
108-
property: "before",
109-
list: [
110-
{ id: 1,},
111-
{ id: 2, },
112-
{ id: 3, name: "item removed" },
113-
],
114-
longText: 'when a text is very 🦕 long, diff-match-patch is used to create a text diff that only captures the changes, comparing each characther',
115-
}, {
116-
property: "after",
117-
newProperty: "added",
118-
list: [
119-
{ id: 2, },
120-
{ id: 1, },
121-
{ id: 4, name: "item added" },
122-
],
123-
longText: 'when a text a bit long, diff-match-patch creates a text diff that captures the changes, comparing each characther',
124-
});
125-
printDiff(delta);
109+
const delta = jsondiffpatch.diff(
110+
{
111+
property: "before",
112+
list: [{ id: 1 }, { id: 2 }, { id: 3, name: "item removed" }],
113+
longText:
114+
"when a text is very 🦕 long, diff-match-patch is used to create a text diff that only captures the changes, comparing each characther",
115+
},
116+
{
117+
property: "after",
118+
newProperty: "added",
119+
list: [{ id: 2 }, { id: 1 }, { id: 4, name: "item added" }],
120+
longText:
121+
"when a text a bit long, diff-match-patch creates a text diff that captures the changes, comparing each characther",
122+
},
123+
);
124+
printDiff(delta);
126125
} else {
127126
Promise.all([files[0], files[1]].map(getJson)).then(([left, right]) => {
128127
const delta = jsondiffpatch.diff(left, right);
129128
if (delta === undefined) {
130129
process.exit(0);
131130
} else {
132-
printDiff(delta);
131+
printDiff(delta);
133132
// exit code 1 to be consistent with GNU diff
134133
process.exit(1);
135134
}

0 commit comments

Comments
 (0)