Sha256: 741a7c3b445dd7321d02a7a0c7e9420dd7a9dc8dd1d8caab7736681b9153baf3

Contents?: true

Size: 1.54 KB

Versions: 117

Compression:

Stored size: 1.54 KB

Contents

'use strict';

var Type = require('../../type');

function resolveJavascriptRegExp(data) {
  if (data === null) return false;
  if (data.length === 0) return false;

  var regexp = data,
      tail   = /\/([gim]*)$/.exec(data),
      modifiers = '';

  // if regexp starts with '/' it can have modifiers and must be properly closed
  // `/foo/gim` - modifiers tail can be maximum 3 chars
  if (regexp[0] === '/') {
    if (tail) modifiers = tail[1];

    if (modifiers.length > 3) return false;
    // if expression starts with /, is should be properly terminated
    if (regexp[regexp.length - modifiers.length - 1] !== '/') return false;
  }

  return true;
}

function constructJavascriptRegExp(data) {
  var regexp = data,
      tail   = /\/([gim]*)$/.exec(data),
      modifiers = '';

  // `/foo/gim` - tail can be maximum 4 chars
  if (regexp[0] === '/') {
    if (tail) modifiers = tail[1];
    regexp = regexp.slice(1, regexp.length - modifiers.length - 1);
  }

  return new RegExp(regexp, modifiers);
}

function representJavascriptRegExp(object /*, style*/) {
  var result = '/' + object.source + '/';

  if (object.global) result += 'g';
  if (object.multiline) result += 'm';
  if (object.ignoreCase) result += 'i';

  return result;
}

function isRegExp(object) {
  return Object.prototype.toString.call(object) === '[object RegExp]';
}

module.exports = new Type('tag:yaml.org,2002:js/regexp', {
  kind: 'scalar',
  resolve: resolveJavascriptRegExp,
  construct: constructJavascriptRegExp,
  predicate: isRegExp,
  represent: representJavascriptRegExp
});

Version data entries

117 entries across 102 versions & 17 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
disco_app-0.18.0 test/dummy/node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
disco_app-0.18.2 test/dummy/node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
appmap-0.72.2 ./node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
appmap-0.72.1 ./node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
appmap-0.72.0 ./node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
appmap-0.71.0 ./node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
disco_app-0.16.1 test/dummy/node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
disco_app-0.16.1 test/dummy/node_modules/@rails/webpacker/node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
appmap-0.70.2 ./node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
appmap-0.70.1 ./node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
appmap-0.70.0 ./node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
appmap-0.69.0 ./node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
appmap-0.68.2 ./node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
appmap-0.68.1 ./node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
disco_app-0.15.2 test/dummy/node_modules/@rails/webpacker/node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
disco_app-0.15.2 test/dummy/node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
appmap-0.68.0 ./node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
appmap-0.67.1 ./node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
appmap-0.67.0 ./node_modules/js-yaml/lib/js-yaml/type/js/regexp.js