Skip to content

Commit 68b8b27

Browse files
committed
fix(json-patch-diff): 🐛 allow string node type change
1 parent 0330f0d commit 68b8b27

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/json-patch-diff/Diff.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ import * as line from '../util/diff/line';
44
import {structHash} from '../json-hash';
55
import type {Operation} from '../json-patch/codec/json/types';
66

7-
export class DiffError extends Error {
8-
constructor(message: string = 'DIFF') {
9-
super(message);
10-
}
11-
}
12-
137
export class Diff {
148
protected patch: Operation[] = [];
159

@@ -83,8 +77,8 @@ export class Diff {
8377
public diffAny(path: string, src: unknown, dst: unknown): void {
8478
switch (typeof src) {
8579
case 'string': {
86-
if (typeof dst !== 'string') throw new DiffError();
87-
this.diffStr(path, src, dst);
80+
if (typeof dst == 'string') this.diffStr(path, src, dst);
81+
else this.diffVal(path, src, dst);
8882
break;
8983
}
9084
case 'number':

src/json-patch-diff/__tests__/Diff.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ describe('obj', () => {
5353
assertDiff(src, dst);
5454
});
5555

56+
test('string key type change', () => {
57+
assertDiff({foo: 'asdf'}, {foo: 123});
58+
assertDiff({foo: 123}, {foo: 'asdf'});
59+
});
60+
5661
test('can insert new key', () => {
5762
const src = {};
5863
const dst = {foo: 'hello!'};
@@ -103,6 +108,11 @@ describe('obj', () => {
103108
});
104109

105110
describe('arr', () => {
111+
test('string element type change', () => {
112+
assertDiff(['asdf'], [123]);
113+
assertDiff([123], ['asdf']);
114+
});
115+
106116
test('can add element to an empty array', () => {
107117
const src: unknown[] = [];
108118
const dst: unknown[] = [1];

0 commit comments

Comments
 (0)