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

FEATURE: Add pop(key) method #167

Closed
wants to merge 1 commit into from
Closed

FEATURE: Add pop(key) method #167

wants to merge 1 commit into from

Conversation

igorissen
Copy link

🦄 Context

When we first used this plugin, we wondered if there was a way to retrieve and delete a value. We found in the documentation that the get method accepts an additional parameter, which is the delete option after retrieval.

For new developers on the project or people who will be reviewing the code, a recurring question we've noticed is what the second parameter of the get method does?

It could be great to have a method with more clarity like Array.prototype.pop() to retrieve and delete a value.

🤖 Proposal

Add a new method with the name pop which retrieves and removes the value associated with the specified key from the request yar store.

🌈 Notes

N/A

💯 Tests

  1. Run the commands npm test and/or npm run test-cov-html
  2. Check tests are all ✅

@kanongil
Copy link

kanongil commented Feb 6, 2024

Thanks for the PR.

While I agree that the clear option for get() has understanding issues when reading the code, your fix is not the correct solution (supposing it is a big enough issue to warrant a fix).

pop() is normally used on arrays (with no argument), and using it for a key-value store adds confusion. Additionally, you would expect to have the counterpart push() available.

If we want to fix this, the simple solution is probably to change it to an option argument with a clear parameter. Used like:

request.yar.get('key', { clear: true });

The even simpler solution, is to remove the option all together, requiring users to explicitly call clear(). The option is a pure developer convenience:

request.yar.get('key');
request.yar.clear('key');

I'm probably in favour of this approach.

While we are at it, we might want to rename clear (both as an option, and the method) to delete, to better match the Map semantics, where clear() removes all elements.

@igorissen
Copy link
Author

The even simpler solution, is to remove the option together, requiring users to call clear explicitly ().

While we are at it, we might want to rename clear (both as an option and the method) to delete, to better match the Map semantics, where clear() removes all elements.

I'm also in favour of the removal of the options parameter. To have a simple function with one goal will be simple to understand and even simpler to test. The developers could create themselves a function which calls the get and delete.

@kanongil do I need to create a new feature request issue or update my current feature request issue? or should I do the modification and create a PR?

@igorissen igorissen closed this Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants