npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@qpyracuk/iterator

v0.1.5

Published

Universal iterator for all data types

Downloads

39

Readme

@qpyracuk/iterator

A universal iterator for complex objects. JavaScript out of the box supports iterating over object fields only at the first nesting level. Perhaps you once needed to traverse the entire object in order to find a specific value, or for custom serialization. To do this, each time I had to write my own iterator. This library solves exactly this problem! It supports primitive types and data structures such as objects, arrays, Map and Set. When traversed, the iterator uses non-recursive traversal algorithms, so you don't have to worry about call stack overflows!

Installation

This is a JS library available through the npm registry. Before installation, you need to download and install Node.js. Requires Node.js v0.1.0 or higher.

If this is a completely new project, be sure to create a package.json file using the npm init command.

To install the package, enter the npm install @qpyracuk/iterator command in the console.

npm install @qpyracuk/iterator

Features

  • Creating a depth-first iterator;
  • Creating a breadth-first iterator;
  • Receiving a ready sequence of nodes in the order corresponding to the selected bypass algorithm;
  • Obtaining a ready-made sequence of primitive values stored in nodes in the order corresponding to the selected traversal algorithm;
  • Takes into account link loops;
  • Non-recursive traversal algorithm;
  • Does not change the structure and data in the original object;
  • Support for data structures such as Object, Array, Map, Set;
  • Support for CommonJS and ESMAScript modules.

Quick Start

After installing the package, import the library.

For ESMAScript modules:

import Iterator from '@qpyracuk/iterator';

For CommonJS modules:

const Iterator = require('@qpyracuk/iterator');

Usage example

We create an object that needs to be traversed

const object = {
  primitive: '1',
  object: {
    primitive: 3,
    set: new Set([1, 2, new Map([['key', { field: { value: 100 } }]])]),
    map: new Map([['key', { value: 10 }]])
  }
};

Traversal in depth

// Create depth-first iterator
const depthIterator = Iterator.createDepthFirstIterator(object);
// Traversal of all nodes
while (depthIterator.has()) {
  console.log('depth', depthIterator.next());
}
// Resetting the current iterator state
depthIterator.reset();
// Traversing the primitive values of all nodes
while (depthIterator.has()) {
  console.log('depth-primitive', depthIterator.nextLiaf());
}

Traversal in breadth

// Create breadth-first iterator
const breadthIterator = Iterator.createBreadthFirstIterator(object);
// Traversal of all nodes
while (breadthIterator.has()) {
  console.log('breadth', breadthIterator.next());
}
// Resetting the current iterator state
breadthIterator.reset();
// Traversing the primitive values of all nodes
while (breadthIterator.has()) {
  console.log('breadth-primitive', breadthIterator.nextLiaf());
}

Getting ready sequences

// The sequence of all nodes of an object during depth-first traversal
console.log(createDepthFirstSequenceNode(object));
// The sequence of all nodes of an object during a breadth-first traversal
console.log(createBreadthFirstSequenceNode(object));
// Sequence of all PRIMITIVE VALUES, in depth-first traversal
console.log(createDepthFirstSequenceLiaf(object));
// Sequence of all PRIMITIVE VALUES, in a breadth-first traversal
console.log(createBreadthFirstSequenceLiaf(object));

Return value

Iterators return an object corresponding to the internal interface INode.

interface INode {
  value: any; // Node value.
  key: string; // Name of the object field (for the root object key === 'root').
  level: number; // Nesting level of the current value.
  type:
    | 'string' // If the current value is a string.
    | 'number' // If the current value is a number.
    | 'boolean' // If the current value is a boolean type.
    | 'symbol' // If the current value is a character.
    | 'bigint' // If the current value is BigInt.
    | 'undefined' // If the current value is undefined.
    | 'null' // If the current value is null.
    | 'object' // If the current node is an object.
    | 'map' // If the current node is a key-value map.
    | 'set' // If the current node is a Set collection.
    | 'array' // If the current node is an array.
    | 'function' // If the current node is a function.
    | ''; // May return in case of unknown data type.
}

Iterator builder methods

All methods take 2 parameters as input root - any value (required). depth - maximum depth level when traversing (default: "Infinity").

createDepthFirstIterator(root, depth): DepthFirstIterator

Creates a depth-first traversal iterator object.

createBreadthFirstIterator(root, depth): BreadthFirstIterator

Creates a breadth-first traversal iterator object.

createDepthFirstSequenceNode(root, depth): INode[]

Creates an array of INode interface objects. The array is a linear sequence of ALL NODES obtained during a depth-first traversal of the object tree.

createBreadthFirstSequenceNode(root, depth): INode[]

Creates an array of INode interface objects. The array is a linear sequence of ALL NODES obtained during a breadth-first traversal of the object tree.

createDepthFirstSequenceLiaf(root, depth): INode[]

Creates an array of INode interface objects. An array is a linear sequence of ALL PRIMITIVE VALUES obtained from a depth-first traversal of the object tree.

createBreadthFirstSequenceLiaf(root, depth): INode[]

Creates an array of INode interface objects. An array is a linear sequence of all PRIMITIVE VALUES obtained from a breadth-first traversal of the object tree.

Iterator methods

has(): boolean

Checks whether the end of the traversal has been reached. true - the end of the tree has not been reached. false - the end of the tree has been reached.

next(): INode | undefined

Returns an INode object or undefined if the end of the tree is reached.

nextLiaf(): INode | undefined

Returns an INode object corresponding only to the primitive values within the object, or undefined if the end of the tree is reached.

get(): INode | undefined

Returns the last node of an object or undefined if the end of the tree is reached.

reset(): void

Resets the iterator state to the initial state

Traversal depth

Each Iterator class method takes a second parameter, depth. By default it is set to "Infinity", which tells the iterator to iterate over the object completely. But you can artificially limit the traversal depth by specifying a numerical value depth > 0.

Algorithms

Iterators implement two non-recursive traversal algorithms:

  1. Depth-first graph traversal using a stack;
  2. Breadth-first graph traversal using a queue.

Due to the fact that the iterator does not use recursion, it is guaranteed that there is no possibility of a call stack overflow.

Protection against reference loops

You don't have to worry about the iterator going into a loop if two objects refer to each other. Since each “vertex of the graph” is written into the Set structure, and if the iterator hits an already processed vertex, the iterator switches to processing the next one.

TypeScript

You can import types from the library

import type { INode, TNode, IIterator } from '@qpyracuk/iterator'

Author

The author of the library is Pobedinskiy David.

Bugs

If you encounter unexpected errors, please let me know. By e-mail [email protected] or in Telegram.

Support the author

If my work has helped you make your life easier, you can support me with your donations.

Boosty Patreon

Search npm for other libraries with the @qpyracuk prefix. Perhaps you will find something useful for your project.