Merge pull request #581 from vicinus/master
improve suffix function to support the same feature set as prefix
This commit is contained in:
commit
db2a321434
3 changed files with 23 additions and 16 deletions
|
@ -904,7 +904,12 @@ Removes leading and trailing whitespace from a string or from every string insid
|
||||||
|
|
||||||
#### `suffix`
|
#### `suffix`
|
||||||
|
|
||||||
Applies a suffix to all elements in an array. For example, `suffix(['a','b','c'], 'p')` returns ['ap','bp','cp']. *Type*: rvalue.
|
Applies a suffix to all elements in an array, or to the keys in a hash.
|
||||||
|
For example:
|
||||||
|
* `suffix(['a','b','c'], 'p')` returns ['ap','bp','cp']
|
||||||
|
* `suffix({'a'=>'b','b'=>'c','c'=>'d'}, 'p')` returns {'ap'=>'b','bp'=>'c','cp'=>'d'}.
|
||||||
|
|
||||||
|
*Type*: rvalue.
|
||||||
|
|
||||||
#### `swapcase`
|
#### `swapcase`
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,8 @@
|
||||||
|
|
||||||
module Puppet::Parser::Functions
|
module Puppet::Parser::Functions
|
||||||
newfunction(:suffix, :type => :rvalue, :doc => <<-EOS
|
newfunction(:suffix, :type => :rvalue, :doc => <<-EOS
|
||||||
This function applies a suffix to all elements in an array.
|
This function applies a suffix to all elements in an array, or to the keys
|
||||||
|
in a hash.
|
||||||
|
|
||||||
*Examples:*
|
*Examples:*
|
||||||
|
|
||||||
|
@ -18,10 +19,10 @@ Will return: ['ap','bp','cp']
|
||||||
raise(Puppet::ParseError, "suffix(): Wrong number of arguments " +
|
raise(Puppet::ParseError, "suffix(): Wrong number of arguments " +
|
||||||
"given (#{arguments.size} for 1)") if arguments.size < 1
|
"given (#{arguments.size} for 1)") if arguments.size < 1
|
||||||
|
|
||||||
array = arguments[0]
|
enumerable = arguments[0]
|
||||||
|
|
||||||
unless array.is_a?(Array)
|
unless enumerable.is_a?(Array) or enumerable.is_a?(Hash)
|
||||||
raise Puppet::ParseError, "suffix(): expected first argument to be an Array, got #{array.inspect}"
|
raise Puppet::ParseError, "suffix(): expected first argument to be an Array or a Hash, got #{enumerable.inspect}"
|
||||||
end
|
end
|
||||||
|
|
||||||
suffix = arguments[1] if arguments[1]
|
suffix = arguments[1] if arguments[1]
|
||||||
|
@ -32,11 +33,18 @@ Will return: ['ap','bp','cp']
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if enumerable.is_a?(Array)
|
||||||
# Turn everything into string same as join would do ...
|
# Turn everything into string same as join would do ...
|
||||||
result = array.collect do |i|
|
result = enumerable.collect do |i|
|
||||||
i = i.to_s
|
i = i.to_s
|
||||||
suffix ? i + suffix : i
|
suffix ? i + suffix : i
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
result = Hash[enumerable.map do |k,v|
|
||||||
|
k = k.to_s
|
||||||
|
[ suffix ? k + suffix : k, v ]
|
||||||
|
end]
|
||||||
|
end
|
||||||
|
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,29 +16,23 @@ describe 'suffix' do
|
||||||
it { is_expected.to run.with_params(['one'], 'post').and_return(['onepost']) }
|
it { is_expected.to run.with_params(['one'], 'post').and_return(['onepost']) }
|
||||||
it { is_expected.to run.with_params(['one', 'two', 'three'], 'post').and_return(['onepost', 'twopost', 'threepost']) }
|
it { is_expected.to run.with_params(['one', 'two', 'three'], 'post').and_return(['onepost', 'twopost', 'threepost']) }
|
||||||
it {
|
it {
|
||||||
pending("implementation of Hash functionality matching prefix")
|
|
||||||
is_expected.to run.with_params({}).and_return({})
|
is_expected.to run.with_params({}).and_return({})
|
||||||
}
|
}
|
||||||
it {
|
it {
|
||||||
pending("implementation of Hash functionality matching prefix")
|
|
||||||
is_expected.to run.with_params({ 'key1' => 'value1', 2 => 3}).and_return({ 'key1' => 'value1', '2' => 3 })
|
is_expected.to run.with_params({ 'key1' => 'value1', 2 => 3}).and_return({ 'key1' => 'value1', '2' => 3 })
|
||||||
}
|
}
|
||||||
it {
|
it {
|
||||||
pending("implementation of Hash functionality matching prefix")
|
|
||||||
is_expected.to run.with_params({}, '').and_return({})
|
is_expected.to run.with_params({}, '').and_return({})
|
||||||
}
|
}
|
||||||
it {
|
it {
|
||||||
pending("implementation of Hash functionality matching prefix")
|
|
||||||
is_expected.to run.with_params({ 'key' => 'value' }, '').and_return({ 'key' => 'value' })
|
is_expected.to run.with_params({ 'key' => 'value' }, '').and_return({ 'key' => 'value' })
|
||||||
}
|
}
|
||||||
it {
|
it {
|
||||||
pending("implementation of Hash functionality matching prefix")
|
|
||||||
is_expected.to run.with_params({ 'key' => 'value' }, 'post').and_return({ 'keypost' => 'value' })
|
is_expected.to run.with_params({ 'key' => 'value' }, 'post').and_return({ 'keypost' => 'value' })
|
||||||
}
|
}
|
||||||
it {
|
it {
|
||||||
pending("implementation of Hash functionality matching prefix")
|
|
||||||
is_expected.to run \
|
is_expected.to run \
|
||||||
.with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'pre') \
|
.with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'post') \
|
||||||
.and_return({ 'key1post' => 'value1', 'key2post' => 'value2', 'key3post' => 'value3' })
|
.and_return({ 'key1post' => 'value1', 'key2post' => 'value2', 'key3post' => 'value3' })
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue