git.iko.soy/typeable/pandoc
on master help sign in
pandoc.List.lua: make `pandoc.List` a callable constructor

It is now possible to construct a new List via `pandoc.List()` instead of `pandoc.List:new()`.

commite98921ae571717d3d7c1fbaf7d7d527361b664c9 authorAlbert Krewinkel <albert@zeitkraut.de> dateFri 10 Jan 2020, 20:13 (7 years ago) parent57637f8
3 files changed+52−19
data/pandoc.List.lua+20−18 doc/lua-filters.md+23−1 test/lua/module/pandoc-list.lua+9
data/pandoc.List.lua+20−18
@@ -1,28 +1,27 @@
11--[[
22List.lua
33
4−Copyright © 2017–2018 Albert Krewinkel
4+Copyright © 2017–2020 Albert Krewinkel
55
6−Permission to use, copy, modify, and/or distribute this software for any purpose
7−with or without fee is hereby granted, provided that the above copyright notice
8−and this permission notice appear in all copies.
6+Permission to use, copy, modify, and/or distribute this software for any
7+purpose with or without fee is hereby granted, provided that the above
8+copyright notice and this permission notice appear in all copies.
99
10−THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11−REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
12−FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13−INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14−OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15−TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
16−THIS SOFTWARE.
17−]]
10+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ]]
1817
1918--- Pandoc's List type and helper methods
2019-- @classmod pandoc.List
2120-- @author Albert Krewinkel
22−-- @copyright © 2017–2018 Albert Krewinkel
21+-- @copyright © 2017–2020 Albert Krewinkel
2322-- @license MIT
2423local List = {
25− _VERSION = "0.1.0"
24+ _VERSION = "1.0.0"
2625}
2726
2827function List:new (o)
@@ -32,10 +31,6 @@ function List:new (o)
3231 return o
3332end
3433
35−function List:__call (o)
36− return self:new(o)
37−end
38−
3934--- Concatenates two lists.
4035-- @param list second list concatenated to the first
4136-- @return a new list containing all elements from list1 and list2
@@ -117,4 +112,11 @@ function List:filter (pred)
117112 return res
118113end
119114
115+-- Set metatable with __call metamethod. This allows the use of `List`
116+-- as a constructor function.
117+local ListMT = {
118+ __call = List.new
119+}
120+setmetatable(List, ListMT)
121+
120122return List
doc/lua-filters.md+23−1
@@ -2662,7 +2662,16 @@ Usage:
26622662
26632663# Module pandoc.List
26642664
2665−Pandoc\'s List type and helper methods
2665+The this module defines pandoc's list type. It comes with useful
2666+methods and convenience functions.
2667+
2668+## Constructor
2669+
2670+[`pandoc.List([table])`]{#pandoc.List}
2671+
2672+: Create a new List. If the optional argument `table` is given,
2673+ set the metatable of that value to `pandoc.List`. This is an
2674+ alias for [`pandoc.List:new([table])`](#pandoc.list:new).
26662675
26672676## Metamethods
26682677
@@ -2684,6 +2693,19 @@ Pandoc\'s List type and helper methods
26842693
26852694: Returns a (shallow) copy of the list.
26862695
2696+[`pandoc.List:new([table])`]{#pandoc.List:new}
2697+
2698+: Create a new List. If the optional argument `table` is given,
2699+ set the metatable of that value to `pandoc.List`.
2700+
2701+ Parameters:
2702+
2703+ `table`:
2704+ : table which should be treatable as a list; defaults to an
2705+ empty table
2706+
2707+ Returns: the updated input value
2708+
26872709[`pandoc.List:includes (needle, init)`]{#pandoc.List:includes}
26882710
26892711: Checks if the list has an item equal to the given needle.
test/lua/module/pandoc-list.lua+9
@@ -6,6 +6,15 @@ local test = tasty.test_case
66local group = tasty.test_group
77
88return {
9+ group 'List as function' {
10+ test('equivalent to List:new', function (x)
11+ local new = List:new {'ramen'}
12+ local list = List {'ramen'}
13+ assert.are_same(new, list)
14+ assert.are_equal(getmetatable(new), getmetatable(list))
15+ end)
16+ },
17+
918 group 'new' {
1019 test('make table usable as list', function ()
1120 local test = List:new{1, 1, 2, 3, 5}