-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transducers): add keyPermutations, tests, update readme
- Loading branch information
1 parent
62f33b5
commit 5110d50
Showing
5 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { IObjectOf, Pair } from "@thi.ng/api"; | ||
import { assocObj } from "../rfn/assoc-obj"; | ||
import { map } from "../xform/map"; | ||
import { mapcat } from "../xform/mapcat"; | ||
import { partition } from "../xform/partition"; | ||
import { pairs } from "./pairs"; | ||
import { permutations } from "./permutations"; | ||
|
||
/** | ||
* Similar to {@link permutations}, however takes an object with each | ||
* key specifying an array of its possible values. Yields an iterable of | ||
* objects of all value permutations. | ||
* | ||
* @remarks | ||
* The resulting object type will be derived from the value types in the | ||
* given `spec` object. | ||
* | ||
* The order of resulting permutations is not guaranteed and depending | ||
* on the VM's iteration behavior of `Object.keys()`. | ||
* | ||
* @example | ||
* ```ts | ||
* [...keyPermutations({ a: [1, 2], b: [true, false], c: ["X", "Y"] })] | ||
* // [ | ||
* // { a: 1, b: true, c: 'X' }, | ||
* // { a: 1, b: true, c: 'Y' }, | ||
* // { a: 1, b: false, c: 'X' }, | ||
* // { a: 1, b: false, c: 'Y' }, | ||
* // { a: 2, b: true, c: 'X' }, | ||
* // { a: 2, b: true, c: 'Y' }, | ||
* // { a: 2, b: false, c: 'X' }, | ||
* // { a: 2, b: false, c: 'Y' } | ||
* // ] | ||
* ``` | ||
* | ||
* @param spec - permutation spec object | ||
*/ | ||
export const keyPermutations = <T extends IObjectOf<any[]>>( | ||
spec: T | ||
): IterableIterator<{ [k in keyof T]: T[k][0] }> => | ||
<any>( | ||
map( | ||
(x) => assocObj(<Iterable<Pair<string, any>>>partition(2, x)), | ||
permutations(...mapcat(([k, v]) => [[k], v], pairs(spec))) | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { keyPermutations } from "../src"; | ||
|
||
import * as assert from "assert"; | ||
|
||
describe("keyPermutations", () => { | ||
it("basic", () => { | ||
assert.deepEqual( | ||
new Set([ | ||
...keyPermutations({ | ||
a: [1, 2], | ||
b: [true, false], | ||
c: ["X", "Y"], | ||
}), | ||
]), | ||
new Set([ | ||
{ a: 1, b: true, c: "X" }, | ||
{ a: 1, b: true, c: "Y" }, | ||
{ a: 1, b: false, c: "X" }, | ||
{ a: 1, b: false, c: "Y" }, | ||
{ a: 2, b: true, c: "X" }, | ||
{ a: 2, b: true, c: "Y" }, | ||
{ a: 2, b: false, c: "X" }, | ||
{ a: 2, b: false, c: "Y" }, | ||
]) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters