Skip to content

Commit

Permalink
feat(types): Added conversion utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sullivanpj committed Jul 28, 2024
1 parent cc573bb commit c45b539
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/types/src/convert/buffer-to-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const bufferToString = (buf: Buffer): string => buf.toString("utf8");
3 changes: 3 additions & 0 deletions packages/types/src/convert/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./buffer-to-string";
export * from "./string-to-uint8array";
export * from "./uint8array-to-string";
4 changes: 4 additions & 0 deletions packages/types/src/convert/string-to-uint8array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const stringToUint8Array = (text: string): Uint8Array =>
Uint8Array.from(
[...encodeURIComponent(text)].map(letter => letter.codePointAt(0)!)
);
2 changes: 2 additions & 0 deletions packages/types/src/convert/uint8array-to-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const uint8ArrayToString = (arr: Uint8Array): string =>
decodeURIComponent(Buffer.from(arr).toString("utf8"));

0 comments on commit c45b539

Please sign in to comment.