@@ -25,6 +25,12 @@ export interface Base32Options {
2525 */
2626 encoder ?: string ;
2727
28+ /**
29+ * Disable padding character when encoding the data. Default false. This option is equivalent
30+ * to setting `padChar` to empty string, and has higher priority than `padChar`.
31+ */
32+ noPadding ?: boolean ;
33+
2834 /**
2935 * Padding character for padding when the encoded string in bytes is not a number to a multiple
3036 * of 8, or set it to empty string to disable padding. The padding character must not be newline
@@ -39,10 +45,16 @@ export class Base32Encoding {
3945 */
4046 private encoder : string ;
4147
48+ /**
49+ * Disable padding character when encoding the data. Default false. This option is equivalent
50+ * to setting `padChar` to empty string, and has higher priority than `padChar`.
51+ */
52+ private noPadding ?: boolean ;
53+
4254 /**
4355 * The default padding character.
4456 */
45- private padChar : string ;
57+ private padChar ? : string ;
4658
4759 /**
4860 * Creates a Base32 encoding instance with the optional encoder alphabets and padding character
@@ -57,16 +69,20 @@ export class Base32Encoding {
5769 * @param options Optional settings for Base32 encoding.
5870 */
5971 constructor ( options ?: Base32Options ) {
60- let padChar : string | undefined ;
61- if ( options ?. padChar !== undefined ) {
62- padChar = checkPadChar ( options . padChar ) ;
72+ this . noPadding = ! ! options ?. noPadding ;
73+
74+ if ( ! this . noPadding ) {
75+ let padChar : string | undefined ;
76+ if ( options ?. padChar !== undefined ) {
77+ padChar = checkPadChar ( options . padChar ) ;
78+ }
79+ this . padChar = padChar !== undefined ? padChar : '=' ;
6380 }
64- this . padChar = padChar !== undefined ? padChar : '=' ;
6581
6682 this . encoder = checkEncoder (
6783 options ?. encoder ? options . encoder : Base32StdEncoder ,
68- this . padChar ,
6984 ENCODER_LENGTH ,
85+ this . padChar ,
7086 ) ;
7187 }
7288
@@ -206,7 +222,9 @@ export class Base32Encoding {
206222 let padChar ;
207223 let encoder ;
208224
209- if ( ! options || options . padChar === undefined || checkPadChar ( options . padChar ) === undefined ) {
225+ if ( this . noPadding || ! ! options ?. noPadding ) {
226+ padChar = '' ;
227+ } else if ( ! options || options . padChar === undefined || checkPadChar ( options . padChar ) === undefined ) {
210228 padChar = this . padChar ;
211229 } else {
212230 padChar = options . padChar ;
@@ -218,11 +236,11 @@ export class Base32Encoding {
218236 encoder = options . encoder ;
219237 }
220238
221- encoder = checkEncoder ( encoder , padChar , 32 ) ;
239+ encoder = checkEncoder ( encoder , ENCODER_LENGTH , padChar ) ;
222240
223241 return {
224242 encoder,
225- padChar,
243+ padChar : padChar ! ,
226244 } ;
227245 }
228246}
0 commit comments