All files / json-patch/codegen/ops test.ts

100% Statements 11/11
100% Branches 4/4
100% Functions 2/2
100% Lines 8/8

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  4x 4x 4x 4x     4x 39x                     39x           39x  
import type {OpTest} from '../../op';
import {$$find} from '@jsonjoy.com/json-pointer/lib/codegen/find';
import {$$deepEqual} from '@jsonjoy.com/util/lib/json-equal/$$deepEqual';
import {type JavaScriptLinked, compileClosure, type JavaScript} from '@jsonjoy.com/util/lib/codegen';
import {predicateOpWrapper} from '../util';
import type {ApplyFn} from '../types';
 
export const $$test = (op: OpTest): JavaScriptLinked<ApplyFn> => {
  const js = /* js */ `
(function(wrapper){
  var find = ${$$find(op.path)};
  var deepEqual = ${$$deepEqual(op.value)};
  return wrapper(function(doc){
    var val = find(doc);
    if (val === undefined) return ${op.not ? 'true' : 'false'};
    return ${op.not ? '!' : ''}deepEqual(val);
  });
})`;
 
  return {
    deps: [predicateOpWrapper] as unknown[],
    js: js as JavaScript<(...deps: unknown[]) => ApplyFn>,
  };
};
 
export const $test = (op: OpTest): ApplyFn => compileClosure($$test(op));