Skip to content

Easily generate subsequences from a provided map, array, or string input.

License

Notifications You must be signed in to change notification settings

forcir/subsequences

Repository files navigation

Forcir Subsequences

Easily generate subsequences from a provided map, array, or string input.

A subsequence is a sequence that can be derived from another sequence by removing zero or more elements, without changing the order of the remaining elements.

Install

pnpm add subsequences
yarn add subsequences
npm install subsequences

Basic Usage

import { Subsequence } from "subsequences";

Generate subsequence from Array

const subsequences = Subsequence.fromArray(["1", "2", "3", "4"]);

console.log({ subsequences });
Output
{
    "subsequences": [
        ["1", "2", "3", "4"],
        ["1", "2", "3"],
        ["1", "2", "4"],
        ["1", "2"],
        ["1", "3", "4"],
        ["1", "3"],
        ["1", "4"],
        ["1"],
        ["2", "3", "4"],
        ["2", "3"],
        ["2", "4"],
        ["2"],
        ["3", "4"],
        ["3"],
        ["4"]
    ]
}

Generate subsequence from String

const subsequences = Subsequence.fromString("1234");

console.log({ subsequences });
Output
{
    "subsequences": [
        ["1", "2", "3", "4"],
        ["1", "2", "3"],
        ["1", "2", "4"],
        ["1", "2"],
        ["1", "3", "4"],
        ["1", "3"],
        ["1", "4"],
        ["1"],
        ["2", "3", "4"],
        ["2", "3"],
        ["2", "4"],
        ["2"],
        ["3", "4"],
        ["3"],
        ["4"]
    ]
}

Advanced Usage

Instantiate with Map (Key/Value Pairs)

import { Subsequence } from "subsequences";

const subsequences = new Subsequence([
    ["first", "1"],
    ["second", "2"],
    ["third", "3"],
    ["fourth", "4"],
]);

console.log(subsequences.entrySubsequences);
console.log(subsequences.i18nSubsequences);
console.log(subsequences.keySubsequences);
console.log(subsequences.valueSubsequences);
entrySubsequences output
[
    ["first_1", "second_2", "third_3", "fourth_4"],
    ["first_1", "second_2", "third_3"],
    ["first_1", "second_2", "fourth_4"],
    ["first_1", "second_2"],
    ["first_1", "third_3", "fourth_4"],
    ["first_1", "third_3"],
    ["first_1", "fourth_4"],
    ["first_1"],
    ["second_2", "third_3", "fourth_4"],
    ["second_2", "third_3"],
    ["second_2", "fourth_4"],
    ["second_2"],
    ["third_3", "fourth_4"],
    ["third_3"],
    ["fourth_4"]
]
i18nSubsequences output
[
    {
        "key": "first1_second2_third3_fourth4",
        "values": { "first": "1", "second": "2", "third": "3", "fourth": "4" }
    },
    {
        "key": "first1_second2_third3",
        "values": { "first": "1", "second": "2", "third": "3" }
    },
    {
        "key": "first1_second2_fourth4",
        "values": { "first": "1", "second": "2", "fourth": "4" }
    },
    { "key": "first1_second2", "values": { "first": "1", "second": "2" } },
    {
        "key": "first1_third3_fourth4",
        "values": { "first": "1", "third": "3", "fourth": "4" }
    },
    { "key": "first1_third3", "values": { "first": "1", "third": "3" } },
    { "key": "first1_fourth4", "values": { "first": "1", "fourth": "4" } },
    { "key": "first1", "values": { "first": "1" } },
    {
        "key": "second2_third3_fourth4",
        "values": { "second": "2", "third": "3", "fourth": "4" }
    },
    { "key": "second2_third3", "values": { "second": "2", "third": "3" } },
    { "key": "second2_fourth4", "values": { "second": "2", "fourth": "4" } },
    { "key": "second2", "values": { "second": "2" } },
    { "key": "third3_fourth4", "values": { "third": "3", "fourth": "4" } },
    { "key": "third3", "values": { "third": "3" } },
    { "key": "fourth4", "values": { "fourth": "4" } }
]
keySubsequences output
[
    ["first", "second", "third", "fourth"],
    ["first", "second", "third"],
    ["first", "second", "fourth"],
    ["first", "second"],
    ["first", "third", "fourth"],
    ["first", "third"],
    ["first", "fourth"],
    ["first"],
    ["second", "third", "fourth"],
    ["second", "third"],
    ["second", "fourth"],
    ["second"],
    ["third", "fourth"],
    ["third"],
    ["fourth"]
]
valueSubsequences output
[
    ["1", "2", "3", "4"],
    ["1", "2", "3"],
    ["1", "2", "4"],
    ["1", "2"],
    ["1", "3", "4"],
    ["1", "3"],
    ["1", "4"],
    ["1"],
    ["2", "3", "4"],
    ["2", "3"],
    ["2", "4"],
    ["2"],
    ["3", "4"],
    ["3"],
    ["4"]
]