File tree Expand file tree Collapse file tree 4 files changed +13
-3
lines changed
Expand file tree Collapse file tree 4 files changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ declare function base(ALPHABET: string): base.BaseConverter;
33export = base ;
44declare namespace base {
55 interface BaseConverter {
6- encode ( buffer : Buffer ) : string ;
6+ encode ( buffer : Buffer | number [ ] | Uint8Array ) : string ;
77 decodeUnsafe ( string : string ) : Buffer | undefined ;
88 decode ( string : string ) : Buffer ;
99 }
Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ function base (ALPHABET) {
2323 var FACTOR = Math . log ( BASE ) / Math . log ( 256 ) // log(BASE) / log(256), rounded up
2424 var iFACTOR = Math . log ( 256 ) / Math . log ( BASE ) // log(256) / log(BASE), rounded up
2525 function encode ( source ) {
26+ if ( Array . isArray ( source ) || source instanceof Uint8Array ) { source = _Buffer . from ( source ) }
2627 if ( ! _Buffer . isBuffer ( source ) ) { throw new TypeError ( 'Expected Buffer' ) }
2728 if ( source . length === 0 ) { return '' }
2829 // Skip & count leading zeroes.
Original file line number Diff line number Diff line change @@ -55,3 +55,11 @@ tape.test('encode throws on string', function (t) {
5555 base . encode ( 'a' )
5656 } , new RegExp ( '^TypeError: Expected Buffer$' ) )
5757} )
58+
59+ tape . test ( 'encode not throw on Array or Uint8Array' , function ( t ) {
60+ var base = bases . base58
61+
62+ t . plan ( 2 )
63+ t . same ( base . encode ( [ 42 , 12 , 34 ] ) , 'F89f' )
64+ t . same ( base . encode ( new Uint8Array ( [ 42 , 12 , 34 ] ) ) , 'F89f' )
65+ } )
Original file line number Diff line number Diff line change @@ -28,7 +28,8 @@ function base (ALPHABET: string): base.BaseConverter {
2828 const FACTOR = Math . log ( BASE ) / Math . log ( 256 ) // log(BASE) / log(256), rounded up
2929 const iFACTOR = Math . log ( 256 ) / Math . log ( BASE ) // log(256) / log(BASE), rounded up
3030
31- function encode ( source : Buffer ) : string {
31+ function encode ( source : Buffer | number [ ] | Uint8Array ) : string {
32+ if ( Array . isArray ( source ) || source instanceof Uint8Array ) source = _Buffer . from ( source )
3233 if ( ! _Buffer . isBuffer ( source ) ) throw new TypeError ( 'Expected Buffer' )
3334 if ( source . length === 0 ) return ''
3435
@@ -156,7 +157,7 @@ export = base;
156157
157158declare namespace base {
158159 interface BaseConverter {
159- encode ( buffer : Buffer ) : string ;
160+ encode ( buffer : Buffer | number [ ] | Uint8Array ) : string ;
160161 decodeUnsafe ( string : string ) : Buffer | undefined ;
161162 decode ( string : string ) : Buffer ;
162163 }
You can’t perform that action at this time.
0 commit comments