All files / json-pointer/src/codegen find.ts

100% Statements 11/11
100% Branches 1/1
100% Functions 1/1
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      1x 4x 3x 3x 7x 7x   3x 3x    
import type {JavaScript} from '@jsonjoy.com/codegen';
import type {Path} from '../types';
 
export const $$find = (path: Path): JavaScript<(doc: unknown) => unknown> => {
  if (path.length === 0) return '(function(x){return x})' as JavaScript<(doc: unknown) => unknown>;
  let fn = '(function(){var h=Object.prototype.hasOwnProperty;return(function(o){var k,u=void 0;try{';
  for (let i = 0; i < path.length; i++) {
    fn += 'k=' + JSON.stringify(path[i]) + ';';
    fn += 'if(!h.call(o,k))return u;o=o[k];';
  }
  fn += 'return o}catch(e){return u}})})()';
  return fn as JavaScript<(doc: unknown) => unknown>;
};