From 0d0734ac9845ef32c55ebf4c3185ba85065c5940 Mon Sep 17 00:00:00 2001 From: David Lu Date: Fri, 27 May 2016 08:35:55 -0700 Subject: Added duplicated trigger validation (#3124) --- utils/utils.go | 21 +++++++++++++++++++++ utils/utils_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 utils/utils.go create mode 100644 utils/utils_test.go (limited to 'utils') diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 000000000..f826c65a0 --- /dev/null +++ b/utils/utils.go @@ -0,0 +1,21 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package utils + +func StringArrayIntersection(arr1, arr2 []string) []string { + arrMap := map[string]bool{} + result := []string{} + + for _, value := range arr1 { + arrMap[value] = true + } + + for _, value := range arr2 { + if arrMap[value] { + result = append(result, value) + } + } + + return result +} diff --git a/utils/utils_test.go b/utils/utils_test.go new file mode 100644 index 000000000..41e995e63 --- /dev/null +++ b/utils/utils_test.go @@ -0,0 +1,30 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package utils + +import ( + "testing" +) + +func TestStringArrayIntersection(t *testing.T) { + a := []string{ + "abc", + "def", + "ghi", + } + b := []string{ + "jkl", + } + c := []string{ + "def", + } + + if len(StringArrayIntersection(a, b)) != 0 { + t.Fatal("should be 0") + } + + if len(StringArrayIntersection(a, c)) != 1 { + t.Fatal("should be 1") + } +} -- cgit v1.2.3-1-g7c22