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

ts-array-extensions

v2.2.3

Published

A library that adds common array higher order function support beyond the built in functions in js, e.g. `map`, `filter`, by extending the `Array` prototype.

Downloads

2,660

Readme

Typescript Array Extensions

A library that adds common array higher order function support beyond the built in functions in js, e.g. map, filter, by extending the Array prototype.

Inspired by swift and LINQ.

Installing

npm i ts-array-extensions

Using

You can import all the functions

import 'ts-array-extensions';

or individual ones as you need them

import 'ts-array-extensions/compactMap';

Array.prototype extensions

any

Returns true if the array contains any elements.

[1, 2, 3].any();
// true

[].any();
// false

compact

Returns elements of the array that are not null or undefined.

[1, null, 2].compact();
// [1, 2]

compactMap

Maps elements and returns results that are not null or undefined.

[1, 2, 3].compactMap(v => {
  if (v % 2 !== 0) return v;
});
// [1, 3]

compactMapAsync

Same as compactMap but works with promises. Will await async callbacks.

await [1, 2, 3].compactMapAsync(async v => {
  await /* something */
  return v;
});
// [1, 2, 3]

cume

Returns the a running total of all the elements in the array, optionally mapping them first with a callback

[1, 10, 100].cume();
// [1, 11, 111]

[1, 10, 100].cume(v => v * 10);
// [10, 110, 1110]

distinct

Returns elements of the array that are unique, using a comparer function if supplied, or === if not.

[1, 1, 2, 2].distinct();
// [1, 2]

[
  { day: 1, month: 1, year: 1979 },
  { day: 1, month: 1, year: 1979 },
  { day: 2, month: 1, year: 1979 },
  { day: 2, month: 1, year: 1979 }
].distinct(
  (a, b) => a.day === b.day && a.month === b.month && a.year === b.year
);
// [
//   { day: 1, month: 1, year: 1979 },
//   { day: 2, month: 1, year: 1979 }
// ]

except

Returns values from the first array, that aren't present in the second, using a comparer function if supplied, or === if not.

[1, 2, 3].except([2, 3, 4]);
// [1]

[
  { day: 1, month: 1, year: 1979 },
  { day: 2, month: 1, year: 1979 }
].except(
  [
    { day: 2, month: 1, year: 1979 },
    { day: 3, month: 1, year: 1979 }
  ],
  (a, b) => a.day === b.day && a.month === b.month && a.year === b.year
);
// [
//   { day: 1, month: 1, year: 1979 }
// ]

first

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

['morning', 'afternoon'].first();
// 'morning'

forEachAsync

Same as forEach but works with promises. Will await async callbacks.

await [1, 2, 3].forEachAsync(async v => {
  await /* something */
});

groupBy

Returns an array of Group<K, V> extracted from the array using a callback, and an optional key comparer.

[
  { make: 'Ford', model: 'Fiesta' },
  { make: 'Ford', model: 'Focus' },
  { make: 'Vauxhall', model: 'Corsa' },
  { make: 'Vauxhall', model: 'Astra' }
].groupBy(k => k.make);
// [
//   {
//     key: 'Ford',
//     values: [
//       { make: 'Ford', model: 'Fiesta' },
//       { make: 'Ford', model: 'Focus' }
//     ]
//   },
//   {
//     key: 'Vauxhall',
//     values: [
//       { make: 'Vauxhall', model: 'Corsa' },
//       { make: 'Vauxhall', model: 'Astra' }
//     ]
//   }
// ]

innerJoin

Matches two array using a callback and returns the joined results.

const leftData = [
  { id: 1, name: 'Apples', groupId: 1 },
  { id: 2, name: 'Oranges', groupId: 1 },
  { id: 3, name: 'Cornflakes', groupId: 2 },
  { id: 4, name: 'Random', groupId: null }
];
const rightData = [
  { id: 1, name: 'Produce' },
  { id: 2, name: 'Grocery' },
  { id: 3, name: 'Confectionary' }
];

leftData.innerJoin(rightData, (l, r) => l.groupId === r.id);
// [
//   {
//     left: { id: 1, name: 'Apples', groupId: 1 },
//     right: { id: 1, name: 'Produce' }
//   },
//   {
//     left: { id: 2, name: 'Oranges', groupId: 1 },
//     right: { id: 1, name: 'Produce' }
//   },
//   {
//     left: { id: 3, name: 'Cornflakes', groupId: 2 },
//     right: { id: 2, name: 'Grocery' }
//   }
// ];

interleave

Adds new elements in between each element of an array.

['one', 'two', 'three'].interleave(() => 'and');
// ['one', 'and', 'two', 'and', 'three']

