Skip to content

Commit

Permalink
fix(map) setdefault will now honor a 'false' value
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Jan 7, 2019
1 parent bc1ea87 commit f9f06de
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lua/pl/Map.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,17 @@ function Map:items()
return ls
end

--- Will return the existing value, or if it doesn't exist it will set
-- a default value and return it.
function Map:setdefault(key, defaultval)
return self[key] or self:set(key,defaultval) or defaultval
--- set a value in the map if it doesn't exist yet.
-- @param key the key
-- @param default value to set
-- @return the value stored in the map (existing value, or the new value)
function Map:setdefault(key, default)
local val = self[key]
if val ~= nil then
return val
end
self:set(key,default)
return default
end

--- size of map.
Expand Down
3 changes: 3 additions & 0 deletions tests/test-map.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ asserteq(
{'gamma', 3},
}
)
v = m:set("alpha", false)
v = m:setdefault("alpha", true) -- falsy value should not be altered
asserteq(false, m:get("alpha"))



Expand Down

0 comments on commit f9f06de

Please sign in to comment.