From 1040a65e6416d4a9c7f758093d4b17f684e1ddbe Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Tue, 25 Apr 2017 09:52:04 +0200 Subject: Rename to Hash::Merge --- META6.json | 26 ++++++++++++++++++++++++-- lib/Hash/Merge.pm6 | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/mergehash.pm6 | 51 --------------------------------------------------- readme.md | 2 ++ t/01-thing.t | 19 +++++++++++++++---- 5 files changed, 92 insertions(+), 57 deletions(-) create mode 100644 lib/Hash/Merge.pm6 delete mode 100644 lib/mergehash.pm6 create mode 100644 readme.md diff --git a/META6.json b/META6.json index 4127794..93f0e07 100644 --- a/META6.json +++ b/META6.json @@ -1,3 +1,25 @@ { - "license": "Artistic-2.0" -} \ No newline at end of file + "perl": "6", + "name": "Hash::Merge", + "version": "1.0.0", + "auth": "github:scriptkitties", + "description": "Module to add deep merge functionality to Hashes", + "license": "Artistic-2.0", + "depends": [ + ], + "provides": { + "Hash::Merge": "lib/Hash/Merge.pm6" + }, + "authors": [ + "Samantha McVey ", + "Patrick Spek " + ], + "tags": [ + "hash", + "utils" + ], + "test-depends": [ + "Test::META" + ], + "source-url": "https://github.com/scriptkitties/p6-Hash-Merge.git" +} diff --git a/lib/Hash/Merge.pm6 b/lib/Hash/Merge.pm6 new file mode 100644 index 0000000..b24f25d --- /dev/null +++ b/lib/Hash/Merge.pm6 @@ -0,0 +1,51 @@ +#! /usr/bin/env false + +use v6.c; +use MONKEY; + +augment class Hash +{ + #| Merges a second hash into the hash the method is called on. Hash given as + #| the argument is not modified. + #| Traverses the full tree, replacing items in the original hash with the + #| hash given in the argument. Does not replace positional elements by default, + #| and instead appends the items from the supplied hash's array to the original + #| hash's array. The object type of positionals is not retained and instead + #| becomes an Array type. + #| Use :no-append-array to replace arrays and positionals instead, which will + #| also retain the original type and not convert to an Array + #| + method merge (%b; Bool:D :$no-append-array = False) + { + hashmerge self, %b, :$no-append-array; + } + + sub hashmerge (%merge-into, %merge-source, Bool:D :$no-append-array) + { + for %merge-source.keys -> $key { + if %merge-into{$key}:exists { + if %merge-source{$key} ~~ Hash { + hashmerge %merge-into{$key}, %merge-source{$key}, :$no-append-array; + } elsif %merge-source{$key} ~~ Positional { + if $no-append-array { + %merge-into{$key} = %merge-source{$key}; + } else { + my @a; + + @a.push: $_ for %merge-into{$key}.list; + @a.push: $_ for %merge-source{$key}.list; + + %merge-into{$key} = @a; + } + } else { + # Non-positionals, so strings or Bools or whatever + %merge-into{$key} = %merge-source{$key}; + } + } else { + %merge-into{$key} = %merge-source{$key}; + } + } + + %merge-into; + } +} diff --git a/lib/mergehash.pm6 b/lib/mergehash.pm6 deleted file mode 100644 index b24f25d..0000000 --- a/lib/mergehash.pm6 +++ /dev/null @@ -1,51 +0,0 @@ -#! /usr/bin/env false - -use v6.c; -use MONKEY; - -augment class Hash -{ - #| Merges a second hash into the hash the method is called on. Hash given as - #| the argument is not modified. - #| Traverses the full tree, replacing items in the original hash with the - #| hash given in the argument. Does not replace positional elements by default, - #| and instead appends the items from the supplied hash's array to the original - #| hash's array. The object type of positionals is not retained and instead - #| becomes an Array type. - #| Use :no-append-array to replace arrays and positionals instead, which will - #| also retain the original type and not convert to an Array - #| - method merge (%b; Bool:D :$no-append-array = False) - { - hashmerge self, %b, :$no-append-array; - } - - sub hashmerge (%merge-into, %merge-source, Bool:D :$no-append-array) - { - for %merge-source.keys -> $key { - if %merge-into{$key}:exists { - if %merge-source{$key} ~~ Hash { - hashmerge %merge-into{$key}, %merge-source{$key}, :$no-append-array; - } elsif %merge-source{$key} ~~ Positional { - if $no-append-array { - %merge-into{$key} = %merge-source{$key}; - } else { - my @a; - - @a.push: $_ for %merge-into{$key}.list; - @a.push: $_ for %merge-source{$key}.list; - - %merge-into{$key} = @a; - } - } else { - # Non-positionals, so strings or Bools or whatever - %merge-into{$key} = %merge-source{$key}; - } - } else { - %merge-into{$key} = %merge-source{$key}; - } - } - - %merge-into; - } -} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..9efc62f --- /dev/null +++ b/readme.md @@ -0,0 +1,2 @@ +# Hash::Merge +Module to add deep merge functionality to Hashes. diff --git a/t/01-thing.t b/t/01-thing.t index 68b092e..4741929 100644 --- a/t/01-thing.t +++ b/t/01-thing.t @@ -1,42 +1,53 @@ -#!/usr/bin/env perl6 +#! /usr/bin/env perl6 + use v6; use lib 'lib'; -use mergehash; use Test; + +use Hash::Merge; + my %a; my %b; + %a = 1; %b = 2; %a = 2; %b = 1; + my %b-orig = %b; my %a-orig = %a; - %a.merge(%b); is-deeply %b, %b-orig; is-deeply %a, {:a(2), :b(1), :y(${:a(1), :z(2)})}; - %a = %a-orig; %b = %b-orig; %a = "orig"; %b = "new"; %a.merge(%b); + is-deeply %a, {Z => 'new', a => 2, b => 1, y => {a => 1, z => 2}}; { my (%z, %y); + %z

= (1,2,3,4); %y

= (5,4,6,7); + %z.merge(%y); + is %z, {y => {p => [1, 2, 3, 4, 5, 4, 6, 7]}}, "appends arrays"; } + { my (%z, %y); + %z

= (1,2,3,4); %y

= (5,4,6,7); + %z.merge(%y, :no-append-array); + is-deeply %z, ${:y(${:p($(5, 4, 6, 7))})}, "no-append-array (replaces the instead)"; } -- cgit v1.1