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
90 lines (78 loc) · 2.53 KB
/
ValidationHelpers.cs
File metadata and controls
90 lines (78 loc) · 2.53 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
85
86
87
88
89
90
using System;
using System.Linq;
using System.Text.RegularExpressions;
using JavaScriptEngineSwitcher.Core.Extensions;
namespace JavaScriptEngineSwitcher.Core.Helpers
{
/// <summary>
/// Validation helpers
/// </summary>
public static class ValidationHelpers
{
/// <summary>
/// List of supported types
/// </summary>
private static readonly Type[] _supportedTypes =
{
typeof(Undefined), typeof(Boolean), typeof(Int32), typeof(Double), typeof(String)
};
/// <summary>
/// List of primitive type codes
/// </summary>
private static readonly TypeCode[] _primitiveTypeCodes =
{
TypeCode.Boolean,
TypeCode.SByte, TypeCode.Byte,
TypeCode.Int16, TypeCode.UInt16, TypeCode.Int32, TypeCode.UInt32, TypeCode.Int64, TypeCode.UInt64,
TypeCode.Single, TypeCode.Double, TypeCode.Decimal,
TypeCode.Char, TypeCode.String
};
/// <summary>
/// Regular expression for working with JS names
/// </summary>
private static readonly Regex _jsNameRegex = new Regex("^" + CommonRegExps.JsNamePattern + "$");
/// <summary>
/// Regular expression for working with document names
/// </summary>
private static readonly Regex _documentNameRegex = new Regex("^" + CommonRegExps.DocumentNamePattern + "$");
/// <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 whether .NET type is primitive
/// </summary>
/// <param name="type">.NET type</param>
/// <returns>Result of check (true - is primitive; false - is not primitive)</returns>
public static bool IsPrimitiveType(Type type)
{
TypeCode typeCode = type.GetTypeCode();
bool result = _primitiveTypeCodes.Contains(typeCode);
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 format of the document name
/// </summary>
/// <param name="name">The document name</param>
/// <returns>Result of check (true - correct format; false - wrong format)</returns>
public static bool CheckDocumentNameFormat(string name)
{
return _documentNameRegex.IsMatch(name);
}
}
}