// ============================================================================ // Helpers // ============================================================================ interface InternalIterationMap { __: [number, "__", "__", number] "-50": [-50, "__", "-49", 0] "-49": [-49, "-50", "-48", 1] "-48": [-48, "-49", "-47", 2] "-47": [-47, "-48", "-46", 3] "-46": [-46, "-47", "-45", 4] "-45": [-45, "-46", "-44", 5] "-44": [-44, "-45", "-43", 6] "-43": [-43, "-44", "-42", 7] "-42": [-42, "-43", "-41", 8] "-41": [-41, "-42", "-40", 9] "-40": [-40, "-41", "-39", 10] "-39": [-39, "-40", "-38", 11] "-38": [-38, "-39", "-37", 12] "-37": [-37, "-38", "-36", 13] "-36": [-36, "-37", "-35", 14] "-35": [-35, "-36", "-34", 15] "-34": [-34, "-35", "-33", 16] "-33": [-33, "-34", "-32", 17] "-32": [-32, "-33", "-31", 18] "-31": [-31, "-32", "-30", 19] "-30": [-30, "-31", "-29", 20] "-29": [-29, "-30", "-28", 21] "-28": [-28, "-29", "-27", 22] "-27": [-27, "-28", "-26", 23] "-26": [-26, "-27", "-25", 24] "-25": [-25, "-26", "-24", 25] "-24": [-24, "-25", "-23", 26] "-23": [-23, "-24", "-22", 27] "-22": [-22, "-23", "-21", 28] "-21": [-21, "-22", "-20", 29] "-20": [-20, "-21", "-19", 30] "-19": [-19, "-20", "-18", 31] "-18": [-18, "-19", "-17", 32] "-17": [-17, "-18", "-16", 33] "-16": [-16, "-17", "-15", 34] "-15": [-15, "-16", "-14", 35] "-14": [-14, "-15", "-13", 36] "-13": [-13, "-14", "-12", 37] "-12": [-12, "-13", "-11", 38] "-11": [-11, "-12", "-10", 39] "-10": [-10, "-11", "-9", 40] "-9": [-9, "-10", "-8", 41] "-8": [-8, "-9", "-7", 42] "-7": [-7, "-8", "-6", 43] "-6": [-6, "-7", "-5", 44] "-5": [-5, "-6", "-4", 45] "-4": [-4, "-5", "-3", 46] "-3": [-3, "-4", "-2", 47] "-2": [-2, "-3", "-1", 48] "-1": [-1, "-2", "0", 49] "0": [0, "-1", "1", 50] "1": [1, "0", "2", 51] "2": [2, "1", "3", 52] "3": [3, "2", "4", 53] "4": [4, "3", "5", 54] "5": [5, "4", "6", 55] "6": [6, "5", "7", 56] "7": [7, "6", "8", 57] "8": [8, "7", "9", 58] "9": [9, "8", "10", 59] "10": [10, "9", "11", 60] "11": [11, "10", "12", 61] "12": [12, "11", "13", 62] "13": [13, "12", "14", 63] "14": [14, "13", "15", 64] "15": [15, "14", "16", 65] "16": [16, "15", "17", 66] "17": [17, "16", "18", 67] "18": [18, "17", "19", 68] "19": [19, "18", "20", 69] "20": [20, "19", "21", 70] "21": [21, "20", "22", 71] "22": [22, "21", "23", 72] "23": [23, "22", "24", 73] "24": [24, "23", "25", 74] "25": [25, "24", "26", 75] "26": [26, "25", "27", 76] "27": [27, "26", "28", 77] "28": [28, "27", "29", 78] "29": [29, "28", "30", 79] "30": [30, "29", "31", 80] "31": [31, "30", "32", 81] "32": [32, "31", "33", 82] "33": [33, "32", "34", 83] "34": [34, "33", "35", 84] "35": [35, "34", "36", 85] "36": [36, "35", "37", 86] "37": [37, "36", "38", 87] "38": [38, "37", "39", 88] "39": [39, "38", "40", 89] "40": [40, "39", "41", 90] "41": [41, "40", "42", 91] "42": [42, "41", "43", 92] "43": [43, "42", "44", 93] "44": [44, "43", "45", 94] "45": [45, "44", "46", 95] "46": [46, "45", "47", 96] "47": [47, "46", "48", 97] "48": [48, "47", "49", 98] "49": [49, "48", "50", 99] "50": [50, "49", "__", 100] } type InternalIterationKey = keyof InternalIterationMap type InternalIterationNumberKey = Exclude // ============================================================================ // Primitives // ============================================================================ /** * @description Define the iteration tuple type. * * @example * ``` * // Expect: [0, "-1", "1", 50] * type Example1 = IterationOf<0> * // Expect: Iteration * type Example2 = Iteration * ``` */ export type Iteration = InternalIterationMap[InternalIterationKey] /** * @description Get the iteration tuple for a number. * * @example * ``` * // Expect: [3, "2", "4", 53] * type Example1 = IterationOf<3> * // Expect: [-2, "-3", "-1", 48] * type Example2 = IterationOf<-2> * // Expect: [number, "__", "__", number] * type Example3 = IterationOf<999> * ``` */ export type IterationOf = `${N}` extends InternalIterationKey ? InternalIterationMap[`${N}`] : InternalIterationMap["__"] // ============================================================================ // Validation // ============================================================================ /** * @description Check if a type is an Iteration tuple. * * @example * ``` * // Expect: true * type Example1 = IterationIsIteration> * // Expect: false * type Example2 = IterationIsIteration * ``` */ export type IterationIsIteration = T extends Iteration ? true : false /** * @description Check if a number is within the built-in iteration range. * * @example * ``` * // Expect: true * type Example1 = IterationIsInRange<10> * // Expect: false * type Example2 = IterationIsInRange<120> * // Expect: true * type Example3 = IterationIsInRange<-50> * ``` */ export type IterationIsInRange = `${N}` extends InternalIterationNumberKey ? true : false // ============================================================================ // Query // ============================================================================ /** * @description Get the key (numeric value) of an iteration. * * @example * ``` * // Expect: 5 * type Example1 = Key> * // Expect: -1 * type Example2 = Key> * ``` */ export type Key = I[0] /** * @description Get the position index of an iteration. * * @example * ``` * // Expect: 50 * type Example1 = Pos> * // Expect: 49 * type Example2 = Pos> * ``` */ export type Pos = I[3] /** * @description Get the previous iteration. * * @example * ``` * // Expect: [2, "1", "3", 52] * type Example1 = Prev> * // Expect: [number, "__", "__", number] * type Example2 = Prev> * ``` */ export type Prev = InternalIterationMap[I[1]] /** * @description Get the next iteration. * * @example * ``` * // Expect: [4, "3", "5", 54] * type Example1 = Next> * // Expect: [number, "__", "__", number] * type Example2 = Next> * ``` */ export type Next = InternalIterationMap[I[2]]