unstorage icon indicating copy to clipboard operation
unstorage copied to clipboard

getItem will return `null` for unexisting keys, instead of `undefined`

Open ur92 opened this issue 1 year ago • 5 comments

Environment

unstorage 1.5.0 nodejs latest

Reproduction

https://stackblitz.com/edit/node-besgzq?file=index.js

you can see that getItem for a key that doesn't exist will resolve to null but should be resolved to undefined

then I initialized new Map and .get returned undefined as expected

Describe the bug

.getItem should return undefined if the key doesn't exist, but it returning null

if many drivers implementation there is an OR condition, like return data.get(key) || null. this is wrong IMO and should return undefined and not null. Also the documentation states clearly that undefined should be returned.

Null might be a misleading return as it might be considered as intentional value and not a key miss.

Additional context

No response

Logs

No response

ur92 avatar Apr 20 '23 10:04 ur92

This is expected behavior, see: https://github.com/unjs/unstorage/blob/d6e0da378bc1acc0b51f4801975f0ee20143b514/test/storage.test.ts#L120-L122

peterroe avatar Apr 20 '23 14:04 peterroe

🤔But I also have the same question, why is it null instead of undefined @pi0

peterroe avatar Apr 20 '23 14:04 peterroe

If we decide that null is intentional so probably it's a good idea to align all cache types to return null on key miss. Also documentation should be updated. Thanks

ur92 avatar Apr 20 '23 22:04 ur92

I think undefined would be better idea only this is a breaking change.

pi0 avatar Apr 21 '23 09:04 pi0

If we follow semver strictly we should consider major. Anyway I will fine tune the PR according your comments and we will discuss further. Thanks

ur92 avatar Apr 21 '23 20:04 ur92