|
1933 | 1933 | // Normalize fancy native splice handling of crazy start values |
1934 | 1934 | if (start < 0) { |
1935 | 1935 | start = array.length - Math.floor(-start); |
1936 | | - } else { |
| 1936 | + } else if (start) { |
1937 | 1937 | start = Math.floor(start); |
1938 | 1938 | } |
1939 | | - if (!start) { |
1940 | | - start = 0; |
| 1939 | + // array.splice does different things based on the number of arguments |
| 1940 | + // you pass in. Therefore, array.splice(0) and array.splice(0, undefined) |
| 1941 | + // do different things. In the former, the whole array is cleared. In the |
| 1942 | + // latter, no items are removed. |
| 1943 | + // This means that we need to detect whether 1. one of the arguments |
| 1944 | + // is actually passed in and then 2. determine how many arguments |
| 1945 | + // we should pass on to the native array.splice |
| 1946 | + // |
| 1947 | + let ret; |
| 1948 | + // Omit any additional arguments if they were not passed in |
| 1949 | + if (start !== undefined && !deleteCount && !items.length) { |
| 1950 | + ret = array.splice(start); |
| 1951 | + // Either start was undefined and the others were defined, but in this |
| 1952 | + // case we can safely pass on all arguments |
| 1953 | + // |
| 1954 | + // Note: this includes the case where none of the arguments were passed in, |
| 1955 | + // e.g. this.splice('array'). However, if both start and deleteCount |
| 1956 | + // are undefined, array.splice will not modify the array (as expected) |
| 1957 | + } else { |
| 1958 | + ret = array.splice(start, deleteCount, ...items); |
1941 | 1959 | } |
1942 | | - let ret = array.splice(start, deleteCount, ...items); |
| 1960 | + // At the end, check whether any items were passed in (e.g. insertions) |
| 1961 | + // or if the return array contains items (e.g. deletions). |
| 1962 | + // Only notify if items were added or deleted. |
1943 | 1963 | if (items.length || ret.length) { |
1944 | 1964 | notifySplice(this, array, info.path, start, items.length, ret); |
1945 | 1965 | } |
|
0 commit comments