Skip to content

Commit

Permalink
refactor: inline code
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Sep 26, 2024
1 parent f55ea68 commit f5ac494
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/internal/base64.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
let toBase64_: (input: string) => string;
if (typeof Buffer === 'undefined') {
toBase64_ = (input: string): string => {
const utf8Bytes = new TextEncoder().encode(input);
const binaryString = Array.from(utf8Bytes, (byte) =>
String.fromCodePoint(byte)
).join('');
return btoa(binaryString);
};
} else {
toBase64_ = (input: string): string => Buffer.from(input).toString('base64');
}

/**
* This works the same as `Buffer.from(input).toString('base64')`
* to work on both Node.js and browser environment.
Expand All @@ -25,4 +12,13 @@ if (typeof Buffer === 'undefined') {
*
* @example const encodedHeader = toBase64(JSON.stringify(header));
*/
export const toBase64 = toBase64_;
export const toBase64: (input: string) => string =
typeof Buffer === 'undefined'
? (input) => {
const utf8Bytes = new TextEncoder().encode(input);
const binaryString = Array.from(utf8Bytes, (byte) =>
String.fromCodePoint(byte)
).join('');
return btoa(binaryString);
}

Check warning on line 23 in src/internal/base64.ts

View check run for this annotation

Codecov / codecov/patch

src/internal/base64.ts#L17-L23

Added lines #L17 - L23 were not covered by tests
: (input) => Buffer.from(input).toString('base64');

0 comments on commit f5ac494

Please sign in to comment.