From 5322de95b922be0cea7933cda7d8eba4ac412e15 Mon Sep 17 00:00:00 2001 From: Dominic Go Date: Sun, 29 Sep 2024 07:40:17 +0800 Subject: [PATCH] =?UTF-8?q?=E2=AD=90=EF=B8=8F=20Impl:=20`Dictionary+Helper?= =?UTF-8?q?s`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ios/Temp/Dictionary+Helpers.swift | 53 +++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 ios/Temp/Dictionary+Helpers.swift diff --git a/ios/Temp/Dictionary+Helpers.swift b/ios/Temp/Dictionary+Helpers.swift new file mode 100644 index 00000000..ea1e3ec6 --- /dev/null +++ b/ios/Temp/Dictionary+Helpers.swift @@ -0,0 +1,53 @@ +// +// Dictionary+Helpers.swift +// react-native-ios-modal +// +// Created by Dominic Go on 9/29/24. +// + +import Foundation + + +public extension Dictionary { + + mutating func merge( + withOther otherDict: Self, + shouldOverwriteValues: Bool = true + ){ + self.merge(otherDict) { + shouldOverwriteValues ? $1 : $0; + }; + }; + + mutating func unwrapAndMerge( + withOther otherDict: Dictionary, + shouldOverwriteValues: Bool = true + ) { + + let filtered = otherDict.compactMapValues { $0 }; + + self.merge(filtered) { + shouldOverwriteValues ? $1 : $0; + }; + }; + + func merging( + withOther otherDict: Self, + shouldOverwriteValues: Bool = true + ) -> Self { + self.merging(otherDict) { + shouldOverwriteValues ? $1 : $0; + }; + }; + + func unwrapBeforeMerging( + withOther otherDict: Dictionary, + shouldOverwriteValues: Bool = true + ) -> Self { + let filtered = otherDict.compactMapValues { $0 }; + + return self.merging(filtered) { + shouldOverwriteValues ? $1 : $0; + }; + }; +};