Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions __tests__/array-methods-slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {Immer, enableArrayMethods} from "../src/immer"
import {clearPlugin, PluginArrayMethods} from "../src/internal"

for (const useArrayMethods of [false, true]) {
for (const autoFreeze of [false, true]) {
describe(`array methods: plugin=${useArrayMethods}, autoFreeze=${autoFreeze}`, () => {
const {produce} = new Immer({autoFreeze})
beforeEach(() => {
if (useArrayMethods) enableArrayMethods()
else clearPlugin(PluginArrayMethods)
})
afterEach(() => clearPlugin(PluginArrayMethods))
test("distinguishes null end from omitted or undefined end", () => {
const base = {items: [{id: 1}, {id: 2}, {id: 3}]}
const result = produce(base, draft => {
for (const args of [
[0, null],
[1, null],
[-2, null],
[null, null],
[0],
[0, undefined]
]) {
expect(draft.items.slice(...args).map(item => item.id)).toEqual(
base.items.slice(...args).map(item => item.id)
)
}
})
expect(result).toBe(base)
})
})
}
}
2 changes: 1 addition & 1 deletion src/plugins/arrayMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ export function enableArrayMethods() {

if (method === "slice") {
const rawStart = args[0] ?? 0
const rawEnd = args[1] ?? source.length
const rawEnd = args[1] === undefined ? source.length : args[1]

// Normalize negative indices
const start = normalizeSliceIndex(rawStart, source.length)
Expand Down