Quick Take

import { strict as assert } from "assert";
import { isEmpty } from "ast-is-empty";

assert.equal(
  isEmpty({
    a: "",
  }),
  true
);

assert.equal(
  isEmpty({
    a: [""],
    b: {
      c: {
        d: "",
      },
    },
  }),
  true
);

assert.equal(
  isEmpty([
    {
      a: [""],
      b: { c: { d: "" } },
    },
    "",
    ["", "", ""],
  ]),
  true
);

Rationale

Sometimes we need to check, does given AST contain only empty structures:

  • empty strings (those that trim to zero length)
  • arrays containing zero or more keys containing empty structures
  • plain objects with containing zero or more keys containing empty structures

This program helps with that.

API

isEmpty(something)

In other words, it's a function which takes one input argument which can be of any type.

It returns boolean or null (in case of non-AST structure, like a function).

Changelog

See it in the monorepo opens in a new tab, on GitHub.

Contributing

To report bugs or request features or assistance, raise an issue on GitHub opens in a new tab.

Any code contributions welcome! All Pull Requests will be dealt promptly.

Licence

MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 ast-deep-contains 4.0.1
Like t.same assert on array of objects, where element order doesn't matter
📦 ast-get-object 3.0.1
Getter/setter for nested parsed HTML AST's, querying objects by key/value pairs
📦 ast-monkey-traverse 3.0.1
Utility library to traverse AST
📦 ast-delete-object 3.0.1
Delete all plain objects in AST if they contain a certain key/value pair
📦 ast-get-values-by-key 4.0.1
Extract values and paths from AST by keys OR set them by keys
📦 ast-monkey-util 2.0.1
Utility library of AST helper functions
📦 ast-monkey-traverse-with-lookahead 3.0.1
Utility library to traverse AST, reports upcoming values