UNPKG

@bluelovers/fill-range

Version:

Fill in a range of numbers or letters, optionally passing an increment or `step` to use, or create a regex-compatible range with `options.toRegex`

81 lines (77 loc) 3.25 kB
import { IOptions as IOptionsToRegexRange } from '@bluelovers/to-regex-range2'; export interface IOptions<V = string | number> extends IOptionsToRegexRange { /** * The increment to use for the range. Can be used with letters or numbers. * @example * // numbers * console.log(fill('1', '10', 2)); //=> [ '1', '3', '5', '7', '9' ] * console.log(fill('1', '10', 3)); //=> [ '1', '4', '7', '10' ] * console.log(fill('1', '10', 4)); //=> [ '1', '5', '9' ] * * // letters * console.log(fill('a', 'z', 5)); //=> [ 'a', 'f', 'k', 'p', 'u', 'z' ] * console.log(fill('a', 'z', 7)); //=> [ 'a', 'h', 'o', 'v' ] * console.log(fill('a', 'z', 9)); //=> [ 'a', 'j', 's' ] */ step?: number; /** * By default, null is returned when an invalid range is passed. Enable this option to throw a RangeError on invalid ranges. */ strictRanges?: boolean; /** * Cast all returned values to strings. By default, integers are returned as numbers. * @example * console.log(fill(1, 5)); //=> [ 1, 2, 3, 4, 5 ] * console.log(fill(1, 5, { stringify: true })); //=> [ '1', '2', '3', '4', '5' ] * */ stringify?: boolean; /** * Create a regex-compatible source string, instead of expanding values to an array. * @example * // alphabetical range * console.log(fill('a', 'e', { toRegex: true })); //=> '[a-e]' * // alphabetical with step * console.log(fill('a', 'z', 3, { toRegex: true })); //=> 'a|d|g|j|m|p|s|v|y' * // numerical range * console.log(fill('1', '100', { toRegex: true })); //=> '[1-9]|[1-9][0-9]|100' * // numerical range with zero padding * console.log(fill('000001', '100000', { toRegex: true })); * //=> '0{5}[1-9]|0{4}[1-9][0-9]|0{3}[1-9][0-9]{2}|0{2}[1-9][0-9]{3}|0[1-9][0-9]{4}|100000' */ toRegex?: boolean; /** * Customize each value in the returned array (or string). (you can also pass this function as the last argument to fill()). * @example * // add zero padding * console.log(fill(1, 5, value => String(value).padStart(4, '0'))); * //=> ['0001', '0002', '0003', '0004', '0005'] */ transform?(val: number, index?: number): V; /** * set limit size */ limit?: number; /** * only allow start < stop */ strictOrder?: boolean; } export declare function fill<V = number | string>(start: number | string, end: number | string, step: IOptions<V> & { toRegex?: false; }, options?: never): V[]; export declare function fill<V = number | string>(start: number | string, end: number | string, step: number, options?: IOptions<V> & { toRegex?: false; }): V[]; export declare function fill<V = number | string>(start: number | string, end: number | string, step: IOptions<V>["transform"], options?: never): V[]; export declare function fill(start: number | string, end: number | string, step: IOptions & { toRegex: true; }, options?: IOptions): string; export declare function fill(start: number | string, end: number | string, step: number | IOptions["transform"], options: IOptions & { toRegex: true; }): string; export declare function fill<R extends any[] | string = string[] | string>(start: number | string, end?: number | string, step?: number | IOptions["transform"] | IOptions, options?: IOptions): R; export { fill as default, }; export {};