Skip to content

Commit f5b650e

Browse files
author
Cocker Koch
committed
add Function extname()
1 parent 32b8bd9 commit f5b650e

3 files changed

Lines changed: 60 additions & 3 deletions

File tree

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,21 @@ userlist:
13681368
ensure_resources('user', hiera_hash('userlist'), {'ensure' => 'present'})
13691369
```
13701370
1371+
#### `stdlib::extname`
1372+
1373+
Returns the Extension (the Portion of Filename in Path starting from the last Period).
1374+
1375+
Example usage:
1376+
1377+
```puppet
1378+
stdlib::extname('test.rb') => '.rb'
1379+
stdlib::extname('a/b/d/test.rb') => '.rb'
1380+
stdlib::extname('test') => ''
1381+
stdlib::extname('.profile') => ''
1382+
```
1383+
1384+
*Type*: rvalue.
1385+
13711386
#### `fact`
13721387
13731388
Return the value of a given fact. Supports the use of dot-notation for referring to structured facts. If a fact requested does not exist, returns Undef.
@@ -1969,12 +1984,12 @@ Arguments: A numeric or a string representing a number.
19691984
19701985
#### `num2bool`
19711986
1972-
Converts a number, or a string representation of a number, into a true Boolean.
1987+
Converts a number, or a string representation of a number, into a true Boolean.
19731988
Zero or anything non-numeric becomes `false`.
19741989
Numbers greater than zero become `true`.
19751990
19761991
Since Puppet 5.0.0, the same can be achieved with the Puppet type system.
1977-
See the [`Boolean.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-boolean)
1992+
See the [`Boolean.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-boolean)
19781993
function in Puppet for the many available type conversions.
19791994
19801995
Boolean(0) # false
@@ -2236,7 +2251,7 @@ Replaces consecutive repeats (such as 'aaaa') in a string with a single characte
22362251
Converts certain strings to a Boolean. This attempts to convert strings that contain the values '1', 'true', 't', 'y', or 'yes' to `true`. Strings that contain values '0', 'false', 'f', 'n', or 'no', or that are an empty string or undefined are converted to `false`. Any other value causes an error. These checks are case insensitive.
22372252
22382253
Since Puppet 5.0.0, the same can be achieved with the Puppet type system.
2239-
See the [`Boolean.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-boolean)
2254+
See the [`Boolean.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-boolean)
22402255
function in Puppet for the many available type conversions.
22412256
22422257
Boolean('false'), Boolean('n'), Boolean('no') # all false
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Returns the Extension (the Portion of Filename in Path starting from the
2+
# last Period).
3+
#
4+
# If Path is a Dotfile, or starts with a Period, then the starting Dot is not
5+
# dealt with the Start of the Extension.
6+
#
7+
# An empty String will also be returned, when the Period is the last Character
8+
# in Path.
9+
10+
Puppet::Functions.create_function(:'stdlib::extname') do
11+
# @param filename The Filename
12+
# @return [String] The Extension starting from the last Period
13+
# @example Determining the Extension of a Filename
14+
# stdlib::extname('test.rb') => '.rb'
15+
# stdlib::extname('a/b/d/test.rb') => '.rb'
16+
# stdlib::extname('test') => ''
17+
# stdlib::extname('.profile') => ''
18+
dispatch :extname do
19+
param 'String', :filename
20+
return_type 'String'
21+
end
22+
23+
def extname(filename)
24+
File.extname(filename)
25+
end
26+
end

spec/functions/extname_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'spec_helper'
2+
3+
describe 'stdlib::extname' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'stdlib::extname' expects 1 argument, got none}) }
6+
it { is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, %r{'stdlib::extname' expects 1 argument, got 2}) }
7+
it { is_expected.to run.with_params([]).and_raise_error(ArgumentError, %r{'stdlib::extname' parameter 'filename' expects a String value, got Array}) }
8+
it { is_expected.to run.with_params('test.rb').and_return('.rb') }
9+
it { is_expected.to run.with_params('a/b/d/test.rb').and_return('.rb') }
10+
it { is_expected.to run.with_params('test').and_return('') }
11+
it { is_expected.to run.with_params('.profile').and_return('') }
12+
13+
context 'with UTF8 and double byte characters' do
14+
it { is_expected.to run.with_params('file_√ạĺűē/竹.rb').and_return('.rb') }
15+
end
16+
end

0 commit comments

Comments
 (0)