-
-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathValidationUtils.js
More file actions
102 lines (86 loc) · 3.53 KB
/
Copy pathValidationUtils.js
File metadata and controls
102 lines (86 loc) · 3.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
91
92
93
94
95
96
97
98
99
100
101
102
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2014 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
// @INCLUDE_IN_API_DOCS
define(function (require, exports, module) {
/**
* Used to validate whether type of unknown value is an integer.
*
* @param {*} value Value for which to validate its type
* @return {boolean} true if value is a finite integer
*/
function isInteger(value) {
// Validate value is a number
if (typeof (value) !== "number" || isNaN(parseInt(value, 10))) {
return false;
}
// Validate number is an integer
if (Math.floor(value) !== value) {
return false;
}
// Validate number is finite
if (!isFinite(value)) {
return false;
}
return true;
}
/**
* Used to validate whether type of unknown value is an integer, and, if so,
* is it within the option lower and upper limits.
*
* @param {*} value Value for which to validate its type
* @param {number=} lowerLimit Optional lower limit (inclusive)
* @param {number=} upperLimit Optional upper limit (inclusive)
* @return {boolean} true if value is an interger, and optionally in specified range.
*/
function isIntegerInRange(value, lowerLimit, upperLimit) {
// Validate value is an integer
if (!isInteger(value)) {
return false;
}
// Validate integer is in range
var hasLowerLimt = (typeof (lowerLimit) === "number"),
hasUpperLimt = (typeof (upperLimit) === "number");
return ((!hasLowerLimt || value >= lowerLimit) && (!hasUpperLimt || value <= upperLimit));
}
/**
* Used to validate whether type of unknown value is a number (including decimals),
* and, if so, is it within the optional lower and upper limits.
*
* @param {*} value Value for which to validate its type
* @param {number=} lowerLimit Optional lower limit (inclusive)
* @param {number=} upperLimit Optional upper limit (inclusive)
* @return {boolean} true if value is a finite number, and optionally in specified range.
*/
function isWithinRange(value, lowerLimit, upperLimit) {
// Validate value is a number
if (typeof (value) !== "number" || !isFinite(value)) {
return false;
}
// Validate number is in range
var hasLowerLimit = (typeof (lowerLimit) === "number"),
hasUpperLimit = (typeof (upperLimit) === "number");
return ((!hasLowerLimit || value >= lowerLimit) && (!hasUpperLimit || value <= upperLimit));
}
// Define public API
exports.isInteger = isInteger;
exports.isIntegerInRange = isIntegerInRange;
exports.isWithinRange = isWithinRange;
});