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

combine-utils

v2.0.0

Published

Utility functions to help with combining items.

Downloads

8

Readme

combine-utils

This package provides you with some utility functions that can be used to combine a list of items into collections of those items.

API Documentation

There are four functions that this package exposes.

createCombinations

This function solves the classical combinatorial problem of finding all possible combinations of certain items. It takes in a list of items and it returns a list of lists of those items. Each element of the returned list represents a possible combination.

Type definition

createCombinations<Item>(
  collection: Item[],
  options?: Options
): Item[][]

Example

const collection = [1, 2, 3];
const combinations = createCombinations(collection);
expect(combinations).toEqual([
  [],
  [1],
  [1, 2],
  [1, 2, 3],
  [1, 3],
  [2],
  [2, 3],
  [3]
]);

createSets

This function solves the problem of how to distribute a list of items into different subsets of those items where each item belongs to exactly one subset.

In other words: We want to build a list of subsets of all the items in such a way that when we join together all subsets we end up with the original set of items without duplicates.

The function takes in a list of items and returns a list of sets. A set is a list of combinations of all items, i.e. a list of lists of items.

Type definition

createSets<Item>(
  collection: Item[],
  options?: Options
): Item[][][]

Example

const collection = [1, 2, 3];
const sets = createSets(collection);
expect(sets).toEqual([
  [[1], [2], [3]],
  [[1], [2, 3]],
  [[1, 2], [3]],
  [[1, 2, 3]],
  [[1, 3], [2]]
]);

createCombinationsWithIdentifiers

This function returns all possible combinations of items where each item contains a list of identifiers. The combinations are built in such a way that when you combine the identifiers of all items in the combination you don't get any duplicates.

The function takes a list of items and a function that maps one item to its identifiers. It returns a list of combinations of the given items, i.e. a list of lists of items.

Type definition

createCombinationsWithIdentifiers<Item>(
  collection: Item[],
  getIdentifiersFromItem: GetIdentifiersFromItem<Item>,
  options?: Options
): Item[][]

type GetIdentifiersFromItem<Item> = (item: Item) => Identifier[];
type Identifier = number | string;

Example

const collection = [
  { itemId: 0, identifiers: [1] },
  { itemId: 1, identifiers: [2] },
  { itemId: 2, identifiers: [1, 2] }
];
const combinations = createCombinationsWithIdentifiers(
  collection,
  item => item.identifiers
);
expect(combinations).toEqual([
  [{ itemId: 0, identifiers: [1] }],
  [{ itemId: 0, identifiers: [1] }, { itemId: 1, identifiers: [2] }],
  [{ itemId: 1, identifiers: [2] }],
  [{ itemId: 2, identifiers: [1, 2] }]
]);

createCompleteCombinationsWithIdentifiers

This function works similarly to the function createCombinationsWithIdentifiers, but it only returns those combinations, where each identifier is contained through an item exactly once.

The function takes a list of items and a function that maps one item to its identifiers. It returns a list of combinations of the given items, i.e. a list of lists of items.

Type definition

createCompleteCombinationsWithIdentifiers<Item>(
  collection: Item[],
  getIdentifiersFromItem: GetIdentifiersFromItem<Item>,
  options?: Options
): Item[][]

type GetIdentifiersFromItem<Item> = (item: Item) => Identifier[];
type Identifier = number | string;

Example

const collection = [
  { itemId: 0, identifiers: [1] },
  { itemId: 1, identifiers: [2] },
  { itemId: 2, identifiers: [1, 2] }
];
const combinations = createCompleteCombinationsWithIdentifiers(
  collection,
  item => item.identifiers
);
expect(combinations).toEqual([
  [{ itemId: 0, identifiers: [1] }, { itemId: 1, identifiers: [2] }],
  [{ itemId: 2, identifiers: [1, 2] }]
]);

Options

The last argument for each function is an object containing optional parameters. These options are the same for each function:

type Options = {
  minimumLength?: number;
  maximumLength?: number;
  storeNumberOfCallsIn?: { calls: number };
};

minimumLength

The minimum length defines a lower bound for the length of combinations or sets that should be included. All combinations that contain fewer elements are ignored.

maximumLength

The maximum length defines an upper bound for the length of combinations or sets that should be included. All combinations that contain more elements are ignored.

storeNumberOfCallsIn

All of the functions that this package exposes are implemented using recursion. The option storeNumberOfCallsIn allows you to track the number of recursive calls.