Skip to content

Commit 4c17c86

Browse files
committed
Add startswith and endswith function
This is helpful in places where you need to use variables in the string which needs to be compare e.g. something like this: ```puppet $test.startswith("part${string}01") ```
1 parent ade894a commit 4c17c86

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

lib/puppet/functions/endswith.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# @summary
2+
# Returns true if str ends with one of the prefixes given. Each of the prefixes should be a String.
3+
#
4+
# @example endswith(String, String)
5+
# endswith('foobar', 'bar') => true
6+
#
7+
# @example startswith(String, ['bar', 'baz'])
8+
# endswith('foobar', ['foo', 'baz']) => false
9+
#
10+
Puppet::Functions.create_function(:endswith) do
11+
# @param str The string to check
12+
# @param prefixes The pattern or string to check against.
13+
# @return [Boolean] True or False
14+
dispatch :endswith do
15+
param 'String', :str
16+
param 'Variant[String,Array[String, 1]]', :prefixes
17+
return_type 'Boolean'
18+
end
19+
20+
def endswith(str, prefixes)
21+
str.end_with?(*prefixes)
22+
end
23+
end

lib/puppet/functions/startswith.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# @summary
2+
# Returns true if str starts with one of the prefixes given. Each of the prefixes should be a String or a Regexp.
3+
#
4+
# @example startswith(String, String)
5+
# startswith('foobar', 'foo') => true
6+
#
7+
# @example startswith(String, Regexp)
8+
# startswith('FOObar', %r{foo}i) => true
9+
#
10+
# @example startswith(String, ['bar', 'baz'])
11+
# startswith('foObar', ['bar', 'baz']) => false
12+
#
13+
Puppet::Functions.create_function(:startswith) do
14+
# @param str The string to check
15+
# @param prefixes The pattern or string to check against.
16+
# @return [Boolean] True or False
17+
dispatch :startswith do
18+
param 'String', :str
19+
param 'Variant[String,Regexp,Array[Variant[String,Regexp], 1]]', :prefixes
20+
return_type 'Boolean'
21+
end
22+
23+
def startswith(str, prefixes)
24+
str.start_with?(*prefixes)
25+
end
26+
end

spec/functions/endswith_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'spec_helper'
2+
3+
describe 'endswith' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{expects 2 arguments, got none}i) }
6+
it { is_expected.to run.with_params('').and_raise_error(ArgumentError, %r{expects 2 arguments, got 1}) }
7+
8+
it { is_expected.to run.with_params('foobar', 'bar').and_return(true) }
9+
it { is_expected.to run.with_params('foobar', ['foo', 'baz']).and_return(false) }
10+
end

spec/functions/startswith_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 'startswith' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{expects 2 arguments, got none}i) }
6+
it { is_expected.to run.with_params('').and_raise_error(ArgumentError, %r{expects 2 arguments, got 1}) }
7+
8+
it { is_expected.to run.with_params('foobar', 'foo').and_return(true) }
9+
it { is_expected.to run.with_params('FOObar', %r{foo}i).and_return(true) }
10+
it { is_expected.to run.with_params('foObar', ['bar', 'baz']).and_return(false) }
11+
end

0 commit comments

Comments
 (0)