leftJoin

Matches two array using a callback and returns the joined results and unmatched results from the left array.

const leftData = [
  { id: 1, name: 'Apples', groupId: 1 },
  { id: 2, name: 'Oranges', groupId: 1 },
  { id: 3, name: 'Cornflakes', groupId: 2 },
  { id: 4, name: 'Random', groupId: null }
];
const rightData = [
  { id: 1, name: 'Produce' },
  { id: 2, name: 'Grocery' },
  { id: 3, name: 'Confectionary' }
];

leftData.leftJoin(rightData, (l, r) => l.groupId === r.id);
// [
//   {
//     left: { id: 1, name: 'Apples', groupId: 1 },
//     right: { id: 1, name: 'Produce' }
//   },
//   {
//     left: { id: 2, name: 'Oranges', groupId: 1 },
//     right: { id: 1, name: 'Produce' }
//   },
//   {
//     left: { id: 3, name: 'Cornflakes', groupId: 2 },
//     right: { id: 2, name: 'Grocery' }
//   },
//   {
//     left: { id: 4, name: 'Random', groupId: null },
//     right: null
//   }
// ];

mapAsync

Same as map but works with promises. Will await async callbacks.

await [1, 2, 3].map(async v => {
  await /* something */
  return v * 2;
});
// [2, 4, 6]

max

Returns the highest element in the array

[-10, 0, 10].max();
// 10

['a', 'b', 'c'].max();
// 'c'

[].max();
// null

min

Returns the lowest element in the array

[-10, 0, 10].min();
// -10

['a', 'b', 'c'].min();
// 'a'

[].min();
// null

none

Returns false if the array contains any elements.

[1, 2, 3].none();
// false

[].none();
// true

outerJoin

Matches two array using a callback and returns the joined results and unmatched results from both arrays.

const leftData = [
  { id: 1, name: 'Apples', groupId: 1 },
  { id: 2, name: 'Oranges', groupId: 1 },
  { id: 3, name: 'Cornflakes', groupId: 2 },
  { id: 4, name: 'Random', groupId: null }
];
const rightData = [
  { id: 1, name: 'Produce' },
  { id: 2, name: 'Grocery' },
  { id: 3, name: 'Confectionary' }
];

leftData.outerJoin(rightData, (l, r) => l.groupId === r.id);
// [
//   {
//     left: { id: 1, name: 'Apples', groupId: 1 },
//     right: { id: 1, name: 'Produce' }
//   },
//   {
//     left: { id: 2, name: 'Oranges', groupId: 1 },
//     right: { id: 1, name: 'Produce' }
//   },
//   {
//     left: { id: 3, name: 'Cornflakes', groupId: 2 },
//     right: { id: 2, name: 'Grocery' }
//   },
//   {
//     left: { id: 4, name: 'Random', groupId: null },
//     right: null
//   },
//   {
//     left: null,
//     right: { id: 3, name: 'Confectionary' }
//   }
// ];

sortBy

Returns a new array, sorted by the result of a callback function.

[
  { name: 'Brian' },
  { name: 'Albert' },
  { name: 'Charlie' }
].sortBy(v => v.name);
// [
//   { name: 'Albert' },
//   { name: 'Brian' },
//   { name: 'Charlie' }
// ]

sum

Returns the total of all the elements in the array, optionally mapping them first with a callback

[1, 10, 100].sum();
// 111

[1, 2, 3, 4].sum(v => v % 2);
// 2

toRecord

Returns a Record<K, V> extracted from the array using callbacks.

Similar in functionality to groupBy, but returns a Record, and the last value with a matching key wins

[
  { name: 'Chloe', age: 19 },
  { name: 'Daniel', age: 16 },
  { name: 'Polly', age: 12 },
  { name: 'Lottie', age: 10 },
  { name: 'Theodore', age: 8 }
].toRecord(
  k => k.name,
  v => v.age
);
// {
//   Chloe: 19,
//   Daniel: 16,
//   Polly: 12,
//   Lottie: 10,
//   Theodore: 8
// }

union

Returns only the values present in both arrays, using a comparer function if supplied, or === if not.

[1, 2, 3].union([2, 3, 4]);
// [2, 3]

[
  { day: 1, month: 1, year: 1979 },
  { day: 2, month: 1, year: 1979 }
].union(
  [
    { day: 2, month: 1, year: 1979 },
    { day: 3, month: 1, year: 1979 }
  ],
  (a, b) => a.day === b.day && a.month === b.month && a.year === b.year
);
// [
//   { day: 2, month: 1, year: 1979 }
// ]

Contributing

Contributions welcome!