Skip to content

Commit

Permalink
Merge ac66791 into 1d17b6d
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Mar 14, 2016
2 parents 1d17b6d + ac66791 commit 697326f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/initMeta.js
Expand Up @@ -7,8 +7,8 @@ const PROPS = [
]
module.exports = (ob, options) => ob
.requestContentLength(options)
.map((x) => {
const threads = splitRange(x, options.range)
return _.assign({}, options, {totalBytes: x, threads, offsets: threads.map((x) => x[0])})
.map((totalBytes) => {
const threads = splitRange(totalBytes, options.range)
return _.assign({}, options, {totalBytes, threads, offsets: threads.map((x) => x[0])})
})
.map((x) => _.pick(x, PROPS))
15 changes: 9 additions & 6 deletions src/splitRange.js
Expand Up @@ -5,11 +5,14 @@
'use strict'
const _ = require('lodash')

module.exports = (range, count) => {
const delta = Math.round(range / count)
const start = _.times(count, (x) => x * delta)
const end = _.times(count, (x) => (x + 1) * delta - 1)
end[count - 1] = range
return _.zip(start, end)
module.exports = (totalBytes, range) => {
if (range > 0) {
const delta = Math.round(totalBytes / range)
const start = _.times(range, (x) => x * delta)
const end = _.times(range, (x) => (x + 1) * delta - 1)
end[range - 1] = totalBytes
return _.zip(start, end)
}
return [[0, totalBytes]]
}

6 changes: 6 additions & 0 deletions test/test.splitRange.js
Expand Up @@ -10,3 +10,9 @@ test((t) => {
t.same(splitRange(100, 2), [[0, 49], [50, 100]])
t.same(splitRange(100, 3), [[0, 32], [33, 65], [66, 100]])
})

test.only('invalid values', (t) => {
t.same(splitRange(100, 0), [[0, 100]])
t.same(splitRange(100, null), [[0, 100]])
t.same(splitRange(100, NaN), [[0, 100]])
})

0 comments on commit 697326f

Please sign in to comment.