Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→ast-contains-only-empty-spacenext

ast-contains-only-empty-space4.2.2

Does AST contain only empty space?

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • PURPOSE
  • API — EMPTY()
  • WHAT COUNTS…
  • ARRAYS AND OB…
  • OPTIONS
  • PROGRESS AND…
  • INVALID OPTIO…
  • API — DEFAULTS
  • TYPES
  • API — VERSION
  • Changelog

No dependencies whatsoever. This package declares no dependencies or devDependencies.

Permalink to InstallationInstallation

Permalink to Quick TakeQuick Take

Permalink to ExamplesExamples

  • Non-object values
  • Non-plain objects
  • Report completion statistics
  • Compose progress into a caller's range
  • Various

Purpose

This package checks whether a value consists entirely of whitespace strings and empty containers. It is useful for deciding whether a branch of a parsed HTML, CSS, or other abstract syntax tree contains meaningful content.

API — empty()

The main function empty() is imported like this:

The function takes a value and an optional reporting-options object:

Input argumentTypeRequiredDescription
input
Type: unknown
inputunknownyesThe value to inspect.
opts
Type: InputOpts, null, or undefined
optsInputOpts, null, or undefinednoProgress and completion options. null and undefined use the published defaults.

The function returns true only when every inspected value is empty according to the rules below. It returns false as soon as it finds meaningful content. It does not mutate the input.

What counts as empty

These values are empty:

  • a string for which JavaScript’s trim() returns an empty string;
  • an empty array or plain object; and
  • an array or plain object whose recursively inspected contents are all empty.

Every other leaf is meaningful and makes the result false. This includes numbers, Booleans, bigints, null, undefined, symbols, functions, boxed primitives, Date, Map, Set, and class instances. A value keeps the same meaning at the root and when nested inside a container.

empty({ nodes: [" \n", { value: "\t" }] });
// => true

empty({ nodes: [" ", { count: 0 }] });
// => false

empty({ value: null });
// => false

Arrays and objects

Arrays are inspected through their own indexed slots. The function never calls the array’s iterator, so replacing, deleting, or poisoning Symbol.iterator does not change the result. A sparse hole is meaningful and returns false; an inherited numeric property does not fill it. Non-index string properties, symbol properties, and non-enumerable extra properties on an array are ignored.

Plain objects contribute their own enumerable string-keyed properties. Symbol-keyed and non-enumerable properties are ignored, but the presence of Symbol.iterator or Symbol.toStringTag metadata does not hide ordinary properties. Null-prototype and cross-realm records are supported.

Traversal is iterative, so deeply nested input does not consume the JavaScript call stack. A cycle is meaningful and returns false. When several paths refer to the same completed array or object, that shared container is inspected only once.

Options

KeyTypeDefaultDescription
reportCompletionFunc
Type: Function or null
Default: null
reportCompletionFuncFunction or nullnullReceives frozen completion statistics after the Boolean result is known.
reportProgressFunc
Type: Function or null
Default: null
reportProgressFuncFunction or nullnullReceives finite, monotonic progress values during traversal.
reportProgressFuncFrom
Type: Finite number
Default: 0
reportProgressFuncFromFinite number0Sets the first value in the composable progress range.
reportProgressFuncTo
Type: Finite number
Default: 100
reportProgressFuncToFinite number100Sets the final value. It must not be lower than the start, and the span must remain finite.

The exported defaults object is frozen. Passing explicit undefined for an option uses its default value.

Progress and completion reports

reportProgressFunc receives the configured start value, sampled monotonic intermediate values for sufficiently large traversals, and the configured end value. Use reportProgressFuncFrom and reportProgressFuncTo when this scan is one stage of a wider operation.

reportCompletionFunc runs once after a valid scan, including an early false result. It receives these frozen statistics:

KeyDescription
aliasesSkipped
aliasesSkippedLinks to already-completed shared containers that did not need another traversal.
arrayElementsVisited
arrayElementsVisitedArray indices inspected, including a sparse hole that determines the result.
maxDepth
maxDepthDeepest value or array slot inspected. The root has depth 0.
objectPropertiesVisited
objectPropertiesVisitedOwn enumerable string-keyed object properties inspected.
timeTakenInMilliseconds
timeTakenInMillisecondsBest-effort, non-negative elapsed time.
uniqueContainersVisited
uniqueContainersVisitedDistinct arrays and plain objects entered.

Reporting is observational. Exceptions thrown by either callback are ignored and cannot change the Boolean result. Clock failures likewise do not affect the result; unavailable elapsed time is reported as 0.

const progress = [];
let completion;

const result = empty([" ", { nested: "\n" }], {
  reportProgressFunc: (percentageDone) => {
    progress.push(percentageDone);
  },
  reportProgressFuncFrom: 20,
  reportProgressFuncTo: 80,
  reportCompletionFunc: (stats) => {
    completion = stats;
  },
});

// result === true
// progress[0] === 20
// progress[progress.length - 1] === 80
// completion.uniqueContainersVisited === 2

Invalid options

The options argument must be a plain object, null, or undefined. Unknown own enumerable string-keyed option properties, invalid callbacks, non-finite range endpoints, a start greater than the end, and an overflowing range span throw a package-owned TypeError or RangeError. Each message starts with ast-contains-only-empty-space/empty(): [THROW_ID_XX].

API — defaults

You can import defaults:

It's a plain object:

The main function calculates the options to be used by merging the options you passed with these defaults.

Types

This TypeScript package exports CompletionStats, InputOpts, and Opts:

import type {
  CompletionStats,
  InputOpts,
  Opts,
} from "ast-contains-only-empty-space";

API — version

You can import version:

Permalink to changelogChangelog

Open Changelog
↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab