50 lines
2.3 KiB
Plaintext
50 lines
2.3 KiB
Plaintext
import { TemplateLiteralParseExact } from './parse.mjs';
|
|
import { TypeBoxError } from '../error/index.mjs';
|
|
// ------------------------------------------------------------------
|
|
// TemplateLiteralFiniteError
|
|
// ------------------------------------------------------------------
|
|
export class TemplateLiteralFiniteError extends TypeBoxError {
|
|
}
|
|
// ------------------------------------------------------------------
|
|
// IsTemplateLiteralFiniteCheck
|
|
// ------------------------------------------------------------------
|
|
// prettier-ignore
|
|
function IsNumberExpression(expression) {
|
|
return (expression.type === 'or' &&
|
|
expression.expr.length === 2 &&
|
|
expression.expr[0].type === 'const' &&
|
|
expression.expr[0].const === '0' &&
|
|
expression.expr[1].type === 'const' &&
|
|
expression.expr[1].const === '[1-9][0-9]*');
|
|
}
|
|
// prettier-ignore
|
|
function IsBooleanExpression(expression) {
|
|
return (expression.type === 'or' &&
|
|
expression.expr.length === 2 &&
|
|
expression.expr[0].type === 'const' &&
|
|
expression.expr[0].const === 'true' &&
|
|
expression.expr[1].type === 'const' &&
|
|
expression.expr[1].const === 'false');
|
|
}
|
|
// prettier-ignore
|
|
function IsStringExpression(expression) {
|
|
return expression.type === 'const' && expression.const === '.*';
|
|
}
|
|
// ------------------------------------------------------------------
|
|
// IsTemplateLiteralExpressionFinite
|
|
// ------------------------------------------------------------------
|
|
// prettier-ignore
|
|
export function IsTemplateLiteralExpressionFinite(expression) {
|
|
return (IsNumberExpression(expression) || IsStringExpression(expression) ? false :
|
|
IsBooleanExpression(expression) ? true :
|
|
(expression.type === 'and') ? expression.expr.every((expr) => IsTemplateLiteralExpressionFinite(expr)) :
|
|
(expression.type === 'or') ? expression.expr.every((expr) => IsTemplateLiteralExpressionFinite(expr)) :
|
|
(expression.type === 'const') ? true :
|
|
(() => { throw new TemplateLiteralFiniteError(`Unknown expression type`); })());
|
|
}
|
|
/** Returns true if this TemplateLiteral resolves to a finite set of values */
|
|
export function IsTemplateLiteralFinite(schema) {
|
|
const expression = TemplateLiteralParseExact(schema.pattern);
|
|
return IsTemplateLiteralExpressionFinite(expression);
|
|
}
|