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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 8x 8x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 192x 192x 192x 192x 192x 192x 24x 8x 8x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { isNumeric } from '../utils/algorithms/number.utils.js' import { parseISO8601, timeNowFrame } from '../utils/algorithms/time.utils.js' import { formatters } from './expression-formatters.js' const defaultFormat = formatters['as is'].format /** @type {CaptureExpressionMap} */ const baseCaptureExpressions = { number: { value: 400, matchPredicate: () => (text) => isNumeric(text), defaultFormat: formatters.number.format, isConstant: true, }, string: { value: 300, matchPredicate: () => (text) => (text.startsWith('"') && text.endsWith('"')) || (text.startsWith('\'') && text.endsWith('\'')) || (text.startsWith('`') && text.endsWith('`')), defaultFormat, isConstant: true, }, } /** @type {CaptureExpressionMap} */ const specialCaptureExpressions = { string: { value: 1 << 20, matchPredicate: (match) => (text) => match === text, defaultFormat, isConstant: true, }, regex: { value: 200, matchPredicate: (regexPattern) => { const regex = new RegExp(regexPattern) return (text) => regex.test(text) }, defaultFormat, isConstant: true, }, any: { value: 100, matchPredicate: () => () => true, defaultFormat, isConstant: true, }, } /** @type {CaptureExpressionMap} */ const baseTimeCaptureExpressions = { // normal times 'unix timestamp': { value: 550, matchPredicate: () => (text) => isNumeric(text), defaultFormat: formatters.datetime.format, isConstant: false, }, 'iso 8601': { value: 550, matchPredicate: () => (text) => !isNaN(parseISO8601(text)), defaultFormat: formatters.datetime.format, isConstant: false, }, date: { value: 500, matchPredicate: () => (text) => isNumeric(text) || !isNaN(parseISO8601(text)), defaultFormat: formatters.datetime.format, isConstant: false, }, 'unix millis': { value: 550, matchPredicate: () => (text) => isNumeric(text), defaultFormat: formatters.timestamp.format, isConstant: false, }, } /** @type {Record<string, RelativeTimeCaptureExpressionPrefix>} */ const relativeTimeCaptureExpressionPrefix = { past: { additionalValue: 50, defaultMatchPredicate: (prev) => () => { const predicate = prev() return (text) => predicate(text) && timeIntervalCaptureExpressionPrefix.millisecond.currentTimeCompare(text) <= 0 }, }, present: { additionalValue: 100, defaultMatchPredicate: (prev) => () => { const predicate = prev() return (text) => predicate(text) && timeIntervalCaptureExpressionPrefix.second.currentTimeCompare(text) === 0 }, }, future: { additionalValue: 50, defaultMatchPredicate: (prev) => () => { const predicate = prev() return (text) => predicate(text) && timeIntervalCaptureExpressionPrefix.millisecond.currentTimeCompare(text) > 0 }, }, } /** @type {Record<string, TimeIntervalCaptureExpressionPrefix>} */ const timeIntervalCaptureExpressionPrefix = { millisecond: { additionalValue: 33, currentTimeCompare: (text) => { const timeText = isNumeric(text) ? new Date((+text) * 1000).valueOf() : parseISO8601(text) const timeNow = timeNowFrame() return timeText - timeNow }, }, second: { additionalValue: 30, currentTimeCompare: (text) => { const timeText = isNumeric(text) ? new Date((+text) * 1000).valueOf() : parseISO8601(text) const timeNow = timeNowFrame() return Math.floor(timeText / 1000) - Math.floor(timeNow / 1000) }, }, minute: { additionalValue: 29, currentTimeCompare: (text) => { const timeText = isNumeric(text) ? new Date((+text) * 1000).valueOf() : parseISO8601(text) const timeNow = timeNowFrame() return Math.floor(timeText / 60_000) - Math.floor(timeNow / 60_000) }, }, hour: { additionalValue: 28, currentTimeCompare: (text) => { const timeText = isNumeric(text) ? new Date((+text) * 1000).valueOf() : parseISO8601(text) const timeNow = timeNowFrame() return Math.floor(timeText / 360_000) - Math.floor(timeNow / 360_000) }, }, day: { additionalValue: 27, currentTimeCompare: (text) => { const timeText = isNumeric(text) ? new Date((+text) * 1000).valueOf() : parseISO8601(text) const timeNow = timeNowFrame() return Math.floor(timeText / 86_400_000) - Math.floor(timeNow / 86_400_000) }, }, week: { additionalValue: 26, currentTimeCompare: (text) => { const date = new Date(isNumeric(text) ? (+text) * 1000 : parseISO8601(text)) const dateNow = new Date(timeNowFrame()) const yearNow = dateNow.getFullYear() const yearDiff = date.getFullYear() - yearNow if (yearDiff !== 0) { return yearDiff } const oneJan = new Date(yearNow, 0, 1) const oneJanDay = oneJan.getDay() const oneJanTimeStamp = oneJan.valueOf() const weekNow = Math.ceil((((dateNow.valueOf() - oneJanTimeStamp) / 86400000) + oneJanDay + 1) / 7) const week = Math.ceil((((date.valueOf() - oneJanTimeStamp) / 86400000) + oneJanDay + 1) / 7) return week - weekNow }, }, month: { additionalValue: 25, currentTimeCompare: (text) => { const date = new Date(isNumeric(text) ? (+text) * 1000 : parseISO8601(text)) const dateNow = new Date(timeNowFrame()) return date.getFullYear() * 12 + date.getMonth() - dateNow.getFullYear() * 12 + dateNow.getMonth() }, }, year: { additionalValue: 24, currentTimeCompare: (text) => { const date = new Date(isNumeric(text) ? (+text) * 1000 : parseISO8601(text)) const dateNow = new Date(timeNowFrame()) return date.getFullYear() - dateNow.getFullYear() }, }, } /** * @returns {CaptureExpressionMap} build time capture expressions */ function buildTimeCaptureExpressions () { const { entries, fromEntries } = Object return fromEntries(entries(baseTimeCaptureExpressions).flatMap(([baseKey, baseInfo]) => { const result = [[baseKey, baseInfo]] for (const [relativePrefixKey, relativePrefixInfo] of entries(relativeTimeCaptureExpressionPrefix)) { const infoWithRelTime = { ...baseInfo, value: baseInfo.value + relativePrefixInfo.additionalValue, matchPredicate: relativePrefixInfo.defaultMatchPredicate(baseInfo.matchPredicate), } result.push([ `${relativePrefixKey} ${baseKey}`, infoWithRelTime, ]) for (const [intervalKey, intervalInfo] of entries(timeIntervalCaptureExpressionPrefix)) { result.push([ `${relativePrefixKey} ${intervalKey} ${baseKey}`, { ...infoWithRelTime, value: infoWithRelTime.value + intervalInfo.additionalValue, }]) } } return result })) } export const captureExpressions = { special: specialCaptureExpressions, named: { ...baseCaptureExpressions, ...buildTimeCaptureExpressions(), }, } /** @typedef {Record<string, CaptureExpressionInfo>} CaptureExpressionMap */ /** * @typedef {object} CaptureExpressionInfo * * Capture expression information used by key parser, as to get the correct matcher * based on the ket priority. * @property {number} value - Priority value of the capture expression, the higher value, the key is used when conflicting keys are found. * @property {MatchPredicateCreator} matchPredicate - Match predicate creator. The resulting match predicate is based on the `parameters` used * (e.g. creating a matcher from a regex pattern). * @property {FormatCall} defaultFormat - Default format to be used when no formatter is explicitly applied. * @property {boolean} isConstant - A flag to indicate whether the matcher will always match the same key in a translation map, * It helps to decide when to query the translation map for the same key. */ /** @typedef {import('./expression-formatters.js').FormatCall} FormatCall */ /** @typedef {(text: string) => boolean} MatchPredicate */ /** @typedef {(...parameters: string[]) => MatchPredicate} MatchPredicateCreator */ /** * @typedef {object} TimeIntervalCaptureExpressionPrefix * @property {number} additionalValue - Additional Priority value of the capture expression, the higher value, the key is used when conflicting keys are found. * @property {(timeStr: string) => number} currentTimeCompare - compare text formatted date to current time. returns negative number when `timeStr` is in the past, positive in the future, 0 in the present */ /** * @typedef {object} RelativeTimeCaptureExpressionPrefix * @property {number} additionalValue - Additional Priority value of the capture expression, the higher value, the key is used when conflicting keys are found. * @property {(prev: MatchPredicateCreator) => MatchPredicateCreator} defaultMatchPredicate - predicate to use when no no time unit is defined (e.g. present **day** date). */ |