How to use the ret.types function in ret

To help you get started, we’ve selected a few ret examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github heydenberk / stochator / src / string.js View on Github external
code === null ? '' : String.fromCharCode(
        ignoreCase && boolean.random(prng) ? changeCase(code) : code);

const expandCharacter = ({value}) => DRange(value);

const expandRange = ({from, to}) => DRange(from, to);

const expandSet = (token, range) => {
    let drange = DRange();
    let setRanges = token.set.map((code) => expand(code, range));
    setRanges.forEach((setRange) => drange.add(setRange));
    return token.not ? range.clone().subtract(drange) : drange;
};

const expanders = {
  [ret.types.SET]: expandSet,
  [ret.types.RANGE]: expandRange,
  [ret.types.CHAR]: expandCharacter
};

const expand = (token, ...args) => expanders[token.type](token, ...args);

// These generators accept a token and the options object and return a character
// code.

const generateCharFromSet = (token, {range, prng}) => {
    const set = expand(token, range);
    return set.index(integer.boundedRandom(0, set.length - 1, prng));
};

const generateCharFromRange = ({from, to}, {prng}) =>
    integer.boundedRandom(from, to, prng);
github heydenberk / stochator / src / string.js View on Github external
ignoreCase && boolean.random(prng) ? changeCase(code) : code);

const expandCharacter = ({value}) => DRange(value);

const expandRange = ({from, to}) => DRange(from, to);

const expandSet = (token, range) => {
    let drange = DRange();
    let setRanges = token.set.map((code) => expand(code, range));
    setRanges.forEach((setRange) => drange.add(setRange));
    return token.not ? range.clone().subtract(drange) : drange;
};

const expanders = {
  [ret.types.SET]: expandSet,
  [ret.types.RANGE]: expandRange,
  [ret.types.CHAR]: expandCharacter
};

const expand = (token, ...args) => expanders[token.type](token, ...args);

// These generators accept a token and the options object and return a character
// code.

const generateCharFromSet = (token, {range, prng}) => {
    const set = expand(token, range);
    return set.index(integer.boundedRandom(0, set.length - 1, prng));
};

const generateCharFromRange = ({from, to}, {prng}) =>
    integer.boundedRandom(from, to, prng);
github heydenberk / stochator / src / string.js View on Github external
const generateFromToken = (token, groups, options) => {
    const result = generators[token.type](token, groups, options);
    if (token.type === ret.types.GROUP && token.remember) {
        groups.push(result);
    }
    return result;
};
github mariohmol / js-brasil / js-brasil-addons.js View on Github external
_expand(token) {
    if (token.type === ret.types.CHAR) {
      return new DRange(token.value);
    } else if (token.type === ret.types.RANGE) {
      return new DRange(token.from, token.to);
    } else {
      let drange = new DRange();
      for (let i = 0; i < token.set.length; i++) {
        let subrange = this._expand(token.set[i]);
        drange.add(subrange);
        if (this.ignoreCase) {
          for (let j = 0; j < subrange.length; j++) {
            let code = subrange.index(j);
            let otherCaseCode = this._toOtherCase(code);
            if (code !== otherCaseCode) {
              drange.add(otherCaseCode);
            }
          }
github mariohmol / js-brasil / demo / js-brasil.js View on Github external
_expand(token) {
    if (token.type === ret.types.CHAR) {
      return new DRange(token.value);
    } else if (token.type === ret.types.RANGE) {
      return new DRange(token.from, token.to);
    } else {
      let drange = new DRange();
      for (let i = 0; i < token.set.length; i++) {
        let subrange = this._expand(token.set[i]);
        drange.add(subrange);
        if (this.ignoreCase) {
          for (let j = 0; j < subrange.length; j++) {
            let code = subrange.index(j);
            let otherCaseCode = this._toOtherCase(code);
            if (code !== otherCaseCode) {
              drange.add(otherCaseCode);
            }
          }
        }
      }
github aaaristo / dyngodb / lib / parser.js View on Github external
_regexp= function (field,val)
                       {
                          var tks= ret(val.source),
                              _chars= function (start)
                              {
                                  return _.collect(tks.stack.slice(start),
                                           function (tk) { return String.fromCharCode(tk.value); })
                                          .join(''); 
                              };

                          if (tks.stack
                              &&tks.stack[0]
                              &&tks.stack[0].type==ret.types.POSITION
                              &&tks.stack[0].value=='^'
                              &&!_.filter(tks.stack.slice(1),function (tk) { return tk.type!=ret.types.CHAR; }).length)
                          {
                            var val= _chars(1);
                            if (val!='')
                              _field(field,val,'BEGINS_WITH');
                          }
                          else
                          if (tks.stack
                              &&!_.filter(tks.stack,function (tk) { return tk.type!=ret.types.CHAR; }).length)
                          {
                            var val= _chars(0);
                            if (val!='')
                              _field(field,val,'CONTAINS');
                          }
                          else
github mariohmol / js-brasil / js-brasil-addons.js View on Github external
},{}],13:[function(require,module,exports){
const ret    = require('ret');
const DRange = require('drange');
const types  = ret.types;


module.exports = class RandExp {
  /**
   * @constructor
   * @param {RegExp|String} regexp
   * @param {String} m
   */
  constructor(regexp, m) {
    this._setDefaults(regexp);
    if (regexp instanceof RegExp) {
      this.ignoreCase = regexp.ignoreCase;
      this.multiline = regexp.multiline;
      regexp = regexp.source;

    } else if (typeof regexp === 'string') {
github mariohmol / js-brasil / js-brasil.js View on Github external
},{}],10:[function(require,module,exports){
const ret    = require('ret');
const DRange = require('drange');
const types  = ret.types;


module.exports = class RandExp {
  /**
   * @constructor
   * @param {RegExp|String} regexp
   * @param {String} m
   */
  constructor(regexp, m) {
    this._setDefaults(regexp);
    if (regexp instanceof RegExp) {
      this.ignoreCase = regexp.ignoreCase;
      this.multiline = regexp.multiline;
      regexp = regexp.source;

    } else if (typeof regexp === 'string') {
github aaaristo / dyngodb / lib / parser.js View on Github external
                              &&!_.filter(tks.stack.slice(1),function (tk) { return tk.type!=ret.types.CHAR; }).length)
                          {
github GoogleContainerTools / kpt / docsy / node_modules / safe-regex / index.js View on Github external
*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var parse = require('ret');
var types = parse.types;

module.exports = function (re, opts) {
    if (!opts) opts = {};
    var replimit = opts.limit === undefined ? 25 : opts.limit;
    
    if (isRegExp(re)) re = re.source;
    else if (typeof re !== 'string') re = String(re);
    
    try { re = parse(re) }
    catch (err) { return false }
    
    var reps = 0;
    return (function walk (node, starHeight) {
        if (node.type === types.REPETITION) {
            starHeight ++;
            reps ++;

ret

Tokenizes a string that represents a regular expression.

MIT
Latest version published 1 year ago

Package Health Score

67 / 100
Full package analysis