Skip to content

traverse

Deeply enumerate an object and any nested objects

1284 bytes

Usage

Recursively visit each property of an object (or each element of an array) and its nested objects or arrays. To traverse non-array iterables (e.g. Map, Set) and class instances, see the Traversing other objects section.

Traversal is performed in a depth-first manner. That means the deepest object will be visited before the last property of the root object.

import * as
import _
_
from 'radashi'
const
const root: {
a: {
b: number;
};
c: number[];
}
root
= {
a: {
b: number;
}
a
: {
b: number
b
: 2 },
c: number[]
c
: [1, 2] }
import _
_
.
function traverse(root: object, visitor: _.TraverseVisitor, options?: (_.TraverseOptions & {
rootNeedsVisit?: null;
}) | null, outerContext?: _.TraverseContext): boolean (+1 overload)
export traverse

Recursively visit each property of an object (or each element of an array) and its nested objects or arrays. By default, the only nested objects to be traversed are plain objects and arrays.

@seehttps://radashi.js.org/reference/object/traverse

@example

import { traverse } from 'radashi'
const root = { a: 1, b: { c: { d: [2] }, e: 3 } }
traverse(root, (value, key, parent, context) => {
console.log(key, '=>', value)
})
// Logs the following:
// a => 1
// b => { … }
// c => { … }
// d => [ 2 ]
// 0 => 2
// e => 3

@version12.2.0

traverse
(
const root: {
a: {
b: number;
};
c: number[];
}
root
, (
value: unknown
value
,
key: string | number | symbol
key
,
parent: object
parent
,
context: _.TraverseContext<string | number | symbol>
context
) => {
const
const depth: number
depth
=
context: _.TraverseContext<string | number | symbol>
context
.
TraverseContext<string | number | symbol>.parents: readonly object[]

The stack of parent objects. The last object is the current parent.

⚠️ This array must not be mutated directly or referenced outside the visitor callback. If that's necessary, you'll want to clone it first.

parents
.
ReadonlyArray<object>.length: number

Gets the length of the array. This is a number one higher than the highest element defined in an array.

length
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(' '.
String.repeat(count: number): string

Returns a String value that is made from count copies appended together. If count is 0, the empty string is returned.

@paramcount number of copies to append

repeat
(
const depth: number
depth
* 2),
key: string | number | symbol
key
, '=>',
value: unknown
value
)
})
// Logs the following:
// a => { b: 2 }
// b => 2
// c => [1, 2]
// 0 => 1
// 1 => 2

Tip: Check out the Advanced section to see what else is possible.

Types

TraverseVisitor

The TraverseVisitor type represents the function passed to traverse as its 2nd argument. If you ever need to declare a visitor separate from a traverse call, you can do so by declaring a function with this type signature.

import {
type TraverseVisitor<Key = string | number | symbol> = (value: unknown, key: Key, parent: object, context: TraverseContext<Key>, options: TraverseOptions<Key> & {
rootNeedsVisit?: null;
}) => (() => boolean | void) | boolean | void

The visitor callback for the traverse function.

TraverseVisitor
} from 'radashi'
const
const visitor: TraverseVisitor
visitor
:
type TraverseVisitor<Key = string | number | symbol> = (value: unknown, key: Key, parent: object, context: TraverseContext<Key>, options: TraverseOptions<Key> & {
rootNeedsVisit?: null;
}) => (() => boolean | void) | boolean | void

The visitor callback for the traverse function.

TraverseVisitor
= (
value: unknown
value
,
key: string | number | symbol
key
,
parent: object
parent
,
context: TraverseContext<string | number | symbol>
context
) => {
// ...
}

TraverseContext

Every visit includes a context object typed with TraverseContext, which contains the following properties:

  • key: The current key being visited.
  • parent: The parent object of the current value.
  • parents: An array of objects (from parent to child) that the current value is contained by.
  • path: An array describing the key path to the current value from the root.
  • skip: A function used for skipping traversal of an object. If no object is provided, the current value is skipped. See Skipping objects for more details.
  • skipped: A set of objects that have been skipped.
  • value: The current value being visited.

TraverseOptions

You may set these options for traverse using an object as its 3rd argument.

  • ownKeys: A function that returns the own enumerable property names of an object.
  • rootNeedsVisit: A boolean indicating whether the root object should be visited.

See the Options section for more details.

Options

Traversing all properties

By default, non-enumerable properties and symbol properties are skipped. You can pass in a custom ownKeys implementation to control which object properties are visited.

This example shows how Reflect.ownKeys can be used to include non-enumerable properties and symbol properties. Note that symbol properties are always traversed last when using Reflect.ownKeys.

import * as
import _
_
from 'radashi'
const
const symbol: typeof symbol
symbol
=
var Symbol: SymbolConstructor
(description?: string | number) => symbol

Returns a new unique Symbol value.

@paramdescription Description of the new Symbol object.

Symbol
('b')
const
const root: {
[symbol]: number;
}
root
= { [
const symbol: typeof symbol
symbol
]: 1 }
var Object: ObjectConstructor

Provides functionality common to all JavaScript objects.

Object
.
ObjectConstructor.defineProperty<{
[symbol]: number;
}>(o: {
[symbol]: number;
}, p: PropertyKey, attributes: PropertyDescriptor & ThisType<any>): {
[symbol]: number;
}

Adds a property to an object, or modifies attributes of an existing property.

@paramo Object on which to add or modify the property. This can be a native JavaScript object (that is, a user-defined object or a built in object) or a DOM object.

@paramp The property name.

@paramattributes Descriptor for the property. It can be for a data property or an accessor property.

defineProperty
(
const root: {
[symbol]: number;
}
root
, 'a', {
PropertyDescriptor.value?: any
value
: 2,
PropertyDescriptor.enumerable?: boolean
enumerable
: false })
import _
_
.
function traverse(root: object, visitor: _.TraverseVisitor, options?: (_.TraverseOptions & {
rootNeedsVisit?: null;
}) | null, outerContext?: _.TraverseContext): boolean (+1 overload)
export traverse

Recursively visit each property of an object (or each element of an array) and its nested objects or arrays. By default, the only nested objects to be traversed are plain objects and arrays.

@seehttps://radashi.js.org/reference/object/traverse

@example

import { traverse } from 'radashi'
const root = { a: 1, b: { c: { d: [2] }, e: 3 } }
traverse(root, (value, key, parent, context) => {
console.log(key, '=>', value)
})
// Logs the following:
// a => 1
// b => { … }
// c => { … }
// d => [ 2 ]
// 0 => 2
// e => 3

@version12.2.0

traverse
(
const root: {
[symbol]: number;
}
root
,
(
value: unknown
value
,
key: string | number | symbol
key
) => {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
key: string | number | symbol
key
, '=>',
value: unknown
value
)
},
{
TraverseOptions<string | number | symbol>.ownKeys?: ((parent: object) => Iterable<string | number | symbol>) | null

A function that returns the keys of the object to be traversed.

@defaultObject.keys

ownKeys
:
namespace Reflect
Reflect
.
function Reflect.ownKeys(target: object): (string | symbol)[]

Returns the string and symbol keys of the own properties of an object. The own properties of an object are those that are defined directly on that object, and are not inherited from the object's prototype.

@paramtarget Object that contains the own properties.

ownKeys
},
)
// Logs the following:
// a => 2
// Symbol(b) => 1

Visiting the root object

By default, your visitor callback will never receive the object passed into traverse. To override this behavior, set the rootNeedsVisit option to true.

When the root object is visited, the key will be null.

import * as
import _
_
from 'radashi'
const
const root: {
a: number;
}
root
= {
a: number
a
: 1 }
import _
_
.
function traverse(root: object, visitor: _.TraverseVisitor<keyof any | null>, options?: _.TraverseOptions<keyof any | null> | null, outerContext?: _.TraverseContext<keyof any | null>): boolean (+1 overload)
export traverse

Recursively visit each property of an object (or each element of an array) and its nested objects or arrays. By default, the only nested objects to be traversed are plain objects and arrays.

@seehttps://radashi.js.org/reference/object/traverse

@example

import { traverse } from 'radashi'
const root = { a: 1, b: { c: { d: [2] }, e: 3 } }
traverse(root, (value, key, parent, context) => {
console.log(key, '=>', value)
})
// Logs the following:
// a => 1
// b => { … }
// c => { … }
// d => [ 2 ]
// 0 => 2
// e => 3

@version12.2.0

traverse
(
const root: {
a: number;
}
root
,
(
value: unknown
value
,
key: string | number | symbol | null
key
) => {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
key: string | number | symbol | null
key
, '=>',
value: unknown
value
)
},
{
TraverseOptions<Key = string | number | symbol>.rootNeedsVisit?: boolean | null

When true, the visitor callback will be invoked for the root object.

@defaultfalse

rootNeedsVisit
: true },
)
// Logs the following:
// null => { a: 1 }
// a => 1

Advanced

Traversing other objects

If traversing plain objects and arrays isn’t enough, try calling traverse from within another traverse callback like follows. This takes advantage of the fact that the root object is always traversed.

import * as
import _
_
from 'radashi'
// Note how we're using a named visitor function so it can reference itself.
import _
_
.
function traverse(root: object, visitor: _.TraverseVisitor, options?: (_.TraverseOptions & {
rootNeedsVisit?: null;
}) | null, outerContext?: _.TraverseContext): boolean (+1 overload)
export traverse

Recursively visit each property of an object (or each element of an array) and its nested objects or arrays. By default, the only nested objects to be traversed are plain objects and arrays.

@seehttps://radashi.js.org/reference/object/traverse

@example

import { traverse } from 'radashi'
const root = { a: 1, b: { c: { d: [2] }, e: 3 } }
traverse(root, (value, key, parent, context) => {
console.log(key, '=>', value)
})
// Logs the following:
// a => 1
// b => { … }
// c => { … }
// d => [ 2 ]
// 0 => 2
// e => 3

@version12.2.0

traverse
(
any
root
, function
function (local function) visitor(value: unknown, key: string | number | symbol, parent: object, context: _.TraverseContext<string | number | symbol>, options: _.TraverseOptions<string | number | symbol> & {
...;
}): boolean | undefined
visitor
(
value: unknown
value
,
key: string | number | symbol
key
,
parent: object
parent
,
context: _.TraverseContext<string | number | symbol>
context
,
options: _.TraverseOptions<string | number | symbol> & {
rootNeedsVisit?: null;
}
options
) {
if (
value: unknown
value
instanceof
any
MyClass
) {
return
import _
_
.
function traverse(root: object, visitor: _.TraverseVisitor, options?: (_.TraverseOptions & {
rootNeedsVisit?: null;
}) | null, outerContext?: _.TraverseContext): boolean (+1 overload)
export traverse

Recursively visit each property of an object (or each element of an array) and its nested objects or arrays. By default, the only nested objects to be traversed are plain objects and arrays.

@seehttps://radashi.js.org/reference/object/traverse

@example

import { traverse } from 'radashi'
const root = { a: 1, b: { c: { d: [2] }, e: 3 } }
traverse(root, (value, key, parent, context) => {
console.log(key, '=>', value)
})
// Logs the following:
// a => 1
// b => { … }
// c => { … }
// d => [ 2 ]
// 0 => 2
// e => 3

@version12.2.0

traverse
(
value: unknown
value
,
function (local function) visitor(value: unknown, key: string | number | symbol, parent: object, context: _.TraverseContext<string | number | symbol>, options: _.TraverseOptions<string | number | symbol> & {
...;
}): boolean | undefined
visitor
,
options: _.TraverseOptions<string | number | symbol> & {
rootNeedsVisit?: null;
}
options
,
context: _.TraverseContext<string | number | symbol>
context
)
}
// TODO: Handle other values as needed.
})

If you didn’t set any options, the options argument can be null:

return _.traverse(root, visitor, null, context)

Skipping objects

Using the TraverseContext::skip method, you can prevent an object from being traversed. By calling skip() with no arguments, the current value won’t be traversed.

import * as
import _
_
from 'radashi'
const
const root: {
a: {
b: number;
};
c: {
d: number;
};
}
root
= {
a: {
b: number;
}
a
: {
b: number
b
: 1 },
c: {
d: number;
}
c
: {
d: number
d
: 2 },
}
import _
_
.
function traverse(root: object, visitor: _.TraverseVisitor, options?: (_.TraverseOptions & {
rootNeedsVisit?: null;
}) | null, outerContext?: _.TraverseContext): boolean (+1 overload)
export traverse

Recursively visit each property of an object (or each element of an array) and its nested objects or arrays. By default, the only nested objects to be traversed are plain objects and arrays.

@seehttps://radashi.js.org/reference/object/traverse

@example

import { traverse } from 'radashi'
const root = { a: 1, b: { c: { d: [2] }, e: 3 } }
traverse(root, (value, key, parent, context) => {
console.log(key, '=>', value)
})
// Logs the following:
// a => 1
// b => { … }
// c => { … }
// d => [ 2 ]
// 0 => 2
// e => 3

@version12.2.0

traverse
(
const root: {
a: {
b: number;
};
c: {
d: number;
};
}
root
, (
value: unknown
value
,
key: string | number | symbol
key
,
parent: object
parent
,
context: _.TraverseContext<string | number | symbol>
context
) => {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
key: string | number | symbol
key
, '=>',
value: unknown
value
)
// Skip traversal of the 'a' object.
if (
key: string | number | symbol
key
=== 'a') {
context: _.TraverseContext<string | number | symbol>
context
.
TraverseContext<string | number | symbol>.skip: (obj?: object) => void

When the current value is a traversable object/iterable, this function can be used to skip traversing it altogether.

If the obj argument is provided, it marks the given object as skipped (instead of the current value), preventing it from being traversed.

skip
()
}
})
// Logs the following:
// a => { b: 1 }
// c => { d: 2 }
// d => 2

You can pass any object to skip() to skip traversal of that object.

import * as
import _
_
from 'radashi'
const
const root: {
a: {
b: {
c: number;
};
};
}
root
= {
a: {
b: {
c: number;
};
}
a
: {
b: {
c: number;
}
b
: {
c: number
c
: 1,
},
},
}
import _
_
.
function traverse(root: object, visitor: _.TraverseVisitor, options?: (_.TraverseOptions & {
rootNeedsVisit?: null;
}) | null, outerContext?: _.TraverseContext): boolean (+1 overload)
export traverse

Recursively visit each property of an object (or each element of an array) and its nested objects or arrays. By default, the only nested objects to be traversed are plain objects and arrays.

@seehttps://radashi.js.org/reference/object/traverse

@example

import { traverse } from 'radashi'
const root = { a: 1, b: { c: { d: [2] }, e: 3 } }
traverse(root, (value, key, parent, context) => {
console.log(key, '=>', value)
})
// Logs the following:
// a => 1
// b => { … }
// c => { … }
// d => [ 2 ]
// 0 => 2
// e => 3

@version12.2.0

