posthtml-ast-is-empty
Find out, is nested array/object/string/AST tree 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 i posthtml-ast-is-empty
const isEmpty = require("posthtml-ast-is-empty");
import isEmpty from "posthtml-ast-is-empty";
console.log(isEmpty(htmlAstObj));
Here's what you'll get:
Type | Key in package.json | Path | Size |
---|
Main export - CommonJS version, transpiled to ES5, contains require and module.exports | main | dist/posthtml-ast-is-empty.cjs.js | 1 KB |
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import /export . | module | dist/posthtml-ast-is-empty.esm.js | 875 B |
UMD build for browsers, transpiled, minified, containing iife 's and has all dependencies baked-in | browser | dist/posthtml-ast-is-empty.umd.js | 1 KB |
⬆ back to top
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.
⬆ back to top
API
Anything-in, Boolean-out.
isEmpty(
input
);
Contributing
-
If you want a new feature in this package or you would like us to change some of its functionality, raise an issue on this repo.
-
If you tried to use this library but it misbehaves, or you need advice setting it up, and its readme doesn't make sense, just document it and raise an issue on this repo.
-
If you would like to add or change some features, just fork it, hack away, and file a pull request. We'll do our best to merge it quickly. Prettier is enabled, so you don't need to worry about the code style.
⬆ back to top
Licence
MIT License (MIT)
Copyright © 2018 Codsen Ltd, Roy Revelt