add diff-arrays utility for efficient array diffs
This commit is contained in:
parent
45e5749cca
commit
373c60b521
2 changed files with 109 additions and 0 deletions
53
packages/backend/test/unit/misc/diff-arrays.ts
Normal file
53
packages/backend/test/unit/misc/diff-arrays.ts
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { diffArrays } from '@/misc/diff-arrays.js';
|
||||
|
||||
describe(diffArrays, () => {
|
||||
it('should return empty result when both inputs are null', () => {
|
||||
const result = diffArrays(null, null);
|
||||
expect(result.added).toHaveLength(0);
|
||||
expect(result.removed).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should return empty result when both inputs are empty', () => {
|
||||
const result = diffArrays([], []);
|
||||
expect(result.added).toHaveLength(0);
|
||||
expect(result.removed).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should remove before when added is empty', () => {
|
||||
const result = diffArrays([1, 2, 3], []);
|
||||
expect(result.added).toHaveLength(0);
|
||||
expect(result.removed).toEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
it('should deduplicate before when added is empty', () => {
|
||||
const result = diffArrays([1, 1, 2, 2, 3], []);
|
||||
expect(result.added).toHaveLength(0);
|
||||
expect(result.removed).toEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
it('should remove after when before is empty', () => {
|
||||
const result = diffArrays([], [1, 2, 3]);
|
||||
expect(result.added).toEqual([1, 2, 3]);
|
||||
expect(result.removed).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should deduplicate after when before is empty', () => {
|
||||
const result = diffArrays([], [1, 1, 2, 2, 3]);
|
||||
expect(result.added).toEqual([1, 2, 3]);
|
||||
expect(result.removed).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should return diff when both have values', () => {
|
||||
const result = diffArrays(
|
||||
['a', 'b', 'c', 'd'],
|
||||
['a', 'c', 'e', 'f'],
|
||||
);
|
||||
expect(result.added).toEqual(['e', 'f']);
|
||||
expect(result.removed).toEqual(['b', 'd']);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue