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()`.
This commit is contained in:
Albert Krewinkel 2020-01-10 20:13:06 +01:00
parent 57637f8aae
commit e98921ae57
No known key found for this signature in database
GPG key ID: 388DC0B21F631124
3 changed files with 52 additions and 19 deletions

View file

@ -1,28 +1,27 @@
--[[
List.lua
Copyright © 20172018 Albert Krewinkel
Copyright © 20172020 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 © 20172018 Albert Krewinkel
-- @copyright © 20172020 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

View file

@ -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.

View file

@ -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}