Skip to content

Commit fab3585

Browse files
committed
fix yaml outputing json and enhance jsonPatcher extensibility
1 parent 0f69e4c commit fab3585

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

Common/Node/jsonPatcher.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@ var jsonPatch = require('fast-json-patch');
44

55
export class JsonPatcher implements patch.IPatcher {
66
constructor(
7-
private patches: patch.IPatch[],
8-
private parse?: (content: string) => any
7+
private patches: patch.IPatch[]
98
) {
10-
if (!this.parse) {
11-
this.parse = (content) => JSON.parse(content);
12-
}
9+
}
10+
11+
protected parse(content: string): any {
12+
return JSON.parse(content);
13+
}
14+
15+
protected stringify(content: any): string {
16+
return JSON.stringify(content);
1317
}
1418

1519
apply(content: string): string {
1620
var json = this.parse(content);
1721
var prevalidate = jsonPatch.validate(this.patches, json);
1822
var result = jsonPatch.apply(json, this.patches, false);
1923
if (result) {
20-
return JSON.stringify(json);
24+
return this.stringify(json);
2125
} else {
2226
throw new Error('Failed to apply patch')
2327
}

Tasks/YamlPatch/yamlPatcher.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import patch = require('./common/patch');
44

55
export class YamlPatcher extends jsonPatcher.JsonPatcher {
66

7-
constructor(patches: patch.IPatch[]) {
8-
super(patches, (content) => jsYaml.safeLoad(content));
7+
parse(content: string): any {
8+
return jsYaml.safeLoad(content);
9+
}
10+
11+
stringify(content: any): string {
12+
return jsYaml.safeDump(content);
913
}
1014
}

0 commit comments

Comments
 (0)