-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
170 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { filterIntersectingItems, removeIntersectingItemsInPlace } from './list.utils'; | ||
|
||
describe('List Utilities', () => { | ||
|
||
describe('filterIntersectingItems', () => { | ||
it('should filter out items from list1 that are present in list2', () => { | ||
const list1 = [1, 2, 3, 4, 5]; | ||
const list2 = [3, 4, 5, 6, 7]; | ||
const result = filterIntersectingItems(list1, list2); | ||
expect(result).toEqual([1, 2]); | ||
expect(list1).toEqual([1, 2, 3, 4, 5]); // Ensure list1 is not modified | ||
}); | ||
|
||
it('should return an empty array if all items in list1 are in list2', () => { | ||
const list1 = [1, 2, 3]; | ||
const list2 = [1, 2, 3]; | ||
const result = filterIntersectingItems(list1, list2); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('should return the original list1 if no items in list1 are in list2', () => { | ||
const list1 = [1, 2, 3]; | ||
const list2 = [4, 5, 6]; | ||
const result = filterIntersectingItems(list1, list2); | ||
expect(result).toEqual([1, 2, 3]); | ||
}); | ||
}); | ||
|
||
describe('removeIntersectingItemsInPlace', () => { | ||
it('should remove items from list1 that are present in list2 in place', () => { | ||
const list1 = [1, 2, 3, 4, 5]; | ||
const list2 = [3, 4, 5, 6, 7]; | ||
removeIntersectingItemsInPlace(list1, list2); | ||
expect(list1).toEqual([1, 2]); | ||
}); | ||
|
||
it('should remove all items from list1 if they are all in list2', () => { | ||
const list1 = [1, 2, 3]; | ||
const list2 = [1, 2, 3]; | ||
removeIntersectingItemsInPlace(list1, list2); | ||
expect(list1).toEqual([]); | ||
}); | ||
|
||
it('should not modify list1 if no items in list1 are in list2', () => { | ||
const list1 = [1, 2, 3]; | ||
const list2 = [4, 5, 6]; | ||
removeIntersectingItemsInPlace(list1, list2); | ||
expect(list1).toEqual([1, 2, 3]); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @summary Filters out items from list1 that are present in list2 and returns a new list. | ||
* @param {T[]} list1 - The first list of items. | ||
* @param {T[]} list2 - The second list of items to be removed from the first list. | ||
* @return {T[]} A new list with items from list1 that are not in list2. | ||
*/ | ||
export function filterIntersectingItems<T>(list1: T[], list2: T[]): T[] { | ||
const set2 = new Set(list2); | ||
return list1.filter(item => !set2.has(item)); | ||
} | ||
|
||
/** | ||
* @summary Removes items from list1 that are present in list2 in place. | ||
* @param {T[]} list1 - The first list of items. | ||
* @param {T[]} list2 - The second list of items to be removed from the first list. | ||
*/ | ||
export function removeIntersectingItemsInPlace<T>(list1: T[], list2: T[]): void { | ||
const set2 = new Set(list2); | ||
let i = 0; | ||
while (i < list1.length) { | ||
if (set2.has(list1[i])) { | ||
list1.splice(i, 1); // Remove the item at index i | ||
} else { | ||
i++; | ||
} | ||
} | ||
} |