forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationHelpers.cs
More file actions
84 lines (76 loc) · 2.41 KB
/
ValidationHelpers.cs
File metadata and controls
84 lines (76 loc) · 2.41 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
namespace JavaScriptEngineSwitcher.Core.Helpers
{
using System;
using System.Linq;
using System.Text.RegularExpressions;
/// <summary>
/// Validation helpers
/// </summary>
public static class ValidationHelpers
{
/// <summary>
/// List of supported types
/// </summary>
private static readonly Type[] _supportedTypes = new[]
{
typeof(Boolean), typeof(Int32), typeof(Double), typeof(String)
};
/// <summary>
/// Regular expression for working with JS-names
/// </summary>
private static readonly Regex _jsNameRegex = new Regex(@"^[A-Za-z_\$]+[0-9A-Za-z_\$]*$",
RegexOptions.Compiled);
/// <summary>
/// List of reserved words of JavaScript language
/// </summary>
private static readonly string[] _jsReservedWords = new[]
{
"abstract",
"boolean", "break", "byte",
"case", "catch", "char", "class", "const", "continue",
"debugger", "default", "delete", "do", "double",
"else", "enum", "export", "extends",
"false", "final", "finally", "float", "for", "function",
"goto",
"if", "implements", "import", "in", "instanceof", "int",
"interface",
"long",
"native", "new", "null",
"package", "private", "protected", "public",
"return",
"short", "static", "super", "switch", "synchronized",
"this", "throw", "throws", "transient", "true", "try", "typeof",
"var", "volatile", "void",
"while", "with"
};
/// <summary>
/// Checks whether supports a .NET type
/// </summary>
/// <param name="type">.NET type</param>
/// <returns>Result of check (true - is supported; false - is not supported)</returns>
public static bool IsSupportedType(Type type)
{
bool result = _supportedTypes.Contains(type);
return result;
}
/// <summary>
/// Checks a format of the name
/// </summary>
/// <param name="name">The name</param>
/// <returns>Result of check (true - correct format; false - wrong format)</returns>
public static bool CheckNameFormat(string name)
{
return _jsNameRegex.IsMatch(name);
}
/// <summary>
/// Checks a allowability of the name (compares with the list of
/// reserved words of JavaScript language)
/// </summary>
/// <param name="name">The name</param>
/// <returns>Result of check (true - allowed; false - forbidden)</returns>
public static bool CheckNameAllowability(string name)
{
return !_jsReservedWords.Contains(name);
}
}
}