forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafeRange.js
More file actions
43 lines (39 loc) · 1.1 KB
/
Copy pathSafeRange.js
File metadata and controls
43 lines (39 loc) · 1.1 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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Tests if the start and end indexes are a safe range for the given array.
*
* @function Phaser.Utils.Array.SafeRange
* @since 3.4.0
*
* @param {array} array - The array to check.
* @param {integer} startIndex - The start index.
* @param {integer} endIndex - The end index.
* @param {boolean} [throwError=true] - Throw an error if the range is out of bounds.
*
* @return {boolean} True if the range is safe, otherwise false.
*/
var SafeRange = function (array, startIndex, endIndex, throwError)
{
var len = array.length;
if (startIndex < 0 ||
startIndex > len ||
startIndex >= endIndex ||
endIndex > len ||
startIndex + endIndex > len)
{
if (throwError)
{
throw new Error('Range Error: Values outside acceptable range');
}
return false;
}
else
{
return true;
}
};
module.exports = SafeRange;