Yangutils - uses statement can be root
[dlux.git] / modules / common-yangutils-resources / src / main / resources / yangutils / services / restrictions.services.js
1 define([], function () {
2     'use strict';
3
4     function RestrictionsService(){
5
6         var service = {
7             getEqualsFnc: getEqualsFnc,
8             getIsDecimalFnc: getIsDecimalFnc,
9             getIsNumberFnc: getIsNumberFnc,
10             getIsUNumberFnc: getIsUNumberFnc,
11             getMinMaxFnc: getMinMaxFnc,
12             getReqexpValidationFnc: getReqexpValidationFnc,
13             isInArray: isInArray,
14         };
15
16         return service;
17
18         // TODO: add service's description
19         function getEqualsFnc(target) {
20             var intTarget = parseInt(target);
21
22             return new RestrictionObject(
23                 function (value) {
24                     var intVal = convertToInteger(value);
25                     return intVal === intTarget;
26                 },
27                 'Value must be equal to ' + target
28             );
29         }
30
31         // TODO: add service's description
32         function getMinMaxFnc(min, max) {
33             var intMin = parseInt(min),
34                 intMax = parseInt(max);
35
36             return new RestrictionObject(
37                 function (value) {
38                     var intVal = convertToInteger(value);
39                     return (intMin <= intVal) && (intVal <= intMax);
40                 },
41                 'Value must be in between ' + min + ' and ' + max
42             );
43         }
44
45         // TODO: add service's description
46         function getReqexpValidationFnc(patternString) {
47             return new RestrictionObject(
48                 function (value) {
49                     var pattern = new RegExp(patternString);
50                     return pattern.test(value.toString());
51                 },
52                 'Value must match ' + patternString
53             );
54         }
55
56         // TODO: add service's description
57         function getIsNumberFnc() {
58             return new RestrictionObject(
59                 function (value) {
60                     var pattern = new RegExp('^[+-]?((0x[0-9A-Fa-f]+)|(0[0-9]+)|([0-9]+))$');
61                     return pattern.test(value.toString());
62                 },
63                 'Value must be number (+/-, 0x and 0) prefixed are permitted'
64             );
65         }
66
67         // TODO: add service's description
68         function getIsUNumberFnc() {
69             return new RestrictionObject(
70                 function (value) {
71                     var pattern = new RegExp('^[+]?((0x[0-9A-Fa-f]+)|(0[0-9]+)|([0-9]+))$');
72                     return pattern.test(value.toString());
73                 },
74                 'Value must be positive number (+, 0x and 0) prefixed are permitted'
75             );
76         }
77
78         // TODO: add service's description
79         function getIsDecimalFnc() {
80             return new RestrictionObject(
81                 function (value) {
82                     var pattern = new RegExp('^[-]?[1-9]?[0-9]+[.|,]?[0-9]*$');
83                     return pattern.test(value.toString());
84                 },
85                 'Value must be decimal number - prefix is permitted'
86             );
87         }
88
89         // TODO: add service's description
90         function isInArray(array) {
91             return new RestrictionObject(
92                 function (value) {
93                     return array.some(function (arrVal) {
94                         return arrVal === value;
95                     });
96                 },
97                 'Value must be in ' + array.toString()
98             );
99         }
100
101         /**
102          * Base restriction object
103          * @param fnc
104          * @param info
105          * @constructor
106          */
107         function RestrictionObject(fnc, info) {
108             this.info = info;
109             this.check = fnc;
110         }
111
112         // TODO: add function's description
113         function convertToInteger(value) {
114             var strVal = typeof value === 'string' ? value : value.toString(),
115                 radix = strVal.indexOf('0x') === 0 ? 16 : strVal.indexOf('0') === 0 ? 8 : 10;
116
117             return parseInt(strVal, radix);
118         }
119     }
120
121     RestrictionsService.$inject = [];
122
123     return RestrictionsService;
124
125 });