diff --git a/src/initMeta.js b/src/initMeta.js index c0c045c..e0c36cd 100644 --- a/src/initMeta.js +++ b/src/initMeta.js @@ -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)) diff --git a/src/splitRange.js b/src/splitRange.js index f11b8ab..c0cdcb5 100644 --- a/src/splitRange.js +++ b/src/splitRange.js @@ -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]] } diff --git a/test/test.splitRange.js b/test/test.splitRange.js index 13b0112..af2ed15 100644 --- a/test/test.splitRange.js +++ b/test/test.splitRange.js @@ -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]]) +})