Skip to content

Commit ec5db59

Browse files
committed
Add es2023 array methods to observablearray
1 parent defba49 commit ec5db59

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

.changeset/great-trainers-beg.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx": minor
3+
---
4+
5+
Added es2023 array methods support to observablearray.

packages/mobx/__tests__/v4/base/array.js

+50
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,34 @@ test("find(findIndex) and remove", function () {
144144
expect(a.remove(20)).toBe(false)
145145
})
146146

147+
test("findLast(findLastIndex) and remove", function () {
148+
const a = mobx.observable([10, 20, 20])
149+
let idx = -1
150+
function predicate(item, index) {
151+
if (item === 20) {
152+
idx = index
153+
return true
154+
}
155+
return false
156+
}
157+
;[].findLastIndex;
158+
expect(a.findLast(predicate)).toBe(20)
159+
expect(a.findLastIndex(predicate)).toBe(2)
160+
expect(a.findLast(predicate)).toBe(20)
161+
162+
expect(a.remove(20)).toBe(true)
163+
expect(a.find(predicate)).toBe(20)
164+
expect(idx).toBe(1)
165+
expect(a.findIndex(predicate)).toBe(1)
166+
idx = -1
167+
expect(a.remove(20)).toBe(true)
168+
expect(a.findLast(predicate)).toBe(undefined)
169+
expect(idx).toBe(-1)
170+
expect(a.findLastIndex(predicate)).toBe(-1)
171+
172+
expect(a.remove(20)).toBe(false)
173+
})
174+
147175
test("concat should automatically slice observable arrays, #260", () => {
148176
const a1 = mobx.observable([1, 2])
149177
const a2 = mobx.observable([3, 4])
@@ -577,6 +605,8 @@ test("correct array should be passed to callbacks #2326", () => {
577605
"filter",
578606
"find",
579607
"findIndex",
608+
"findLast",
609+
"findLastIndex",
580610
"flatMap",
581611
"forEach",
582612
"map",
@@ -756,6 +786,26 @@ describe("dehances", () => {
756786
expect([...array.values()]).toEqual([...dehanced.values()])
757787
})
758788

789+
test("toReversed", () => {
790+
expect(array.toReversed()).toEqual(dehanced.toReversed())
791+
})
792+
793+
test("toSorted", () => {
794+
expect(array.toSorted()).toEqual(dehanced.toSorted())
795+
})
796+
797+
test("toSorted with args", () => {
798+
expect(array.toSorted((a, b) => a - b)).toEqual(dehanced.toSorted((a, b) => a - b))
799+
})
800+
801+
test("toSpliced", () => {
802+
expect(array.toSpliced(1, 2)).toEqual(dehanced.toSpliced(1, 2))
803+
})
804+
805+
test("with", () => {
806+
expect(array.with(1, 5)).toEqual(dehanced.with(1, 5))
807+
})
808+
759809
test("flat/flatMap", () => {
760810
// not supported in V4
761811
})

packages/mobx/__tests__/v5/base/array.js

+50
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,34 @@ test("find(findIndex) and remove", function () {
178178
expect(a.remove(20)).toBe(false)
179179
})
180180

181+
test("findLast(findLastIndex) and remove", function () {
182+
const a = mobx.observable([10, 20, 20])
183+
let idx = -1
184+
function predicate(item, index) {
185+
if (item === 20) {
186+
idx = index
187+
return true
188+
}
189+
return false
190+
}
191+
;[].findLastIndex;
192+
expect(a.findLast(predicate)).toBe(20)
193+
expect(a.findLastIndex(predicate)).toBe(2)
194+
expect(a.findLast(predicate)).toBe(20)
195+
196+
expect(a.remove(20)).toBe(true)
197+
expect(a.find(predicate)).toBe(20)
198+
expect(idx).toBe(1)
199+
expect(a.findIndex(predicate)).toBe(1)
200+
idx = -1
201+
expect(a.remove(20)).toBe(true)
202+
expect(a.findLast(predicate)).toBe(undefined)
203+
expect(idx).toBe(-1)
204+
expect(a.findLastIndex(predicate)).toBe(-1)
205+
206+
expect(a.remove(20)).toBe(false)
207+
})
208+
181209
test("concat should automatically slice observable arrays, #260", () => {
182210
const a1 = mobx.observable([1, 2])
183211
const a2 = mobx.observable([3, 4])
@@ -630,6 +658,8 @@ test("correct array should be passed to callbacks #2326", () => {
630658
"filter",
631659
"find",
632660
"findIndex",
661+
"findLast",
662+
"findLastIndex",
633663
"flatMap",
634664
"forEach",
635665
"map",
@@ -807,6 +837,26 @@ describe("dehances", () => {
807837
expect([...array.values()]).toEqual([...dehanced.values()])
808838
})
809839

840+
test("toReversed", () => {
841+
expect(array.toReversed()).toEqual(dehanced.toReversed())
842+
})
843+
844+
test("toSorted", () => {
845+
expect(array.toSorted()).toEqual(dehanced.toSorted())
846+
})
847+
848+
test("toSorted with args", () => {
849+
expect(array.toSorted((a, b) => a - b)).toEqual(dehanced.toSorted((a, b) => a - b))
850+
})
851+
852+
test("toSpliced", () => {
853+
expect(array.toSpliced(1, 2)).toEqual(dehanced.toSpliced(1, 2))
854+
})
855+
856+
test("with", () => {
857+
expect(array.with(1, 5)).toEqual(dehanced.with(1, 5))
858+
})
859+
810860
test("flat/flatMap", () => {
811861
const nestedArray = [{ value: 1 }, [{ value: 2 }, [{ value: 3 }]]]
812862
const dehancedNestedArray = nestedArray.map(dehancer)

packages/mobx/src/types/observablearray.ts

+6
Original file line numberDiff line numberDiff line change
@@ -527,15 +527,21 @@ addArrayExtension("lastIndexOf", simpleFunc)
527527
addArrayExtension("slice", simpleFunc)
528528
addArrayExtension("toString", simpleFunc)
529529
addArrayExtension("toLocaleString", simpleFunc)
530+
addArrayExtension("toSorted", simpleFunc)
531+
addArrayExtension("toSpliced", simpleFunc)
532+
addArrayExtension("with", simpleFunc)
530533
// map
531534
addArrayExtension("every", mapLikeFunc)
532535
addArrayExtension("filter", mapLikeFunc)
533536
addArrayExtension("find", mapLikeFunc)
534537
addArrayExtension("findIndex", mapLikeFunc)
538+
addArrayExtension("findLast", mapLikeFunc)
539+
addArrayExtension("findLastIndex", mapLikeFunc)
535540
addArrayExtension("flatMap", mapLikeFunc)
536541
addArrayExtension("forEach", mapLikeFunc)
537542
addArrayExtension("map", mapLikeFunc)
538543
addArrayExtension("some", mapLikeFunc)
544+
addArrayExtension("toReversed", mapLikeFunc)
539545
// reduce
540546
addArrayExtension("reduce", reduceLikeFunc)
541547
addArrayExtension("reduceRight", reduceLikeFunc)

0 commit comments

Comments
 (0)