diff options
author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2019-01-07 17:36:30 +0300 |
---|---|---|
committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2019-01-07 18:17:01 +0300 |
commit | f9f06de79e87f64a839bc01cb2d80201b53e3047 (patch) | |
tree | 3c72bdc11e9020878cfe4af0ec99c92ddb683faf | |
parent | bc1ea872257a87667eabe5ee6548e00a88a48f23 (diff) |
fix(map) setdefault will now honor a 'false' value
-rw-r--r-- | lua/pl/Map.lua | 15 | ||||
-rw-r--r-- | tests/test-map.lua | 3 |
2 files changed, 14 insertions, 4 deletions
diff --git a/lua/pl/Map.lua b/lua/pl/Map.lua index 2624dc8..572b22a 100644 --- a/lua/pl/Map.lua +++ b/lua/pl/Map.lua @@ -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. diff --git a/tests/test-map.lua b/tests/test-map.lua index 93e511a..1c0bee6 100644 --- a/tests/test-map.lua +++ b/tests/test-map.lua @@ -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")) |