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

@b08/array

v2.0.1

Published

set of DRY functions to work with array

Downloads

52

Readme

@b08/array, seeded from @b08/library-seed, library type: dry

Set of DRY functions to work with array. Typescript style.
Why not lodash? lodash is heavy, this package is light as air.
And supports es5/es6/esnext in compile time, i.e. a bit more efficient code in node and chrome without runtime checks for platform.
To use it with ES5, import it as "@b08/array/es5"

all

Method returns true if all items in the array fulfill the condition. Empty array also yields true.

import { all } from "@b08/array"

const a = [1, 2, 3];

const allHigher = all(a, item => item > 2); // false
const allNumbers = all(a, isNumber); // true

areEqual

Method returns true if two arrays are equal by criteria, and go in the same order.

const a = [ 
  { id: 1, name: 'a' }, 
  { id: 2, name: 'b' }
];

const b = [ 
  { id: 1, name: 'c' }, 
  { id: 2, name: 'd' }
];

const c = [ 
  { id: 2, name: 'b' }, 
  { id: 1, name: 'a' }
];

const anb = areEqual(a, b, item => item.id); // true
const anc = areEqual(a, c, item => item.id); // false

areEquivalent

Method returns true if two arrays are equal by criteria, order does not matter.

contains

Returns true if item is present in the array. Useful if array.prototype.includes is not available

intersect and except

intersect returns items present in both arrays, except return items from first array not present in second array

const a = [1, 2];
const b = [2, 3];

const result1 = intersect(a, b); // [2]
const result2 = except(a, b); // [1]

flatMap

Combination of map and then flatten, aka concatenation of array of arrays.

const a = [1, 2];

const b = flatMap(a, item => [item, 3]); // [1, 3, 2, 3];

hasElements

Returns true if array is an instance of Array and contains at least one element

first and last

Returns first and last elements of array satisfying the criteria, or undefined if no such elements present.

const a = [1, 2, 3];

const result1 = first(a, item => item > 1); // 2
const result2 = last(a, item => item > 1); // 3

min and max

Returns element minimal or maximal by given criteria

const a = [1, 2, 3];

const result1 = min(a, item => -item); // 3
const result2 = max(a, item => -item); // 1

regexToArray

Current interface of working with /g regex is a bit inconvenient. This method transforms it to array of matches. So you can execute .map on it and etc.

const matches = regexToArray(/\d/g, 'a1b2c'); 
// two matches, matches[0][0] = '1', matches[1][0] = '2'

splitInGroups

Splits array into a provided number of arrays

const a = [1, 2, 3, 4, 5, 6];
const result = splitInGroups(a, 2); // [[1, 2, 3], [4, 5, 6]];

splitInGroupsBy

Splits array into arrays of given size

const a = [1, 2, 3, 4, 5, 6];
const result = splitInGroups(a, 2); // [[1, 2], [3, 4], [5, 6]];

unique

Returns items from array unique by given criteria

const a = [ 
  { id: 1, name: 'a' }, 
  { id: 1, name: 'b' },
  { id: 2, name: 'c' }, 
  { id: 2, name: 'd' }
];

const result = unique(a, item => item.id); //  [a[0], a[2]]

range

Returns array of numbers from start(inclusive) to end(non-inclusive)

trim

Removes amount of items from end of the array, one by default.

const items = [1, 2, 3, 4];

const result = trim(items, 2); // [1, 2]