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

array-prototype-functions

v2.13.2

Published

Array prototype augmentation for easier arithmetics and overall pleasure!

Downloads

31

Readme

Array prototype functions

The API for arrays in JavaScript is quite vast. Functions such as filter(), map() and reduce() are quite powerful. The only problem with them is that they are very very generic.

The purpose of this library is to augment the Array.prototype to provide functions that have a more distinct meaning, such as finding the min or max value in an array or grouping an array of objects by the key.

I wrote those functions far too many times and have gotten sick maintaining them throughout multiple projects.

Usage

The usage is dead simple. Just import the package and you're done:

// ES6 module
import 'array-prototype-functions/avg'
import 'array-prototype-functions/first'
import 'array-prototype-functions/group-by'
import 'array-prototype-functions/group-by-as-map'
import 'array-prototype-functions/head'
import 'array-prototype-functions/last'
import 'array-prototype-functions/max'
import 'array-prototype-functions/median'
import 'array-prototype-functions/min'
import 'array-prototype-functions/pluck'
import 'array-prototype-functions/sort-by'
import 'array-prototype-functions/static'
import 'array-prototype-functions/sum'
import 'array-prototype-functions/tail'
import 'array-prototype-functions/uniq'

// node.js
require('array-prototype-functions/avg')
require('array-prototype-functions/first')
require('array-prototype-functions/group-by')
require('array-prototype-functions/group-by-as-map')
require('array-prototype-functions/head')
require('array-prototype-functions/last')
require('array-prototype-functions/max')
require('array-prototype-functions/median')
require('array-prototype-functions/min')
require('array-prototype-functions/pluck')
require('array-prototype-functions/sort-by')
require('array-prototype-functions/static')
require('array-prototype-functions/sum')
require('array-prototype-functions/tail')
require('array-prototype-functions/uniq')

Please be aware that there are some dependencies between the imported functions (see below in the API section). You will need to import the dependencies yourself.

Once the file is executed the Array.prototype is automatically augmented.

Please note that if the functions are already present they will not be overriden! This means that if at any point in time the Array.prototype will gain functions like head, tail or any other that is provided by this library they will take precedense over the ones from this library.

Why augmenting the Array.prototype?

The question you might be asking is why do I augment the prototype instead of providing standalone functions that would do the same? The reason for it is simple: readability of the code that uses them. Here are a few examples:

const a = [ 1, 2, 3, 4 ]

a.filter(x => x % 2 == 0).sum()
a.filter(x => x % 2 == 0).avg()
a.filter(x => x % 2 == 0).median()

const a = [ { x: 1 }, { x: 2 } ]
a.sum('x')

This way the built-in functions can directly be the source and the syntax flows nicely with the rest of the code.

API

Array.prototype.head()

Dependencies: none

Returns the first element of the array or null if the array is empty.

Array.prototype.tail()

Dependencies: none

Returns all elements of the array besides the first element. Returns an empty array if the original array was empty or had just one element.

Array.prototype.first(n = null)

Dependencies: none

