Skip to content

Commit f2c1344

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 f2c1344

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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, ['bar', 'baz'])
8+
# startswith('foObar', ['bar', 'baz']) => false
9+
#
10+
Puppet::Functions.create_function(:startswith) 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 :startswith do
15+
param 'String', :str
16+
param 'Variant[String,Array[String, 1]]', :prefixes
17+
return_type 'Boolean'
18+
end
19+
20+
def startswith(str, prefixes)
21+
str.start_with?(*prefixes)
22+
end
23+
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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', ['bar', 'baz']).and_return(false) }
10+
end

0 commit comments

Comments
 (0)