-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(relate): add method to dedupe dobjs by identity
- Loading branch information
1 parent
40b4e88
commit ef13b1e
Showing
2 changed files
with
85 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { DomainEntity } from '../../instantiation/DomainEntity'; | ||
import { DomainValueObject } from '../../instantiation/DomainValueObject'; | ||
import { dedupe } from './dedupe'; | ||
|
||
describe('dedupe', () => { | ||
interface SeaTurtle { | ||
saltwaterSecurityNumber: string; | ||
name: string; | ||
} | ||
class SeaTurtle extends DomainEntity<SeaTurtle> implements SeaTurtle { | ||
public static unique = ['saltwaterSecurityNumber']; | ||
} | ||
|
||
interface Region { | ||
name: string; | ||
} | ||
class Region extends DomainValueObject<Region> implements Region {} | ||
|
||
// some entities with a dupe | ||
const turtleOne = new SeaTurtle({ | ||
saltwaterSecurityNumber: '821', | ||
name: 'Shelly C.', | ||
}); | ||
const turtleTwo = new SeaTurtle({ | ||
saltwaterSecurityNumber: '921', | ||
name: 'Shellbert', | ||
}); | ||
const turtleOneAlias = new SeaTurtle({ | ||
...turtleOne, | ||
name: 'Shellina C.', | ||
}); | ||
|
||
// some value objects with a dupe | ||
const regionOne = new Region({ | ||
name: 'Great Barrier Reef', | ||
}); | ||
const regionTwo = new Region({ | ||
name: 'Puerto Rico Trench', | ||
}); | ||
const regionThree = new Region({ | ||
name: 'East Pacific', | ||
}); | ||
|
||
it('should keep only the one instance per distinct identity', () => { | ||
const deduped = dedupe([ | ||
// entities | ||
turtleOne, | ||
turtleTwo, | ||
turtleTwo, // exact dupe | ||
turtleOneAlias, // identity dupe | ||
turtleTwo, // exact dupe | ||
regionOne, | ||
regionThree, | ||
regionTwo, | ||
regionTwo, | ||
regionOne, | ||
]); | ||
expect(deduped).toHaveLength(5); | ||
}); | ||
it('should keep only the first instance per distinct identity', () => { | ||
const deduped = dedupe([ | ||
turtleOne, // exact dupe | ||
turtleOneAlias, // identity dupe | ||
turtleOne, // exact dupe | ||
turtleOneAlias, // identity dupe | ||
]); | ||
expect(deduped).toHaveLength(1); | ||
expect(deduped[0].name).toEqual('Shelly C.'); | ||
}); | ||
}); |
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,15 @@ | ||
import { DomainObject } from '../../instantiation/DomainObject'; | ||
import { getUniqueIdentifier } from '../getUniqueIdentifier'; | ||
import { serialize } from '../serde/serialize'; | ||
|
||
export const dedupe = <T extends DomainObject<Record<string, any>>>( | ||
objs: T[], | ||
): T[] => | ||
objs.filter((thisObj, thisIndex) => { | ||
const indexOfFirstOccurrence = objs.findIndex( | ||
(otherObj) => | ||
serialize(getUniqueIdentifier(thisObj)) === | ||
serialize(getUniqueIdentifier(otherObj)), | ||
); | ||
return indexOfFirstOccurrence === thisIndex; // if this index is the same as its first index, then keep it; otherwise, its a dupe | ||
}); |