All files / node_modules/js-yaml/lib/js-yaml/type/js regexp.js

15.79% Statements 3/19
100% Branches 0/0
100% Functions 0/0
15.79% Lines 3/19
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 611x   1x                                                                                                     1x              
'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
});