Skip to content

Instantly share code, notes, and snippets.

@mr21
Created July 29, 2018 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mr21/18b079a853f174bcb3840415f28b01dd to your computer and use it in GitHub Desktop.
Save mr21/18b079a853f174bcb3840415f28b01dd to your computer and use it in GitHub Desktop.
Avoid (by alerting) stringified number used as key into Map
( function() {
const
protoHas = Map.prototype.has,
protoGet = Map.prototype.get,
protoSet = Map.prototype.set,
protoDelete = Map.prototype.delete;
Map.prototype.has = function( id ) {
if ( typeof id === "string" && !isNaN( +id ) ) {
console.warn( `Map.has( "${ id }" )` );
}
return protoHas.call( this, id );
};
Map.prototype.get = function( id ) {
if ( typeof id === "string" && !isNaN( +id ) ) {
console.warn( `Map.get( "${ id }" )` );
}
const res = protoGet.call( this, id );
if ( res === undefined ) {
console.warn( `Map.get( "${ id }" )`, undefined );
}
return res;
};
Map.prototype.set = function( id, val ) {
if ( typeof id === "string" && !isNaN( +id ) ) {
console.warn( `Map.set( "${ id }", ${ val } )` );
}
return protoSet.call( this, id, val );
};
Map.prototype.delete = function( id ) {
if ( typeof id === "string" && !isNaN( +id ) ) {
console.warn( `Map.delete( "${ id }" )` );
}
return protoDelete.call( this, id );
};
} )();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment