Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

UniCoderGroup/set-of-ranges

Repository files navigation

Set of ranges

GitHub Workflow Statuscodecov

Install

npm i set-of-ranges

Usage

  1. import

    import { RangeSet } from "set-of-ranges";
  2. construct

    let rs = new RangeSet("(10,20][40,infinity)");
    //or
    let ranges = [
      {
        value: 1,
        equal: true,
      },
      {
        value: 2,
        equal: false,
      },
    ]; // or = parseRanges("[1,2)");
    let rs = new RangeSet(ranges);

    If construct with a string, it will use parseRanges to parse the string to Ranges.

    When use parseRanges, spaces and lower/uppercases are ignored. When input is not allowed, it throws an error.

  3. operate

    import { parseRanges, RangeSet, stringifyRanges } from "set-of-ranges";
    let rs1: RangeSet, rs2: RangeSet, rs3: RangeSet;
    rs1.union(rs2); // ============= rs1 = operate(truthTable_union, rs1, rs2);
    rs1.intersection(rs2); // ====== rs1 = operate(truthTable_intersection, rs1, rs2);
    
    // You can also do operation to more than 2 ranges.
    rs1.union(rs2, rs3); // ========== rs1 = operate(truthTable_union, rs1, operate(truthTable_union, rs4, rs3));
    
    // You can use a customized truth table, too.
    rs1.operate(
      [
        [false, false],
        [false, true],
      ],
      rs2
    );

    TruthTable

    Copied from sourcecode.

    export type OperateTruthTable = [
      //          R:true    R:false
      /*L:true */ [boolean, boolean],
      /*L:flase*/ [boolean, boolean]
    ];
  4. output

    import { RangeSet, stringifyRanges } from "set-of-ranges";
    let rs1 = new RangeSet("(10  ,20][ 40,infinity )");
    console.log(stringifyRanges(rs1)); //=> "(10,20][40,infinity)"

Authors

_Kerman @ UniCoderGroup

License

MIT License