Initial commit
[yaffs-website] / node_modules / js-yaml / lib / js-yaml / type / js / regexp.js
1 'use strict';
2
3 var Type = require('../../type');
4
5 function resolveJavascriptRegExp(data) {
6   if (data === null) return false;
7   if (data.length === 0) return false;
8
9   var regexp = data,
10       tail   = /\/([gim]*)$/.exec(data),
11       modifiers = '';
12
13   // if regexp starts with '/' it can have modifiers and must be properly closed
14   // `/foo/gim` - modifiers tail can be maximum 3 chars
15   if (regexp[0] === '/') {
16     if (tail) modifiers = tail[1];
17
18     if (modifiers.length > 3) return false;
19     // if expression starts with /, is should be properly terminated
20     if (regexp[regexp.length - modifiers.length - 1] !== '/') return false;
21   }
22
23   return true;
24 }
25
26 function constructJavascriptRegExp(data) {
27   var regexp = data,
28       tail   = /\/([gim]*)$/.exec(data),
29       modifiers = '';
30
31   // `/foo/gim` - tail can be maximum 4 chars
32   if (regexp[0] === '/') {
33     if (tail) modifiers = tail[1];
34     regexp = regexp.slice(1, regexp.length - modifiers.length - 1);
35   }
36
37   return new RegExp(regexp, modifiers);
38 }
39
40 function representJavascriptRegExp(object /*, style*/) {
41   var result = '/' + object.source + '/';
42
43   if (object.global) result += 'g';
44   if (object.multiline) result += 'm';
45   if (object.ignoreCase) result += 'i';
46
47   return result;
48 }
49
50 function isRegExp(object) {
51   return Object.prototype.toString.call(object) === '[object RegExp]';
52 }
53
54 module.exports = new Type('tag:yaml.org,2002:js/regexp', {
55   kind: 'scalar',
56   resolve: resolveJavascriptRegExp,
57   construct: constructJavascriptRegExp,
58   predicate: isRegExp,
59   represent: representJavascriptRegExp
60 });