-
-
Notifications
You must be signed in to change notification settings - Fork 22
Add ssp for base64 string encoding #117
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import type { EncodeAndDecodeOptions } from '$lib/types'; | ||
|
|
||
| /** | ||
| * Encodes and decodes a Unicode string as utf-8 Base64 URL safe. | ||
| * | ||
| * See: https://developer.mozilla.org/en-US/docs/Glossary/Base64 | ||
| */ | ||
| function sspBase64( | ||
| defaultValue: string, | ||
| ): EncodeAndDecodeOptions<string> & { defaultValue: string }; | ||
| function sspBase64(): EncodeAndDecodeOptions<string> & { | ||
| defaultValue: undefined; | ||
| }; | ||
| function sspBase64(defaultValue?: string): EncodeAndDecodeOptions<string> { | ||
| return { | ||
| encode: (value: string) => { | ||
| if (value === '') return undefined; | ||
| const bytes = new TextEncoder().encode(value); | ||
| const binString = Array.from(bytes, (byte) => | ||
| String.fromCodePoint(byte), | ||
| ).join(''); | ||
| return btoa(binString) | ||
| .replace(/\+/g, '-') | ||
| .replace(/\//g, '_') | ||
| .replace(/=/g, ''); | ||
| }, | ||
| decode: (value: string | null) => { | ||
| if (value === null) return ''; | ||
| const binString = atob(value.replace(/-/g, '+').replace(/_/g, '/')); | ||
| const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0)!); | ||
| return new TextDecoder().decode(bytes); | ||
| }, | ||
| defaultValue, | ||
| }; | ||
|
Comment on lines
+27
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider optimizing the decode method for readability and performance. The decode method correctly reverses the encoding process, including handling of null input and URL-safe character replacements. However, similar to the encode method, it could be optimized for better readability and potentially improved performance. Consider the following optimizations:
Here's a suggested implementation: decode: (value: string | null) => {
if (value === null) return '';
return decodeURIComponent(
atob(value.replace(/-/g, '+').replace(/_/g, '/'))
.split('')
.map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2))
.join('')
);
},This version is more concise and potentially more efficient, while maintaining the same functionality. It also handles potential decoding errors more gracefully by using |
||
| } | ||
|
|
||
| export { sspBase64 }; | ||
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.
🛠️ Refactor suggestion
Consider optimizing the encode method for readability and performance.
The encode method correctly handles UTF-8 encoding and produces a URL-safe Base64 string. However, it could be optimized for better readability and potentially improved performance.
Consider the following optimizations:
encodeURIComponentto handle UTF-8 encoding, which is simpler and potentially faster than using TextEncoder.btoadirectly on the encoded string, eliminating the need for the intermediate binary string conversion.replacecall with a regular expression for URL-safe character replacements.Here's a suggested implementation:
This version is more concise and potentially more efficient, while maintaining the same functionality.