unstorage
unstorage copied to clipboard
getItem will return `null` for unexisting keys, instead of `undefined`
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
This is expected behavior, see: https://github.com/unjs/unstorage/blob/d6e0da378bc1acc0b51f4801975f0ee20143b514/test/storage.test.ts#L120-L122
🤔But I also have the same question, why is it null
instead of undefined
@pi0
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
I think undefined
would be better idea only this is a breaking change.
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