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

aggregate-array-extensions

v0.0.7

Published

Simple extensions to the Array prototype for aggregating elements

Downloads

3

Readme

aggregate-array-extensions

groupBy

Array.prototype.groupBy = function(keySelector, elementSelector, aggregateTransform)

Parameter|Description -|- keySelector|Function that returns a value to group on elementSelector|Function that is applied to each element of each group aggregateTransform|Function that is applied to each group at the end of processing

expect([1, 2, 3, 4, 5]
  .groupBy(x => x % 2 === 0 ? 'even' : 'odd'))
  .toEqual([
    { key: 'odd', value: [1, 3, 5] },
    { key: 'even', value: [2, 4] }
  ])

groupIntoObject

Array.prototype.groupIntoObject = function(keySelector, elementSelector, aggregateTransform)

Parameter|Description -|- keySelector|Function that returns a value to group on elementSelector|Function that is applied to each element of each group aggregateTransform|Function that is applied to each group at the end of processing

expect([1, 2, 3, 4, 5]
  .groupIntoObject(x => x % 2 === 0 ? 'even' : 'odd'))
  .toEqual({ 
    odd: [1, 3, 5],
    even: [2, 4]
  })

nearestUpperBound

Array.prototype.nearestUpperBound = function(value, selector)

Parameter|Description -|- value|Value to find nearest upper bound for selector|Function to select the bounded value. The array must be sorted by this value.

var source = [1, 2, 4, 8, 11, 32]
expect(source.nearestUpperBound(0)).toBe(-1)
expect(source.nearestUpperBound(3)).toBe(1)
expect(source.nearestUpperBound(9)).toBe(3)
expect(source.nearestUpperBound(100)).toBe(5)

nearestLowerBound

Array.prototype.nearestLowerBound = function(value, selector)

Parameter|Description -|- value|Value to find nearest lower bound for selector|Function to select the bounded value. The array must be sorted by this value.

  var source = [1, 2, 4, 8, 11, 32]
expect(source.nearestLowerBound(0)).toBe(0)
expect(source.nearestLowerBound(3)).toBe(2)
expect(source.nearestLowerBound(9)).toBe(4)
expect(source.nearestLowerBound(100)).toBe(-1)

findRange

Array.prototype.findRange = function(lower, upper, selector)

Parameter|Description -|- lower|The lower bound upper|The upper bound selector|Function to select the bounded value. The array must be sorted by this value.

var source = [1, 2, 4, 8, 11, 32]
expect(source.findRange(1, 32)).toEqual(source)
expect(source.findRange(3, 20)).toEqual([4, 8, 11])
expect(source.findRange(7)).toEqual([8, 11, 32])
expect(source.findRange(null, 7)).toEqual([1, 2, 4])

sum

Array.prototype.sum = function(selector)

Parameter|Description -|- selector|Function to select the value to sum

expect([1, 2, 3, 4].sum()).toBe(10)

mapIntoObject

Array.prototype.mapIntoObject = function(keySelector, valueSelector)

Parameter|Description -|- keySelector|Function to select the key on the result object valueSelector|Function to select the value to assign

expect([
    { v: 1 },
    { v: 2 },
    { v: 3 }
  ].mapIntoObject(x => x.v, x => x.v * 2))
  .toEqual({
    1: 2,
    2: 4,
    3: 6
  })