array-override
The array-override
overrides functions under Array.prototype to change their behaviour according to the given parameters. .
Parameters
method
The Array function to override. Possible values to override the property with: push, includes
.
Yes
n.a.
needle
The string or regex used to determine which function calls to trap. If the needle matches the parameter to the function call, the snippet will work, else the function will behave as normal. If the string begins and ends with a slash ( / ), the text in between is treated as a regular expression.
Yes
n.a.
returnValue
Optional parameter that is only relevant for includes
. This parameter determines what to return when the parameter matches the needle. Accepts: true
or false
. Default is false
.
No
false
Filter examples
array-override push test
Will ignore array.push(“test“) while allowing other values to be added to the array.
Example: const arr = []; arr.push(“1“); arr.push(“2“); arr.push(“test“)
arr will only have the values “1” and “2” inside of it.
array-override push /first.*second/
Will ignore the call if the parameter to array.push starts with “first“ and ends with “second“ while allowing other values to be added to the array.
Example: const arr = []; arr.push(“1“); arr.push(“2“); arr.push(“first-123-second“)
arr will only have the values “1” and “2” inside of it.
array-override includes test true
array.includes(“test“) will always return true regardless of whether it’s in the array or not.
array-override includes /first.*second/ false
array.includes(“first-123-second“) will always return false regardless of whether it’s in the array or not.
Debugging
The following table contains messages you'll find useful during debugging:
Wrapped Array.prototype.push
After we have wrapped the Array.prototype.push API.
From this moment on push calls are intercepted by us.
Wrapped Array.prototype.includes
After we have wrapped the Array.prototype.includes API.
From this moment on includes calls are intercepted by us.
Array.push is ignored for needle: <>
FILTER: array-override <>
Needle matches a call to Array.push.
The call to Array.push was ignored.
Array.includes returned <> for <>
FILTER: array-override <>
Needle matches a call to Array.includes.
Array.includes returned the value given with returnValue instead of the correct value.
Last updated
Was this helpful?