Package Exports
- ast-is-empty
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ast-is-empty) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ast-is-empty
Find out, is nested array/object/string/AST tree is empty
Table of Contents
Install
npm i ast-is-empty// consume as CommonJS require:
const isEmpty = require("ast-is-empty");
// or as a ES Module:
import isEmpty from "ast-is-empty";
// then, for example, feed a parsed HTML tree into it:
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/ast-is-empty.cjs.js |
1 KB |
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import/export. |
module |
dist/ast-is-empty.esm.js |
875 B |
UMD build for browsers, transpiled, minified, containing iife's and has all dependencies baked-in |
browser |
dist/ast-is-empty.umd.js |
1 KB |
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, by which I mean:
- Arrays or objects with no keys, or
- Arrays or objects that have all keys equal to zero-length strings
- Arrays or objects that have all keys equal to strings that
.trim()to zero-length - Zero-length strings
- Or strings that would
.trim()to zero-length (this includes tabs, line breaks, spaces or mix thereof)
These are empty things, for example:
{
a: "";
}or
{
a: [""];
b: {
c: {
d: "";
}
}
}or
[
{
a: ['']
b: {c: {d: ''}}
},
'',
['', '', '']
]Practically speaking, when you work with AST's, all the mentioned empty things are a noise which probably doesn't need to be processed (or needs to be removed altogether).
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 — both will yield null.
API
Anything-in, Boolean-out.
Also, when inappropriate things are given that don't belong to AST's, null-out.
isEmpty(
input // AST tree, or object or array or whatever. Can be deeply-nested.
);
// => true||falseContributing
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.
Licence
MIT License (MIT)
Copyright © 2018 Codsen Ltd, Roy Revelt