Sfoglia il codice sorgente

add arrayUtils.test.ts, revise ArrayUtils, refact dateUtils

Olivier Massot 2 anni fa
parent
commit
d08d3b3bf3

+ 1 - 1
components/Layout/Header/Notification.vue

@@ -125,7 +125,7 @@ let { data: collection, pending, refresh } = await fetchCollection(Notification)
  */
 const notifications: ComputedRef = computed(() => {
   const items = collection.value !== null ? collection.value.items : []
-  return ArrayUtils.sortArrayOfObject(items, 'id')
+  return ArrayUtils.sortObjectsByProp(items, 'id')
 })
 
 /**

+ 15 - 7
services/utils/arrayUtils.ts

@@ -3,19 +3,27 @@ export default class ArrayUtils {
 
   /**
    * Trie un tableau
+   *
    * @param array
+   * @param reverse
    */
-  public static sort(array: Array<any>): Array<Object> {
-    return array.sort((a: any, b: any) => a - b)
+  public static sort(array: Array<any>, reverse: boolean = false): Array<Object> {
+    return array.sort((a, b) => {
+      return ((a < b) ? -1 : (a > b ? 1 : 0)) * (reverse ? -1 : 1)
+    })
   }
 
   /**
-   * Trie un tableau d'objets
-   * @param array
-   * @param param
+   * Trie un tableau d'objets selon une propriété commune
+   *
+   * @param array Une array d'objets
+   * @param property Le nom d'une propriété possédée par tous les objets
+   * @param reverse
    */
-  public static sortArrayOfObject(array: Array<Object>, param: any): Array<Object> {
-    return array.sort((a: any, b: any) => a[param] - b[param])
+  public static sortObjectsByProp(array: Array<Object>, property: string, reverse: boolean = false): Array<Object> {
+    return array.sort((a: any, b: any) => {
+      return ((a[property] < b[property]) ? -1 : (a[property] > b[property] ? 1 : 0)) * (reverse ? -1 : 1)
+    })
   }
 
 }

+ 2 - 9
services/utils/dateUtils.ts

@@ -1,4 +1,5 @@
 import { format } from 'date-fns';
+import ArrayUtils from "~/services/utils/arrayUtils";
 
 export default class DateUtils {
 
@@ -25,14 +26,6 @@ export default class DateUtils {
    * @param reverse
    */
   public static sort(dates: Array<Date>, reverse: boolean = false): Array<Date> {
-    return dates.sort((a, b) => {
-      if (a < b) {
-        return reverse ? 1 : -1;
-      }
-      if (a > b) {
-        return reverse ? -1 : 1;
-      }
-      return 0;
-    })
+    return ArrayUtils.sort(dates, reverse) as Array<Date>
   }
 }

+ 1 - 1
services/utils/i18nUtils.ts

@@ -26,7 +26,7 @@ export default class I18nUtils {
         )
 
         if (sort) {
-            enum_ = ArrayUtils.sortArrayOfObject(enum_, 'label') as EnumChoices
+            enum_ = ArrayUtils.sortObjectsByProp(enum_, 'label') as EnumChoices
         }
 
         return enum_

+ 70 - 0
tests/units/services/utils/arrayUtils.test.ts

@@ -0,0 +1,70 @@
+import { describe, test, it, expect } from 'vitest'
+import ArrayUtils from "~/services/utils/arrayUtils";
+import DateUtils from "~/services/utils/dateUtils";
+
+describe('sort', () => {
+    test('with integers', () => {
+        expect(ArrayUtils.sort([2, 1, 3])).toEqual([1, 2, 3])
+        expect(ArrayUtils.sort([2, 1, 3], true)).toEqual([3, 2, 1])
+    })
+    test('with string', () => {
+        expect(ArrayUtils.sort(['b', 'a', 'c'])).toEqual(['a', 'b', 'c'])
+        expect(ArrayUtils.sort(['b', 'a', 'c'], true)).toEqual(['c', 'b', 'a'])
+    })
+    test('with dates', () => {
+        const input = [
+            new Date(2023, 0, 12),
+            new Date(2023, 0, 10),
+            new Date(2023, 0, 11)
+        ]
+
+        const expected = [
+            new Date(2023, 0, 10),
+            new Date(2023, 0, 11),
+            new Date(2023, 0, 12)
+        ]
+
+        const expectedReverse = [
+            new Date(2023, 0, 12),
+            new Date(2023, 0, 11),
+            new Date(2023, 0, 10)
+        ]
+
+        expect(DateUtils.sort(input)).toEqual(expected)
+        expect(DateUtils.sort(input, true)).toEqual(expectedReverse)
+    })
+})
+
+describe('sortObjectsByProp', () => {
+    test('existing prop', () => {
+        const input = [
+            {'id': 2, 'name': 'b'},
+            {'id': 1, 'name': 'a'},
+            {'id': 3, 'name': 'c'},
+        ]
+
+        const expected = [
+            {'id': 1, 'name': 'a'},
+            {'id': 2, 'name': 'b'},
+            {'id': 3, 'name': 'c'},
+        ]
+
+        expect(ArrayUtils.sortObjectsByProp(input, 'id')).toEqual(expected)
+    })
+    test('existing prop (reverse)', () => {
+        const input = [
+            {'id': 2, 'name': 'b'},
+            {'id': 1, 'name': 'a'},
+            {'id': 3, 'name': 'c'},
+        ]
+
+        const expected = [
+            {'id': 3, 'name': 'c'},
+            {'id': 2, 'name': 'b'},
+            {'id': 1, 'name': 'a'},
+        ]
+
+        expect(ArrayUtils.sortObjectsByProp(input, 'id', true)).toEqual(expected)
+    })
+})
+

+ 2 - 30
tests/units/services/utils/dateUtils.test.ts

@@ -48,35 +48,7 @@ describe('formatDatesAndConcat', () => {
 })
 
 describe('sortDate', () => {
-    test('simple sort', () => {
-        const input = [
-            new Date(2023, 0, 12),
-            new Date(2023, 0, 10),
-            new Date(2023, 0, 11)
-        ]
-
-        const expected = [
-            new Date(2023, 0, 10),
-            new Date(2023, 0, 11),
-            new Date(2023, 0, 12)
-        ]
-
-        expect(DateUtils.sort(input)).toEqual(expected)
-    })
-
-    test('reverse sort', () => {
-        const input = [
-            new Date(2023, 0, 12),
-            new Date(2023, 0, 10),
-            new Date(2023, 0, 11)
-        ]
-
-        const expected = [
-            new Date(2023, 0, 12),
-            new Date(2023, 0, 11),
-            new Date(2023, 0, 10)
-        ]
-
-        expect(DateUtils.sort(input, true)).toEqual(expected)
+    test('sort', () => {
+        // TODO: should assert that ArrayUtils.sort is called
     })
 })