Browse Source

Add js implementation for #27

Ben Hsieh 8 years ago
parent
commit
5bbe990afa
1 changed files with 24 additions and 0 deletions
  1. 24
    0
      src/fs.js

+ 24
- 0
src/fs.js View File

@@ -127,6 +127,30 @@ function mkdir(path:string):Promise {
127 127
 
128 128
 }
129 129
 
130
+/**
131
+ * Wrapper method of readStream.
132
+ * @param  {string} path Path of the file.
133
+ * @param  {'base64' | 'utf8' | 'ascii'} encoding Encoding of read stream.
134
+ * @return {Promise<Array<number> | string>}
135
+ */
136
+function readFile(path:string, encoding:string, bufferSize:number):Promise<any> {
137
+  return RNFetchBlob.readFile(path, encoding)
138
+}
139
+
140
+function writeFile(path:string, encoding:string, data:string | Array<number>):Promise {
141
+  if(encoding.toLocaleLowerCase() === 'ascii') {
142
+    if(!Array.isArray(data))
143
+      Promise.reject(`Expected "data" is an Array when encoding is "ascii", however got ${typeof data}`)
144
+    else
145
+      return RNFetchBlob.writeFileArray(path, data);
146
+  } else {
147
+    if(typeof data !== 'string')
148
+      Promise.reject(`Expected "data" is a String when encoding is "utf8" or "base64", however got ${typeof data}`)
149
+    else
150
+      return RNFetchBlob.writeFile(path, encoding, data);
151
+  }
152
+}
153
+
130 154
 /**
131 155
  * Show statistic data of a path.
132 156
  * @param  {string} path Target path