Skip to content

Commit

Permalink
refactor(transducers-binary): update reducer handling due to updates …
Browse files Browse the repository at this point in the history
…in thi.ng/transducers pkg
  • Loading branch information
postspectacular committed Apr 3, 2024
1 parent e0e5654 commit 907c961
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions packages/transducers-binary/src/base64.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Reducer, Transducer } from "@thi.ng/transducers";
import { compR } from "@thi.ng/transducers/compr";
import { iterator, iterator1, __iter } from "@thi.ng/transducers/iterator";
import { __iter, iterator, iterator1 } from "@thi.ng/transducers/iterator";
import { isReduced, reduced } from "@thi.ng/transducers/reduced";

const B64_CHARS =
Expand All @@ -16,7 +16,7 @@ export function base64Decode(src: string): Uint8Array;
export function base64Decode(src?: string): any {
return src
? new Uint8Array(iterator1(base64Decode(), src))
: (rfn: Reducer<any, number>) => {
: (rfn: Reducer<number, any>) => {
const r = rfn[2];
let bc = 0,
bs = 0;
Expand Down Expand Up @@ -68,13 +68,13 @@ export function base64Encode(...args: any[]): any {
if (iter) {
return [...iter].join("");
}
return ([init, complete, reduce]: Reducer<any, string>) => {
return ([init, complete, reduce]: Reducer<string, any>) => {
let state = 0;
let b: number;
const opts = { safe: false, buffer: 1024, ...args[0] };
const chars = opts.safe ? B64_SAFE : B64_CHARS;
const buf: string[] = [];
return <Reducer<any, number>>[
return <Reducer<number, any>>[
init,
(acc) => {
switch (state) {
Expand Down
2 changes: 1 addition & 1 deletion packages/transducers-binary/src/bits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function bits(
export function bits(...args: any[]): any {
return (
__iter(bits, args, iterator) ||
((rfn: Reducer<any, number>) => {
((rfn: Reducer<number, any>) => {
const reduce = rfn[2];
const size = args[0] || 8;
const msb = args[1] !== false;
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 @@ -147,7 +147,7 @@ export function asBytes(src?: Iterable<BinStructItem>): any {
});
}

export function bytes(cap?: number): Reducer<Uint8Array, BinStructItem>;
export function bytes(cap?: number): Reducer<BinStructItem, Uint8Array>;
export function bytes(cap: number, src: Iterable<BinStructItem>): Uint8Array;
export function bytes(cap = 1024, src?: Iterable<BinStructItem>) {
let view: DataView;
Expand Down Expand Up @@ -179,7 +179,7 @@ export function bytes(cap = 1024, src?: Iterable<BinStructItem>) {

return src
? reduce(bytes(cap), src)
: <Reducer<Uint8Array, BinStructItem>>[
: <Reducer<BinStructItem, Uint8Array>>[
() => new Uint8Array(cap),
(acc) => acc.subarray(0, pos),
(acc, [type, x, le = false]) => {
Expand Down
10 changes: 5 additions & 5 deletions packages/transducers-binary/src/partition-bits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function partitionBits(
export function partitionBits(...args: any[]): any {
return (
__iter(partitionBits, args, iterator) ||
((rfn: Reducer<any, number>) => {
((rfn: Reducer<number, any>) => {
const destSize = args[0];
const srcSize = args[1] || 8;
return destSize < srcSize
Expand All @@ -37,10 +37,10 @@ export function partitionBits(...args: any[]): any {
}

const small = (
[init, complete, reduce]: Reducer<any, number>,
[init, complete, reduce]: Reducer<number, any>,
n: number,
wordSize: number
): Reducer<any, number> => {
): Reducer<number, any> => {
const maxb = wordSize - n;
const m1 = (1 << wordSize) - 1;
const m2 = (1 << n) - 1;
Expand All @@ -66,10 +66,10 @@ const small = (
};

const large = (
[init, complete, reduce]: Reducer<any, number>,
[init, complete, reduce]: Reducer<number, any>,
n: number,
wordSize: number
): Reducer<any, number> => {
): Reducer<number, any> => {
const m1 = (1 << wordSize) - 1;
let r = 0;
let y = 0;
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 @@ -16,7 +16,7 @@ export function utf8Decode(src: Iterable<number>): string;
export function utf8Decode(src?: Iterable<number>): any {
return src
? [...iterator1(utf8Decode(), src)].join("")
: (rfn: Reducer<any, string>) => {
: (rfn: Reducer<string, any>) => {
const r = rfn[2];
let state = 0;
let u0: number;
Expand Down Expand Up @@ -135,7 +135,7 @@ export function utf8Encode(src: string): Uint8Array;
export function utf8Encode(src?: string): any {
return src != null
? new Uint8Array(iterator(utf8Encode(), src))
: (rfn: Reducer<any, number>) => {
: (rfn: Reducer<number, any>) => {
const r = rfn[2];
return compR(rfn, (acc, x: string) => {
let u = x.charCodeAt(0),
Expand Down

0 comments on commit 907c961

Please sign in to comment.