All files / src/namespace index.ts

64.15% Statements 68/106
38.89% Branches 14/36
66.67% Functions 10/15
68.75% Lines 66/96
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185  15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x   15x           3x 3x       3x   3x 3x 2x     1x     4x 4x       4x     7x               4x   4x 4x 4x     2x 2x     2x 2x 2x 2x 2x                                       15x     15x       2x 2x 2x 2x   15x   17x 15x   17x     17x   15x 15x                             15x                                             15x 15x 15x 15x 6x 6x 2x   4x 4x     15x 15x 15x 15x 15x 15x 15x 15x                                    
import ClauseRef from "../models/ClauseRef";
 
import { cat, or, fclause, shape } from  "../core";
import isClause from "../utils/isClause";
import isPred from "../utils/isPred";
import isBool from "../preds/isBool";
import walk from "../walk";
import resolveWithRegistry from "./resolve";
import coerceIntoClause from "../utils/coerceIntoClause";
import * as oPath from "./simpleObjectPath";
 
import { isNamespacePath, isClauseRef } from "../utils";
import { GetNSFnClause, SetNSFnClause, NamespaceFnClause,
  SetMetaFnClause, GetMetaFnClause } from "../clauses/namespace.types";
let reg;
 
const _get = fclause( {
  args: cat( isNamespacePath ),
  ret: isClauseRef,
} ).instrument( _getUnchecked );
 
functionI _getUnchecked( ref ) {
  function getFn( prefix? ) {
    let path = reg;
    if ( prefix ) {
      path = prefix + ref;
    } else {
      path = ref;
    }
    var nObj = oPath.get( reg, _slashToDot( path ) );
 
    if ( nObj ) {
      return Object.assign( nObj[ '.expr' ], nObj[ '.meta' ] );
    } else {
      return undefined;
    }
  }

  var sr = new ClauseRef( { ref, getFn, conformFn: null } );
  sr.conform = function clauseRefConform( x ) {
    var ss = getFn();
    return walk( ss, x, { conform: true } );
  }
  return sr;
}
 
function _slashToDot( p ) {
  return p.replace( /^(.+)(\/)(.+)$/, '$1.$3' ).replace( /^\//, '' );
}
 
// var PartialRefMapClause = shape({
//   req: {
//     'refDefs': [isNamespacePath, ExprOrPartialRefMapClause]
//   }
// });
 
function getNamespacePath( { nsPath } ) {
  var retVal;
 
  var nameObj = _get( nsPath );
  retVal = nameObj;
 
  reEturn retVal;
}E
 
function setNamespacePath( { nsPath, expression } ) {
  _processVal( nsPath, expression );
}
 
function _processVal( prefix, expression ) {
  if ( expression ) {
    if ( expression.clause || expression.pred ) {
      var expr = expression.clause || expression.pred;
      _set( prefix, { '.expr': expr } );
      return expr;
    } else {
      console.error( expression );
      throw '!';
    }
  // TODO
  // } else if ( val.partialRefMap ) {
  //   var { refDefs } = val.partialRefMap;
  //   for ( var k in refDefs ) {
  //     if ( refDefs.hasOwnProperty( k ) ) {
  //       var retVal = _processVal( refDefs[ k ] );
  //     }
  //   }
  } else {
    console.error( expression );
    throw '!';
  }
}
 
var NameObjClause = shape( {
  req: { '.expr': or( isClause, isPred ) }
} );
 
var _set = fclause( {
  args: cat( isNamespacePath, NameObjClause ),
  ret: isBool,
} ).instrument( function _set( n, nObj ) {
  _maybeInitRegistry();
  var existing = oPath.get( reg, _slashToDot( n ) );
  oPath.set( reg, _slashToDot( n ), Object.assign( {}, existing, nObj ) );
  return true;
} );
 
var K = '___CLAUSEJS_REGISTRY';
 
function _maybeInitRegistry() {
  if ( !reg ) {
    clearRegistry();
  }
  return reg;
}

export function clearRegistry() {
  reg = global[ K ] = {};
}

export const setMeta = SetMetaFnClause.instrumentConformed(
  function setMeta( { source: { namespacePath, expression }, metaObj, registry } ) {
    if ( !registry ) {
      registry = reg;
    }
    if ( namespacePath ) {
      var nObj = oPath.get( registry, _slashToDot( namespacePath ) );
      var currMeta = nObj && nObj[ '.meta' ];
      oPath.set( registry, _slashToDot( namespacePath ), Object.assign( {}, nObj, { '.meta': Object.assign( {}, currMeta, metaObj ) } ) );
    } else if ( expression ) {
      const clause = coerceIntoClause( expression );
      clause.meta = Object.assign( clause.meta, metaObj );
    }
  }
);

export const getMeta = GetMetaFnClause.instrumentConformed(
  function getMeta( { source: { namespacePath, expression }, registry } ) {
    if ( !registry ) {
      registry = reg;
    }
    if ( namespacePath ) {
      let nObj = oPath.get( registry, _slashToDot( namespacePath ) );
      let meta = nObj && nObj[ '.meta' ];
      return meta;
    } else if ( expression ) {
      const clause = coerceIntoClause( expression );
      return clause.meta;
    }
  }
);
 
export function resolve( expr, reg ) {
  if ( !reg ) {
    return resolveWithRegistry( expr, getRegistry() );
  } else E{
    return resolveWithRegistry( expr, reg );
  }
}
 
_maybeInitRegistry();
 
export const getRegistry = () => reg;
 
const namespaceGetOrSet = NamespaceFnClause.instrumentConformed(
  function namespaceGetOrSet( { register, retrieve } ) {
    if ( register ) {
      return setNamespacePath( register );
    } else if ( retrieve ) {
      return getNamespacePath( retrieve );
    }
  }
)
 
export const get = GetNSFnClause.instrumentConformed( getNamespacePath );
export const set = SetNSFnClause.instrumentConformed( setNamespacePath );
 
namespaceGetOrSet.get = get;
namespaceGetOrSet.set = set;
namespaceGetOrSet.clearRegistry = clearRegistry;
namespaceGetOrSet.getRegistry = getRegistry;
namespaceGetOrSet.setMeta = setMeta;
 
export default namespaceGetOrSet;