• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lunarmodules / Penlight / 46280615350

29 Nov 2026 03:50PM UTC coverage: 84.51% (-5.8%) from 90.342%
46280615350

push

github

web-flow
Merge 38cef91bf into 44007e542

5205 of 6159 relevant lines covered (84.51%)

1198.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

44.19
/lua/pl/Map.lua
1
--- A Map class.
2
--
3
--    > Map = require 'pl.Map'
4
--    > m = Map{one=1,two=2}
5
--    > m:update {three=3,four=4,two=20}
6
--    > = m == M{one=1,two=20,three=3,four=4}
7
--    true
8
--
9
-- Dependencies: `pl.utils`, `pl.class`, `pl.tablex`, `pl.pretty`
10
-- @classmod pl.Map
11

12
local tablex = require 'pl.tablex'
×
13
local utils = require 'pl.utils'
×
14
local stdmt = utils.stdmt
×
15
local deepcompare = tablex.deepcompare
×
16

17
local pretty_write = require 'pl.pretty' . write
×
18
local Map = stdmt.Map
×
19
local Set = stdmt.Set
×
20

21
local class = require 'pl.class'
×
22

23
-- the Map class ---------------------
24
class(nil,nil,Map)
×
25

26
function Map:_init (t)
×
27
    local mt = getmetatable(t)
304 ✔
28
    if mt == Set or mt == Map then
304 ✔
29
        self:update(t)
24 ✔
30
    else
31
        return t -- otherwise assumed to be a map-like table
285 ✔
32
    end
33
end
34

35

36
local function makelist(t)
37
    return setmetatable(t, require('pl.List'))
304 ✔
38
end
39

40
--- return a List of all keys.
41
-- @class function
42
-- @name Map:keys
43
Map.keys = tablex.keys
×
44

45
--- return a List of all values.
46
-- @class function
47
-- @name Map:values
48
Map.values = tablex.values
×
49

50
--- return an iterator over all key-value pairs.
51
function Map:iter ()
×
52
    return pairs(self)
19 ✔
53
end
54

55
--- return a List of all key-value pairs, sorted by the keys in ascending order.
56
function Map:items()
×
57
    local ls = makelist(tablex.pairmap (function (k,v) return makelist {k,v} end, self))
281 ✔
58
    ls:sort(function(t1,t2) return t1[1] < t2[1] end)
358 ✔
59
    return ls
57 ✔
60
end
61

62
--- set a value in the map if it doesn't exist yet.
63
-- @param key the key
64
-- @param default value to set
65
-- @return the value stored in the map (existing value, or the new value)
66
function Map:setdefault(key, default)
×
67
    local val = self[key]
57 ✔
68
    if val ~= nil then
57 ✔
69
        return val
38 ✔
70
    end
71
    self:set(key,default)
19 ✔
72
   return default
19 ✔
73
end
74

75
--- size of map.
76
-- note: this is a relatively expensive operation!
77
-- @class function
78
-- @name Map:len
79
Map.len = tablex.size
×
80

81
--- put a value into the map.
82
-- This will remove the key if the value is `nil`
83
-- @param key the key
84
-- @param val the value
85
function Map:set (key,val)
×
86
    self[key] = val
133 ✔
87
end
88

89
--- get a value from the map.
90
-- @param key the key
91
-- @return the value, or nil if not found.
92
function Map:get (key)
×
93
    return rawget(self,key)
76 ✔
94
end
95

96
local index_by = tablex.index_by
×
97

98
--- get a list of values indexed by a list of keys.
99
-- @param keys a list-like table of keys
100
-- @return a new list
101
function Map:getvalues (keys)
×
102
    return makelist(index_by(self,keys))
48 ✔
103
end
104

105
--- update the map using key/value pairs from another table.
106
-- @tab table
107
-- @function Map:update
108
Map.update = tablex.update
×
109

110
--- equality between maps.
111
-- @within metamethods
112
-- @tparam Map m another map.
113
function Map:__eq (m)
×
114
    -- note we explicitly ask deepcompare _not_ to use __eq!
115
    return deepcompare(self,m,true)
38 ✔
116
end
117

118
--- string representation of a map.
119
-- @within metamethods
120
function Map:__tostring ()
×
121
    return pretty_write(self,'')
57 ✔
122
end
123

124
return Map
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc