Skip to content

Commit

Permalink
Remove magic strings for time units
Browse files Browse the repository at this point in the history
  • Loading branch information
msorens committed Oct 6, 2015
1 parent 8bf2627 commit 827b39e
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions Components/Initialize.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,37 @@ function GenerateCaptions([string]$suppliedDisplayName)
# and determine display strings relating to it.
function NormalizeInterval([string]$suppliedInterval)
{
$hour_label = 'hours'
$minute_label = 'minutes'
$second_label = 'seconds'
$validUnits = "'$second_label', '$minute_label', or '$hour_label'"

$intervalValue =
switch -regex ($suppliedInterval) {
'^\d+$' {
[int]$suppliedInterval
break
}
'^(?<number>\d+)\s*(?<unit>\w+)$' {
'^(?<number>\d+)(?<unit>\w+)$' {
$re, $number = "^$($matches.unit)", $matches.number
$multiplier = if ('hours' -match $re) { 3600 }
elseif ('minutes' -match $re) { 60 }
elseif ('seconds' -match $re) { 1 }
$multiplier = if ($hour_label -match $re) { 3600 }
elseif ($minute_label -match $re) { 60 }
elseif ($second_label -match $re) { 1 }
else {
throw "Invalid unit [$($matches.unit)]: must be 'seconds', 'minutes', or 'hours'"
throw "Invalid unit [$($matches.unit)]: must be $validUnits"
};
[int]$number * $multiplier
break
}
default {
throw "Invalid interval [$suppliedInterval]: must be an integer optionally followed by 'seconds', 'minutes', or 'hours'"
throw "Invalid interval [$suppliedInterval]: must be an integer optionally followed by $validUnits"
}
}

$displayIntervalValue, $displayIntervalUnits =
if ($intervalValue -le $Default.CutOffSecondsToShowSeconds) { $intervalValue, 'seconds'}
elseif ($intervalValue -le $Default.CutOffSecondsToShowMinutes) { ($intervalValue / 60), 'minutes' }
else { ($intervalValue / 3600), 'hours' }
if ($intervalValue -le $Default.CutOffSecondsToShowSeconds) { $intervalValue, $second_label }
elseif ($intervalValue -le $Default.CutOffSecondsToShowMinutes) { ($intervalValue / 60), $minute_label }
else { ($intervalValue / 3600), $hour_label }

$intervalFormatString =
if ([int]$displayIntervalValue -eq $displayIntervalValue)
Expand Down

0 comments on commit 827b39e

Please sign in to comment.