Nickelback Plays Music

npm

Need private packages and team management tools?Check out npm Orgs. »

ast-is-empty

1.9.50 • Public • Published

ast-is-empty

Find out, is nested array/object/string/AST tree is empty

Minimum Node version required Repository is on GitLab Coverage View dependencies as 2D chart Downloads/Month Test in browser Code style: prettier MIT License

Table of Contents

Install

npm i ast-is-empty

The default is exported, so instead of "isEmpty" below, you can name the consumed function however you want.

Consume via a require():

const isEmpty = require("ast-is-empty");

or as an ES Module:

import isEmpty from "ast-is-empty";

or for web pages, as a production-ready minified script file (so-called "UMD build"), straight from CDN:

<script src="https://cdn.jsdelivr.net/npm/ast-is-empty/dist/ast-is-empty.umd.js"></script>
// in which case you get a global variable "astIsEmpty" which you consume like this:
const isEmpty = astIsEmpty;

This package has three builds in dist/ folder:

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

⬆ 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, 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.

⬆ back to top

API

Default function is exported. Its API is "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||false||null

⬆ back to top

Contributing

  • 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 GitLab, then write code, then file a pull request on GitLab. 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.

⬆ back to top

Licence

MIT License

Copyright (c) 2015-2019 Roy Revelt and other contributors

install

npm i ast-is-empty

Downloadsweekly downloads

486

version

1.9.50

license

MIT

homepage

gitlab.com

repository

Gitgitlab

last publish

collaborators

  • avatar
Report a vulnerability