Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/observablecollection/src/Shared/ObservableList.lua
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,26 @@ function ObservableList.Get<T>(self: ObservableList<T>, 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<T>(self: ObservableList<T>, 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
Expand Down
4 changes: 4 additions & 0 deletions src/observablecollection/src/Shared/ObservableList.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading