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 |
1 KB |
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 see an error, raise an issue.
- If you want a new feature but can't code it up yourself, also raise an issue. Let's discuss it.
- If you tried to use this package, but something didn't work out, also raise an issue. We'll try to help.
- If you want to contribute some code, fork the monorepo via BitBucket, then write code, then file a pull request via BitBucket. We'll merge it in and release.
In monorepo, npm libraries are located in packages/ folder. Inside, the source code is located either in src/ folder (normal npm library) or in the root, cli.js (if it's a command line application).
The npm script "dev", the "dev": "rollup -c --dev --silent" builds the development version retaining all console.logs with row numbers. It's handy to have js-row-num-cli installed globally so you can automatically update the row numbers on all console.logs.
Licence
MIT License
Copyright (c) 2015-2019 Roy Revelt and other contributors