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

@oscbco/get-nested-array-element-by-position

v0.0.3

Published

This small library allows for traversing a structure of nested arrays as a single one-dimensional array. Making it possible to use a single for loop to iterate through all the nested array nodes

Downloads

2

Readme

get-nested-array-element-by-position

Project website at oscbco-libraries.gitlab.io/get-nested-array-element-by-position/

This small library allows for traversing a structure of nested arrays as a single one-dimensional array. Making it possible to use a single for loop to iterate through all the nested array nodes

Nodes are returned by their position in the nested structure.

Nodes in the current array will be returned first, however if a node has a "child array" then nodes in the "child array" are returned before continuing with the current array. This is true for their children as well.

Required properties for these array structures:

An object with these 2 properties:

value: This is the payload of the node.

child: Reference to the child array, or if it doesn't have one then it MUST be null

Installation:

  npm install --save @oscbco/get-nested-array-element-by-position

You can import it like this:

  let getNestedArrayElementByPosition = require('@oscbco/get-nested-array-element-by-position');

For example, the following code:

  const getNestedArrayElementByPosition = require('@oscbco/get-nested-array-element-by-position');

  const collection = [
    {
      value: '/public',
      child: [
        {
          value: '/public/js',
          child: [
            {
              value: '/public/js/index.js',
              child: null
            }
          ]
        }
      ]
    },
    {
      value: '/public/images',
      child: [
        {
          value: '/public/images/logo.png',
          child: null
        },
        {
          value: '/public/images/header.png',
          child: null
        },
        {
          value: '/public/images/background.png',
          child: null
        }
      ]
    },
    {
      value: '/source',
      child: [
        {
          value: '/source/index.js',
          child: null
        }
      ]
    }
  ];

  let counter = 0;
  let node = null;
  let concatenatedNodes = '';
  do {
    node = getNestedArrayElementByPosition.findNode({collection, requestedIndex: counter});
    concatenatedNodes += node.value ? node.value + '\n' : '';
    counter++;
  } while (node.keypath !== undefined);
  console.log(concatenatedNodes);

  console.log("Example: Node at position 6\n", JSON.stringify(getNestedArrayElementByPosition.findNode({collection, requestedIndex: 6}), null, 2));

  console.log("Example: Node at a nonexistent position\n", JSON.stringify(getNestedArrayElementByPosition.findNode({collection, requestedIndex: 9}), null, 2));

Results in:

/public
/public/js
/public/js/index.js
/public/images
/public/images/logo.png
/public/images/header.png
/public/images/background.png
/source
/source/index.js

Example: Node at position 6
{
  "value": "/public/images/background.png",
  "count": 6,
  "keypath": [
    1,
    2
  ]
}
Example: Node at a nonexistent position
{}

findNode returns the value property of the node as well as the count and keypath.

count and keypath are used by the library to speed up subsequent lookups because we don't have to start counting from zero, instead we use the last known count and keypath and start from there.

If a node at a given position does not exist, then findNode returns:

  {
    value: undefined,
    count: undefined,
    keypath: undefined
  }

ToDo:

  • Wrap the array structure in a class and add CRUD methods
  • A method to count all elements

Development

If you want more info on how this project is structured and how this helps with development, testing, building the documentation and its own static website visit: http://oscbco-boilerplate.gitlab.io/webpack-npm-library-boilerplate/