Skip to content

Commit c79c483

Browse files
committed
Add is_empty querying function
1 parent b0b0278 commit c79c483

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,14 @@ If `a` and `b` are `Doc` elements, then `a / b` puts `a` above `b`.
324324
If `a` and `b` are `Doc` elements, then `a // b` puts `a` above
325325
`b`, inserting a blank line between them.
326326

327+
### Document Querying
328+
329+
#### is_empty
330+
331+
`is_empty (doc)`
332+
333+
Returns `true` iff `doc` is the empty document, `false` otherwise.
334+
327335

328336
License
329337
-------

src/Foreign/Lua/Module/DocLayout.hs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ module Foreign.Lua.Module.DocLayout (
4848
, space
4949
, vfill
5050

51-
-- * Functions
51+
-- * Rendering
5252
, render
5353

54+
-- * Document Querying
55+
, is_empty
56+
5457
-- * Marshaling
5558
, peekDoc
5659
, pushDoc
@@ -109,6 +112,8 @@ pushModule = do
109112
Lua.addfunction "prefixed" prefixed
110113
Lua.addfunction "rblock" rblock
111114
Lua.addfunction "vfill" vfill
115+
-- querying
116+
Lua.addfunction "is_empty" is_empty
112117
-- rendering
113118
Lua.addfunction "render" render
114119
return 1
@@ -124,6 +129,15 @@ preloadModule = flip Lua.preloadhs pushModule
124129
render :: Doc Text -> Optional Int -> Lua Text
125130
render doc optLength = return $ Doc.render (Lua.fromOptional optLength) doc
126131

132+
--
133+
-- Querying
134+
--
135+
136+
-- | @True@ iff the document is empty.
137+
is_empty :: Doc Text -> Lua Bool
138+
is_empty = return . Doc.isEmpty
139+
140+
127141
--
128142
-- Constructors
129143
--

test/test-doclayout.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ return {
5353
end)
5454
},
5555

56+
group 'document querying' {
57+
test('is_empty', function ()
58+
assert.is_truthy(doclayout.is_empty(doclayout.empty))
59+
assert.is_truthy(doclayout.is_empty(''))
60+
assert.is_falsy(doclayout.is_empty('non-empty'))
61+
end)
62+
},
63+
5664
group 'Doc type' {
5765
test('empty strings equal the empty Doc', function ()
5866
assert.are_equal(doclayout.empty .. '', doclayout.empty)

0 commit comments

Comments
 (0)