Skip to content

Commit

Permalink
feat(transducers-binary): update return types
Browse files Browse the repository at this point in the history
- return Uint8Array if input given for base64Decode() or utf8Encode()
- update asBytes() return type (if input given)
  • Loading branch information
postspectacular committed Sep 27, 2021
1 parent 9866ab3 commit 70217e8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions packages/transducers-binary/src/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const B64_SAFE = B64_CHARS.substr(0, 62) + "-_";
* Supports URL safe & unsafe flavors.
*/
export function base64Decode(): Transducer<string, number>;
export function base64Decode(src: string): IterableIterator<number>;
export function base64Decode(src: string): Uint8Array;
export function base64Decode(src?: string): any {
return src
? iterator1(base64Decode(), src)
? new Uint8Array([...iterator1(base64Decode(), src)])
: (rfn: Reducer<any, number>) => {
const r = rfn[2];
let bc = 0,
Expand All @@ -32,7 +32,7 @@ export function base64Decode(src?: string): any {
return reduced(acc);
default:
}
let y = B64_CHARS.indexOf(x);
const y = B64_CHARS.indexOf(x);
bs = bc & 3 ? (bs << 6) + y : y;
if (bc++ & 3) {
acc = r(acc, 255 & (bs >> ((-2 * bc) & 6)));
Expand Down
4 changes: 2 additions & 2 deletions packages/transducers-binary/src/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const str = (x: string): BinStructItem => ["str", x];
* ```
*/
export function asBytes(): Transducer<BinStructItem, number>;
export function asBytes(src: Iterable<BinStructItem>): IterableIterator<number>;
export function asBytes(src: Iterable<BinStructItem>): Iterable<number>;
export function asBytes(src?: Iterable<BinStructItem>): any {
return src
? iterator(asBytes(), src)
Expand Down Expand Up @@ -275,7 +275,7 @@ export function bytes(cap = 1024, src?: Iterable<BinStructItem>) {
acc = setArray("setFloat64", 8, acc, x, le);
break;
case "str": {
let utf = [...utf8Encode(<string>x)];
let utf = utf8Encode(<string>x);
acc = ensure(acc, utf.length);
acc.set(utf, pos);
pos += utf.length;
Expand Down
4 changes: 2 additions & 2 deletions packages/transducers-binary/src/utf8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ export function utf8Decode(src?: Iterable<number>): any {
* ```
*/
export function utf8Encode(): Transducer<string, number>;
export function utf8Encode(src: string): IterableIterator<number>;
export function utf8Encode(src: string): Uint8Array;
export function utf8Encode(src?: string): any {
return src != null
? iterator(utf8Encode(), src)
? new Uint8Array([...iterator(utf8Encode(), src)])
: (rfn: Reducer<any, number>) => {
const r = rfn[2];
return compR(rfn, (acc, x: string) => {
Expand Down

0 comments on commit 70217e8

Please sign in to comment.