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

utils4array

v1.2.0

Published

This is a collection of utility functions for working with arrays in JavaScript. The functions are organized into the following categories:

Downloads

2

Readme

Array Utilities

This is a collection of utility functions for working with arrays in JavaScript. The functions are organized into the following categories:

Categories

Transforming: functions that transform arrays in some way, such as shuffling, grouping, or sorting

Filtering: functions that filter arrays based on some criteria, such as removing falsy values or finding duplicates

Manipulating: functions that manipulate arrays in some way, such as shifting elements or finding the difference between two arrays

Accessing: functions that access elements in an array, such as getting the maximum or minimum value, or accessing the last element

Nested Arrays: functions that work with nested arrays, such as flattening or creating nested arrays from flat data

Installation

npm install utils4array

Usage

To use the functions, import them from the array-utilities package and pass in the array you want to work with as a parameter. For example:

const arrayUtils = require('utils4array');

const myArray = [1, 2, 3, 4, 5];
const shuffledArray = arrayUtils.shuffle(myArray);
console.log(shuffledArray);
// Output: [4, 1, 2, 5, 3]

You can find the documentation for all the functions in the API reference section below.

API Reference

Transforming

shuffle(array)

  • Returns a shuffled version of the input array.

groupBy(array, callback)

  • Groups the elements in the input array by the result of calling the given callback function on each element. Returns an object where the keys are the grouping values and the values are arrays of elements that match that grouping value.

sortBy(array, callback)

  • Sorts the input array by the result of calling the given callback function on each element.

Filtering

findduplicates(array)

  • Returns an array of duplicate elements in the input array.

removeFalsy(array)

  • Returns a new array with all falsy values removed.

countBy(array, callback)

  • Returns an object with a count of how many times each value appears in the input array, grouped by the result of calling the given callback function on each element.

difference(array1, array2)

  • Returns an array of elements that are in array1 but not in array2.

intersection(...arrays)

  • Returns an array of elements that are in all of the input arrays.

pluck(array, property)

  • Returns an array of values for the given property on each element in the input array.

unique(...array)

  • Returns an array of unique elements from the input arrays.

sample(array, n)

  • Returns n random elements from the input array.

Manipulating

shift(array, positions)

  • Returns a new array with the elements of the input array shifted to the left by positions positions.

chunk(array, size)

  • Returns an array of arrays, where each sub-array contains size elements from the input array.

setparent(array, key)

  • Returns an object where each key is the value of the key property on each element in the input array, and the value is the corresponding element.

zip(...arrays)

  • Returns a new array where each element is an array containing the corresponding elements from the input arrays.

Accessing

max(...array)

  • Returns the maximum value in the input array.

min(...array)

  • Returns the minimum value in the input array.

last(array)

  • Returns the last element in the input array.

sum(array)

  • Returns the sum of all values in the input array.

Nested Arrays

flatten(nestedData)

  • Returns a flat list from a nested list
example
const nestedData = [
  {
    id: 1,
    name: 'Parent 1',
    children: [
      {
        id: 2,
        name: 'Child 1',
        children: [
          {
            id: 4,
            name: 'Grandchild 1'
          },
          {
            id: 5,
            name: 'Grandchild 2'
          }
        ]
      },
      {
        id: 3,
        name: 'Child 2'
      }
    ]
  },
  {
    id: 6,
    name: 'Parent 2'
  }
];

const flattenedData = flatten(nestedData);
console.log(flattenedData);
// Output: [
//   { id: 1, name: 'Parent 1' },
//   { id: 2, name: 'Child 1' },
//   { id: 4, name: 'Grandchild 1' },
//   { id: 5, name: 'Grandchild 2' },
//   { id: 3, name: 'Child 2' },
//   { id: 6, name: 'Parent 2' }
// ]

makeNestedArray(data)

  • Return a nested array. this does exactly the opposite of the flatten function

People

License

ISC