Skip to content

Commit a2abfb9

Browse files
committed
Merge pull request #155 from AlexCline/feature/master/array_comparison_functions
(#20684) Add array comparison functions, difference, intersection and union
2 parents dad3a29 + 737aa31 commit a2abfb9

File tree

7 files changed

+194
-0
lines changed

7 files changed

+194
-0
lines changed

README.markdown

+33
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,18 @@ Would return: ['a','c']
195195

196196
- *Type*: rvalue
197197

198+
difference
199+
----------
200+
This function returns the difference between two arrays.
201+
The returned array is a copy of the original array, removing any items that
202+
also appear in the second array.
203+
204+
*Examples:*
205+
206+
difference(["a","b","c"],["b","c","d"])
207+
208+
Would return: ["a"]
209+
198210
dirname
199211
-------
200212
Returns the `dirname` of a path.
@@ -416,6 +428,16 @@ Would return: {'a'=>1,'b'=>2,'c'=>3}
416428

417429
- *Type*: rvalue
418430

431+
intersection
432+
-----------
433+
This function returns an array an intersection of two.
434+
435+
*Examples:*
436+
437+
intersection(["a","b","c"],["b","c","d"])
438+
439+
Would return: ["b","c"]
440+
419441
is_array
420442
--------
421443
Returns true if the variable passed to this function is an array.
@@ -868,6 +890,17 @@ Returns the type when passed a variable. Type can be one of:
868890

869891
- *Type*: rvalue
870892

893+
union
894+
-----
895+
This function returns a union of two arrays.
896+
897+
*Examples:*
898+
899+
union(["a","b","c"],["b","c","d"])
900+
901+
Would return: ["a","b","c","d"]
902+
903+
871904
unique
872905
------
873906
This function will remove duplicates from strings and arrays.
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#
2+
# difference.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:difference, :type => :rvalue, :doc => <<-EOS
7+
This function returns the difference between two arrays.
8+
The returned array is a copy of the original array, removing any items that
9+
also appear in the second array.
10+
11+
*Examples:*
12+
13+
difference(["a","b","c"],["b","c","d"])
14+
15+
Would return: ["a"]
16+
EOS
17+
) do |arguments|
18+
19+
# Two arguments are required
20+
raise(Puppet::ParseError, "difference(): Wrong number of arguments " +
21+
"given (#{arguments.size} for 2)") if arguments.size != 2
22+
23+
first = arguments[0]
24+
second = arguments[1]
25+
26+
unless first.is_a?(Array) && second.is_a?(Array)
27+
raise(Puppet::ParseError, 'difference(): Requires 2 arrays')
28+
end
29+
30+
result = first - second
31+
32+
return result
33+
end
34+
end
35+
36+
# vim: set ts=2 sw=2 et :
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# intersection.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:intersection, :type => :rvalue, :doc => <<-EOS
7+
This function returns an array an intersection of two.
8+
9+
*Examples:*
10+
11+
intersection(["a","b","c"],["b","c","d"])
12+
13+
Would return: ["b","c"]
14+
EOS
15+
) do |arguments|
16+
17+
# Two arguments are required
18+
raise(Puppet::ParseError, "intersection(): Wrong number of arguments " +
19+
"given (#{arguments.size} for 2)") if arguments.size != 2
20+
21+
first = arguments[0]
22+
second = arguments[1]
23+
24+
unless first.is_a?(Array) && second.is_a?(Array)
25+
raise(Puppet::ParseError, 'intersection(): Requires 2 arrays')
26+
end
27+
28+
result = first & second
29+
30+
return result
31+
end
32+
end
33+
34+
# vim: set ts=2 sw=2 et :

lib/puppet/parser/functions/union.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# union.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:union, :type => :rvalue, :doc => <<-EOS
7+
This function returns a union of two arrays.
8+
9+
*Examples:*
10+
11+
union(["a","b","c"],["b","c","d"])
12+
13+
Would return: ["a","b","c","d"]
14+
EOS
15+
) do |arguments|
16+
17+
# Two arguments are required
18+
raise(Puppet::ParseError, "union(): Wrong number of arguments " +
19+
"given (#{arguments.size} for 2)") if arguments.size != 2
20+
21+
first = arguments[0]
22+
second = arguments[1]
23+
24+
unless first.is_a?(Array) && second.is_a?(Array)
25+
raise(Puppet::ParseError, 'union(): Requires 2 arrays')
26+
end
27+
28+
result = first | second
29+
30+
return result
31+
end
32+
end
33+
34+
# vim: set ts=2 sw=2 et :
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
describe "the difference function" do
5+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6+
7+
it "should exist" do
8+
Puppet::Parser::Functions.function("difference").should == "function_difference"
9+
end
10+
11+
it "should raise a ParseError if there are fewer than 2 arguments" do
12+
lambda { scope.function_difference([]) }.should( raise_error(Puppet::ParseError) )
13+
end
14+
15+
it "should return the difference between two arrays" do
16+
result = scope.function_difference([["a","b","c"],["b","c","d"]])
17+
result.should(eq(["a"]))
18+
end
19+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
describe "the intersection function" do
5+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6+
7+
it "should exist" do
8+
Puppet::Parser::Functions.function("intersection").should == "function_intersection"
9+
end
10+
11+
it "should raise a ParseError if there are fewer than 2 arguments" do
12+
lambda { scope.function_intersection([]) }.should( raise_error(Puppet::ParseError) )
13+
end
14+
15+
it "should return the intersection of two arrays" do
16+
result = scope.function_intersection([["a","b","c"],["b","c","d"]])
17+
result.should(eq(["b","c"]))
18+
end
19+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
describe "the union function" do
5+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6+
7+
it "should exist" do
8+
Puppet::Parser::Functions.function("union").should == "function_union"
9+
end
10+
11+
it "should raise a ParseError if there are fewer than 2 arguments" do
12+
lambda { scope.function_union([]) }.should( raise_error(Puppet::ParseError) )
13+
end
14+
15+
it "should join two arrays together" do
16+
result = scope.function_union([["a","b","c"],["b","c","d"]])
17+
result.should(eq(["a","b","c","d"]))
18+
end
19+
end

0 commit comments

Comments
 (0)