Add a new method called pop()
Runtime
nodejs
Runtime version
20.10.0
Module version
11.0.1
Used with
hapi
Any other relevant information
No response
What problem are you trying to solve?
Currently, my colleagues and I are using your plugin on this project Pix, and we've noticed that the use of the method get() isn't clear, especially when someone unfamiliar with the plugin sees this line request.yar.get('key', true);.
It's not clear what the value true means. For that we need to go to the documentation page. It will be great to use something like pop('key').
We think that as JavaScript developers we will somehow see more frequently this method pop used with arrays.
Do you have a new or modified API suggestion to solve the problem?
This method pop() will have an argument which is the key and will removes the key if it is available in the store.
If the key does not exist, it will return null as value.
I have created a PR #167
Your proposal to introduce a pop() method as an alternative to get() sounds promising. Here's a brief outline of the proposed method and its functionality:
Proposed pop() Method:
/**
* Retrieves and removes the value associated with the specified key from the request yar store.
* @param {string} key - The key to retrieve and remove from the store.
* @returns {*} The value associated with the key if it exists; otherwise, null.
*/
request.yar.pop = function(key) {
// Logic to retrieve and remove the value associated with the key from the store
// Return the value if it exists, otherwise return null
};
Usage Example:
// Example of using the proposed pop() method
const value = request.yar.pop('key');
if (value !== null) {
// Handle the retrieved value
} else {
// Key does not exist in the store
}
Benefits:
-
Clarity: The
pop()method provides a clear indication of its functionality, similar to theArray.prototype.pop()method in JavaScript. -
Familiarity: Developers familiar with JavaScript arrays will find the
pop()method intuitive to use. - Conciseness: The method name succinctly conveys its purpose, reducing the need for additional documentation or explanation.
Next Steps:
-
Review and Testing: Ensure that the proposed
pop()method behaves as expected in different scenarios and edge cases. -
Documentation Update: Update the project documentation to reflect the addition of the
pop()method and provide usage examples for developers. - Integration: Integrate the changes into the Pix project, ensuring compatibility with existing code and workflows.
By incorporating the pop() method, developers can benefit from improved clarity and ease of use when interacting with the request yar store in the Pix project. If you encounter any issues or have further suggestions, feel free to iterate on the proposal and collaborate with the project team for enhancements.