From ec5db592d7756826c31e710b1c759d7e9406b153 Mon Sep 17 00:00:00 2001 From: Antoine Raoul Date: Mon, 9 Oct 2023 21:50:41 +1100 Subject: [PATCH] Add es2023 array methods to observablearray --- .changeset/great-trainers-beg.md | 5 +++ packages/mobx/__tests__/v4/base/array.js | 50 ++++++++++++++++++++++ packages/mobx/__tests__/v5/base/array.js | 50 ++++++++++++++++++++++ packages/mobx/src/types/observablearray.ts | 6 +++ 4 files changed, 111 insertions(+) create mode 100644 .changeset/great-trainers-beg.md diff --git a/.changeset/great-trainers-beg.md b/.changeset/great-trainers-beg.md new file mode 100644 index 000000000..0dd18d20e --- /dev/null +++ b/.changeset/great-trainers-beg.md @@ -0,0 +1,5 @@ +--- +"mobx": minor +--- + +Added es2023 array methods support to observablearray. diff --git a/packages/mobx/__tests__/v4/base/array.js b/packages/mobx/__tests__/v4/base/array.js index 42d60683e..3acf53294 100644 --- a/packages/mobx/__tests__/v4/base/array.js +++ b/packages/mobx/__tests__/v4/base/array.js @@ -144,6 +144,34 @@ test("find(findIndex) and remove", function () { expect(a.remove(20)).toBe(false) }) +test("findLast(findLastIndex) and remove", function () { + const a = mobx.observable([10, 20, 20]) + let idx = -1 + function predicate(item, index) { + if (item === 20) { + idx = index + return true + } + return false + } + ;[].findLastIndex; + expect(a.findLast(predicate)).toBe(20) + expect(a.findLastIndex(predicate)).toBe(2) + expect(a.findLast(predicate)).toBe(20) + + expect(a.remove(20)).toBe(true) + expect(a.find(predicate)).toBe(20) + expect(idx).toBe(1) + expect(a.findIndex(predicate)).toBe(1) + idx = -1 + expect(a.remove(20)).toBe(true) + expect(a.findLast(predicate)).toBe(undefined) + expect(idx).toBe(-1) + expect(a.findLastIndex(predicate)).toBe(-1) + + expect(a.remove(20)).toBe(false) +}) + test("concat should automatically slice observable arrays, #260", () => { const a1 = mobx.observable([1, 2]) const a2 = mobx.observable([3, 4]) @@ -577,6 +605,8 @@ test("correct array should be passed to callbacks #2326", () => { "filter", "find", "findIndex", + "findLast", + "findLastIndex", "flatMap", "forEach", "map", @@ -756,6 +786,26 @@ describe("dehances", () => { expect([...array.values()]).toEqual([...dehanced.values()]) }) + test("toReversed", () => { + expect(array.toReversed()).toEqual(dehanced.toReversed()) + }) + + test("toSorted", () => { + expect(array.toSorted()).toEqual(dehanced.toSorted()) + }) + + test("toSorted with args", () => { + expect(array.toSorted((a, b) => a - b)).toEqual(dehanced.toSorted((a, b) => a - b)) + }) + + test("toSpliced", () => { + expect(array.toSpliced(1, 2)).toEqual(dehanced.toSpliced(1, 2)) + }) + + test("with", () => { + expect(array.with(1, 5)).toEqual(dehanced.with(1, 5)) + }) + test("flat/flatMap", () => { // not supported in V4 }) diff --git a/packages/mobx/__tests__/v5/base/array.js b/packages/mobx/__tests__/v5/base/array.js index 8b9cc576c..e083ac878 100644 --- a/packages/mobx/__tests__/v5/base/array.js +++ b/packages/mobx/__tests__/v5/base/array.js @@ -178,6 +178,34 @@ test("find(findIndex) and remove", function () { expect(a.remove(20)).toBe(false) }) +test("findLast(findLastIndex) and remove", function () { + const a = mobx.observable([10, 20, 20]) + let idx = -1 + function predicate(item, index) { + if (item === 20) { + idx = index + return true + } + return false + } + ;[].findLastIndex; + expect(a.findLast(predicate)).toBe(20) + expect(a.findLastIndex(predicate)).toBe(2) + expect(a.findLast(predicate)).toBe(20) + + expect(a.remove(20)).toBe(true) + expect(a.find(predicate)).toBe(20) + expect(idx).toBe(1) + expect(a.findIndex(predicate)).toBe(1) + idx = -1 + expect(a.remove(20)).toBe(true) + expect(a.findLast(predicate)).toBe(undefined) + expect(idx).toBe(-1) + expect(a.findLastIndex(predicate)).toBe(-1) + + expect(a.remove(20)).toBe(false) +}) + test("concat should automatically slice observable arrays, #260", () => { const a1 = mobx.observable([1, 2]) const a2 = mobx.observable([3, 4]) @@ -630,6 +658,8 @@ test("correct array should be passed to callbacks #2326", () => { "filter", "find", "findIndex", + "findLast", + "findLastIndex", "flatMap", "forEach", "map", @@ -807,6 +837,26 @@ describe("dehances", () => { expect([...array.values()]).toEqual([...dehanced.values()]) }) + test("toReversed", () => { + expect(array.toReversed()).toEqual(dehanced.toReversed()) + }) + + test("toSorted", () => { + expect(array.toSorted()).toEqual(dehanced.toSorted()) + }) + + test("toSorted with args", () => { + expect(array.toSorted((a, b) => a - b)).toEqual(dehanced.toSorted((a, b) => a - b)) + }) + + test("toSpliced", () => { + expect(array.toSpliced(1, 2)).toEqual(dehanced.toSpliced(1, 2)) + }) + + test("with", () => { + expect(array.with(1, 5)).toEqual(dehanced.with(1, 5)) + }) + test("flat/flatMap", () => { const nestedArray = [{ value: 1 }, [{ value: 2 }, [{ value: 3 }]]] const dehancedNestedArray = nestedArray.map(dehancer) diff --git a/packages/mobx/src/types/observablearray.ts b/packages/mobx/src/types/observablearray.ts index 6c3859b3f..a16cdc039 100644 --- a/packages/mobx/src/types/observablearray.ts +++ b/packages/mobx/src/types/observablearray.ts @@ -527,15 +527,21 @@ addArrayExtension("lastIndexOf", simpleFunc) addArrayExtension("slice", simpleFunc) addArrayExtension("toString", simpleFunc) addArrayExtension("toLocaleString", simpleFunc) +addArrayExtension("toSorted", simpleFunc) +addArrayExtension("toSpliced", simpleFunc) +addArrayExtension("with", simpleFunc) // map addArrayExtension("every", mapLikeFunc) addArrayExtension("filter", mapLikeFunc) addArrayExtension("find", mapLikeFunc) addArrayExtension("findIndex", mapLikeFunc) +addArrayExtension("findLast", mapLikeFunc) +addArrayExtension("findLastIndex", mapLikeFunc) addArrayExtension("flatMap", mapLikeFunc) addArrayExtension("forEach", mapLikeFunc) addArrayExtension("map", mapLikeFunc) addArrayExtension("some", mapLikeFunc) +addArrayExtension("toReversed", mapLikeFunc) // reduce addArrayExtension("reduce", reduceLikeFunc) addArrayExtension("reduceRight", reduceLikeFunc)