-
Notifications
You must be signed in to change notification settings - Fork 346
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
chore(common): apply decorators function #3999
base: next
Are you sure you want to change the base?
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* This applies decorators to an ampersand state object | ||
* It wraps the function in the order the decorators are provided | ||
* This means that when you call the method, the decorators are applied in reverse order | ||
* @param {*} object - The object to apply the decorators to | ||
* @param {*} decoratedMethods | ||
* @returns {undefined} | ||
*/ | ||
export function applyDecorators(object, decoratedMethods) { | ||
Object.entries(decoratedMethods).forEach(([method, decorators]) => { | ||
object[method] = decorators.reduce( | ||
(decorated, decorator) => decorator(decorated), | ||
object[method] | ||
); | ||
}); | ||
} | ||
|
||
export default { | ||
applyDecorators, | ||
}; | ||
Comment on lines
+18
to
+20
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. 💡 Codebase verification Consider removing default export since only named export is used The codebase exclusively uses the named import
🔗 Analysis chainVerify import usage consistency across the codebase. Having both named and default exports is fine, but let's ensure they're used consistently. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Check how this module is imported across the codebase
rg -t js "import.*from.*apply-decorators"
Length of output: 169 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {assert} from '@webex/test-helper-chai'; | ||
import sinon from 'sinon'; | ||
import {applyDecorators} from '@webex/common/src/apply-decorators'; | ||
|
||
describe('applyDecorators()', () => { | ||
it('applies the decorators in order', () => { | ||
Comment on lines
+5
to
+6
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. 🛠️ Refactor suggestion Consider adding more test cases The test suite currently only verifies the order of decorator application. Given that this PR addresses VSCode issues with ampersand state objects, consider adding test cases for:
Would you like me to help generate these additional test cases? |
||
const method = sinon.stub(); | ||
|
||
const obj = { | ||
method | ||
} | ||
|
||
const decorator1 = (fn) => { | ||
return function decorated1() { | ||
fn.apply(this, [...arguments, 'decorated1']); | ||
}; | ||
} | ||
|
||
const decorator2 = (fn) => { | ||
return function decorated2() { | ||
fn.apply(this, [...arguments, 'decorated2']); | ||
}; | ||
} | ||
|
||
applyDecorators(obj, { | ||
method: [decorator1, decorator2] | ||
}); | ||
|
||
obj.method('arg'); | ||
|
||
assert.calledOnce(method); | ||
|
||
assert.calledWithExactly(method, 'arg', 'decorated2', 'decorated1'); | ||
}); | ||
}); |
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.
Add input validation to prevent runtime errors.
The function should validate its inputs to ensure robust operation and provide clear error messages.
📝 Committable suggestion