ast-contains-only-empty-space
Returns Boolean depending if passed AST contain only empty space
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 ast-contains-only-empty-space
const containsOnlyEmptySpace = require('ast-contains-only-empty-space')
import containsOnlyEmptySpace from 'ast-contains-only-empty-space'
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/ast-contains-only-empty-space.cjs.js | 917 B |
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import /export . | module | dist/ast-contains-only-empty-space.esm.js | 724 B |
UMD build for browsers, transpiled, minified, containing iife 's and has all dependencies baked-in | browser | dist/ast-contains-only-empty-space.umd.js | 13 KB |
⬆ back to top
Use
const empty = require('ast-contains-only-empty-space')
...
console.log(empty([{ 'content': {} }]))
console.log(empty([{ 'tag': 'style' }]))
console.log(empty([' ', ' ']))
console.log(empty(' '))
console.log(empty({a: null}))
console.log(empty(
{
a: [{
x: {
y: [
{
z: [
'\n'
]
}
]
}
}],
b: ['\t\t\t '],
c: ['\n \n\n'],
d: ['\t ']
}
))
⬆ back to top
Rationale
Working with parsed HTML is always a battle against white space. Often you need to know, does certain AST piece (object/array/whatever) contain anything real, or just an empty space. That's what this library is for.
In real life, parsed HTML trees will have many levels of nested arrays, objects and strings. While it's easy to check does a plain object contain only empty space (\n
,
, \t
, line break or a mix of thereof), it's not so easy when your object has arrays of empty objects. I want a solid, tested library which can identify emptiness (or lack of) in anything, nested or not nested.
By the way, weird things (like functions, which don't belong to parsed HTML structures) will yield cause result false
.
⬆ back to top
API
Input - anything. Output - Boolean.
empty (input);
This library does not mutate the input arguments.
⬆ back to top
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 an 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. Code style is airbnb-base
, 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 © 2018 Codsen Ltd, Roy Revelt