aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2020-01-10 20:13:06 +0100
committerAlbert Krewinkel <albert@zeitkraut.de>2020-01-11 19:38:27 +0100
commite98921ae571717d3d7c1fbaf7d7d527361b664c9 (patch)
treeae6886402a129baa0b69e4ce6d461486a760ffda
parent57637f8aae6172e7713e885f41f4574cfa2770b3 (diff)
downloadpandoc-e98921ae571717d3d7c1fbaf7d7d527361b664c9.tar.gz
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()`.
-rw-r--r--data/pandoc.List.lua38
-rw-r--r--doc/lua-filters.md24
-rw-r--r--test/lua/module/pandoc-list.lua9
3 files changed, 52 insertions, 19 deletions
diff --git a/data/pandoc.List.lua b/data/pandoc.List.lua
index 6b3188a65..442e9c397 100644
--- a/data/pandoc.List.lua
+++ b/data/pandoc.List.lua
@@ -1,28 +1,27 @@
--[[
List.lua
-Copyright © 2017–2018 Albert Krewinkel
+Copyright © 2017–2020 Albert Krewinkel
-Permission to use, copy, modify, and/or distribute this software for any purpose
-with or without fee is hereby granted, provided that the above copyright notice
-and this permission notice appear in all copies.
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
-REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
-FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
-INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
-OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
-TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
-THIS SOFTWARE.
-]]
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ]]
--- Pandoc's List type and helper methods
-- @classmod pandoc.List
-- @author Albert Krewinkel
--- @copyright © 2017–2018 Albert Krewinkel
+-- @copyright © 2017–2020 Albert Krewinkel
-- @license MIT
local List = {
- _VERSION = "0.1.0"
+ _VERSION = "1.0.0"
}
function List:new (o)
@@ -32,10 +31,6 @@ function List:new (o)
return o
end
-function List:__call (o)
- return self:new(o)
-end
-
--- Concatenates two lists.
-- @param list second list concatenated to the first
-- @return a new list containing all elements from list1 and list2
@@ -117,4 +112,11 @@ function List:filter (pred)
return res
end
+-- Set metatable with __call metamethod. This allows the use of `List`
+-- as a constructor function.
+local ListMT = {
+ __call = List.new
+}
+setmetatable(List, ListMT)
+
return List
diff --git a/doc/lua-filters.md b/doc/lua-filters.md
index 7d084ecb4..ae7b02c76 100644
--- a/doc/lua-filters.md
+++ b/doc/lua-filters.md
@@ -2662,7 +2662,16 @@ Usage:
# Module pandoc.List
-Pandoc\'s List type and helper methods
+The this module defines pandoc's list type. It comes with useful
+methods and convenience functions.
+
+## Constructor
+
+[`pandoc.List([table])`]{#pandoc.List}
+
+: Create a new List. If the optional argument `table` is given,
+ set the metatable of that value to `pandoc.List`. This is an
+ alias for [`pandoc.List:new([table])`](#pandoc.list:new).
## Metamethods
@@ -2684,6 +2693,19 @@ Pandoc\'s List type and helper methods
: Returns a (shallow) copy of the list.
+[`pandoc.List:new([table])`]{#pandoc.List:new}
+
+: Create a new List. If the optional argument `table` is given,
+ set the metatable of that value to `pandoc.List`.
+
+ Parameters:
+
+ `table`:
+ : table which should be treatable as a list; defaults to an
+ empty table
+
+ Returns: the updated input value
+
[`pandoc.List:includes (needle, init)`]{#pandoc.List:includes}
: Checks if the list has an item equal to the given needle.
diff --git a/test/lua/module/pandoc-list.lua b/test/lua/module/pandoc-list.lua
index faa5e966e..fb0c1c495 100644
--- a/test/lua/module/pandoc-list.lua
+++ b/test/lua/module/pandoc-list.lua
@@ -6,6 +6,15 @@ local test = tasty.test_case
local group = tasty.test_group
return {
+ group 'List as function' {
+ test('equivalent to List:new', function (x)
+ local new = List:new {'ramen'}
+ local list = List {'ramen'}
+ assert.are_same(new, list)
+ assert.are_equal(getmetatable(new), getmetatable(list))
+ end)
+ },
+
group 'new' {
test('make table usable as list', function ()
local test = List:new{1, 1, 2, 3, 5}