MCPcopy Create free account
hub / github.com/arktypeio/arktype / scanRange

Function scanRange

ark/type/deferred.ts:329–385  ·  view source on GitHub ↗
(def: string)

Source from the content-addressed store, hash-verified

327}
328
329const scanRange = (def: string): Facts | undefined => {
330 const m = range.exec(def)
331 if (!m) return
332 const [, left, leftOp, word, op, right] = m
333 // a limit too long for a number to hold as it's written throws parsing
334 if (!writesAsParsed(right) || (left && !writesAsParsed(left))) return
335 const of =
336 word === "string" || word === "number" || word === "number.integer" ?
337 scanTerm(word)
338 : word.endsWith("[]") ? scanTerm(word)
339 : undefined
340 if (!of?.pure || !of.base) return
341 const isLength = of.base === "string" || of.base === "array"
342 // a length's bound is an integer
343 const step = of.base === "number" ? 0 : 1
344 let min: number | undefined
345 let max: number | undefined
346 if (op[0] === "<") max = +right - (op === "<" ? step : 0)
347 else min = +right + (op === ">" ? step : 0)
348 if (left) {
349 // a bound before an array of a range bounds that range, as a parser reads it
350 if (op[0] !== "<" || range.test(word.replace(/(?:\[])+$/, ""))) return
351 min = +left + (leftOp === "<" ? step : 0)
352 }
353 // a length is positive and a range isn't empty
354 if (
355 (min !== undefined && !Number.isFinite(min)) ||
356 (max !== undefined && !Number.isFinite(max))
357 )
358 return
359 if ((min ?? -Infinity) > (max ?? Infinity)) return
360 if (min === max && (op.length === 1 || leftOp === "<")) return
361 if (isLength && ((max ?? 0) < 0 || (min ?? 0) < 0)) return
362 if (isLength || (op.length === 2 && (!left || leftOp === "<=")))
363 return rangeFacts(of, min, max)
364
365 if (
366 of.base === "integer" &&
367 ((min !== undefined && !Number.isSafeInteger(min)) ||
368 (max !== undefined && !Number.isSafeInteger(max)))
369 )
370 return
371
372 const minExcluded = left ? leftOp === "<" : op === ">"
373 // bounds apart from what they exclude
374 return {
375 key: def,
376 domains: numberBits,
377 pure: true,
378 base: of.base,
379 min,
380 minExclusive: of.base === "number" && minExcluded,
381 max,
382 maxExclusive: of.base === "number" && op === "<",
383 ends: endsOf(minExcluded ? undefined : min, op === "<" ? undefined : max)
384 }
385}
386

Callers 1

scanTermFunction · 0.85

Calls 7

writesAsParsedFunction · 0.85
scanTermFunction · 0.85
rangeFactsFunction · 0.85
endsOfFunction · 0.85
execMethod · 0.80
testMethod · 0.80
replaceMethod · 0.80

Tested by

no test coverage detected