Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

advarr

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

advarr

Advanced Array utility functions. Extra arguments for map and forEach including first, last, brk, odd, even, penultimate, and more!

  • 0.4.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
156
decreased by-8.77%
Maintainers
1
Weekly downloads
 
Created
Source

advarr

Advanced Array utility functions.

features

  • intuitive
  • light weight
  • lots of parameters
  • break out of multiple loops at once

background

I write a lot of NodeJS scripts for processing and transforming data. They need to run fast, but the most important thing to me is that the code is readable. I found myself repeating a lot of logic when using Array's forEach and map, like checking if an element is the first in the array and figuring out what percentage of the forEach loop is complete. Instead of repeating myself (and in order to simplify my scripts), I wrote this library. I hope you find it useful, too.

install

npm install advarr

usage

const { forEach } = require("advarr");

const people = ["Peter", "Paul", "Mary"];
let text = "";
forEach(people, ({ value: person, first, last }) => {
  if (!first && !last) text += ",";
  if (last) text += " and";
  if (!first) text += " ";
  text += person;
});
// text is "Peter, Paul and Mary"

speed

A manual for (let i = 0; i < arr.length; i++) will likely always be the fastest way to iterate over values. If speed is the most important priority, I recommend using a for loop. That said, I have written this library to be as fast as possible while providing an intuitive and large set of callback arguments. Several of the callback values like length and percent are actually getters and only calculated if you use them.

functions

Here's a complete list of functions

  • every
  • filter
  • find
  • findIndex
  • flatMap
  • forEach
  • map
  • some

parameters

Here's a complete list of parameters passed into the callback function

namedescription
valuecurrent element in the array and same as currentValue. You can also use it, item, element, or currentValue.
iindex of current element in the array and same as index. You can also use index.
arraythe original array and same as array
percentpercentage of the array processed including current element
lengthlength of the array
previousprevious element in the array
nextnext element in the array
beforepart of the array before the current element
afterpart of the array after the current element
brkfunction to break the loop. pass in a number to specify how many loops to break
first, second, ... tenthordinal numbers
lastlast element in the array
penultimatesecond to last element in the array
antepenultimatethird to last element in the array
oddindex is odd (starting with the second element in the array because the second element has an index of 1)
eveneven number element (starting with the first element in the array because the first element has an index of 0)
firstValuethe first value of the array. You can also use firstElement, firstItem, or firstIt.
lastValuethe last value of the array You can also use lastElement, lastItem, or lastIt.

more examples

breaking out of multiple loops

const { forEach } = require("advarr");

const nums = [0, 1, 2, 3, 4];
forEach(nums, ({ value: v1 }) => {
  forEach(nums, ({ value: v2 }) => {
    forEach(nums, ({ value: v3, brk }) => {
      // break out of the two inner loops (but not the top most one)
      if (Math.random() < 0.5) brk(2);
    });  
  });
});

more examples coming soon!

Keywords

FAQs

Package last updated on 10 Apr 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc