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 List.lua
Copyright © 20172018 Albert Krewinkel Copyright © 20172020 Albert Krewinkel
Permission to use, copy, modify, and/or distribute this software for any purpose Permission to use, copy, modify, and/or distribute this software for any
with or without fee is hereby granted, provided that the above copyright notice purpose with or without fee is hereby granted, provided that the above
and this permission notice appear in all copies. copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
THIS SOFTWARE. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ]]
]]
--- Pandoc's List type and helper methods --- Pandoc's List type and helper methods
-- @classmod pandoc.List -- @classmod pandoc.List
-- @author Albert Krewinkel -- @author Albert Krewinkel
-- @copyright © 20172018 Albert Krewinkel -- @copyright © 20172020 Albert Krewinkel
-- @license MIT -- @license MIT
local List = { local List = {
_VERSION = "0.1.0" _VERSION = "1.0.0"
} }
function List:new (o) function List:new (o)
@ -32,10 +31,6 @@ function List:new (o)
return o return o
end end
function List:__call (o)
return self:new(o)
end
--- Concatenates two lists. --- Concatenates two lists.
-- @param list second list concatenated to the first -- @param list second list concatenated to the first
-- @return a new list containing all elements from list1 and list2 -- @return a new list containing all elements from list1 and list2
@ -117,4 +112,11 @@ function List:filter (pred)
return res return res
end 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 return List

View file

@ -2662,7 +2662,16 @@ Usage:
# Module pandoc.List # 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 ## Metamethods
@ -2684,6 +2693,19 @@ Pandoc\'s List type and helper methods
: Returns a (shallow) copy of the list. : 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} [`pandoc.List:includes (needle, init)`]{#pandoc.List:includes}
: Checks if the list has an item equal to the given needle. : 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 local group = tasty.test_group
return { 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' { group 'new' {
test('make table usable as list', function () test('make table usable as list', function ()
local test = List:new{1, 1, 2, 3, 5} local test = List:new{1, 1, 2, 3, 5}