diff --git a/webapp/pages/post/_id/_slug/index.spec.js b/webapp/pages/post/_id/_slug/index.spec.js index 176041ecb..bc54edf53 100644 --- a/webapp/pages/post/_id/_slug/index.spec.js +++ b/webapp/pages/post/_id/_slug/index.spec.js @@ -156,7 +156,7 @@ describe('PostSlug', () => { { id: 'Bw' }, { id: 'a' }, ] - + wrapper = await Wrapper() }) @@ -167,16 +167,16 @@ describe('PostSlug', () => { it('are alphabetically ordered', async () => { // Get all HcHastag components - let wrappers = wrapper.findAll(HcHashtag).wrappers + const wrappers = wrapper.findAll(HcHashtag).wrappers // Exctract ID properties (tag names) from component. - let ids = [] + const ids = [] wrappers.forEach((x) => { ids.push({ id: x.props().id, }) }) // Compare extracted IDs with solution. - let idsAlphabetically = [ + const idsAlphabetically = [ { id: '42' }, { id: 'a' }, { id: 'BQ' }, diff --git a/webapp/pages/post/_id/_slug/index.vue b/webapp/pages/post/_id/_slug/index.vue index 2082cea0c..e552062f4 100644 --- a/webapp/pages/post/_id/_slug/index.vue +++ b/webapp/pages/post/_id/_slug/index.vue @@ -182,13 +182,16 @@ export default { sortedTags() { // Make sure the property is valid. if (!this.post.tags || !this.post.tags.length) return false - // Sorting actually happens in place but that should not be a problem. - return this.post.tags.sort(function (a, b) { - // Converting to lowercase to make sort case insensitive. - const tagA = a.id.toLowerCase() - const tagB = b.id.toLowerCase() - return tagA < tagB ? -1 : tagA > tagB ? 1 : 0 - }) + /* Using .slice(0) to make a shallow copy. Otherwise a vue/no-side-effects-in-computed-properties error + would be thrown because sort() sorts in place. A shallow copy is fine because only first level objects are + affected by the sort, the original this.post.tags object remains unchanged. + */ + return this.post.tags.slice(0).sort(function (a, b) { + // Converting to lowercase to make sort case insensitive. + const tagA = a.id.toLowerCase() + const tagB = b.id.toLowerCase() + return tagA < tagB ? -1 : tagA > tagB ? 1 : 0 + }) }, }, methods: { diff --git a/webapp/pages/post/_id/_slug/more-info.vue b/webapp/pages/post/_id/_slug/more-info.vue index 5dcaf4029..ac1a6694d 100644 --- a/webapp/pages/post/_id/_slug/more-info.vue +++ b/webapp/pages/post/_id/_slug/more-info.vue @@ -65,13 +65,16 @@ export default { sortedTags() { // Make sure the property is valid. if (!this.post.tags || !this.post.tags.length) return false - // Sorting actually happens in place but that should not be a problem. - return this.post.tags.sort(function (a, b) { - // Converting to lowercase to make sort case insensitive. - const tagA = a.id.toLowerCase() - const tagB = b.id.toLowerCase() - return tagA < tagB ? -1 : tagA > tagB ? 1 : 0 - }) + /* Using .slice(0) to make a shallow copy. Otherwise a vue/no-side-effects-in-computed-properties error + would be thrown because sort() sorts in place. A shallow copy is fine because only first level objects are + affected by the sort, the original this.post.tags object remains unchanged. + */ + return this.post.tags.slice(0).sort(function (a, b) { + // Converting to lowercase to make sort case insensitive. + const tagA = a.id.toLowerCase() + const tagB = b.id.toLowerCase() + return tagA < tagB ? -1 : tagA > tagB ? 1 : 0 + }) }, }, methods: {