Skip to content

Commit

Permalink
Add es2023 array methods to observablearray
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyraoul committed Nov 2, 2023
1 parent defba49 commit ec5db59
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-trainers-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx": minor
---

Added es2023 array methods support to observablearray.
50 changes: 50 additions & 0 deletions packages/mobx/__tests__/v4/base/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -577,6 +605,8 @@ test("correct array should be passed to callbacks #2326", () => {
"filter",
"find",
"findIndex",
"findLast",
"findLastIndex",
"flatMap",
"forEach",
"map",
Expand Down Expand Up @@ -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
})
Expand Down
50 changes: 50 additions & 0 deletions packages/mobx/__tests__/v5/base/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -630,6 +658,8 @@ test("correct array should be passed to callbacks #2326", () => {
"filter",
"find",
"findIndex",
"findLast",
"findLastIndex",
"flatMap",
"forEach",
"map",
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions packages/mobx/src/types/observablearray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ec5db59

Please sign in to comment.