@@ -51,6 +51,8 @@ class Dish {
5151 case "bignumber" :
5252 case "big number" :
5353 return Dish . BIG_NUMBER ;
54+ case "list<file>" :
55+ return Dish . LIST_FILE ;
5456 default :
5557 throw "Invalid data type string. No matching enum." ;
5658 }
@@ -77,6 +79,8 @@ class Dish {
7779 return "ArrayBuffer" ;
7880 case Dish . BIG_NUMBER :
7981 return "BigNumber" ;
82+ case Dish . LIST_FILE :
83+ return "List<File>" ;
8084 default :
8185 throw "Invalid data type enum. No matching type." ;
8286 }
@@ -86,7 +90,7 @@ class Dish {
8690 /**
8791 * Sets the data value and type and then validates them.
8892 *
89- * @param {byteArray|string|number|ArrayBuffer|BigNumber } value
93+ * @param {* } value
9094 * - The value of the input data.
9195 * @param {number } type
9296 * - The data type of value, see Dish enums.
@@ -112,15 +116,14 @@ class Dish {
112116 *
113117 * @param {number } type - The data type of value, see Dish enums.
114118 * @param {boolean } [notUTF8=false] - Do not treat strings as UTF8.
115- * @returns {byteArray|string|number|ArrayBuffer|BigNumber }
116- * The value of the output data.
119+ * @returns {* } - The value of the output data.
117120 */
118- get ( type , notUTF8 = false ) {
121+ async get ( type , notUTF8 = false ) {
119122 if ( typeof type === "string" ) {
120123 type = Dish . typeEnum ( type ) ;
121124 }
122125 if ( this . type !== type ) {
123- this . translate ( type , notUTF8 ) ;
126+ await this . _translate ( type , notUTF8 ) ;
124127 }
125128 return this . value ;
126129 }
@@ -132,7 +135,7 @@ class Dish {
132135 * @param {number } toType - The data type of value, see Dish enums.
133136 * @param {boolean } [notUTF8=false] - Do not treat strings as UTF8.
134137 */
135- translate ( toType , notUTF8 = false ) {
138+ async _translate ( toType , notUTF8 = false ) {
136139 log . debug ( `Translating Dish from ${ Dish . enumLookup ( this . type ) } to ${ Dish . enumLookup ( toType ) } ` ) ;
137140 const byteArrayToStr = notUTF8 ? Utils . byteArrayToChars : Utils . byteArrayToUtf8 ;
138141
@@ -142,7 +145,7 @@ class Dish {
142145 this . value = this . value ? Utils . strToByteArray ( this . value ) : [ ] ;
143146 break ;
144147 case Dish . NUMBER :
145- this . value = typeof this . value == "number" ? Utils . strToByteArray ( this . value . toString ( ) ) : [ ] ;
148+ this . value = typeof this . value === "number" ? Utils . strToByteArray ( this . value . toString ( ) ) : [ ] ;
146149 break ;
147150 case Dish . HTML :
148151 this . value = this . value ? Utils . strToByteArray ( Utils . unescapeHtml ( Utils . stripHtmlTags ( this . value , true ) ) ) : [ ] ;
@@ -154,6 +157,11 @@ class Dish {
154157 case Dish . BIG_NUMBER :
155158 this . value = this . value instanceof BigNumber ? Utils . strToByteArray ( this . value . toFixed ( ) ) : [ ] ;
156159 break ;
160+ case Dish . LIST_FILE :
161+ this . value = await Promise . all ( this . value . map ( async f => Utils . readFile ( f ) ) ) ;
162+ this . value = this . value . map ( b => Array . prototype . slice . call ( b ) ) ;
163+ this . value = [ ] . concat . apply ( [ ] , this . value ) ;
164+ break ;
157165 default :
158166 break ;
159167 }
@@ -183,6 +191,10 @@ class Dish {
183191 }
184192 this . type = Dish . BIG_NUMBER ;
185193 break ;
194+ case Dish . LIST_FILE :
195+ this . value = new File ( this . value , "unknown" ) ;
196+ this . type = Dish . LIST_FILE ;
197+ break ;
186198 default :
187199 break ;
188200 }
@@ -220,6 +232,9 @@ class Dish {
220232 return this . value instanceof ArrayBuffer ;
221233 case Dish . BIG_NUMBER :
222234 return this . value instanceof BigNumber ;
235+ case Dish . LIST_FILE :
236+ return this . value instanceof Array &&
237+ this . value . reduce ( ( acc , curr ) => acc && curr instanceof File , true ) ;
223238 default :
224239 return false ;
225240 }
@@ -244,6 +259,8 @@ class Dish {
244259 return this . value . toString ( ) . length ;
245260 case Dish . ARRAY_BUFFER :
246261 return this . value . byteLength ;
262+ case Dish . LIST_FILE :
263+ return this . value . reduce ( ( acc , curr ) => acc + curr . size , 0 ) ;
247264 default :
248265 return - 1 ;
249266 }
@@ -288,6 +305,12 @@ Dish.ARRAY_BUFFER = 4;
288305 * @enum
289306 */
290307Dish . BIG_NUMBER = 5 ;
308+ /**
309+ * Dish data type enum for lists of files.
310+ * @readonly
311+ * @enum
312+ */
313+ Dish . LIST_FILE = 6 ;
291314
292315
293316export default Dish ;
0 commit comments