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

Respect function argument count #18

Merged
merged 2 commits into from
Mar 21, 2018
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
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ type EqualityFn = (a: mixed, b: mixed) => boolean;

const simpleIsEqual: EqualityFn = (a: mixed, b: mixed): boolean => a === b;

const defineProperty = (target: Object, property: string, value: mixed) =>
Object.defineProperty(target, property, {
writable: false,
configurable: true,
value: value,
});

// <ResultFn: (...Array<any>) => mixed>
// The purpose of this typing is to ensure that the returned memoized
// function has the same type as the provided function (`resultFn`).
Expand Down Expand Up @@ -33,6 +40,10 @@ export default function <ResultFn: (...Array<any>) => mixed>(resultFn: ResultFn,
return lastResult;
};

defineProperty(result, 'length', resultFn.length);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but we should totally ship the length property!


defineProperty(result, 'name', `memoized_${resultFn.name || 'one'}`);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a little worried that this might slow things done a bit. I am not sure if the effort is worth the sugar. Would you mind taking a look at the impact?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to make this getter. Then it shall not have any impact.
Anyway - it is easy to test.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could make them both getters! As you said - worth testing!

Thanks @theKashey!


// telling flow to ignore the type of `result` as we know it is `ResultFn`
return (result: any);
}
18 changes: 18 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,24 @@ describe('memoizeOne', () => {
});
});

describe('js typing', () => {
it('should maintain function arguments count', () => {
expect(memoizeOne(a => a).length).to.equal(1);
expect(memoizeOne((a, b) => a + b).length).to.equal(2);
expect(memoizeOne((...rest) => rest).length).to.equal(0);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this equal 0?
and should the next one equal 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the final form of this function operates on arguments, not leaving anything in the function signature.
That's the problem.

expect(memoizeOne((a, ...rest) => a + rest).length).to.equal(1);
});

it('should maintain function name', () => {
const fn = a => a;
expect(memoizeOne(fn).name).to.equal('memoized_fn');
expect(memoizeOne(a => a).name).to.equal('memoized_one');
expect(memoizeOne(function test(a) {
return a;
}).name).to.equal('memoized_test');
});
});

describe('flow typing', () => {
it('should maintain the type of the original function', () => {
// this test will create a flow error if the typing is incorrect
Expand Down