posthtml-ast-is-empty

Find out, is nested array/object/string tree (like PostHTML-parsed AST) is empty

This is not a PostHTML plugin but a regular JS library. It is meant to work with anything, including nested array/object trees, like parsed HTML, that is, AST (abstract syntax trees) that come out of the PostHTML-parser. Hence the name, posthtml-ast-
.
Table of Contents
Install
$ npm install --save posthtml-ast-is-empty
Use
var isEmpty = require('posthtml-ast-is-empty')
...
console.log(isEmpty(htmlAstObj))
Rationale
Imagine, that you have a nested array which contains plain objects, arrays and strings. Huge tree. This library can tell if it consists of only empty things.
These are empty things, for example:
{
a: ''
}
or
{
a: ['']
b: {c: {d: ''}}
}
or
[
{
a: ['']
b: {c: {d: ''}}
},
'',
['', '', '']
]
Here's how we judge if things are empty:
- Plain object is empty if each of its keys has empty string or a nested tree of empty things (arrays/plain objects/strings) OR if it has no keys
- Array is empty if each of its elements has empty string or a nested tree of empty things(arrays/plain objects/strings) OR if it has no elements
- A string is empty if it's equal to
''
.
Functions are not considered to be empty and this library will return null
if it encounters one anywhere within the input
. Same with as undefined
or null
inputs.
API
isEmpty (
input
);
Testing
$ npm test
Unit tests use AVA, Istanbul CLI (to count test coverage) and, of course, JS Standard notation.
Contributing
All contributions are welcome. Please stick to Standard JavaScript notation and supplement the test.js
with new unit tests covering your feature(s).
If you see anything incorrect whatsoever, do raise an issue. If you file a pull request, I'll do my best to help you to get it merged soon. If you have any comments on the code, including ideas how to improve things, don't hesitate to contact me by email.
Licence
MIT License (MIT)
Copyright (c) 2017 Codsen Ltd, Roy Revelt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.