Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | 94x 94x 94x 94x 94x 2x 94x 382x 94x 13x 94x 31x 1x 30x 29x 29x 23x 9x 23x 17x 23x 18x 23x 14x 29x 94x 249x 249x 245x 109x 174x 174x 136x 245x 94x 31x 31x 1x 30x 30x 30x 94x 94x 94x 94x 29x 29x 145x 145x 55x 55x 55x 29x | /** * @module Utils */ import * as MC from "@lisn/globals/minification-constants"; import * as MH from "@lisn/globals/minification-helpers"; import { ScrollOffsetSpec, View, CommaSeparatedStr } from "@lisn/globals/types"; import { isValidStrList, validateStrList } from "@lisn/utils/validation"; import { newBitSpaces, createBitSpace } from "@lisn/modules/bit-spaces"; /** * Returns true if the given string is a valid {@link ScrollOffsetSpec}. * * @category Validation */ export const isValidScrollOffset = ( offset: string, ): offset is ScrollOffsetSpec => offset.match(OFFSET_REGEX) !== null; /** * Returns true if the given string is a valid "view". * * @category Validation */ export const isValidView = (view: string): view is View => MH.includes(VIEWS, view); /** * Returns true if the given string or array is a list of valid views. * * @category Validation */ export const isValidViewList = (views: string | string[]) => isValidStrList(views, isValidView, false); /** * Returns the views that are opposite to the given set of views. * * Above and below are opposites, and so are left and right. * * "at" is a special case. It is considered opposite to any view in the sense * that if it is not present in `views` it will always be included in the * returned array. However it is not "strongly" opposite in the sense that it * will not cause other views to be included in the result unless it is the * only view in `views`. That is, there are two sets of strongly opposite pairs * ("above"/"below" and "left"/"right") and at least one of the two opposing * views of a pair must be present for the other one to be included, _except in * the special case of `views` being "at"_. See examples below for * clarification. * * **Note** that the order of the returned array is not defined. * * @example * Returns ["above", "below", "left", "right"] (not definite order), since * "at" is the only view present and is opposite to all: * * ```javascript * getOppositeViews("at"); // -> ["above", "below", "left", "right"] (not necessarily in this order) * ``` * * @example * Returns ["below"]. "left" and "right" are NOT included even though "at" is * given, because at least one of the two opposing views of a pair must be * present for the other one to be included (except in the special case of * `views` being "at"). * * ```javascript * getOppositeViews("at,above"); // -> ["below"] * ``` * * @example * ```javascript * getOppositeViews("above"); // -> ["at", "below"] (not necessarily in this order) * ``` * * @example * ```javascript * getOppositeViews("above,below"); // -> ["at"] * ``` * * @example * ```javascript * getOppositeViews("at,above,below"); // -> [] * ``` * * @example * ```javascript * getOppositeViews("above,right"); // -> ["at", "below", "left"] (not necessarily in this order) * ``` * * @example * ```javascript * getOppositeViews("at,above,right"); // -> ["below", "left"] (not necessarily in this order) * ``` * * @throws {@link Errors.LisnUsageError | LisnUsageError} * If the given view is not valid, including if it's empty "". * * @category Views */ export const getOppositeViews = ( views: CommaSeparatedStr<View> | View[], ): View[] => { if (!views) { throw MH.usageError("'views' cannot be empty"); } const bitmask = getViewsBitmask(views); let oppositeBitmask = VIEWS_SPACE.bitmask & ~bitmask; // initial, all not present in bitmask // If the given view is "at", then include all the other ones. // Otherwise include only the opposite views of those directional // (above/below/left/right) that are present. I.e. if neither left not right // is given, then don't include them if (bitmask !== VIEWS_SPACE.bit.at) { // remove the opposite ones to those not present if (!(bitmask & VIEWS_SPACE.bit.above)) { oppositeBitmask &= ~VIEWS_SPACE.bit.below; } if (!(bitmask & VIEWS_SPACE.bit.below)) { oppositeBitmask &= ~VIEWS_SPACE.bit.above; } if (!(bitmask & VIEWS_SPACE.bit.left)) { oppositeBitmask &= ~VIEWS_SPACE.bit.right; } if (!(bitmask & VIEWS_SPACE.bit.right)) { oppositeBitmask &= ~VIEWS_SPACE.bit.left; } } return getViewsFromBitmask(oppositeBitmask); }; /** * @ignore * @internal */ export const getViewsBitmask = ( viewsStr: View[] | string | undefined, ): number => { let viewsBitmask = 0; const views = validateStrList("views", viewsStr, isValidView); if (views) { for (const v of views) { Iif (!isValidView(v)) { throw MH.usageError(`Unknown view '${v}'`); } viewsBitmask |= VIEWS_SPACE.bit[v]; } } else { viewsBitmask = VIEWS_SPACE.bitmask; // default: all } return viewsBitmask; }; /** * @ignore * @internal */ export const parseScrollOffset = (input: string) => { const match = input.match(OFFSET_REGEX); if (!match) { throw MH.usageError(`Invalid offset: '${input}'`); } const reference = match[1]; const value = match[2]; /* istanbul ignore next */ // shouldn't happen if (!reference || !value) { throw MH.bugError("Offset regex: blank capture groups"); } return { reference, value }; }; const VIEWS: View[] = [ MC.S_AT, MC.S_ABOVE, MC.S_BELOW, MC.S_LEFT, MC.S_RIGHT, ] as const; /** * @ignore * @internal */ export const VIEWS_SPACE = createBitSpace<View>(newBitSpaces(), ...VIEWS); // -------------------- // Don't use capture groups for old browser support const OFFSET_REGEX = RegExp("(top|bottom|left|right): *([^ ].+)"); const getViewsFromBitmask = (bitmask: number): View[] => { const views: View[] = []; for (let bit = VIEWS_SPACE.start; bit <= VIEWS_SPACE.end; bit++) { const value = 1 << bit; if (bitmask & value) { const name = VIEWS_SPACE.nameOf(value); if (name) { views.push(name); } } } return views; }; |