From d4dbff084eb564678b8fb8e9b398cb3d1b86780c Mon Sep 17 00:00:00 2001 From: Julien Gilli Date: Wed, 5 Apr 2017 14:40:09 -0700 Subject: [PATCH] address comments from latest code review --- lib/do_volume/do_create.js | 4 +++- lib/volumes.js | 14 +++++++++----- test/unit/parseVolumeSize.test.js | 7 ++++--- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/do_volume/do_create.js b/lib/do_volume/do_create.js index 7676670..9f48c83 100644 --- a/lib/do_volume/do_create.js +++ b/lib/do_volume/do_create.js @@ -154,7 +154,9 @@ do_create.options = [ helpArg: 'SIZE', help: 'The size of the volume to create, in the form ' + '``, e.g. `20G`. must be > 0. Supported ' + - 'units are `G` or `g` for gibibytes and `M` or `m` for mebibytes.', + 'units are `G` or `g` for gibibytes and `M` or `m` for mebibytes.' + + ' If is not provided, is considered to be in ' + + 'mebibytes.', completionType: 'tritonvolumesize' }, { diff --git a/lib/volumes.js b/lib/volumes.js index ae27696..4ac8168 100644 --- a/lib/volumes.js +++ b/lib/volumes.js @@ -25,8 +25,8 @@ function throwInvalidSize(size) { * * Examples: * - the string '100' represents 100 mebibytes + * - the strings '100m' and '100M' represent 100 mebibytes * - the strings '100g' and '100G' represent 100 gibibytes - * - the string '100' represents 100 mebibytes * * If "size" is not a valid size string, an error is thrown. */ @@ -42,18 +42,22 @@ function parseVolumeSize(size) { M: 1 }; - var multiplierSymbol, multiplier; + var multiplier = 1; /* default unit is mebibytes */ + var multiplierSymbol; var baseValue; - var matches = size.match(/^([1-9]\d*)(g|m|G|M)$/); + var matches = size.match(/^([1-9]\d*)(g|m|G|M)?$/); if (!matches) { throwInvalidSize(size); } multiplierSymbol = matches[2]; - multiplier = MULTIPLIERS_TABLE[multiplierSymbol]; + if (multiplierSymbol) { + multiplier = MULTIPLIERS_TABLE[multiplierSymbol]; + } + baseValue = Number(matches[1]); - if (isNaN(baseValue) || multiplier === undefined) { + if (isNaN(baseValue)) { throwInvalidSize(size); } diff --git a/test/unit/parseVolumeSize.test.js b/test/unit/parseVolumeSize.test.js index 3e92ac5..bab6f06 100644 --- a/test/unit/parseVolumeSize.test.js +++ b/test/unit/parseVolumeSize.test.js @@ -63,10 +63,11 @@ test('parseVolumeSize', function (tt) { tt.test('parsing valid sizes', function (t) { var validVolumeSizes = [ - {input: '42g', expectedOutput: 42 * 1024}, - {input: '42G', expectedOutput: 42 * 1024}, + {input: '42', expectedOutput: 42}, {input: '42m', expectedOutput: 42}, - {input: '42M', expectedOutput: 42} + {input: '42M', expectedOutput: 42}, + {input: '42g', expectedOutput: 42 * 1024}, + {input: '42G', expectedOutput: 42 * 1024} ]; validVolumeSizes.forEach(function parse(validVolumeSize) {