Skip to content

Commit

Permalink
Do not copy Function#length (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored and sindresorhus committed Jun 11, 2019
1 parent dbda8fe commit c229a73
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
declare const mimicFn: {
/**
Make a function mimic another one. It will copy over the properties `name`, `length`, `displayName`, and any custom properties you may have set.
Make a function mimic another one. It will copy over the properties `name`, `displayName`, and any custom properties you may have set. The `length` property won't be copied.
@param to - Mimicking function.
@param from - Function to mimic.
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use strict';

const shouldCopyProperty = property => property !== 'length';

const mimicFn = (to, from) => {
for (const prop of Reflect.ownKeys(from)) {
const props = Reflect.ownKeys(from).filter(shouldCopyProperty);

for (const prop of props) {
Object.defineProperty(to, prop, Object.getOwnPropertyDescriptor(from, prop));
}

Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ console.log(wrapper.unicorn);

## API

It will copy over the properties `name`, `length`, `displayName`, and any custom properties you may have set.
It will copy over the properties `name`, `displayName`, and any custom properties you may have set. The `length` property won't be copied.

### mimicFn(to, from)

Expand All @@ -61,7 +61,7 @@ Function to mimic.
## Related

- [rename-fn](https://github.com/sindresorhus/rename-fn) - Rename a function
- [keep-func-props](https://github.com/ehmicky/keep-func-props) - Wrap a function without changing its name, length and other properties
- [keep-func-props](https://github.com/ehmicky/keep-func-props) - Wrap a function without changing its name and other properties


---
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('main', t => {
t.is(mimickFn(wrapper, foo), wrapper);

t.is(wrapper.name, 'foo');
t.is(wrapper.length, 1);
t.is(wrapper.length, 0);
t.is(wrapper.unicorn, '🦄');
t.is(wrapper[symbol], '✨');
});

0 comments on commit c229a73

Please sign in to comment.