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
path
Optional parameter that enables “object checking mode“ and looks for needle in that given path. Example:
arr.push({obj: {child: "test"}})
If you want to ignore this call, path would be obj.child in this case and needle would be test
No
''
stack
Comma separated list of strings to check in the stack trace. If provided, the override will only apply when the stack trace contains at least one of these patterns
No
''
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.
array-override forEach test
array.forEach(()=>{ // Do something })
in this call the array with value= test will be ignored even if it’s in the array.
array-override push test '' '' ads.js
array.push("test")
will be ignored, but only for ads.js file, it will work normally in other files. You can also give the name of the function or the stack depth as the parameter instead of the file name.
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:>>needle>>
FILTER: array-override >>params>>
Needle matches a call to Array.push.
The call to Array.push was ignored.
Array.includes returned >>returnValue>> for >>needle>>
FILTER: array-override >>params>>
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?