Dictionary
Handles dictionary-like operations.
Dictionaries
are unordered tables with key-value pairs.
Functions
count
Dictionary.
count
(
dictionary:
{
[
Key
]
:
Value
}
,
predicate:
(
(
Value
,
Key
)
→
boolean
)
?
) →
number
Returns the number of pairs that match the predicate
.
If the predicate
is not given, all pairs will be considered a match.
Dictionary.count({ a = 1, b = 2, c = 3 }).count()
-- 3
Dictionary.count({ a = 1, b = 2, c = 3 }, function(value, key)
return value % 2 == 0
end)
-- 1
equals
Dictionary.
equals
(
dictionaryA:
{
[
Key
]
:
Value
}
,
dictionaryB:
any
) →
boolean
Returns true if both Dictionaries have value equality.
info
If you only require reference equal, use the ==
operator.
local dictionary1 = { a = 1, b = 2, c = 3 }
local dictionary2 = { a = 1, b = 2, c = 3 }
Dictionary.equals(dictionary1, dictionary2)
-- true
every
Dictionary.
every
(
list:
{
[
Key
]
:
Value
}
,
predicate:
(
Value
,
Key
)
→
boolean
) →
boolean
Returns true if predicate
returns true for all entries in the Dictionary.
Dictionary.every({ a = 1, b = 2, c = 3 }, function(value, key)
return value < 10
end)
-- true
filter
Dictionary.
filter
(
dictionary:
{
[
Key
]
:
Value
}
,
predicate:
(
(
Value
,
Key
)
→
boolean
)
) →
{
[
Key
]
:
Value
}
Returns a Dictionary of only entries for which the predicate
function returns true.
Dictionary.filter({ a = 1, b = 2, c = 3, d = 4 }, function(value, key)
return value % 2 == 0
end)
-- { b = 2, d = 4 }
filterNot
Dictionary.
filterNot
(
dictionary:
{
[
Key
]
:
Value
}
,
predicate:
(
Value
,
Key
)
→
boolean
) →
{
[
Key
]
:
Value
}
Returns a Dictionary of only entries for which the predicate
function returns false.
Dictionary.filterNot({ a = 1, b = 2, c = 3, d = 4 }, function(value, key)
return value % 2 == 0
end)
-- { a = 1, c = 3 }
find
Dictionary.
find
(
dictionary:
{
[
Key
]
:
Value
}
,
predicate:
(
Value
,
Key
)
→
boolean
,
notSetValue:
Value?
) →
Value?
Returns the first value for which the predicate
returns true.
Dictionary.find({ a = 1, b = 2, c = 3 }, function(value, key)
return value % 2 == 0
end)
-- 2
findKey
Dictionary.
findKey
(
dictionary:
{
[
Key
]
:
Value
}
,
predicate:
(
Value
,
Key
)
→
boolean
,
notSetValue:
Key?
) →
Key?
Returns the first key for which the predicate
returns true.
Dictionary.findKey({ a = 1, b = 2, c = 3 }, function(value, key)
return value % 2 == 0
end)
-- "b"
findPair
Dictionary.
findPair
(
list:
{
[
Key
]
:
Value
}
,
predicate:
(
(
Value
,
Key
)
→
boolean
)
) →
(
Key?
,
Value?
)
Returns the first (key, value) pair for which the predicate
returns true.
Dictionary.findPair({ a = 1, b = 2 c = 3, d = 4 }, function(value, key)
return value % 2 == 0
end)
-- ( "b", 2 )
flatten
Dictionary.
flatten
(
dictionary:
{
[
any
]
:
any
}
,
depth:
number?
) →
{
[
any
]
:
any
}
Returns a flattened dictionary by combining keys of the lowest depth.
If provided depth
, the flattening will early-out.
Dictionary.flatten({
foo = 1,
foobar = {
bar = 2,
baz = {
etc = 3,
},
},
})
-- { foo = 1, bar = 2, etc = 3 }
flip
Dictionary.
flip
(
dictionary:
{
[
Key
]
:
Value
}
) →
{
[
Value
]
:
Key
}
Returns a new map where keys and values are flipped.
Dictionary.flip({ a = "x", b = "y", c = "z" )
-- { x = "a", y = "b", z = "c" }
forEach
Dictionary.
forEach
(
dictionary:
{
[
Key
]
:
Value
}
,
sideEffect:
(
Value
,
Key
)
→
boolean
) →
number
While the Dictionary is iterated, the sideEffect
is executed for every entry.
If any call of the sideEffect
returns false
, the iteration will stop.
Returns the number of entries iterated (including the last iteration which returned false).
Dictionary.forEach({ a = 10, b = 20, c = -10, d = 30 }, function(value, key)
return value > 0
end)
-- 3
get
Dictionary.
get
(
dictionary:
{
[
Key
]
:
Value
}
,
key:
Key
,
notSetValue:
Value?
) →
Value?
Returns the value at the given key, otherwise returns notSetValue
if the given value is not found.
Dictionary.get({ a = 1, b = 2, c = 3 }, "a")
-- 1
Dictionary.get({ a = 1, b = 2, c = 3 }, "foobar", "default")
-- "default"
getIn
Dictionary.
getIn
(
dictionary:
{
[
any
]
:
any
}
,
keyPath:
{
any
}
,
notSetValue:
Value?
) →
Value?
Returns the value if found by following a path of keys, otherwise returns notSetValue
if the given value is nil.
local dictionary = { a = { b = "c" } }
Dictionary.getIn(dictionary), { "a", "b" })
-- "c"
has
Dictionary.
has
(
dictionary:
{
[
Key
]
:
Value
}
,
key:
Key
) →
boolean
Returns true if the key exists within the Dictionary.
Dictionary.has({ a = 1, b = 2, c = 3 }, "b")
-- true
hasIn
Dictionary.
hasIn
(
dictionary:
{
[
Key
]
:
Value
}
,
keyPath:
{
any
}
) →
boolean
Returns true if the value is found by following a path of keys.
local dictionary = { a = { b = "c" } }
Dictionary.hasIn(dictionary, { "a", "b" })
-- true
includes
Dictionary.
includes
(
dictionary:
{
[
Key
]
:
Value
}
,
value:
Value
) →
boolean
Returns true if the value is found within the Dictionary.
Dictionary.includes({ a = 1, b = 2, c = 3 }, 2)
-- true
joinAsString
Dictionary.
joinAsString
(
dictionary:
{
[
Key
]
:
Value
}
,
separator:
string?
) →
string
Joins values together as a string, inserting a separator between each.
The default separator is ","
.
Dictionary.joinAsString({ a = 1, b = 2, c = 3 }, ", ")
-- "a=1, b=2, c=3"
keys
Dictionary.
keys
(
dictionary:
{
[
Key
]
:
Value
}
) →
{
Key
}
Returns a list of keys. Order is undefined.
Dictionary.keys({ a = 1, b = 2, c = 3 })
-- { "a", "b", "c" }
map
Dictionary.
map
(
dictionary:
{
[
Key
]
:
Value
}
,
mapper:
(
Value
,
Key
)
→
(
NewValue
,
NewKey
)
) →
{
[
NewKey
]
:
NewValue?
}
Returns a new Dictionary with keys and values passed through a mapper
function.
Returning a second value in the mapper function will reassign the key.
Dictionary.map({ a = 1, b = 2, c = 3 }, function(value, key)
return value * 10, key
end)
-- { a = 10, b = 20, c = 30 }
max
Dictionary.
max
(
dictionary:
{
[
Key
]
:
Value
}
,
comparator:
(
(
Value
,
Value
)
→
boolean
)
?
) →
(
Value
,
Key?
)
Returns the maximum value and key in this dictionary. If any values are comparatively equivalent, the first one found will be returned. (Order is undefined.)
The comparator
is used in the same way as table.sort
. If it is not provided, the default comparator is >
.
Dictionary.max({ a = 10, b = 200, c = 30 })
-- ( 200, "c" )
Dictionary.max({ alice = child, bertram = teen, charlie = adult }, function(personA, personB)
return personA.age > personB.age
end)
-- ( adult, "charlie" )
merge
Dictionary.
merge
(
...:
{
[
Key
]
:
Value
}
) →
{
[
any
]
:
any
}
Returns a merged result of all given dictionaries.
If Freeze.None
is a value assigned to a key, it will delete that key from the resulting dictionary.
Dictionary.merge({ a = 10, b = 20 }, { c = 30 })
-- { a = 10, b = 20, c = 30 }
Dictionary.merge({ a = 10, b = 20 }, { c = 30, d = 40 }, { b = Freeze.None })
-- { a = 10, c = 30, d = 40 }
mergeIn
Dictionary.
mergeIn
(
dictionary:
{
[
any
]
:
any
}
,
keyPath:
{
any
}
,
...:
any
) →
{
[
any
]
:
any
}
A combination of updateIn
and merge
, returning a new dictionary,
but performing a merge at the point arrived by following the key path.
local dictionary = {
persons = {
alice = {
age = 10,
},
},
}
Dictionary.mergeIn(dictionary, { "persons", "alice" }, { age = 11 })
-- { persons = { alice = { age = 11 } } }
min
Dictionary.
min
(
dictionary:
{
[
Key
]
:
Value
}
,
comparator:
(
(
Value
,
Value
)
→
boolean
)
?
) →
(
Value
,
Key?
)
Returns the minium value and key in this dictionary. If any values are comparatively equivalent, the first one found will be returned. (Order is undefined.)
The comparator
is used in the same way as table.sort
. If it is not provided, the default comparator is >
.
Dictionary.min({ a = 10, b = 200, c = 30 })
-- ( 10, "a" )
Dictionary.min({ alice = child, bertram = teen, charlie = adult }, function(personA, personB)
return personA.age > personB.age
end)
-- ( child, "alice" )
remove
Dictionary.
remove
(
dictionary:
{
[
Key
]
:
Value
}
,
...:
Key
) →
{
[
Key
]
:
Value
}
Returns a Dictionary which excludes the given keys
.
Dictionary.remove({ a = 10, b = 20, c = 30 }).remove("c")
-- { a = 10, b = 20 }
Dictionary.remove({ a = 10, b = 20, c = 30 }).remove("b", "c")
-- { a = 10 }
removeIn
Dictionary.
removeIn
(
dictionary:
{
[
any
]
:
any
}
,
keyPath:
{
any
}
) →
{
[
any
]
:
any
}
Returns a new map having removed the value at this keyPath
.
Will create a new path if it does not exist.
Dictionary.removeIn({ a = { b = { "c" } } }, { "a", "b" })
-- { a = {} }
removeValue
Dictionary.
removeValue
(
dictionary:
{
[
Key
]
:
Value
}
,
value:
Value
) →
{
[
Key
]
:
Value
}
Returns a new map having removed the all pairs with the given value
.
Dictionary.removeValue({ a = 10, b = 20, c = 30 }, 10)
-- { b = 20, c = 30 }
set
Dictionary.
set
(
dictionary:
{
[
Key
]
:
Value
}
,
key:
Key
,
value:
Value
) →
{
[
Key
]
:
Value
}
Returns a Dictionary which includes value
at key
.
If key
already exists, it will be replaced.
Dictionary.set({ a = 10, b = 20, c = 30 }, "a", 100)
-- { a = 100, b = 20, c = 30 }
setIn
Dictionary.
setIn
(
dictionary:
{
[
any
]
:
any
}
,
keyPath:
{
any
}
,
value:
any
) →
{
[
any
]
:
any
}
Returns a dictionary having set value
at this keyPath
.
If any keys in keyPath
do not exist, a new dictionary will be created at that key.
local dictionary = {
persons = {
alice = {
age = 10,
}
},
}
Dictionary.setIn(dictionary, { "persons", "alice", "age" }, 11)
-- { persons = { alice = { age = 11 } } }
some
Dictionary.
some
(
dictionary:
{
[
Key
]
:
Value
}
,
predicate:
(
Value
,
Key
)
→
boolean
) →
boolean
Returns true if predicate
returns true for any entry in the dictionary.
Dictionary.some({ a = 1, b = 2, c = 3 }), function(value, key)
return value % 2 == 0
end)
-- true
update
Dictionary.
update
(
dictionary:
{
[
Key
]
:
Value
}
,
key:
Key
,
updater:
(
Value?
)
→
Value
,
notSetValue:
Value?
) →
{
[
Key
]
:
Value
}
Returns a dictionary with the entry at key
updated to the result of updater
.
If the entry does not exist, updater
will be given notSetValue
or nil.
Dictionary.update({ a = 10, b = 20, c = 30 }, "a", function(value)
return value * 100
end)
-- { a = 1000, b = 20, c = 30 }
Dictionary.update({ a = 10, b = 20, c = 30 }, "d", function(value)
return value * 100
end, 1)
-- { a = 10, b = 20, c = 30, d = 100 }
updateIn
Dictionary.
updateIn
(
dictionary:
{
[
any
]
:
any
}
,
keyPath:
{
any
}
,
updater:
(
any
)
→
any
,
notSetValue:
any?
) →
{
[
any
]
:
any
}
Returns a Dictionary with the entry at keyPath
updated to the result of updater
.
If any entry does not exist along keyPath
, an empty Dictionary will be created in its place.
If the final entry does not exist, updater
will be given notSetValue
or nil.
local dictionary = {
persons = {
alice = {
age = 10,
},
},
}
Dictionary.updateIn({ "persons", "alice", "age" }, function(value: number)
assert(value, "will exist")
return value + 1
end)
-- { persons = { alice = { age = 11 } } }
values
Dictionary.
values
(
dictionary:
{
[
Key
]
:
Value
}
) →
{
Value
}
Returns a list of values. Order is undefined.
Dictionary.values({ a = 1, b = 2, c = 3 })
-- { 1, 2, 3 }
join
This was deprecated in v0.0.4
Use [Dictionary.merge] instead.
Dictionary.
join
(
...:
{
[
Key
]
:
Value
}
) →
{
[
any
]
:
any
}
removeKey
This was deprecated in v0.0.4
Use [Dictionary.remove] instead.
Dictionary.
removeKey
(
dictionary:
{
[
Key
]
:
Value
}
,
key:
Key
) →
{
[
Key
]
:
Value
}
removeKeys
This was deprecated in v0.0.4
Use [Dictionary.remove] instead.
Dictionary.
removeKeys
(
dictionary:
{
[
Key
]
:
Value
}
,
...:
Key
) →
{
[
Key
]
:
Value
}
removeValues
This was deprecated in v0.0.4
Use [Dictionary.removeValue] instead.
Dictionary.
removeValues
(
dictionary:
{
[
Key
]
:
Value
}
,
...:
Value
) →
{
[
Key
]
:
Value
}
toArray
This was deprecated in v0.0.4
Use [Dictionary.values] instead.
Dictionary.
toArray
(
dictionary:
{
[
Key
]
:
Value
}
) →
{
Value
}