mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-05 05:47:10 -04:00
feat(new-tool): diff of two json objects
This commit is contained in:
parent
61ece2387f
commit
362f2fa280
12 changed files with 751 additions and 134 deletions
80
src/tools/json-diff/json-diff.models.test.ts
Normal file
80
src/tools/json-diff/json-diff.models.test.ts
Normal file
|
@ -0,0 +1,80 @@
|
|||
import { expect, describe, it } from 'vitest';
|
||||
import { diff } from './json-diff.models';
|
||||
|
||||
describe('json-diff models', () => {
|
||||
describe('diff', () => {
|
||||
it('list object differences', () => {
|
||||
const obj = { a: 1, b: 2 };
|
||||
const newObj = { a: 1, b: 2, c: 3 };
|
||||
const result = diff(obj, newObj);
|
||||
|
||||
expect(result).toEqual({
|
||||
key: '',
|
||||
type: 'object',
|
||||
children: [
|
||||
{
|
||||
key: 'a',
|
||||
type: 'value',
|
||||
value: 1,
|
||||
oldValue: 1,
|
||||
status: 'unchanged',
|
||||
},
|
||||
{
|
||||
key: 'b',
|
||||
type: 'value',
|
||||
value: 2,
|
||||
oldValue: 2,
|
||||
status: 'unchanged',
|
||||
},
|
||||
{
|
||||
key: 'c',
|
||||
type: 'value',
|
||||
value: 3,
|
||||
oldValue: undefined,
|
||||
status: 'added',
|
||||
},
|
||||
],
|
||||
oldValue: { a: 1, b: 2 },
|
||||
value: { a: 1, b: 2, c: 3 },
|
||||
status: 'children-updated',
|
||||
});
|
||||
});
|
||||
|
||||
it('list array differences', () => {
|
||||
const obj = [1, 2];
|
||||
const newObj = [1, 2, 3];
|
||||
const result = diff(obj, newObj);
|
||||
|
||||
expect(result).toEqual({
|
||||
key: '',
|
||||
type: 'array',
|
||||
children: [
|
||||
{
|
||||
key: 0,
|
||||
type: 'value',
|
||||
value: 1,
|
||||
oldValue: 1,
|
||||
status: 'unchanged',
|
||||
},
|
||||
{
|
||||
key: 1,
|
||||
type: 'value',
|
||||
value: 2,
|
||||
oldValue: 2,
|
||||
status: 'unchanged',
|
||||
},
|
||||
{
|
||||
key: 2,
|
||||
type: 'value',
|
||||
value: 3,
|
||||
oldValue: undefined,
|
||||
status: 'added',
|
||||
},
|
||||
],
|
||||
oldValue: [1, 2],
|
||||
value: [1, 2, 3],
|
||||
status: 'children-updated',
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue