Skip to content

Commit

Permalink
Replace JSON.parse(JSON.stringify(json)) with structuredClone
Browse files Browse the repository at this point in the history
  • Loading branch information
barmintor committed Jan 27, 2025
1 parent 88cc3f9 commit 3032bc5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions __tests__/src/selectors/manifests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ describe('getManifestSearchService', () => {
});

it('supports v1 of the search spec', () => {
const v1 = JSON.parse(JSON.stringify(manifestFixtureFg165hz3589));
const v1 = structuredClone(manifestFixtureFg165hz3589);
v1.service[0].profile = 'http://iiif.io/api/search/1/search';
const state = { manifests: { x: { json: v1 } } };
expect(getManifestSearchService(state, { manifestId: 'x' }).id).toEqual('https://contentsearch.stanford.edu/fg165hz3589/search');
Expand All @@ -525,7 +525,7 @@ describe('getManifestAutocompleteService', () => {
});

it('supports v1 of the search spec', () => {
const v1 = JSON.parse(JSON.stringify(manifestFixtureFg165hz3589));
const v1 = structuredClone(manifestFixtureFg165hz3589);
v1.service[0].profile = 'http://iiif.io/api/search/1/search';
v1.service[0].service.profile = 'http://iiif.io/api/search/1/autocomplete';
const state = { manifests: { x: { json: v1 } } };
Expand Down
4 changes: 2 additions & 2 deletions src/state/selectors/manifests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { getConfig } from './config';
/** */
function createManifestoInstance(json, locale) {
if (!json) return undefined;
// Use JSON stringify/parse to create a deep copy and prevent Manifesto from mutating the json
const manifestoObject = Utils.parseManifest(JSON.parse(JSON.stringify(json)), locale ? { locale } : undefined);
// Use structuredClone to create a deep copy and prevent Manifesto from mutating the json
const manifestoObject = Utils.parseManifest(structuredClone(json), locale ? { locale } : undefined);
// Local patching of Manifesto so that when its a Collection, it behaves similarly
if (typeof manifestoObject.getSequences != 'function') {
manifestoObject.getSequences = () => [];
Expand Down

0 comments on commit 3032bc5

Please sign in to comment.