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 | 3x 3x 3x 96x 96x 96x 3x 30x 166x 166x 3x 54x 5x 5x 5x 14x 7x 7x 7x 5x 32x 32x 4x 4x 4x 32x 32x 15x 15x 15x 32x 32x 4x 4x 4x 15x 15x 15x 15x 3x 3x 3x 11x 11x 11x 3x 8x 8x 8x 8x 3x 3x 1x 2x 2x 2x 2x 2x 2x | import {Expression, type ExpressionResult, Literal} from '../codegen-steps';
import * as util from '../util';
import type * as types from '../types';
const binaryOperands = (
expr: types.BinaryExpression<any>,
ctx: types.OperatorEvalCtx,
): [left: unknown, right: unknown] => {
const left = ctx.eval(expr[1], ctx);
const right = ctx.eval(expr[2], ctx);
return [left, right];
};
const createValidationOperator = <E extends types.Expr>(name: string, validate: (value: unknown) => boolean) => {
return [
name + '?',
[],
1,
(expr: E, ctx) => {
const email = ctx.eval(expr[1], ctx);
return validate(email);
},
(ctx: types.OperatorCodegenCtx<E>): ExpressionResult => {
ctx.link(validate, 'is_' + name);
return new Expression(`is_${name}(${ctx.operands[0]})`);
},
] as types.OperatorDefinition<E>;
};
export const stringOperators: types.OperatorDefinition<any>[] = [
[
'.',
['cat'],
-1,
(expr: types.ExprCat, ctx) => {
return expr.slice(1).reduce((acc, e) => acc + util.str(ctx.eval(e, ctx)), '');
},
(ctx: types.OperatorCodegenCtx<types.ExprCat>): ExpressionResult => {
ctx.link(util.str, 'str');
const parts: string[] = [];
for (const operand of ctx.operands) {
if (operand instanceof Literal) {
parts.push(JSON.stringify(util.str(operand.val)));
} else if (operand instanceof Expression) {
parts.push(`str(${operand})`);
}
}
return new Expression(parts.join('+'));
},
] as types.OperatorDefinition<types.ExprCat>,
[
'contains',
[],
2,
(expr: types.ExprContains, ctx) => {
const [outer, inner] = binaryOperands(expr, ctx);
return util.contains(outer, inner);
},
(ctx: types.OperatorCodegenCtx<types.ExprContains>): ExpressionResult => {
ctx.link(util.contains, 'contains');
const js = `contains(${ctx.operands[0]},${ctx.operands[1]})`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprContains>,
[
'starts',
[],
2,
(expr: types.ExprStarts, ctx) => {
const [outer, inner] = binaryOperands(expr, ctx);
return util.starts(outer, inner);
},
(ctx: types.OperatorCodegenCtx<types.ExprStarts>): ExpressionResult => {
ctx.link(util.starts, 'starts');
const js = `starts(${ctx.operands[0]},${ctx.operands[1]})`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprStarts>,
[
'ends',
[],
2,
(expr: types.ExprEnds, ctx) => {
const [outer, inner] = binaryOperands(expr, ctx);
return util.ends(outer, inner);
},
(ctx: types.OperatorCodegenCtx<types.ExprEnds>): ExpressionResult => {
ctx.link(util.ends, 'ends');
const js = `ends(${ctx.operands[0]},${ctx.operands[1]})`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprEnds>,
[
'substr',
[],
3,
(expr: types.ExprSubstr, ctx) => {
const str = ctx.eval(expr[1], ctx);
const start = ctx.eval(expr[2], ctx);
const end = ctx.eval(expr[3], ctx);
return util.substr(str, start, end);
},
(ctx: types.OperatorCodegenCtx<types.ExprSubstr>): ExpressionResult => {
ctx.link(util.substr, 'substr');
const js = `substr(${ctx.operands[0]},${ctx.operands[1]},${ctx.operands[2]})`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprSubstr>,
[
'matches',
[],
2,
(expr: types.ExprEnds, ctx) => {
let pattern = expr[2];
Iif (pattern instanceof Array && pattern.length === 1) pattern = pattern[0];
if (typeof pattern !== 'string')
throw new Error('"matches" second argument should be a regular expression string.');
Iif (!ctx.createPattern)
throw new Error('"matches" operator requires ".createPattern()" option to be implemented.');
const fn = ctx.createPattern(pattern);
const outer = ctx.eval(expr[1], ctx);
return fn(util.str(outer));
},
(ctx: types.OperatorCodegenCtx<types.ExprEnds>): ExpressionResult => {
const pattern = ctx.operands[1];
if (!(pattern instanceof Literal) || typeof pattern.val !== 'string')
throw new Error('"matches" second argument should be a regular expression string.');
Iif (!ctx.createPattern)
throw new Error('"matches" operator requires ".createPattern()" option to be implemented.');
const fn = ctx.createPattern(pattern.val);
const d = ctx.link(fn);
ctx.link(util.str, 'str');
const subject = ctx.operands[0];
return new Expression(`${d}(str(${subject}))`);
},
] as types.OperatorDefinition<types.ExprEnds>,
createValidationOperator<types.ExprIsEmail>('email', util.isEmail),
createValidationOperator<types.ExprIsHostname>('hostname', util.isHostname),
createValidationOperator<types.ExprIsIp4>('ip4', util.isIp4),
createValidationOperator<types.ExprIsIp6>('ip6', util.isIp6),
createValidationOperator<types.ExprIsUuid>('uuid', util.isUuid),
createValidationOperator<types.ExprIsUri>('uri', util.isUri),
createValidationOperator<types.ExprIsDuration>('duration', util.isDuration),
createValidationOperator<types.ExprIsDate>('date', util.isDate),
createValidationOperator<types.ExprIsTime>('time', util.isTime),
createValidationOperator<types.ExprIsDateTime>('dateTime', util.isDateTime),
];
|