[Draft] V2 - adamjgrant/permutations GitHub Wiki

New Features for a v2 draft of permutations

Allow empty strings

{
  "main": [
    "Good morning! ", "", [
      "How are you?"
    ]
  ]
}
Good morning! How are you?
How are you?

Rotate

Rotate automatically calculates all the unordered permutations of a one-level array and translates this into a new array.

So this...

{
  "main": [
    { "rotate": [ "foo", "bar" ] }, [
      "fizz", "buzz"
    ]
  ]
}

...is the equivalent of

{
  "main": [
    "foobar", "barfoo", [
      "fizz", "buzz"
    ]
  ]
}
{
  "main": [
    "Example DNA Sequence ", [
      { "rotate": ["A", "T"] }, [
        { "rotate": ["G", "C"] }
      ]
    ]
  ]
}
ATGC
TAGC
TACG
ATCG

Specify a custom join string, such as a space.

{
  "main": [
    "All personnel ", {
      "rotate": ["must have a parents signature", "ages 18 and younger"], "join": " "
    }
  ]
}
All personnel ages 18 and younger must have a parents signature
All personnel must have a parents signature ages 18 and younger

One random is its own method

Instead of new Tree(data, true) where the second parameter is "one_random" the one_random selection is based on a call, not the instantiation.

const tree = new Tree(data);
tree.permutations; // Return all permutations
tree.one;          // Return one random permutation

Unwrapped branches

Right now, branches just plop into place as an independent array, preventing you from doing some things easily. If I want to use either branch A or B, but then tack on C at the end, I have to do this.

{
  "main": [
    { "branch": "A", "then": { "branch": "C" } }, 
    { "branch": "B", "then": { "branch": "C" } }
  ],
  ...
}

Unwrapping the branch adds the branch's array with just its members.

{
  "main": [
    { "branch": "A" }
  ],
  "A": ["A", "B"]
}

is the equivalent of

{
  "main": [
    ["A", "B"]
  ]
}

but

{
  "main": [
    { "*branch": "A" }
  ],
  "A": ["A", "B"]
}

is the equivalent of

{
  "main": ["A", "B"]
}

Now to return to the original example

{
  "main": [
    { "branch": "A", "then": { "branch": "C" } }, 
    { "branch": "B", "then": { "branch": "C" } }
  ],
  ...
}

This could be written instead as

{
  "main": [
    { "*branch": "A" }, { "*branch": "B" }, { "branch": "C" }
  ],
  ...
}

Sub branch definitions

{
  "main": [
    ["A", { "branch": "letters.B" }, { "branch": "letters.C" } ]
  ],
  "letters": {
    "B": ["b"],
    "C": ["c"]
  }
}

Functional permutations

  • It's not clear what "string" would be here in more complex examples.
[
  "Foo"
  [
    { "fn": "(string) => string.toLowerCase()" },
    { "fn": "(string) => string.toUpperCase()" }
  ]
]
["foo", "FOO"]

Config

Separate JSON object that can optionally be passed in to set configuration values.

const permutation_obj = { main: ["a", ["b", "c"]] },
      config          = {
      },

      Permute = require("./permute"),
      tree = new Permute(permutation_object, config);

Delimiter

Specify a delimiter between each concatenation. Default is the empty string.

const permutation_obj = { main: ["a", ["b", "c"]] },
      config          = {
        delimiter: " AND "
      },

      Permute = require("./permute"),
      tree = new Permute(permutation_object, config);
      
      tree.permutations;
a AND b
a AND c