No dependencies whatsoever. This package declares no dependencies or devDependencies.
Permalink to InstallationInstallation
Permalink to Quick TakeQuick Take
Permalink to ExamplesExamples
- Check whether an array contains a value
- Treat a deeply nested array as non-empty
- Treat every number, including zero and NaN, as non-empty
- Check whether an object contains a property
- Treat nullish and Boolean values as empty
Purpose
nonEmpty() reports whether one supported value is shallowly non-empty. It
checks only the outer string, array, or record; it never traverses nested
values and does not mutate the input.
| Input | Result | Rule |
|---|---|---|
Omitted, undefined, or null | ||
Omitted, undefined, or null | false | These values are empty. |
| Primitive string | ||
| Primitive string | true when length > 0 | Whitespace is content. |
| Native array | ||
| Native array | true when length > 0 | Sparse slots count; expando properties do not. |
| Plain record | ||
| Plain record | true when it has an own enumerable string key | Symbol, non-enumerable, and inherited keys do not count. |
| Primitive number | ||
| Primitive number | true | This includes zero, NaN, and both infinities. |
| Any other value | ||
| Any other value | false | This includes booleans, bigints, symbols, functions, collections, boxed primitives, and class instances. |
Nested contents do not affect the result. For example, [[]] is non-empty
because its outer array has one slot. A zero-length array remains empty even if
it has an enumerable string property.
nonEmpty();
// => false
nonEmpty(" \n\t");
// => true
nonEmpty(new Array(1));
// => true
nonEmpty({ value: undefined });
// => true
nonEmpty(new Map([["value", true]]));
// => false
Plain records
Supported records include object literals, values created by new Object(),
direct Object.create(null) records, and ordinary records from another realm.
Custom-prototype objects and class instances are not plain records.
Record non-emptiness follows Object.keys() semantics. Only own, enumerable,
string-keyed properties count. The function does not read ordinary property
values or invoke their getters while discovering keys.
Recursive alternatives
Use ast-is-empty when you need to traverse nested arrays and plain objects and treat only zero-length strings as empty. Whitespace is content under that strict definition.
Use ast-contains-only-empty-space when nested strings containing only spaces, tabs, or line breaks should also count as empty.
API — nonEmpty()
The main function nonEmpty() is imported like this:
The function accepts one optional input. Omitting it is equivalent to passing
undefined:
It returns a boolean according to the shallow rules above.
API — version
You can import version: