npm.io
0.4.2 • Published 1 year ago

@whi/object-walk

Licence
ISC
Version
0.4.2
Deps
0
Size
13 kB
Vulns
0
Weekly
0

Object.walk( obj, replacer )

This module is intended to help with recursively traversing an object and optionally replacing values.

Overview

Doesn't traverse do this?

Traverse is a great library, but it hasn't been updated since 2013. The internal assumptions (specifically around the immutable/copy feature) do not support newer types such as Node's Buffer class. You may see this error message when using traverse with Buffer

TypeError: Method get TypedArray.prototype.length called on incompatible receiver [object Object]

Another reason you might use Object.walk instead of traverse is to avoid dependence on this context. I don't see anything wrong with the object oriented approach, but this library is intended to feel like a native method. Other native implementations with callbacks (such as JSON.stringify) don't add contextual API's to this.

Install

npm i @whi/object-walk

Usage

import { walk } from '@whi/object-walk';

walk( some_obj, function ( key, value ) {
    return value;
});

Alternatively, attach walk to the native Object as a method.

import { bindNative } from '@whi/object-walk';
bindNative();

Object.walk( some_obj, function ( key, value ) {
    return value;
});
API Reference

See docs/API.md

Contributing

See CONTRIBUTING.md