§ Quick Take

import { strict as assert } from "assert";
import nonEmpty from "util-nonempty";

assert.equal(nonEmpty("z"), true);
assert.equal(nonEmpty(""), false);
assert.equal(nonEmpty(["a"]), true);
assert.equal(nonEmpty([123]), true);
assert.equal(nonEmpty([[[[[[[[[[[]]]]]]]]]]]), true);
assert.equal(nonEmpty({ a: "" }), true);
assert.equal(nonEmpty({ a: "a" }), true);
assert.equal(nonEmpty({}), false);

const f = () => {
  return "z";
assert.equal(nonEmpty(f), false);
// (answer is instantly false if input is not array, plain object or string)

§ Purpose

It is a quick utility function, to be able to detect is the input not empty.

If you want to check non-emptiness of complex nested trees of objects, arrays and strings (like parsed HTML AST), you need a library which can recursively traverse that. There are two options:

  • If you want to check for strict emptiness, that is [] or {} is empty, but {aaa: ' \n\n\n ', ' \t'} is not, see ast-is-empty
  • If your "emptiness" definition is "everything that String.trim()'s to an empty string'" (this includes tabs, spaces and line breaks for example, but not letters), see ast-contains-only-empty-space, or plain objects without keys or zero-length arrays.



It's a function: anything-in, boolean-out.

§ Changelog

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

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 util-array-object-or-both 2.8.0
Validate and normalise user choice: array, object or both?