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, 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 | 893 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
Hi! 99% of people in the society are passive - consumers. They wait for others to take action, they prefer to blend in. The remaining 1% are proactive citizens who will do something rather than wait. If you are one of that 1%, you're in luck because I am the same and together we can make something happen.
-
If you want a new feature in this package or you would like to change some of its functionality, raise an issue on this repo. Also, you can email me. Just let it out.
-
If you tried to use this library but it misbehaves, or you need an advice setting it up, and its readme doesn't make sense, just document it and raise an issue on this repo. Alternatively, you can email me.
-
If you don't like the code in here and would like to give an advice about how something could be done better, please do. Same drill - GitHub issues or email, your choice.
-
If you would like to add or change some features, just fork it, hack away, and file a pull request. I'll do my best to merge it quickly. Code style is airbnb
, only without semicolons. If you use a good code editor, it will pick up the established ESLint setup.
⬆ back to top
Licence
MIT License (MIT)
Copyright © 2017 Codsen Ltd, Roy Revelt