From f936d8e2b20e4f409765f093c5c4f780f863722b Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Sat, 5 Oct 2019 21:51:39 +0200 Subject: [PATCH] Added helper functions for Arrays and Hashtables --- lib/core/tools/Add-IcingaHashtableItem.psm1 | 24 +++++++++++++++++++ lib/core/tools/Pop-IcingaArrayListItem.psm1 | 19 +++++++++++++++ .../tools/Remove-IcingaHashtableItem.psm1 | 15 ++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 lib/core/tools/Add-IcingaHashtableItem.psm1 create mode 100644 lib/core/tools/Pop-IcingaArrayListItem.psm1 create mode 100644 lib/core/tools/Remove-IcingaHashtableItem.psm1 diff --git a/lib/core/tools/Add-IcingaHashtableItem.psm1 b/lib/core/tools/Add-IcingaHashtableItem.psm1 new file mode 100644 index 0000000..49a2b0e --- /dev/null +++ b/lib/core/tools/Add-IcingaHashtableItem.psm1 @@ -0,0 +1,24 @@ +function Add-IcingaHashtableItem() +{ + param( + $Hashtable, + $Key, + $Value, + [switch]$Override + ); + + if ($null -eq $Hashtable) { + return $FALSE; + } + + if ($Hashtable.ContainsKey($Key) -eq $FALSE) { + $Hashtable.Add($Key, $Value); + return $TRUE; + } else { + if ($Override) { + $Hashtable[$Key] = $Value; + return $TRUE; + } + } + return $FALSE; +} diff --git a/lib/core/tools/Pop-IcingaArrayListItem.psm1 b/lib/core/tools/Pop-IcingaArrayListItem.psm1 new file mode 100644 index 0000000..b6ed815 --- /dev/null +++ b/lib/core/tools/Pop-IcingaArrayListItem.psm1 @@ -0,0 +1,19 @@ +function Pop-IcingaArrayListItem() +{ + param( + [System.Collections.ArrayList]$Array + ); + + if ($null -eq $Array) { + return $null; + } + + if ($Array.Count -eq 0) { + return $null; + } + + $Content = $Array[0]; + $Array.RemoveAt(0); + + return $Content; +} diff --git a/lib/core/tools/Remove-IcingaHashtableItem.psm1 b/lib/core/tools/Remove-IcingaHashtableItem.psm1 new file mode 100644 index 0000000..8be4987 --- /dev/null +++ b/lib/core/tools/Remove-IcingaHashtableItem.psm1 @@ -0,0 +1,15 @@ +function Remove-IcingaHashtableItem() +{ + param( + $Hashtable, + $Key + ); + + if ($null -eq $Hashtable) { + return; + } + + if ($Hashtable.ContainsKey($Key)) { + $Hashtable.Remove($Key); + } +}