Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 9c2687a

Browse files
committed
fs.slice handle negative arguments #89
1 parent ce596a0 commit 9c2687a

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/fs.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,25 @@ function exists(path:string):Promise<bool, bool> {
300300
}
301301

302302
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))
304322
}
305323

306324
function isDir(path:string):Promise<bool, bool> {

0 commit comments

Comments
 (0)