Options
All
  • Public
  • Public/Protected
  • All
Menu

@pinemach/obj-permute

Coverage Status Build Status NPM version MIT License

@pinemach/obj-permute is a small JavaScript package which can be used to iterate the list of objects representing every combination of attribute values as specified by an input object whose attributes refer to lists of possible values.

You can read the full API documentation at pineapplemachine.github.io/obj-permute-js/.

Installation

You can install this package with the package manager of your choice. For example,

npm install @pinemach/obj-permute

You can then import and use the module like so:

// CommonJS
const getObjectPermutations = require("@pinemach/obj-permute").getObjectPermutations;
// ES6 modules
import getObjectPermutations from "@pinemach/obj-permute";

Usage

This package exports the getObjectPermutations function, which accepts an object whose attributes associate keys with lists of values.

The function returns an iterator which enumerates objects representing every combination of attribute values within those lists.

For example, the states object input {x: [0, 1], y: [2, 3]} corresponds to the list of permutations containing the objects {x: 0, y: 2}, {x: 1, y: 2}, {x: 0, y: 3}, and {x: 1, y: 3}.

import getObjectPermutations from "@pinemach/obj-permute";

// Get an iterator for every combination of attribute values
const permutations = getObjectPermutations({
    x: [0, 1],
    y: [2, 3],
});

// Get an array from that iterator
const permutationsArray = permutations.array();

// Verify that each expected item is present
assert(permutationsArray.length === 4);
assert(permutationsArray.find(
    result => result.x === 0 && result.y === 2
));
assert(permutationsArray.find(
    result => result.x === 1 && result.y === 2
));
assert(permutationsArray.find(
    result => result.x === 0 && result.y === 3
));
assert(permutationsArray.find(
    result => result.x === 1 && result.y === 3
));

Index

Type aliases

ObjectPermutation

ObjectPermutation<T>: object

Given an object which would be passed as an input to getObjectPermutations, resolves to the type of an individual permutation object.

Type parameters

  • T

Type declaration

Functions

getObjectPermutations

  • Accepts an object whose attributes associate keys with lists of values. Returns an iterator which enumerates objects representing every combination of attribute values within those lists.

    For example, the states object input {x: [0, 1], y: [2, 3]} corresponds to the list of permutations containing the objects {x: 0, y: 2}, {x: 1, y: 2}, {x: 0, y: 3}, and {x: 1, y: 3}.

    When any key in a states object refers to an empty array or any value which is not array, that key will be disregarded and will not be included in any way in the outputted permutation objects.

    Note that the order of enumerated objects is not guaranteed, nor is the ordering of keys of each individual permutation object.

    Type parameters

    • T: object

    Parameters

    • states: T

      A states object associating keys with lists of possible values.

    Returns ObjectPermutationsIterator<T>

    an ObjectPermutationsIterator instance which iterates all those permutations represented by the inputted states object.

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc