address comments from latest code review

This commit is contained in:
Julien Gilli 2017-04-05 14:40:09 -07:00
parent cb725eb587
commit d4dbff084e
3 changed files with 16 additions and 9 deletions

View File

@ -154,7 +154,9 @@ do_create.options = [
helpArg: 'SIZE', helpArg: 'SIZE',
help: 'The size of the volume to create, in the form ' + help: 'The size of the volume to create, in the form ' +
'`<integer><unit>`, e.g. `20G`. <integer> must be > 0. Supported ' + '`<integer><unit>`, e.g. `20G`. <integer> 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 <unit> is not provided, <integer> is considered to be in ' +
'mebibytes.',
completionType: 'tritonvolumesize' completionType: 'tritonvolumesize'
}, },
{ {

View File

@ -25,8 +25,8 @@ function throwInvalidSize(size) {
* *
* Examples: * Examples:
* - the string '100' represents 100 mebibytes * - the string '100' represents 100 mebibytes
* - the strings '100m' and '100M' represent 100 mebibytes
* - the strings '100g' and '100G' represent 100 gibibytes * - 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. * If "size" is not a valid size string, an error is thrown.
*/ */
@ -42,18 +42,22 @@ function parseVolumeSize(size) {
M: 1 M: 1
}; };
var multiplierSymbol, multiplier; var multiplier = 1; /* default unit is mebibytes */
var multiplierSymbol;
var baseValue; 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) { if (!matches) {
throwInvalidSize(size); throwInvalidSize(size);
} }
multiplierSymbol = matches[2]; multiplierSymbol = matches[2];
multiplier = MULTIPLIERS_TABLE[multiplierSymbol]; if (multiplierSymbol) {
multiplier = MULTIPLIERS_TABLE[multiplierSymbol];
}
baseValue = Number(matches[1]); baseValue = Number(matches[1]);
if (isNaN(baseValue) || multiplier === undefined) { if (isNaN(baseValue)) {
throwInvalidSize(size); throwInvalidSize(size);
} }

View File

@ -63,10 +63,11 @@ test('parseVolumeSize', function (tt) {
tt.test('parsing valid sizes', function (t) { tt.test('parsing valid sizes', function (t) {
var validVolumeSizes = [ var validVolumeSizes = [
{input: '42g', expectedOutput: 42 * 1024}, {input: '42', expectedOutput: 42},
{input: '42G', expectedOutput: 42 * 1024},
{input: '42m', 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) { validVolumeSizes.forEach(function parse(validVolumeSize) {