Skip to content

Commit 812dae2

Browse files
committed
Add a round function to complement ceiling and floor
1 parent cb59da2 commit 812dae2

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

README.markdown

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,10 @@ Searches through an array and rejects all elements that match the provided regul
996996

997997
Reverses the order of a string or array. *Type*: rvalue.
998998

999+
#### `stdlib::round`
1000+
1001+
Rounds a number to the nearest integer
1002+
9991003
#### `rstrip`
10001004

10011005
Strips spaces to the right of the string. *Type*: rvalue.
@@ -1501,7 +1505,7 @@ The deprecation messages you get can vary, depending on the modules and data tha
15011505

15021506
The `validate_legacy` function helps you move from Puppet 3 style validation to Puppet 4 validation without breaking functionality your module's users depend on.
15031507

1504-
Moving to Puppet 4 type validation allows much better defined type checking using [data types](https://docs.puppet.com/puppet/latest/reference/lang_data.html). Many of Puppet 3's `validate_*` functions have surprising holes in their validation. For example, [validate_numeric](#validate_numeric) allows not only numbers, but also arrays of numbers or strings that look like numbers, without giving you any control over the specifics.
1508+
Moving to Puppet 4 type validation allows much better defined type checking using [data types](https://docs.puppet.com/puppet/latest/reference/lang_data.html). Many of Puppet 3's `validate_*` functions have surprising holes in their validation. For example, [validate_numeric](#validate_numeric) allows not only numbers, but also arrays of numbers or strings that look like numbers, without giving you any control over the specifics.
15051509

15061510
For each parameter of your classes and defined types, choose a new Puppet 4 data type to use. In most cases, the new data type allows a different set of values than the original `validate_*` function. The situation then looks like this:
15071511

functions/round.pp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function stdlib::round(
2+
Numeric $input,
3+
) {
4+
if $input >= 0 {
5+
Integer( $input + 0.5 )
6+
} else {
7+
Integer( $input - 0.5 )
8+
}
9+
}

spec/functions/round_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'spec_helper'
2+
3+
describe 'stdlib::round' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params(34.3).and_return(34) }
6+
it { is_expected.to run.with_params(-34.3).and_return(-34) }
7+
it { is_expected.to run.with_params(34.5).and_return(35) }
8+
it { is_expected.to run.with_params(-34.5).and_return(-35) }
9+
it { is_expected.to run.with_params(34.7).and_return(35) }
10+
it { is_expected.to run.with_params(-34.7).and_return(-35) }
11+
end

0 commit comments

Comments
 (0)