mirror of
https://github.com/minetest/minetest.git
synced 2024-11-23 08:03:45 +01:00
Add table.keyof()
(#14910)
This commit is contained in:
parent
a677d33bdf
commit
dc7a7a0ed9
@ -20,7 +20,7 @@ read_globals = {
|
|||||||
"PerlinNoise", "PerlinNoiseMap",
|
"PerlinNoise", "PerlinNoiseMap",
|
||||||
|
|
||||||
string = {fields = {"split", "trim"}},
|
string = {fields = {"split", "trim"}},
|
||||||
table = {fields = {"copy", "getn", "indexof", "insert_all"}},
|
table = {fields = {"copy", "getn", "indexof", "keyof", "insert_all"}},
|
||||||
math = {fields = {"hypot", "round"}},
|
math = {fields = {"hypot", "round"}},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,6 +205,16 @@ function table.indexof(list, val)
|
|||||||
return -1
|
return -1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
function table.keyof(tb, val)
|
||||||
|
for k, v in pairs(tb) do
|
||||||
|
if v == val then
|
||||||
|
return k
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
function string:trim()
|
function string:trim()
|
||||||
return self:match("^%s*(.-)%s*$")
|
return self:match("^%s*(.-)%s*$")
|
||||||
|
@ -166,6 +166,16 @@ describe("table", function()
|
|||||||
it("indexof()", function()
|
it("indexof()", function()
|
||||||
assert.equal(1, table.indexof({"foo", "bar"}, "foo"))
|
assert.equal(1, table.indexof({"foo", "bar"}, "foo"))
|
||||||
assert.equal(-1, table.indexof({"foo", "bar"}, "baz"))
|
assert.equal(-1, table.indexof({"foo", "bar"}, "baz"))
|
||||||
|
assert.equal(-1, table.indexof({[2] = "foo", [3] = "bar"}, "foo"))
|
||||||
|
assert.equal(-1, table.indexof({[1] = "foo", [3] = "bar"}, "bar"))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("keyof()", function()
|
||||||
|
assert.equal("a", table.keyof({a = "foo", b = "bar"}, "foo"))
|
||||||
|
assert.equal(nil, table.keyof({a = "foo", b = "bar"}, "baz"))
|
||||||
|
assert.equal(1, table.keyof({"foo", "bar"}, "foo"))
|
||||||
|
assert.equal(2, table.keyof({[2] = "foo", [3] = "bar"}, "foo"))
|
||||||
|
assert.equal(3, table.keyof({[1] = "foo", [3] = "bar"}, "bar"))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
@ -4025,6 +4025,10 @@ Helper functions
|
|||||||
the value `val` in the table `list`. Non-numerical indices are ignored.
|
the value `val` in the table `list`. Non-numerical indices are ignored.
|
||||||
If `val` could not be found, `-1` is returned. `list` must not have
|
If `val` could not be found, `-1` is returned. `list` must not have
|
||||||
negative indices.
|
negative indices.
|
||||||
|
* `table.keyof(table, val)`: returns the key containing
|
||||||
|
the value `val` in the table `table`. If multiple keys contain `val`,
|
||||||
|
it is unspecified which key will be returned.
|
||||||
|
If `val` could not be found, `nil` is returned.
|
||||||
* `table.insert_all(table, other_table)`:
|
* `table.insert_all(table, other_table)`:
|
||||||
* Appends all values in `other_table` to `table` - uses `#table + 1` to
|
* Appends all values in `other_table` to `table` - uses `#table + 1` to
|
||||||
find new indices.
|
find new indices.
|
||||||
|
Loading…
Reference in New Issue
Block a user