示例#1
0
/**
 * expands a token to a DiscontinuousRange of characters which has a 
 * length and an index function (for random selecting)
 *
 * @param {Object} token
 * @return {DiscontinuousRange}
 */
function expand(token) {
  if (token.type === ret.types.CHAR) {
    return new DRange(token.value);
  }
  if (token.type === ret.types.RANGE) {
    return new DRange(token.from, token.to);
  }
  if (token.type === ret.types.SET) {
    var drange = new DRange();
    for (var i = 0; i < token.set.length; i++) {
      var subrange = expand.call(this, token.set[i]);
      drange.add(subrange);
      if (this.ignoreCase) {
        for (var j = 0; j < subrange.length; j++) {
          var code = subrange.index(j);
          var otherCaseCode = toOtherCase(code);
          if (code !== otherCaseCode) {
            drange.add(otherCaseCode);
          }
        }
      }
    }
    if (token.not) {
      return this.defaultRange.clone().subtract(drange);
    } else {
      return drange;
    }
  }
  throw new Error('unexpandable token type: ' + token.type);
}
示例#2
0
/**
 * expands a token to a DiscontinuousRange of characters which has a 
 * length and an index function (for random selecting)
 *
 * @param {Object} token
 * @return {DiscontinuousRange}
 */
function expand(token) {
  if (token.type === ret.types.CHAR) return new DRange(token.value);
  if (token.type === ret.types.RANGE) return new DRange(token.from, token.to);
  if (token.type === ret.types.SET) {
    var drange = new DRange();
    for (var i = 0; i < token.set.length; i++) {
      drange.add(expand.call(this, token.set[i]));
    }
    if (token.not) {
      return this.defaultRange.clone().subtract(drange);
    } else {
      return drange;
    }
  }
  throw new Error('unexpandable token type: ' + token.type);
};