Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
9 changes: 8 additions & 1 deletion src/utils/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function strictCopy(target: any) {
return Object.create(Object.getPrototypeOf(target), descriptors);
}

const propIsEnum = Object.prototype.propertyIsEnumerable;

export function shallowCopy(original: any, options?: Options<any, any>) {
let markResult: any;
if (Array.isArray(original)) {
Expand Down Expand Up @@ -55,7 +57,12 @@ export function shallowCopy(original: any, options?: Options<any, any>) {
// don't use `Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));` by default.
const copy: Record<string | symbol, any> = {};
Object.keys(original).forEach((key) => {
copy![key] = original[key];
copy[key] = original[key];
});
Object.getOwnPropertySymbols(original).forEach((key) => {
if (propIsEnum.call(original, key)) {
copy[key] = original[key];
}
});
return copy;
} else {
Expand Down
6 changes: 2 additions & 4 deletions test/immer/__tests__/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,13 +909,11 @@ function runBaseTest(name, autoFreeze, useStrictShallowCopy, useListener) {
const test = Symbol('test');
const baseState = { [test]: true };
const nextState = produce(baseState, (s) => {
// !!! This is different from immer
// expect(s[test]).toBeTruthy();
expect(s[test]).toBeTruthy();
s.foo = true;
});
// !!! This is different from immer
expect(nextState).toEqual({
// [test]: true,
[test]: true,
foo: true,
});
});
Expand Down