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 | 3x 3x 3x 10x 10x 3x 8x 3x 4x 4x 4x 4x 4x 4x 6x 6x 6x 6x 6x 7x 7x 6x 6x 5x 4x 4x 4x 4x 4x 3x 1x 1x 1x 4x 4x 4x 5x 5x 4x 1x 1x 5x 5x 4x 5x 5x 5x 5x 5x 9x 9x 5x 3x 3x 3x 3x 3x 3x 3x 5x 5x 3x 2x 2x 5x 3x | import * as util from '../util';
import {Expression, type ExpressionResult, Literal} from '../codegen-steps';
import type * as types from '../types';
const validateSetOperandCount = (count: number) => {
Iif (count < 3) {
throw new Error('Not enough operands for "o.set".');
}
Iif (count % 2 !== 0) {
throw new Error('Invalid number of operands for "o.set" operand.');
}
};
const validateDelOperandCount = (count: number) => {
Iif (count < 3) {
throw new Error('Not enough operands for "o.del".');
}
};
export const objectOperators: types.OperatorDefinition<any>[] = [
[
'keys',
[],
1,
(expr: types.ExprKeys, ctx) => {
const operand = ctx.eval(expr[1], ctx);
return util.keys(operand);
},
(ctx: types.OperatorCodegenCtx<types.ExprKeys>): ExpressionResult => {
ctx.link(util.keys, 'keys');
const js = `keys(${ctx.operands[0]})`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprKeys>,
[
'values',
[],
1,
(expr: types.ExprValues, ctx) => {
const operand = ctx.eval(expr[1], ctx);
return util.values(operand);
},
(ctx: types.OperatorCodegenCtx<types.ExprValues>): ExpressionResult => {
ctx.link(util.values, 'values');
const js = `values(${ctx.operands[0]})`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprValues>,
[
'entries',
[],
1,
(expr: types.ExprEntries, ctx) => {
const operand = ctx.eval(expr[1], ctx);
return util.entries(operand);
},
(ctx: types.OperatorCodegenCtx<types.ExprEntries>): ExpressionResult => {
ctx.link(util.entries, 'entries');
const js = `entries(${ctx.operands[0]})`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprEntries>,
[
'o.set',
[],
-1,
/**
* Set one or more properties on an object.
*
* ```
* ['o.set', {},
* 'a', 1,
* 'b', ['+', 2, 3],
* ]
* ```
*
* Results in:
*
* ```
* {
* a: 1,
* b: 5,
* }
* ```
*/
(expr: types.ExprObjectSet, ctx) => {
let i = 1;
const length = expr.length;
validateSetOperandCount(length);
const doc = util.asObj(ctx.eval(expr[i++], ctx)) as Record<string, unknown>;
while (i < length) {
const prop = util.str(ctx.eval(expr[i++], ctx)) as string;
if (prop === '__proto__') throw new Error('PROTO_KEY');
const value = ctx.eval(expr[i++], ctx);
doc[prop] = value;
}
return doc;
},
(ctx: types.OperatorCodegenCtx<types.ExprObjectSet>): ExpressionResult => {
const length = ctx.operands.length;
validateSetOperandCount(length + 1);
let i = 0;
let curr = ctx.operands[i++];
if (curr instanceof Literal) {
curr = new Literal(util.asObj(curr.val));
} else if (curr instanceof Expression) {
ctx.link(util.asObj, 'asObj');
curr = new Expression(`asObj(${curr})`);
}
ctx.link(util.str, 'str');
ctx.link(util.objSetRaw, 'objSetRaw');
while (i < length) {
let prop = ctx.operands[i++];
if (prop instanceof Literal) {
prop = new Literal(util.str(prop.val));
} else if (prop instanceof Expression) {
prop = new Expression(`str(${prop})`);
}
const value = ctx.operands[i++];
curr = new Expression(`objSetRaw(${curr}, ${prop}, ${value})`);
}
return curr;
},
] as types.OperatorDefinition<types.ExprObjectSet>,
[
'o.del',
[],
-1,
/**
* Delete one or more properties from an object.
*
* ```
* ['o.del', {}, 'prop1', 'prop2']
* ```
*/
(expr: types.ExprObjectSet, ctx) => {
let i = 1;
const length = expr.length;
validateDelOperandCount(length);
const doc = util.asObj(ctx.eval(expr[i++], ctx)) as Record<string, unknown>;
while (i < length) {
const prop = util.str(ctx.eval(expr[i++], ctx)) as string;
delete doc[prop];
}
return doc;
},
(ctx: types.OperatorCodegenCtx<types.ExprObjectSet>): ExpressionResult => {
const length = ctx.operands.length;
validateDelOperandCount(length + 1);
let i = 0;
let curr = ctx.operands[i++];
ctx.link(util.str, 'str');
ctx.link(util.objDelRaw, 'objDelRaw');
while (i < length) {
let prop = ctx.operands[i++];
if (prop instanceof Literal) {
prop = new Literal(util.str(prop.val));
} else if (prop instanceof Expression) {
prop = new Expression(`str(${prop})`);
}
curr = new Expression(`objDelRaw(${curr}, ${prop})`);
}
return curr;
},
] as types.OperatorDefinition<types.ExprObjectSet>,
];
|