-
Notifications
You must be signed in to change notification settings - Fork 80
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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`). | ||
|
@@ -33,6 +40,10 @@ export default function <ResultFn: (...Array<any>) => mixed>(resultFn: ResultFn, | |
return lastResult; | ||
}; | ||
|
||
defineProperty(result, 'length', resultFn.length); | ||
|
||
defineProperty(result, 'name', `memoized_${resultFn.name || 'one'}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this equal 0? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, the final form of this function operates on |
||
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 | ||
|
There was a problem hiding this comment.
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!