@@ -160,7 +160,20 @@ function readFile(path: string, encoding: string = 'utf8'): Promise<any> {
160160 if ( typeof path !== 'string' ) {
161161 return Promise . reject ( addCode ( 'EINVAL' , new TypeError ( 'Missing argument "path" ' ) ) ) ;
162162 }
163- return ReactNativeBlobUtil . readFile ( path , encoding ) ;
163+ return ReactNativeBlobUtil . readFile ( path , encoding, false) ;
164+ }
165+
166+ /**
167+ * Reads the file, then transforms it before returning the content
168+ * @param {string } path Path of the file.
169+ * @param {'base64' | 'utf8' | 'ascii' } encoding Encoding of read stream.
170+ * @return {Promise<Array<number> | string> }
171+ */
172+ function readFileWithTransform ( path : string , encoding : string = 'utf8' ) : Promise < any > {
173+ if ( typeof path !== 'string' ) {
174+ return Promise . reject ( addCode ( 'EINVAL' , new TypeError ( 'Missing argument "path" ' ) ) )
175+ }
176+ return ReactNativeBlobUtil . readFile ( path , encoding, true) ;
164177}
165178
166179/**
@@ -186,7 +199,31 @@ function writeFile(path: string, data: string | Array<number>, encoding: ?string
186199 return Promise . reject ( addCode ( 'EINVAL' , new TypeError ( `"data" must be a String when encoding is "utf8" or "base64", but it is "${ typeof data } "` ) ) ) ;
187200 }
188201 else
189- return ReactNativeBlobUtil . writeFile ( path , encoding , data , false ) ;
202+ return ReactNativeBlobUtil . writeFile ( path , encoding , data , false , false ) ;
203+ }
204+ }
205+
206+ /**
207+ * Transforms the data and then writes to the file.
208+ * @param {string } path Path of the file.
209+ * @param {string | number[] } data Data to write to the file.
210+ * @param {string } encoding Encoding of data (Optional).
211+ * @return {Promise }
212+ */
213+ function writeFileWithTransform ( path : string , data : string | Array < number > , encoding : ?string = 'utf8' ) : Promise {
214+ if ( typeof path !== 'string' ) {
215+ return Promise . reject ( addCode ( 'EINVAL' , new TypeError ( 'Missing argument "path" ' ) ) )
216+ }
217+ if ( encoding . toLocaleLowerCase ( ) === 'ascii' ) {
218+ return Promise . reject ( addCode ( 'EINVAL' , new TypeError ( 'ascii is not supported for converted files' ) ) )
219+ }
220+ else {
221+ if ( typeof data !== 'string' ) {
222+ return Promise . reject ( addCode ( 'EINVAL' , new TypeError ( `"data" must be a String when encoding is "utf8" or "base64", but it is "${ typeof data } "` ) ) )
223+ }
224+
225+ else
226+ return ReactNativeBlobUtil . writeFile ( path , encoding , data , true , false )
190227 }
191228}
192229
@@ -415,6 +452,8 @@ export default {
415452 cp,
416453 writeStream,
417454 writeFile,
455+ writeFileWithTransform,
456+ readFileWithTransform,
418457 appendFile,
419458 pathForAppGroup,
420459 syncPathAppGroup,
0 commit comments