guarded-array
A slow bounds checked array to help with debugging.
Example
var guard = //First create any old arrayvar array = 0 1 2 3 4 5 //Then we protect it using guard!var guardedArray = //The guarded array works just like a normal array:console //Accessing elements in the middle of array is okguardedArray3 = 100 //Writes propagate to underlying arrayconsole //But reading out of bounds throws an exceptiontry console catche console try console catche console //You can also create guarded arrays for subarrays of arraysvar subGuard = //Now you can only access elements in the range 1-3 of array safelyconsole //Writing/reading out of bounds throws an exceptionconsole //Use this module in your unit tests to help track down//pesky off-by-one errors
Output:
6 [ 0, 1, 2, 3, 4, 5 ]
[ 0, 1, 2, 100, 4, 5 ]
[Error: read out of bounds: -1]
[Error: read out of bounds: 10]
1 2
Error: read out of bounds: 3
at Array.get [as 3] (/Users/mikolalysenko/GitHub/guarded-array/guarded-array.js:39:15)
at Object.<anonymous> (/Users/mikolalysenko/GitHub/guarded-array/example/example.js:38:21)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
Install
npm install guarded-array
API
require('guarded-array')(array[,lo, hi])
Creates a guarded view of array
. Attempting to access outside of the bounds [lo,hi)
or changing the length of the array will result in an exception.
array
is an ordinary JavaScript arraylo
is an optional lower bound (default0
)hi
is an optional upper bound (defaultarray.length
)
Returns A guarded view of the array object. Writes to the guarded array propagate down to the underlying array object. Out of bounds access will trigger an exception.
Notes/Warnings
Array.isArray
doesn't work on guarded-array
. I'm open to suggestions on how to get this to work.
Credits
(c) 2014 Mikola Lysenko. MIT License