Browse Source

fs.slice handle negative arguments #89

Ben Hsieh 8 years ago
parent
commit
9c2687a719
1 changed files with 19 additions and 1 deletions
  1. 19
    1
      src/fs.js

+ 19
- 1
src/fs.js View File

@@ -300,7 +300,25 @@ function exists(path:string):Promise<bool, bool> {
300 300
 }
301 301
 
302 302
 function slice(src:string, dest:string, start:number, end:number):Promise {
303
-  return RNFetchBlob.slice(src, dest, start, end)
303
+  let p = Promise.resolve()
304
+  let size = 0
305
+  function normalize(num, size) {
306
+    if(num < 0)
307
+      return Math.max(0, size + num)
308
+    if(!num && num !== 0)
309
+      return size
310
+    return num
311
+  }
312
+  if(start < 0 || end < 0 || !start || !end) {
313
+    p = p.then(() => stat(src))
314
+         .then((stat) => {
315
+           size = Math.floor(stat.size)
316
+           start = normalize(start || 0, size)
317
+           end = normalize(end, size)
318
+           return Promise.resolve()
319
+         })
320
+  }
321
+  return p.then(() => RNFetchBlob.slice(src, dest, start, end))
304 322
 }
305 323
 
306 324
 function isDir(path:string):Promise<bool, bool> {