traverse
(
const root: {
a: {
b: {
c: number;
};
};
}
root
, (
value: unknown
value
,
key: string | number | symbol
key
,
parent: object
parent
,
context: _.TraverseContext<string | number | symbol>
context
) => {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
key: string | number | symbol
key
, '=>',
value: unknown
value
)
// Visit the properties of the current object, but skip any objects nested within.
var Object: ObjectConstructor

Provides functionality common to all JavaScript objects.

Object
.
ObjectConstructor.values<unknown>(o: {
[s: string]: unknown;
} | ArrayLike<unknown>): unknown[] (+1 overload)

Returns an array of values of the enumerable own properties of an object

@paramo Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.

values
(
value: unknown
value
).
Array<unknown>.forEach(callbackfn: (value: unknown, index: number, array: unknown[]) => void, thisArg?: any): void

Performs the specified action for each element in an array.

@paramcallbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

forEach
(
nestedValue: unknown
nestedValue
=> {
if (
import _
_
.
function isObject(value: unknown): value is object
export isObject

Returns true if value is a plain object, a class instance (excluding built-in classes like Date/RegExp), or an Object.create(null) result. Objects from other realms are also supported.

@seehttps://radashi.js.org/reference/typed/isObject

@example

isObject({}) // true
isObject(new Object()) // true
isObject(Object.create(null)) // true
isObject(new class {}) // true
isObject([]) // false
isObject(/.+/g) // false
isObject(new Date()) // false
isObject(new Map()) // false
isObject(new Set()) // false

@version12.1.0

isObject
(
nestedValue: unknown
nestedValue
)) {
context: _.TraverseContext<string | number | symbol>
context
.
TraverseContext<string | number | symbol>.skip: (obj?: object) => void

When the current value is a traversable object/iterable, this function can be used to skip traversing it altogether.

If the obj argument is provided, it marks the given object as skipped (instead of the current value), preventing it from being traversed.

skip
(
nestedValue: object
nestedValue
)
}
})
})
// Logs the following:
// a => { b: { c: 1 } }
// b => { c: 1 }

Exiting early

If your visitor callback returns false, traverse will exit early and also return false. This is useful if you found what you wanted, so you don’t need to traverse the rest of the objects.

import * as
import _
_
from 'radashi'
let
let found: any
found
= null
import _
_
.
function traverse(root: object, visitor: _.TraverseVisitor, options?: (_.TraverseOptions & {
rootNeedsVisit?: null;
}) | null, outerContext?: _.TraverseContext): boolean (+1 overload)
export traverse

Recursively visit each property of an object (or each element of an array) and its nested objects or arrays. By default, the only nested objects to be traversed are plain objects and arrays.

@seehttps://radashi.js.org/reference/object/traverse

@example

import { traverse } from 'radashi'
const root = { a: 1, b: { c: { d: [2] }, e: 3 } }
traverse(root, (value, key, parent, context) => {
console.log(key, '=>', value)
})
// Logs the following:
// a => 1
// b => { … }
// c => { … }
// d => [ 2 ]
// 0 => 2
// e => 3

@version12.2.0

traverse
(
any
root
,
value: unknown
value
=> {
if (
any
isWhatImLookingFor
(
value: unknown
value
)) {
let found: any
found
=
value: unknown
value
return false
}
})

Leave callbacks

If your visitor callback returns a function, it will be called once traverse has visited every visitable property/element within the current object. This is known as a “leave callback”.

Your leave callback can return false to exit traversal early.

import * as
import _
_
from 'radashi'
import _
_
.
function traverse(root: object, visitor: _.TraverseVisitor, options?: (_.TraverseOptions & {
rootNeedsVisit?: null;
}) | null, outerContext?: _.TraverseContext): boolean (+1 overload)
export traverse

Recursively visit each property of an object (or each element of an array) and its nested objects or arrays. By default, the only nested objects to be traversed are plain objects and arrays.

@seehttps://radashi.js.org/reference/object/traverse

@example

import { traverse } from 'radashi'
const root = { a: 1, b: { c: { d: [2] }, e: 3 } }
traverse(root, (value, key, parent, context) => {
console.log(key, '=>', value)
})
// Logs the following:
// a => 1
// b => { … }
// c => { … }
// d => [ 2 ]
// 0 => 2
// e => 3

@version12.2.0

traverse
({
arr: string[]
arr
: ['a', 'b'] }, (
value: unknown
value
,
key: string | number | symbol
key
) => {
if (
any
isArray
(
value: unknown
value
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('start of array')
return () => {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('end of array')
return false
}
} else {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
key: string | number | symbol
key
, '=>',
value: unknown
value
)
}
})
// Logs the following:
// start of array
// 0 => 'a'
// 1 => 'b'
// end of array