Browse Source

fix(admin-ui): Allow SortPipe to work with frozen arrays

Apollo Client now freezes objects from the cache.
Michael Bromley 5 years ago
parent
commit
00e0af93fd

+ 5 - 0
packages/admin-ui/src/lib/core/src/shared/pipes/sort.pipe.spec.ts

@@ -18,4 +18,9 @@ describe('SortPipe', () => {
             { id: 9 },
         ]);
     });
+
+    it('sorts a frozen array', () => {
+        const input = Object.freeze([5, 4, 2, 3, 2, 7, 1]);
+        expect(sortPipe.transform(input)).toEqual([1, 2, 2, 3, 4, 5, 7]);
+    });
 });

+ 2 - 2
packages/admin-ui/src/lib/core/src/shared/pipes/sort.pipe.ts

@@ -9,8 +9,8 @@ import { Pipe, PipeTransform } from '@angular/core';
     name: 'sort',
 })
 export class SortPipe implements PipeTransform {
-    transform<T>(value: T[], orderByProp?: keyof T) {
-        return value.sort((a, b) => {
+    transform<T>(value: T[] | readonly T[], orderByProp?: keyof T) {
+        return value.slice().sort((a, b) => {
             const aProp = orderByProp ? a[orderByProp] : a;
             const bProp = orderByProp ? b[orderByProp] : b;
             if (aProp === bProp) {