-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (50 loc) · 1.06 KB
/
index.js
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
'use strict';
/**
* @file safer-regex main
* @module safer-regex
* @subpackage main
* @version 0.2.0
* @author hex7c0 <hex7c0@gmail.com>
* @copyright hex7c0 2016
* @license GPLv3
*/
/*
* initialize module
*/
var safe = require('safe-regex');
/**
* option setting
*
* @exports safer
* @function safer
* @param {String} regex - check this string regex
* @param {Boolean} [exception] - raise an exception if true
*/
function safer(regex, exception) {
var e = exception === undefined ? true : Boolean(exception);
var output;
if (e) {
output = function(err) {
if (err) {
throw new Error(err);
}
};
} else {
output = function(err) {
return Boolean(!err);
};
}
if (!regex || typeof regex !== 'string') { // string validation
return output('argument must be a string');
}
try { // regex validation
var _ = new RegExp(regex);
} catch (err) {
return output(err);
}
if (safe(regex) === false) {
return output('regex is invalid or not safe');
}
return output();
}
module.exports = safer;