swig.ie8.js
/**
* Returns a valid javascript code that will
* check if a variable (or property chain) exists
* in the evaled context. For example:
*  check('foo.bar.baz')
* will return the following string:
*  typeof foo !== 'undefined' && typeof foo.bar !== 'undefined' && typeof foo.bar.baz !== 'undefined'
*/
Returns a valid javascript code that will
check if a variable (or property chain) exists
in the evaled context.
Returns a valid javascript code that will check if a variable ( or property chain ) exists in the evaled context .
function check(variable, context) {
  if (_.isArray(variable)) {
    return '(true)';
  }
  variable = variable.replace(/^this/, '_this.__currentContext');
  if (exports.isLiteral(variable)) {
    return '(true)';
  }
  var props = variable.split(/(\.|\[|\])/),
    chain = '',
    output = [],
    inArr = false,
    prevDot = false;
  if (typeof context === 'string' && context.length) {
    props.unshift(context);
  }
  props = _.reject(props, function (val) {
    return val === '';
  });
  props = filterVariablePath(props);
  _.each(props, function (prop) {
    if (prop === '.') {
      prevDot = true;
      return;
    }
    if (prop === '[') {
      inArr = true;
      return;
    }
    if (prop === ']') {
      inArr = false;
      return;
    }
    if (!chain) {
      chain = prop;
    } else if (inArr) {
      if (!exports.isStringLiteral(prop)) {
        if (prevDot) {
          output[output.length - 1] = _.last(output).replace(/\] !== "undefined"$/, '_' + prop + '] !== "undefined"');
          chain = chain.replace(/\]$/, '_' + prop + ']');
          return;
        }
        chain += '[___' + prop + ']';
      } else {
        chain += '[' + prop + ']';
      }
    } else {
      chain += '.' + prop;
    }
    prevDot = false;
    output.push('typeof ' + chain + ' !== "undefined"');
  });
  return '(' + output.join(' && ') + ')';
}
function check ( variable , context ) { if ( _ . isArray ( variable ) ) { return '(true)' ; } variable = variable . replace ( / ^this / , '_this.__currentContext' ) ; if ( exports . isLiteral ( variable ) ) { return '(true)' ; } var props = variable . split ( / (\.|\[|\]) / ) , chain = '' , output = [ ] , inArr = false , prevDot = false ; if ( typeof context === 'string' && context . length ) { props . unshift ( context ) ; } props = _ . reject ( props , function ( val ) { return val === '' ; } ) ; props = filterVariablePath ( props ) ; _ . each ( props , function ( prop ) { if ( prop === '.' ) { prevDot = true ; return ; } if ( prop === '[' ) { inArr = true ; return ; } if ( prop === ']' ) { inArr = false ; return ; } if ( ! chain ) { chain = prop ; } else if ( inArr ) { if ( ! exports . isStringLiteral ( prop ) ) { if ( prevDot ) { output [ output . length - 1 ] = _ . last ( output ) . replace ( / \] !== "undefined"$ / , '_' + prop + '] !== "undefined"' ) ; chain = chain . replace ( / \]$ / , '_' + prop + ']' ) ; return ; } chain += '[___' + prop + ']' ; } else { chain += '[' + prop + ']' ; } } else { chain += '.' + prop ; } prevDot = false ; output . push ( 'typeof ' + chain + ' !== "undefined"' ) ; } ) ; return '(' + output . join ( ' && ' ) + ')' ; }
Returns a valid javascript code that will
check if a variable (or property chain) exists
in the evaled context.
Returns a valid javascript code that will check if a variable ( or property chain ) exists in the evaled context .