Skip to content

Commit

Permalink
clean_blank_lines: Add a new function to clean blank Lines
Browse files Browse the repository at this point in the history
This functions removes all blan lines from a block of text e.g

input = ```
foo

bar

foobar
```

output=```
foo
bar
foobar
```
  • Loading branch information
b4ldr committed Jan 18, 2025
1 parent 1fbe9cc commit e33d86e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
19 changes: 19 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [`extlib::cache_data`](#extlib--cache_data): Retrieves data from a cache file, or creates it with supplied data if the file doesn't exist
* [`extlib::cidr_to_netmask`](#extlib--cidr_to_netmask): Converts an CIDR address of the form 192.168.0.1/24 into its netmask.
* [`extlib::cidr_to_network`](#extlib--cidr_to_network): Converts a CIDR address of the form 2001:DB8::/32 or 192.0.2.0/24 into their network address (also known as net address)
* [`extlib::clean_blank_lines`](#extlib--clean_blank_lines): Remove blank lines from a string
* [`extlib::compare_ip`](#extlib--compare_ip): A function that compares two IP addresses. To be used with the built-in sort() function.
* [`extlib::default_content`](#extlib--default_content): Takes an optional content and an optional template name and returns the contents of a file.
* [`extlib::dir_clean`](#extlib--dir_clean): Take a path and normalise it to its Unix form.
Expand Down Expand Up @@ -175,6 +176,24 @@ Data type: `Variant[Stdlib::IP::Address::V4::CIDR,Stdlib::IP::Address::V6::CIDR]

IPv6 or IPv4 address in CIDR notation

### <a name="extlib--clean_blank_lines"></a>`extlib::clean_blank_lines`

Type: Puppet Language

Remove blank lines from a string

#### `extlib::clean_blank_lines(String[1] $content)`

The extlib::clean_blank_lines function.

Returns: `String[1]` The content with blank lines removed

##### `content`

Data type: `String[1]`

The content to remove blank lines from

### <a name="extlib--compare_ip"></a>`extlib::compare_ip`

Type: Ruby 4.x API
Expand Down
8 changes: 8 additions & 0 deletions functions/remove_blank_lines.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# @summary Remove blank lines from a string
# @param content The content to remove blank lines from
# @return The content with blank lines removed
function extlib::clean_blank_lines (
String[1] $content,
) >> String[1] {
return $content.split("\n").filter |$x| { !$x.empty }.join("\n")
}
20 changes: 20 additions & 0 deletions spec/functions/remove_blank_lines_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require 'spec_helper'

input = <<-TEXT
string_name: "string_value"
int_name: 42
true_name: yes
TEXT
output = <<-TEXT
string_name: "string_value"
int_name: 42
true_name: yes
TEXT

describe 'extlib::remove_blank_lines' do
it { is_expected.to run.with_params(input).and_return(output) }

Check failure on line 19 in spec/functions/remove_blank_lines_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 8 (Ruby 3.2)

extlib::remove_blank_lines Failure/Error: it { is_expected.to run.with_params(input).and_return(output) } Puppet::ParseErrorWithIssue: Unacceptable location. The name 'extlib::clean_blank_lines' is unacceptable in file '/home/runner/work/puppet-extlib/puppet-extlib/spec/fixtures/modules/extlib/functions/remove_blank_lines.pp' (file: /home/runner/work/puppet-extlib/puppet-extlib/spec/fixtures/modules/extlib/functions/remove_blank_lines.pp, line: 4, column: 1)

Check failure on line 19 in spec/functions/remove_blank_lines_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 (Ruby 2.7)

extlib::remove_blank_lines Failure/Error: it { is_expected.to run.with_params(input).and_return(output) } Puppet::ParseErrorWithIssue: Unacceptable location. The name 'extlib::clean_blank_lines' is unacceptable in file '/home/runner/work/puppet-extlib/puppet-extlib/spec/fixtures/modules/extlib/functions/remove_blank_lines.pp' (file: /home/runner/work/puppet-extlib/puppet-extlib/spec/fixtures/modules/extlib/functions/remove_blank_lines.pp, line: 4, column: 1)
end

0 comments on commit e33d86e

Please sign in to comment.