Skip to content

Commit

Permalink
feat: add Texter class and deps
Browse files Browse the repository at this point in the history
  • Loading branch information
jimcase committed Oct 16, 2024
1 parent 1b747f8 commit 18b1f5a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/keri/core/matter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export class MatterCodex extends Codex {
StrB64_Big_L0: string = '7AAA'; // String Base64 Only Big Lead Size 0
StrB64_Big_L1: string = '8AAA'; // String Base64 Only Big Lead Size 1
StrB64_Big_L2: string = '9AAA'; // String Base64 Only Big Lead Size 2
Bytes_L0: string = '4B'; // Byte String Lead Size 0
Bytes_L1: string = '5B'; // Byte String Lead Size 1
Bytes_L2: string = '6B'; // Byte String Lead Size 2
Bytes_Big_L0: string = '7AAB'; // Byte String Big Lead Size 0
Bytes_Big_L1: string = '8AAB'; // Byte String Big Lead Size 1
Bytes_Big_L2: string = '9AAB'; // Byte String Big Lead Size 2
}

export const MtrDex = new MatterCodex();
Expand Down
63 changes: 63 additions & 0 deletions src/keri/core/texter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Matter, MtrDex } from './matter';
import { EmptyMaterialError } from "./kering";

interface TexterArgs {
raw?: Uint8Array;
qb64b?: Uint8Array;
qb64?: string;
qb2?: Uint8Array;
code?: string;
text?: string | Uint8Array;
}

class TextCodex {
static readonly Bytes_L0: string = '4B'; // Byte String lead size 0
static readonly Bytes_L1: string = '5B'; // Byte String lead size 1
static readonly Bytes_L2: string = '6B'; // Byte String lead size 2
static readonly Bytes_Big_L0: string = '7AAB'; // Byte String big lead size 0
static readonly Bytes_Big_L1: string = '8AAB'; // Byte String big lead size 1
static readonly Bytes_Big_L2: string = '9AAB'; // Byte String big lead size 2

static *iterator(): IterableIterator<string> {
yield TextCodex.Bytes_L0;
yield TextCodex.Bytes_L1;
yield TextCodex.Bytes_L2;
yield TextCodex.Bytes_Big_L0;
yield TextCodex.Bytes_Big_L1;
yield TextCodex.Bytes_Big_L2;
}
}

export class Texter extends Matter {

constructor({
raw = null,
qb64b = null,
qb64 = null,
qb2 = null,
code = MtrDex.Bytes_L0,
text = null,
}: TexterArgs) {

if (!raw && !qb64b && !qb64 && !qb2) {
if (!text) {
throw new EmptyMaterialError("Missing text string.");
}
if (typeof text === "string") {
raw = new TextEncoder().encode(text);
} else {
raw = text;
}
}

super({ raw, qb64b, qb64, qb2, code });

if (!code || ![...TextCodex.iterator()].includes(code)) {
throw new Error(`Invalid code = ${code} for Texter.`);
}
}

get text(): string {
return new TextDecoder().decode(this.raw);
}
}

0 comments on commit 18b1f5a

Please sign in to comment.