@@ -9,6 +9,7 @@ import * as b36 from '../src/bases/base36.ts'
99import * as b58 from '../src/bases/base58.ts'
1010import * as b64 from '../src/bases/base64.ts'
1111import * as b8 from '../src/bases/base8.ts'
12+ import * as id from '../src/bases/identity.ts'
1213import * as bytes from '../src/bytes.ts'
1314
1415const { base16, base32, base58btc, base64 } = { ...b16 , ...b32 , ...b58 , ...b64 }
@@ -64,7 +65,7 @@ describe('multibase', () => {
6465 const buff = bytes . fromString ( 'test' )
6566 const nonPrintableBuff = Uint8Array . from ( [ 239 , 250 , 254 ] )
6667
67- const baseTest = ( bases : typeof b2 | typeof b8 | typeof b10 | typeof b16 | typeof b32 | typeof b36 | typeof b58 | typeof b64 ) : void => {
68+ const baseTest = ( bases : typeof b2 | typeof b8 | typeof b10 | typeof b16 | typeof b32 | typeof b36 | typeof b58 | typeof b64 | typeof id ) : void => {
6869 for ( const base of Object . values ( bases ) ) {
6970 if ( ( ( base as { name : string } ) ?. name ) !== '' ) {
7071 it ( `encode/decode ${ base . name } ` , ( ) => {
@@ -118,6 +119,46 @@ describe('multibase', () => {
118119 baseTest ( b64 )
119120 } )
120121
122+ describe ( 'identity' , ( ) => {
123+ baseTest ( id )
124+
125+ it ( 'should round-trip unprintable characters' , ( ) => {
126+ const u = new Uint8Array ( [
127+ 6 , 22 , 184 , 240 , 237 , 178 ,
128+ 112 , 0 , 150 , 137 , 182 , 54 ,
129+ 220 , 1 , 217 , 221
130+ ] )
131+
132+ const s = id . identity . encode ( u )
133+ const b = id . identity . decode ( s )
134+
135+ assert . equalBytes ( b , u )
136+ } )
137+
138+ it ( 'should round-trip emojis' , ( ) => {
139+ const input = '😵💫🎉'
140+ const u = new TextEncoder ( ) . encode ( input )
141+ const s = id . identity . encode ( u )
142+ const b = id . identity . decode ( s )
143+ const output = new TextDecoder ( ) . decode ( b )
144+
145+ assert . equalBytes ( b , u )
146+ assert . equal ( output , input )
147+ } )
148+
149+ it ( 'should round-trip multi-byte characters' , ( ) => {
150+ // https://www.kanshudo.com/kanji/%F0%A0%AE%B7
151+ const input = '𠮷'
152+ const u = new TextEncoder ( ) . encode ( input )
153+ const s = id . identity . encode ( u )
154+ const b = id . identity . decode ( s )
155+ const output = new TextDecoder ( ) . decode ( b )
156+
157+ assert . equalBytes ( b , u )
158+ assert . equal ( output , input )
159+ } )
160+ } )
161+
121162 it ( 'multibase mismatch' , ( ) => {
122163 const b64 = base64 . encode ( bytes . fromString ( 'test' ) )
123164 const msg = `Unable to decode multibase string "${ b64 } ", base32 decoder only supports inputs prefixed with ${ base32 . prefix } `
@@ -158,7 +199,7 @@ describe('multibase', () => {
158199 assert . throws ( ( ) => base64 . decode ( b64 . substring ( 0 , b64 . length - 1 ) ) , 'Unexpected end of data' )
159200 } )
160201
161- it ( 'infers prefix and name corretly ' , ( ) => {
202+ it ( 'infers prefix and name correctly ' , ( ) => {
162203 const name = base32 . name
163204
164205 // @ts -expect-error - TS catches mismatch
0 commit comments