Setation (NodeJS)
Releases | Latest () | Pre () |
---|---|---|
📝 Description
A NodeJS module to list permutations and combinations from a set.
📚 Documentation
Getting Started
- NodeJS ^ v12.20.0 || ^ v14.15.0 || >= v16.13.0
npm install @hugoalh/setation
/* Either */
import { ... } from "@hugoalh/setation";// Named Import
import * as setation from "@hugoalh/setation";// Namespace Import
API
Generator Function
-
combination<T>(set: T[] | Set<T>, size: number | number[]): Generator<T[], void, unknown>; combination<T>(set: T[] | Set<T>, sizeMinimum: number, sizeMaximum: number): Generator<T[], void, unknown>; combination<T>(set: T[] | Set<T>, options: CombinationOptions = {}): Generator<T[], void, unknown>;
-
/* >= v1.2.0 */ combinationMatrix<V>(set: { [x: string]: V | V[]; } | Map<string, V | V[]>): Generator<{ [x: string]: V; }, void, unknown>;
-
permutation<T>(set: T[] | Set<T>, size: number | number[]): Generator<T[], void, unknown>; permutation<T>(set: T[] | Set<T>, sizeMinimum: number, sizeMaximum: number): Generator<T[], void, unknown>; permutation<T>(set: T[] | Set<T>, options: PermutationOptions = {}): Generator<T[], void, unknown>;
Interface / Type
-
interface CombinationOptions { allowRepeat: boolean = false;// Whether to allow the same element repeat appear in the same subset. size?: number | number[];// Size of the subset. sizeMaximum?: number;// Maximum size of the subset. sizeMinimum?: number;// Minimum size of the subset. }
-
interface PermutationOptions { allowRepeat: boolean = false;// Whether to allow the same element repeat appear in the same subset. size?: number | number[];// Size of the subset. sizeMaximum?: number;// Maximum size of the subset. sizeMinimum?: number;// Minimum size of the subset. }
Example
let item = ["a", "b", "c", "d", "e", "f"];
Array.from(combination(item, 3));
/*=>
[
[ "a", "b", "c" ], [ "a", "b", "d" ],
[ "a", "b", "e" ], [ "a", "b", "f" ],
[ "a", "c", "d" ], [ "a", "c", "e" ],
[ "a", "c", "f" ], [ "a", "d", "e" ],
[ "a", "d", "f" ], [ "a", "e", "f" ],
[ "b", "c", "d" ], [ "b", "c", "e" ],
[ "b", "c", "f" ], [ "b", "d", "e" ],
[ "b", "d", "f" ], [ "b", "e", "f" ],
[ "c", "d", "e" ], [ "c", "d", "f" ],
[ "c", "e", "f" ], [ "d", "e", "f" ]
]
*/
Array.from(permutation(item, 3));
/*=>
[
[ "a", "b", "c" ], [ "a", "b", "d" ],
[ "a", "b", "e" ], [ "a", "b", "f" ],
[ "a", "c", "b" ], [ "a", "c", "d" ],
[ "a", "c", "e" ], [ "a", "c", "f" ],
[ "a", "d", "b" ], [ "a", "d", "c" ],
[ "a", "d", "e" ], [ "a", "d", "f" ],
[ "a", "e", "b" ], [ "a", "e", "c" ],
[ "a", "e", "d" ], [ "a", "e", "f" ],
[ "a", "f", "b" ], [ "a", "f", "c" ],
[ "a", "f", "d" ], [ "a", "f", "e" ],
[ "b", "a", "c" ], [ "b", "a", "d" ],
[ "b", "a", "e" ], [ "b", "a", "f" ],
[ "b", "c", "a" ], [ "b", "c", "d" ],
[ "b", "c", "e" ], [ "b", "c", "f" ],
[ "b", "d", "a" ], [ "b", "d", "c" ],
[ "b", "d", "e" ], [ "b", "d", "f" ],
[ "b", "e", "a" ], [ "b", "e", "c" ],
[ "b", "e", "d" ], [ "b", "e", "f" ],
[ "b", "f", "a" ], [ "b", "f", "c" ],
[ "b", "f", "d" ], [ "b", "f", "e" ],
... +80
]
*/
Array.from(combinationMatrix({ foo: [1, 2, 3], bar: [4, 5, 6] }));
/*=>
[
{ foo: 1, bar: 4 }, { foo: 1, bar: 5 },
{ foo: 1, bar: 6 }, { foo: 2, bar: 4 },
{ foo: 2, bar: 5 }, { foo: 2, bar: 6 },
{ foo: 3, bar: 4 }, { foo: 3, bar: 5 },
{ foo: 3, bar: 6 }
]
*/