Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: copy function in instance #266

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ lib-cov
# Coverage directory used by tools like istanbul
coverage

# idea
.idea

# nyc test coverage
.nyc_output

Expand Down
8 changes: 7 additions & 1 deletion src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ export function getKeys(
return ([] as Array<string | symbol>).concat(
// NOTE: Object.getOwnPropertyNames can get all own keys.
Object.keys(collection as Record<string, unknown>),
Object.getOwnPropertySymbols(collection as Record<symbol, unknown>)
Object.getOwnPropertySymbols(collection as Record<symbol, unknown>),
(collection as any).__proto__.constructor ===
Object.prototype.constructor
? []
: Object.getOwnPropertyNames(
Object.getPrototypeOf(collection)
).filter((key) => key !== 'constructor')
);
case typeMap:
case typeSet:
Expand Down
18 changes: 18 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,24 @@ describe('deepcopy', function () {
assert((result.get(3) || {}).b === 8);
assert((result.get(3) || {}).c === 9);
});

it('function in class', function () {
class Test {
fn() {
return this;
}
}

const origin = { inside: new Test() };
const result = deepcopy(origin);
assert(typeof result.inside.fn === 'function');
});

it('function in Object', function () {
const origin = { fn: () => console.log('function') };
const result = deepcopy(origin);
assert(typeof result.fn === 'function');
});
});
});

Expand Down