Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
ruihe774 committed Jan 2, 2024
1 parent 831c938 commit 8612acf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
52 changes: 31 additions & 21 deletions src/polyfill.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
export function toBase64(array: Uint8Array | Uint8ClampedArray): string {
let base64 = "";
const base64: string[] = [];
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const padding = array.length % 3;

for (let i = 0; i < array.length - padding; i += 3) {
let i = 0;
for (; i < array.length - padding; i += 3) {
const combined = (array[i] << 16) | (array[i + 1] << 8) | array[i + 2];
base64 +=
characters[(combined >> 18) & 63] +
characters[(combined >> 12) & 63] +
characters[(combined >> 6) & 63] +
characters[combined & 63];
base64.push(
characters[(combined >>> 18) & 63],
characters[(combined >>> 12) & 63],
characters[(combined >>> 6) & 63],
characters[combined & 63],
);
}

if (padding === 2) {
const combined = (array[array.length - 2] << 8) | array[array.length - 1];
base64 +=
characters[(combined >> 10) & 63] +
characters[(combined >> 4) & 63] +
characters[(combined << 2) & 63] +
"=";
} else if (padding === 1) {
const combined = array[array.length - 1];
base64 += characters[(combined >> 2) & 63] + characters[(combined << 4) & 63] + "==";
switch (padding) {
case 2: {
const combined = (array[i] << 8) | array[i + 1];
base64.push(
characters[(combined >>> 10) & 63],
characters[(combined >>> 4) & 63],
characters[(combined << 2) & 63],
"=",
);
break;
}
case 1: {
const combined = array[i];
base64.push(
characters[(combined >>> 2) & 63],
characters[(combined << 4) & 63],
"=",
"=",
);
break;
}
}

return base64;
return base64.join("");
}
2 changes: 1 addition & 1 deletion src/watchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function watchInner(
return watch(
source,
(newV, oldV, onCleanup) => {
if (oldValue === voidSymbol) {
if (oldValue == voidSymbol) {
oldValue = oldV;
}
return debounced(newV, onCleanup);
Expand Down

0 comments on commit 8612acf

Please sign in to comment.