@@ -9,7 +9,7 @@ import { decodeShortString, encodeShortString, splitLongString } from '../shortS
99 * @example
1010 * ```typescript
1111 * const myByteArray = {
12- * data: [ '0x00' ],
12+ * data: [],
1313 * pending_word: '0x414243444546474849',
1414 * pending_word_len: 9
1515 * }
@@ -36,39 +36,28 @@ export function stringFromByteArray(myByteArray: ByteArray): string {
3636 * @returns Cairo representation of a LongString
3737 * @example
3838 * ```typescript
39- * const myByteArray: ByteArray = byteArrayFromStr ("ABCDEFGHI");
39+ * const myByteArray: ByteArray = byteArrayFromString ("ABCDEFGHI");
4040 * ```
4141 * Result is :
4242 * {
43- * data: [ '0x00' ],
43+ * data: [],
4444 * pending_word: '0x414243444546474849',
4545 * pending_word_len: 9
4646 * }
4747 */
48- export function byteArrayFromString ( myString : string ) : ByteArray {
49- if ( myString . length === 0 ) {
50- return {
51- data : [ '0x00' ] ,
52- pending_word : '0x00' ,
53- pending_word_len : 0 ,
54- } as ByteArray ;
55- }
56- const myShortStrings : string [ ] = splitLongString ( myString ) ;
57- const remains : string = myShortStrings [ myShortStrings . length - 1 ] ;
58- const myShortStringsEncoded : BigNumberish [ ] = myShortStrings . map ( ( shortStr ) =>
59- encodeShortString ( shortStr )
60- ) ;
61- if ( remains . length === 31 ) {
62- return {
63- data : myShortStringsEncoded ,
64- pending_word : '0x00' ,
65- pending_word_len : 0 ,
66- } as ByteArray ;
67- }
68- const pendingEncodedWord : BigNumberish = myShortStringsEncoded . pop ( ) ! ;
48+ export function byteArrayFromString ( targetString : string ) : ByteArray {
49+ const shortStrings : string [ ] = splitLongString ( targetString ) ;
50+ const remainder : string = shortStrings [ shortStrings . length - 1 ] ;
51+ const shortStringsEncoded : BigNumberish [ ] = shortStrings . map ( encodeShortString ) ;
52+
53+ const [ pendingWord , pendingWordLength ] =
54+ remainder === undefined || remainder . length === 31
55+ ? [ '0x00' , 0 ]
56+ : [ shortStringsEncoded . pop ( ) ! , remainder . length ] ;
57+
6958 return {
70- data : myShortStringsEncoded . length === 0 ? [ '0x00' ] : myShortStringsEncoded ,
71- pending_word : pendingEncodedWord ,
72- pending_word_len : remains . length ,
73- } as ByteArray ;
59+ data : shortStringsEncoded . length === 0 ? [ ] : shortStringsEncoded ,
60+ pending_word : pendingWord ,
61+ pending_word_len : pendingWordLength ,
62+ } ;
7463}
0 commit comments