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

Value as string converted into number #277

Open
olivbau opened this issue Jul 24, 2023 · 3 comments · May be fixed by #505
Open

Value as string converted into number #277

olivbau opened this issue Jul 24, 2023 · 3 comments · May be fixed by #505
Labels
breaking bug Something isn't working discussion

Comments

@olivbau
Copy link

olivbau commented Jul 24, 2023

Environment

nodejs
v1.8.0

Reproduction

const storage = createStorage();
await storage.setItem('123', '456')
const item = await storage.getItem('123');
console.log(item, typeof item)
// 456, number
// instead of: "456", string

Describe the bug

Storage convert string into number for no reason

EDIT:After investigation, it's probably due to destr
https://github.com/unjs/unstorage/blob/main/src/storage.ts#L173C9-L173C21

Additional context

No response

Logs

No response

@pi0
Copy link
Member

pi0 commented Jul 24, 2023

Hi. Yes this is the default behavior as we try to keep native JSON types. I think we might support an option to opt-out from this feature but in the meantime you can use setItem(key, '"123")

@pi0 pi0 added bug Something isn't working breaking labels May 1, 2024
@molvqingtai
Copy link

molvqingtai commented Nov 5, 2024

return String(value);

The error is caused by the String(value) method in stringify. Should the behavior of stringify be consistent with JSON.stringify?

String():

String('1') // '1'
String(true) // 'true'
String(null) // 'null'
String('[]') // '[]'

JSON.stringify & JSON.parse :

JSON.stringify('1') // '"1"'
JSON.stringify(true) // 'true'
JSON.stringify(null) // 'null'
JSON.stringify('[]') // '"[]"'


JSON.parse(JSON.stringify('1')) //  '1'
JSON.parse(JSON.stringify(true)) // true
JSON.parse(JSON.stringify(null)) // null
JSON.parse(JSON.stringify('[]')) // '[]'

idb-keyval:

import { set, get } from 'idb-keyval'

await set('foo', '[]')
await get('foo') // '[]'

await set('bar', null)
await get('bar') // null

Inconsistent behavior of String() and JSON.stringify() will lead to confusion, which makes it impossible to migrate directly from idb-keyval to unstorage

I use jsonr to store the compressed json into storage. Because the value obtained by getItem is not the value originally stored, an error is reported.

@pi0

@molvqingtai
Copy link

I tried using JSON.stringify to handle the string value, but this broke too many test cases

@molvqingtai molvqingtai linked a pull request Nov 5, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking bug Something isn't working discussion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants