Skip to content

Commit

Permalink
fix(array): fix array handler
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Jan 12, 2025
1 parent a19b152 commit 6103b6a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const draftsCache = new WeakSet<object>();

let arrayHandling = false;

const proxyArrayMethods = ['splice', 'shift', 'unshift'];
const proxyArrayMethods = ['splice', 'shift', 'unshift', 'reverse', 'sort'];

const proxyHandler: ProxyHandler<ProxyDraft> = {
get(target: ProxyDraft, key: string | number | symbol, receiver: any) {
Expand Down Expand Up @@ -116,10 +116,7 @@ const proxyHandler: ProxyHandler<ProxyDraft> = {
arrayHandling = true;
try {
returnValue = value.apply(this, args);
if (key === 'splice' && returnValue.length > 0) {
returnValue = returnValue.map(handleItem);
}
if (key === 'shift' && typeof returnValue === 'object') {
if (typeof returnValue === 'object') {
returnValue = handleItem(returnValue);
}
return returnValue;
Expand Down
18 changes: 18 additions & 0 deletions test/__snapshots__/apply.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4635,6 +4635,15 @@ exports[`array - update 22`] = `

exports[`array - update 23`] = `
[
{
"op": "replace",
"path": [
"a",
2,
"i",
],
"value": 18,
},
{
"op": "replace",
"path": [
Expand Down Expand Up @@ -4840,6 +4849,15 @@ exports[`array - update 23`] = `

exports[`array - update 24`] = `
[
{
"op": "replace",
"path": [
"a",
2,
"i",
],
"value": 17,
},
{
"op": "replace",
"path": [
Expand Down

0 comments on commit 6103b6a

Please sign in to comment.