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
feat(useBase64): accept objects #1706
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
092667f
feat: added functionality for objects in useBase64
chaii3 85857db
chore: update
antfu 9159582
chore: type
antfu e60ea61
Merge branch 'main' into extend_base64_for_objects
antfu 50cdac9
Merge branch 'main' into extend_base64_for_objects
antfu File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { useBase64 } from '@vueuse/core' | ||
import { describe, expect } from 'vitest' | ||
|
||
function decode(encoded: string) { | ||
const decodedStr = Buffer.from(encoded.split(',')[1], 'base64').toString('utf-8') | ||
|
||
if (!decodedStr) | ||
return '' | ||
|
||
return JSON.parse(decodedStr) | ||
} | ||
|
||
describe('useBase64', () => { | ||
it('should work with record', async () => { | ||
const template = { test: 5 } | ||
|
||
const { promise, base64 } = useBase64(template) | ||
|
||
await promise.value | ||
|
||
expect(decode(base64.value)).toEqual(template) | ||
}) | ||
|
||
it('should work with map and default serialize function', async () => { | ||
const map = new Map([['test', 1]]) | ||
|
||
const { promise, base64 } = useBase64(map) | ||
|
||
await promise.value | ||
|
||
expect(decode(base64.value)).toEqual(Object.fromEntries(map)) | ||
}) | ||
|
||
it('should work with set', async () => { | ||
const set = new Set([1]) | ||
|
||
const { promise, base64 } = useBase64(set) | ||
|
||
await promise.value | ||
|
||
expect(decode(base64.value)).toEqual(Array.from(set)) | ||
}) | ||
|
||
it('should work with array', async () => { | ||
const arr = [1, 2, 3] | ||
|
||
const { promise, base64 } = useBase64(arr) | ||
|
||
await promise.value | ||
|
||
expect(decode(base64.value)).toEqual(arr) | ||
}) | ||
|
||
it('should work with custom serialize function', async () => { | ||
const arr = [1, 2, 3] | ||
|
||
const serializer = (arr: number[]) => { | ||
return JSON.stringify(arr.map(el => el * 2)) | ||
} | ||
|
||
const { promise, base64 } = useBase64(arr, { serializer }) | ||
|
||
await promise.value | ||
|
||
expect(decode(base64.value)).toEqual(JSON.parse(serializer(arr))) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const defaults = { | ||
array: (v: unknown[]) => JSON.stringify(v), | ||
object: (v: Record<string, unknown>) => JSON.stringify(v), | ||
set: (v: Set<unknown>) => JSON.stringify(Array.from(v)), | ||
map: (v: Map<string, unknown>) => JSON.stringify(Object.fromEntries(v)), | ||
null: () => '', | ||
} | ||
|
||
export function getDefaultSerialization<T extends Object>(target: T) { | ||
if (!target) | ||
return defaults.null | ||
|
||
if (target instanceof Map) | ||
return defaults.map | ||
else if (target instanceof Set) | ||
return defaults.set | ||
else if (Array.isArray(target)) | ||
return defaults.array | ||
else | ||
return defaults.object | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason that null is being excluded here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's more obvious if we check all cases for typeof _target === 'object' in one place. I check this one in getDefaultSerialization but I'm not really sure this decision.