map-override
The map-override
snippet overrides functions under Map.prototype to change their behaviour according to the given parameters.
Parameters
method
The Map function to override. Possible values to override the property with: set
, get
, has
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.
For 'set' this matches the value. For 'get' and 'has' this matches the key.
Yes
n.a.
returnValue
The return value for the matched function calls. For 'set' ignored. For 'get’ the value to return. For 'has', true or false.
No
''
path
Path to look for the needle. Only relevant for 'set' method when checking for object values.
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
map-override set test
Will ignore map.set("test")
while allowing other pairs to be added to the map.
Example:
const map = new Map(); map.set("key1", "value1"); map.set("key2", "value2"); map.set("key3", "test");
map will only have the key1:value1 and key2:value2 pairs inside of it.
map-override set /first.*second/
Will ignore the call if the value parameter to map.set
starts with "first" and ends with "second" while allowing other values to be added to the map.
Example:
const map = new Map(); map.set("key1", "value1"); map.set("key2", "value2"); map.set("key3", "first-123-second");
map will only have the key1:value1
and key2:value2
pairs inside of it.
map-override set test '' obj.child
map.set("key1": {obj: {child: "test"})
call will be ignored.
map-override get key1 customValue
map.get("key1")
will always return "customValue".
map-override get /first.*second/ ''
map.get("first-123-second")
will always return undefined regardless of whether it’s in the map or not.
map-override has key1 false
map.has("key1")
will always return false regardless of whether it’s in the map or not.
map-override set test '' '' ads.js
map.set("key1", "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 Map.prototype.set
After we have wrapped the Map.prototype.set API.
From this moment on set calls are intercepted by us.
Wrapped Map.prototype.get
After we have wrapped the Map.prototype.get API.
From this moment on get calls are intercepted by us.
Wrapped Map.prototype.has
After we have wrapped the Map.prototype.has API.
From this moment on has calls are intercepted by us.
Map.set is ignored for value matching needle:>>needle>>
FILTER: map-override >>params>>
Needle matches a call to Map.set with the given pair value.
The call to Map.set was ignored.
Map.get returned >>returnValue>>
for key: >>needle>>
FILTER: map-override >>params>>
Needle matches a call to Map.get.
Map.get returned the value given with returnValue instead of the correct value.
Map.has returned >>returnValue>>
for key: >>needle>>
FILTER: map-override >>params>>
Needle matches a call to Map.has.
Map.has returned the value given with returnValue instead of the correct value.
Last updated
Was this helpful?