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

@codura/array-method-extension

v1.0.7

Published

Extra array methods for JavaScript and TypeScript

Downloads

324

Readme

Array Method Extensions

This package provides a set of useful methods to extend the functionality of the Array class in TypeScript.

Using these methods, you can easily group, map, distinct, merge, and sum arrays. It reduces code complexity and makes your code more readable.

Index

Installation

Using Yarn

yarn add @codura/array-method-extension

Using npm

npm install @codura/array-method-extension

Usage

export * from '@codura/array-method-extension';

Methods

distinctBy

Remove duplicates from an Array using a key function

const usersWithDuplicates = [
  { id: '1', gender: 'male', name: 'John' },
  { id: '2', gender: 'female', name: 'Jane' },
  { id: '1', gender: 'male', name: 'John' },
];
const usersWithoutDuplicates = usersWithDuplicates.distinctBy(user => user.id); // [{ id: '1', gender: 'male', name: 'John' }, { id: '2', gender: 'female', name: 'Jane' }]

Remove duplicates from an Array using a key function and a value function

const usersWithDuplicates = [
  { id: '1', gender: 'male', name: 'John' },
  { id: '2', gender: 'female', name: 'Jane' },
  { id: '1', gender: 'male', name: 'John' },
];
const namesWithoutDuplicates = usersWithDuplicates.distinctBy(user => user.id, user => user.name); // ['John', 'Jane']

Check if two arrays, containing decimals, are equal using decimal.js

import Decimal from 'decimal.js';

const decimals1 = [
  new Decimal(23.56457),
  new Decimal(12.34543),
  new Decimal(34.12345),
];
const decimals2 = [
  new Decimal(12.34543),
  new Decimal(23.56457),
  new Decimal(34.12345),
];
const areEqual = decimals1.equals(decimals2); // true

equals

Check if two arrays, containing custom comparable objects, are equal

class ComparableItem {
  constructor(readonly item: string) {
  }

  equals(comparableItem: ComparableItem) {
    return this.item === comparableItem.item;
  }
}

const comparableItems1 = [new ComparableItem('a'), new ComparableItem('b')];
const comparableItems2 = [new ComparableItem('b'), new ComparableItem('a')];
const areEqual = comparableItems1.equals(comparableItems2); // true

Check if two arrays, containing decimals, are equal using decimal.js

import Decimal from 'decimal.js';

const decimals1 = [
  new Decimal(23.56457),
  new Decimal(12.34543),
  new Decimal(34.12345),
];
const decimals2 = [
  new Decimal(12.34543),
  new Decimal(23.56457),
  new Decimal(34.12345),
];
const areEqual = decimals1.equals(decimals2); // true

groupBy

Group an Array to a Map using a key function

const users = [
  { id: '1', gender: 'male', name: 'John' },
  { id: '2', gender: 'female', name: 'Jane' },
  { id: '3', gender: 'male', name: 'Joe' },
];
const usersByGroup = users.groupBy(user => user.gender); // Map([['male', [{id: '1', gender: 'male', name: 'John'}, {id: '3', gender: 'male', name: 'Joe'}]], ['female', [{id: '2', gender: 'female, name: 'Jane'}]]])

Group an Array to a map using a key function and a value function

const users = [
  { id: '1', gender: 'male', name: 'John' },
  { id: '2', gender: 'female', name: 'Jane' },
  { id: '3', gender: 'male', name: 'Joe' },
];
const userNamesById = users.groupBy(user => user.gender, user => user.name); // Map([['male', ['John', 'Joe']], ['female', ['Jane']]])

mapBy

Map an Array to a map using a key function

const users = [
  { id: '1', name: 'John' },
  { id: '2', name: 'Jane' },
  { id: '3', name: 'Joe' },
];
const usersById = users.mapBy(user => user.id); // Map([['1', {id: '1', gender: 'male', name: 'John'}], ['2', {id: '2', gender: 'female, name: 'Jane'}], ['3', {id: '3', gender: 'male', name: 'Joe'}]])

Map an Array to a map using a key function and a value function

const users = [
  { id: '1', name: 'John' },
  { id: '2', name: 'Jane' },
  { id: '3', name: 'Joe' },
];
const userNamesById = users.mapBy(user => user.id, user => user.name); // Map([['1', 'John'], ['2', 'Jane'], ['3', 'Joe']])

mergeBy

Merge custom mergeable objects in an Array

class Drink {
  constructor(readonly name: string, readonly amount: number) {
  }

  merge(drink: Drink) {
    return new Drink(this.name, this.amount + drink.amount);
  }
}

const drinks = [
  new Drink('beer', 5),
  new Drink('wine', 2),
  new Drink('beer', 3),
];
const sum = drinks.merge(drink => drink.name); // [Drink('beer', 8), Drink('wine', 2)]

sum

Sum custom addable objects in an Array

class Payment {
  constructor(readonly note: string, readonly amount: number) {
  }

  add(payment: Payment) {
    return new Payment(this.note, this.amount + payment.amount);
  }
}

const payments = [
  new Payment('first', 100),
  new Payment('second', 200),
  new Payment('third', 300),
];
const sum = payments.sum(new Payment('sum', 0)); // Payment('sum', 600)

Sum decimals in an Array using decimal.js

import Decimal from 'decimal.js';

const decimals = [
  new Decimal(23.56457),
  new Decimal(12.34543),
  new Decimal(34.12345),
];
const sum = payments.sum(new Decimal(0)); // Decimal(70.03345)