Skip to content

Commit

Permalink
Refactor getSongDiffFromManifest to handle file renames.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioanlucut committed Sep 25, 2023
1 parent 924ce86 commit e14d317
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
42 changes: 42 additions & 0 deletions src/converterService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,47 @@ describe('converterService', () => {
}
`);
});

it('should work correctly for file renames only (no changes)', () => {
expect(
getSongDiffFromManifest(
{
inventory: [
{
id: 'de8UGb2cKEsThEuMwFvjQq',
fileName: 'A Mea iubire si mantuire.txt',
contentHash: 'a83f0e',
},
],

updatedOn: '2023-09-25-17:14:16',
},
{
inventory: [
{
id: 'de8UGb2cKEsThEuMwFvjQq',
fileName: 'A mea iubire si mantuire.txt',
contentHash: 'a83f0e',
},
],

updatedOn: '2023-09-25-17:14:16',
},
),
).toMatchInlineSnapshot(`
{
"newOrUpdatedSongs": [
{
"contentHash": "a83f0e",
"fileName": "A Mea iubire si mantuire.txt",
"id": "de8UGb2cKEsThEuMwFvjQq",
},
],
"toBeRemovedFileNames": [
"A mea iubire si mantuire.txt",
],
}
`);
});
});
});
15 changes: 8 additions & 7 deletions src/converterService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,29 @@ export const transformManifestToHashMapForFasterRetrievals = (
);

export const getSongDiffFromManifest = (
currentManifest: SongsInventoryManifest,
newManifest: SongsInventoryManifest,
previousManifest: SongsInventoryManifest,
) => {
const previousManifestHashMap = transformManifestToHashMapForFasterRetrievals(
previousManifest.inventory,
);
const currentManifestHashMap = transformManifestToHashMapForFasterRetrievals(
currentManifest.inventory,
newManifest.inventory,
);

const newOrUpdatedSongs = currentManifest.inventory.filter(function ({
const newOrUpdatedSongs = newManifest.inventory.filter(function ({
id,
fileName,
contentHash,
}) {
const maybePreviousSong = previousManifestHashMap[id];
const isNewSong = !maybePreviousSong;
const isExistingSongChanged =
!isNewSong && !isEqual(maybePreviousSong?.contentHash, contentHash);
const isFileNameChanged =
!isNewSong && !isEqual(previousManifestHashMap[id]?.fileName, fileName);

return (
isNewSong ||
(!isNewSong && !isEqual(maybePreviousSong?.contentHash, contentHash))
);
return isNewSong || isExistingSongChanged || isFileNameChanged;
});

const toBeRemovedFileNames = previousManifest.inventory
Expand Down

0 comments on commit e14d317

Please sign in to comment.