Skip to content

Commit b109787

Browse files
committed
Merge pull request #514 from DavidS/add-convert_base
Adds a convert_base function, which can convert numbers between bases
2 parents 1bed010 + 2d4f5aa commit b109787

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

README.markdown

+6
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ Appends the contents of multiple arrays onto the first array given. For example:
155155
* `concat(['1','2','3'],'4',['5','6','7'])` returns ['1','2','3','4','5','6','7'].
156156
*Type*: rvalue.
157157

158+
#### `convert_base`
159+
160+
Converts a given integer or base 10 string representing an integer to a specified base, as a string. For example:
161+
* `convert_base(5, 2)` results in: '101'
162+
* `convert_base('254', '16')` results in: 'fe'
163+
158164
#### `count`
159165

160166
If called with only an array, it counts the number of elements that are **not** nil/undef. If called with a second argument, counts the number of elements in an array that matches the second argument. *Type*: rvalue.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Puppet::Parser::Functions
2+
3+
newfunction(:convert_base, :type => :rvalue, :arity => 2, :doc => <<-'ENDHEREDOC') do |args|
4+
5+
Converts a given integer or base 10 string representing an integer to a specified base, as a string.
6+
7+
Usage:
8+
9+
$binary_repr = convert_base(5, 2) # $binary_repr is now set to "101"
10+
$hex_repr = convert_base("254", "16") # $hex_repr is now set to "fe"
11+
12+
ENDHEREDOC
13+
14+
raise Puppet::ParseError, ("convert_base(): First argument must be either a string or an integer") unless (args[0].is_a?(Integer) or args[0].is_a?(String))
15+
raise Puppet::ParseError, ("convert_base(): Second argument must be either a string or an integer") unless (args[1].is_a?(Integer) or args[1].is_a?(String))
16+
17+
if args[0].is_a?(String)
18+
raise Puppet::ParseError, ("convert_base(): First argument must be an integer or a string corresponding to an integer in base 10") unless args[0] =~ /^[0-9]+$/
19+
end
20+
21+
if args[1].is_a?(String)
22+
raise Puppet::ParseError, ("convert_base(): First argument must be an integer or a string corresponding to an integer in base 10") unless args[1] =~ /^[0-9]+$/
23+
end
24+
25+
number_to_convert = args[0]
26+
new_base = args[1]
27+
28+
number_to_convert = number_to_convert.to_i()
29+
new_base = new_base.to_i()
30+
31+
raise Puppet::ParseError, ("convert_base(): base must be at least 2 and must not be greater than 36") unless new_base >= 2 and new_base <= 36
32+
33+
return number_to_convert.to_s(new_base)
34+
end
35+
end

spec/functions/convert_base_spec.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'spec_helper'
2+
3+
describe 'convert_base' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
6+
it { is_expected.to run.with_params("asdf").and_raise_error(ArgumentError) }
7+
it { is_expected.to run.with_params("asdf","moo","cow").and_raise_error(ArgumentError) }
8+
it { is_expected.to run.with_params(["1"],"2").and_raise_error(Puppet::ParseError, /argument must be either a string or an integer/) }
9+
it { is_expected.to run.with_params("1",["2"]).and_raise_error(Puppet::ParseError, /argument must be either a string or an integer/) }
10+
it { is_expected.to run.with_params("1",1).and_raise_error(Puppet::ParseError, /base must be at least 2 and must not be greater than 36/) }
11+
it { is_expected.to run.with_params("1",37).and_raise_error(Puppet::ParseError, /base must be at least 2 and must not be greater than 36/) }
12+
13+
it "should raise a ParseError if argument 1 is a string that does not correspond to an integer in base 10" do
14+
is_expected.to run.with_params("ten",6).and_raise_error(Puppet::ParseError, /argument must be an integer or a string corresponding to an integer in base 10/)
15+
end
16+
17+
it "should raise a ParseError if argument 2 is a string and does not correspond to an integer in base 10" do
18+
is_expected.to run.with_params(100,"hex").and_raise_error(Puppet::ParseError, /argument must be an integer or a string corresponding to an integer in base 10/)
19+
end
20+
21+
it { is_expected.to run.with_params("11",'16').and_return('b') }
22+
it { is_expected.to run.with_params("35",'36').and_return('z') }
23+
it { is_expected.to run.with_params(5, 2).and_return('101') }
24+
end

0 commit comments

Comments
 (0)