diff --git a/src/observablecollection/src/Shared/ObservableList.lua b/src/observablecollection/src/Shared/ObservableList.lua index a5e932fecd..beff27bcdf 100644 --- a/src/observablecollection/src/Shared/ObservableList.lua +++ b/src/observablecollection/src/Shared/ObservableList.lua @@ -323,6 +323,26 @@ function ObservableList.Get(self: ObservableList, index: number): T? return self._contents[key] end +--[=[ + Checks if the list contains a value + + :::warning + This can scan the whole list each call; cost is linear in the number of entries. + ::: + + @param value T + @return boolean +]=] +function ObservableList.Contains(self: ObservableList, value: T): boolean + for _, item in self._contents do + if item == value then + return true + end + end + + return false +end + --[=[ Adds the item to the list at the specified index @param item T diff --git a/src/observablecollection/src/Shared/ObservableList.spec.lua b/src/observablecollection/src/Shared/ObservableList.spec.lua index 40b2952807..b1130259f0 100644 --- a/src/observablecollection/src/Shared/ObservableList.spec.lua +++ b/src/observablecollection/src/Shared/ObservableList.spec.lua @@ -35,6 +35,10 @@ describe("ObservableList.new()", function() expect(observableList:Get(-2)).toEqual(nil) end) + it("should check if the list contains a value", function() + expect(observableList:Contains("a")).toEqual(true) + end) + it("should allow false as a value", function() expect(observableList:Get(2)).toEqual(nil) observableList:Add(false)