Returns the first element of the array or null if the array is empty. If the n parameter is given it returns an array of n or less elements (if the array doesn't contain as many elements to begin with)

Array.prototype.last(n = null)

Dependencies: none

Returns the last element of the array or null if the array is empty. If the n parameter is given it returns an array of n or less elements (if the array doesn't contain as many elements to begin with)

Array.prototype.sum(fieldOrMapper = null)

Dependencies: none

Returns a sum of elements in the array. The fieldOrMapper can be one of:

  • null (or not provided) which means the elements will be treated as sumable
  • string - in which case it will be a name of field to extract from the objects in the array and then to sum them up
  • a function that takes the array element as parameter and shall return a sum-able value

Array.prototype.avg(fieldOrMapper = null)

Dependencies: sum

Returns an average of values from the array (sum of all divided by the number of elements). Returns null if the array was empty. The fieldOrMapper can be one of:

  • null (or not provided) which means the elements will be treated as sum-able
  • string - in which case it will be a name of field to extract from the objects in the array and then to sum them up
  • a function that takes the array element as parameter and shall return a sum-able value

Array.prototype.pluck(fieldOrMapper)

Dependencies: none

Returns an array containing objects having a subset of properties of the original elements of the array. The fieldOrMapper can be one of:

  • string - in which case it will be a name of field to extract from the objects in the array
  • a function that takes the array element as parameter and shall return the values to pluck

Array.prototype.sortBy(fieldOrMapper = null, order = 1)

Dependencies: none

Returns a copy of the array that is sorted. The fieldOrMapper can be one of:

  • null (or not provided) which means the elements will be treated as comparable
  • string - in which case it will be a name of field to extract from the objects in the array and then to sort them up
  • a function that takes the array element as parameter and shall return a comparable value

The order parameter is either a positive or negative number. If it is negative it will reverse the order of sorted elements.

Array.ptototype.min(fieldOrMapper = null)

Dependencies: sort-by, first

Returns the smallest element in the list. The fieldOrMapper can be one of:

  • null (or not provided) which means the elements will be treated as comparable
  • string - in which case it will be a name of field to extract from the objects in the array and then to sort them up
  • a function that takes the array element as parameter and shall return a comparable value

Array.prototype.max(fieldOrMapper = null)

Dependencies: sort-by, first

Returns the largest element in the list. The fieldOrMapper can be one of:

  • null (or not provided) which means the elements will be treated as comparable
  • string - in which case it will be a name of field to extract from the objects in the array and then to sort them up
  • a function that takes the array element as parameter and shall return a comparable value

Array.prototype.median(fieldOrMapper = null)

Dependencies: sort-by

Returns the median element in the list. The fieldOrMapper can be one of:

  • null (or not provided) which means the elements will be treated as comparable
  • string - in which case it will be a name of field to extract from the objects in the array and then to sort them up
  • a function that takes the array element as parameter and shall return a comparable value

Array.prototype.groupBy(fieldOrMapper)

Dependencies: none

Returns a dictionary of lists of objects from the array grouped by a field name. The fieldOrMapper is required and can be one of:

  • string - in which case it will be a name of field to extract from the objects in the array and then to sort them up
  • a function that takes the array element as parameter and shall return a string to group objects by

Objects that don't have the field in them will be skipped.

Array.prototype.groupByAsMap(field)

Dependencies: none

Returns a Map of lists of objects from the array grouped by a field name. The fieldOrMapper is required and can be one of:

  • string - in which case it will be a name of field to extract from the objects in the array and then to sort them up
  • a function that takes the array element as parameter and shall return a value to group objects by

The difference to groupBy(field) is that the return value is an instance of Map<any, T[]> which means that the original type of the values of the field is also preserved and not converted to a string. This also means that if throughout the objects in the source array field values have different types they will be treated differently and you'll end up having more than one item in the result.

Objects that don't have the field in them will be skipped.

Array.prototype.uniq(fieldOrMapper = null)

Dependencies: none

Returns a list of unique values from the original array. The fieldOrMapper can be one of:

  • null (or not provided) which means the elements will be treated as comparable
  • string - in which case it will be a name of field to extract from the objects in the array and then to sort them up
  • a function that takes the array element as parameter and shall return a comparable value

If the fieldOrMapper is specified the final list will return only values of the specified field or the values returned by the mapper.

Array.generate<T>(length: number, init: index => T | null): T[]

Dependencies: none

Returns a new list with elements initialized with the init method.

For example, to generate 10 random numbers:

const values = Array.generate(10, Math.random)

or to generate a list of numbers from 0 to 9

const values = Array.generate(10, index => index)

Additional information

There's an extensive suite of tests in this package. You can run them by running the issuing the following command:

$ npm run test:unit

or run them continuously while doing changes by running the following command:

$ npm run test:unit:watch

To build the latest output files run

$ npm run build