1- import type {
2- Type_bytesToBase64 ,
3- Type_base64ToBytes ,
4- Type_encode ,
5- Type_decode ,
6- Type_decodeToString ,
7- } from '../type.d.ts'
1+ /**
2+ * @param omitPadding
3+ * A boolean specifying whether to omit padding characters (=) at the end of the base64 string.
4+ * The default is false.
5+ */
6+ export type bytesToBase64Func = ( bytes : Uint8Array , omitPadding ?: boolean ) => string
7+
8+ export type base64ToBytesFunc = ( base64 : string ) => Uint8Array < ArrayBuffer >
9+
10+ export type encodeFunc = ( input : Uint8Array | string , omitPadding ?: boolean ) => string
11+
12+ export type decodeFunc = base64ToBytesFunc
13+
14+ export type decodeToStringFunc = ( input : string , textDecoder ?: TextDecoder ) => string
815
916let __textEncoder : TextEncoder | undefined
1017let __textDecoder : TextDecoder | undefined
1118
12- export function make_bytesToBase64 ( alphabet : string ) : Type_bytesToBase64 {
19+ export function make_bytesToBase64 ( alphabet : string ) : bytesToBase64Func {
1320 if ( typeof alphabet != 'string' || alphabet . length !== 64 ) throw TypeError ( `invalid alphabet format` ) ;
1421 const _alphabet = new Uint8Array ( 64 )
1522 for ( let code , i = 0 ; i < 64 ; i ++ ) {
@@ -41,7 +48,7 @@ export function make_bytesToBase64(alphabet: string): Type_bytesToBase64 {
4148 }
4249}
4350
44- export function make_base64ToBytes ( alphabet : string ) : Type_base64ToBytes {
51+ export function make_base64ToBytes ( alphabet : string ) : base64ToBytesFunc {
4552 if ( typeof alphabet != 'string' || alphabet . length !== 64 ) throw TypeError ( `invalid alphabet length` ) ;
4653 const _alphabet = new Uint8Array ( 128 ) . fill ( 64 )
4754 for ( let code , i = 0 ; i < 64 ; i ++ ) {
@@ -84,7 +91,7 @@ export function make_base64ToBytes(alphabet: string): Type_base64ToBytes {
8491
8592export function make_base64ToBytes_NodeJS (
8693 base64Write : ( this : Uint8Array , base64 : string ) => number
87- ) : Type_base64ToBytes {
94+ ) : base64ToBytesFunc {
8895 return base64 => {
8996 const b64len = base64 . length
9097 , out = new Uint8Array ( b64len * .75 - (
@@ -98,7 +105,7 @@ export function make_base64ToBytes_NodeJS(
98105 }
99106}
100107
101- export function make_encode ( toBase64 : Type_bytesToBase64 ) : Type_encode {
108+ export function make_encode ( toBase64 : bytesToBase64Func ) : encodeFunc {
102109 return ( input , omitPadding ) => toBase64 (
103110 typeof input == 'string'
104111 ? ( __textEncoder ||= new TextEncoder ) . encode ( input )
@@ -107,11 +114,11 @@ export function make_encode(toBase64: Type_bytesToBase64): Type_encode {
107114 )
108115}
109116
110- export function make_decode ( toBytes : Type_base64ToBytes ) : Type_decode {
117+ export function make_decode ( toBytes : base64ToBytesFunc ) : decodeFunc {
111118 return base64 => toBytes ( '' + base64 )
112119}
113120
114- export function make_decodeToString ( _decode : Type_decode ) : Type_decodeToString {
121+ export function make_decodeToString ( _decode : decodeFunc ) : decodeToStringFunc {
115122 return ( input , textDecoder ) =>
116123 ( textDecoder || ( __textDecoder ||= new TextDecoder ) ) . decode ( _decode ( input ) )
117124}
0 commit comments