/** * A `[min, max]` tuple to denote one integer range. */ export type Range = readonly [min: number, max: number]; /** * An immutable Range array. This is the fundamental data type of this package. * * **Warning**: Most functions of this package work correctly * only when **normalized** MultiIntegerRange's are passed. * If you have a Range array that may not be sorted, use `normalize()` first. */ export type MultiIntegerRange = readonly Range[]; export type MIR = MultiIntegerRange; export type Options = { /** * If set to true, allows parsing negative integers enclosed in parentheses. */ readonly parseNegative?: boolean; /** * If set to true, allows parsing unbounded ranges like `10-` or `-10`. */ readonly parseUnbounded?: boolean; }; /** * Parses a string and creates a new MultiIntegerRange. * * - `options.parseNegative` (boolean): When set to true, parses negative integers enclosed in parentheses. * - `options.parseUnbounded` (boolean): When set to true, parses unbounded ranges like `10-` or `-10`. * * This is the default parser, but you don't necessarily have to use this. * You can create your own parser to suit your needs * as long as it produces a normalized array of `Range`s. * * @param data - The string to parse. * @param options - Options to modify the parsing behavior. * @returns A new normalized MultiIntegerRange. * @example * parse('1-10'); // [[1, 10]] * parse(' 10-, 7', \{ parseUnbounded: true \}); // [[7, 7], [10, Infinity]] */ export declare const parse: (data: string, options?: Options) => MIR; /** * Takes a number or an unsorted array of ranges, * and returns a new normalized MultiIntegerRange. * * Here, "normalized" means the range data is in the smallest possible * representation and is sorted in ascending order. * * This is the only function that can take an unsorted array of Range's. * Unsorted range data MUST be normalized before being passed to * other functions such as `append()` and `length()`. * * @param data - A number or an unsorted array, e.g., `[[7, 5], 1]`. * @returns Normalized array, e.g., `[[1, 1], [5, 7]]`. * @example * normalize(5); // [[5, 5]] * normalize([1, 8]); // [[1, 1], [8, 8]] * normalize([[1, 8]]); // [[1, 8]] * normalize([2, 3, 1, 5, 4, 0, 1, 3]); // [[0, 5]] * normalize([[Infinity, 1]]); // [[1, Infinity]] */ export declare const normalize: (data?: (number | Range)[] | number) => MIR; /** * Takes any supported data and returns a normalized MultiIntegerRange. * Conditionally calls either `parse()` or `normalize()` under the hood. * This is an equivalent of "initializer" constructor of version ≤ 4. * @param data - Anything understood by either `parse()` or `normalize()`. * @param options - Parse options passed to `parse()`. * @returns A new normalized MultiIntegerRange. * @example * initialize(5); // [[5, 5]] * initialize('2-8'); // [[2,8]] */ export declare const initialize: (data?: (number | Range)[] | number | string, options?: Options) => MIR; /** * Appends two MultiIntegerRange's. * @param a - The first value. * @param b - The second value. * @returns A new MultiIntegerRange containing all integers that belong to * **either `a` or `b` (or both)**. * @example * append([[1, 5]], [[3, 8], [10, 15]]); // [[1, 8], [10, 15]] * append([[5, 9]], [[-Infinity, 2]]); // [[-Infinity, 2], [5, 9]] */ export declare const append: (a: MIR, b: MIR) => MIR; /** * Subtracts the second value from the first value. * @param a - The value to be subtracted. * @param b - The value to subtract. * @returns A new MultiIntegerRange containing all integers that belong to * **`a` but not `b`**. * @example * subtract([[1, 7]], [[2, 4]]); // [[1, 1], [5, 7]] * subtract([[-Infinity, Infinity]], [[2, 4]]); // [[-Infinity, 1], [5, Infinity]] */ export declare const subtract: (a: MIR, b: MIR) => MIR; /** * Calculates the intersection (common integers) of the two MultiIntegerRange's. * @param a - The first value. * @param b - The second value. * @returns A new MultiIntegerRange containing all integers * that belong to **both `a` and `b`**. * @example * intersect([[2, 5]], [[4, 9]]); // [[4, 5]] * intersect([[5, 10]], [[-Infinity, Infinity]]); // [[5, 10]] */ export declare const intersect: (a: MIR, b: MIR) => MIR; /** * Checks if `a` contains or is equal to `b` (a ⊇ b). * @param a - The value that possibly contains `b`. * @param b - The value that is possibly contained by `a`. * @returns True if `b` is equal to or a subset of `a`. * @example * has([[0, 100]], [[2, 10]]); // true * has([[5, 7]], [[5, 7]]); // true * has([[2, 10]], [[0, 100]]); // false */ export declare const has: (a: MIR, b: MIR) => boolean; /** * Calculates how many integers are included in the given MultiIntegerRange. * * Note: If you want to know the number of Ranges (segments), just use the * standard `Array#length`. * @param data - The value to calculate the length on. * @returns The number of integers contained in `data`. May be `Infinity`. * @example * length([[1, 3], [8, 10]]); // 6 * length([[1, Infinity]]); // Infinity */ export declare const length: (data: MIR) => number; /** * Checks if the data contains an unbounded (aka inifinite) range. * @param data - The value to check. * @returns True if `data` is unbounded. * @example * isUnbounded([[1, Infinity]]); // true * isUnbounded([[-Infinity, 4]]); // true * isUnbounded([[7, 9]]); // false */ export declare const isUnbounded: (data: MIR) => boolean; /** * Checks if the two values are the same. (Altenatively, you can use any * "deep-equal" utility function.) * @param a - The first value to compare. * @param b - The second value to compare. * @returns True if `a` and `b` have the same range data. * @example * equals([[1, 5], [7, 8]], [[1, 5], [7, 8]]); // true * equals([[1, 5]], [[2, 7]]); // false */ export declare const equals: (a: MIR, b: MIR) => boolean; /** * Returns the minimum integer of the given MultiIntegerRange. * @param data - The value. * @returns The minimum integer. May be `undefined` or `-Infinity`. * @example * min([[2, 5], [8, 10]]); // 2 * min([[-Infinity, 0]]); // -Infinity * min([]); // undefined */ export declare const min: (data: MIR) => number | undefined; /** * Returns the maximum integer of the given MultiIntegerRange. * @param data - The value. * @returns The minimum integer. May be `undefined` or `Infinity`. * @example * max([[2, 5], [8, 10]]); // 10 * max([[3, Infinity]]); // Infinity * max([]); // undefined */ export declare const max: (data: MIR) => number | undefined; /** * Returns the integer at the specified 0-based index. * If a negative index is given, the index is counted from the end. * @param data - The value. * @param index - The 0-based index of the integer to return. Can be negative. * @returns The integer at the specified index. * Returns `undefined` if the index is out of bounds. * @example * at([[2, 4], [8, 10]], 4); // 9 * at([[2, 4], [8, 10]], 6); // undefined * at([[2, 4], [8, 10]], -1); // 10 */ export declare const at: (data: MIR, index: number) => number | undefined; /** * Returns all but the minimum integer. * @param data - The value. * @returns A new MultiIntegerRange which is almost the same as `data` but with * its minimum integer removed. * @example * tail([[2, 5], [8, 10]]); // [[3, 5], [8, 10]] */ export declare const tail: (data: MIR) => MIR; /** * Returns all but the maximum integer. * @param data - The value. * @returns A new MultiIntegerRange which is almost the same as `data` but with * its maximum integer removed. * @example * init([[2, 5], [8, 10]]); // [[2, 5], [8, 9]] */ export declare const init: (data: MIR) => MIR; /** * Options for the `stringify()` function. */ export interface StringifyOptions { individualThreshold?: number; } /** * Returns the string respresentation of the given MultiIntegerRange. * * - `options.individualThreshold` (number): If set, small ranges with a length * smaller than or equal to this will be output as individual integers. * Defaults to `1`, which means only ranges with a length of 1 will be * output as a single integer. * * @param data - The MultiIntegerRange to stringify. * @param options - Options for the stringification. * @returns The string representation of the given data. * @example * stringify([[2, 3], [5, 5], [7, 9]]); // '2-3,5,7-9' * stringify([[2, 3], [5, 5], [7, 9]], { individualThreshold: 0 }); // '2-3,5-5,7-9' * stringify([[2, 3], [5, 5], [7, 9]], { individualThreshold: 2 }); // '2,3,5,7-9' * stringify([[2, 3], [5, 5], [7, 9]], { individualThreshold: 3 }); // '2,3,5,7,8,9' * stringify([[3, 5], [7, Infinity]]); // '3-5,7-' */ export declare const stringify: (data: MIR, options?: StringifyOptions) => string; /** * Builds a flattened array of integers. * Note that this may be slow and memory-consuming for large ranges. * Consider using the iterator whenever possible. * @param data - The value to build an array on. * @returns The flattened array of numbers. * @example * flatten([[-1, 1], [7, 9]]); // [-1, 0, 1, 7, 8, 9] */ export declare const flatten: (data: MIR) => number[]; /** * Options for the `iterate()` function. */ export interface IterateOptions { /** * Whether to iterate in descending order. */ readonly descending?: boolean; } /** * Returns an Iterable with which you can use `for-of` or the spread syntax. * * - `options.descending` (boolean): If set to true, the iterator will iterate in descending order. * * @param data - The normalized MultiIntegerRange to iterate over. * @param options - Options for the iteration. * @returns An Iterable object. * @example * Array.from(iterate([[1, 3], [7, 9]])); // [1, 2, 3, 7, 8, 9] * Array.from(iterate([[1, 3], [7, 9]], { descending: true })); // [9, 8, 7, 3, 2, 1] * [...iterate([[-1, 2]])]; // [-1, 0, 1, 2] */ export declare const iterate: (data: MIR, options?: IterateOptions) => Iterable;