[api] colors.setTheme will warn and ignore any attempts to overwrite important String.prototypes

This commit is contained in:
Marak Squires 2011-12-09 21:02:03 -08:00
parent 75858e0e5a
commit 19f23dd070

View File

@ -45,7 +45,6 @@ if (!isHeadless) {
// Prototypes the string object to have additional method calls that add terminal colors
//
var addProperty = function (color, func) {
var allowOverride = ['bold'];
exports[color] = function(str) {
return func.apply(str);
};
@ -100,10 +99,24 @@ exports.addSequencer('zebra', function (letter, i, exploded) {
});
exports.setTheme = function (theme) {
//
// Remark: This is a list of methods that exist
// on String that you should not overwrite.
//
var stringPrototypeBlacklist = [
'__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', 'charAt', 'constructor',
'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf', 'charCodeAt',
'indexOf', 'lastIndexof', 'length', 'localeCompare', 'match', 'replace', 'search', 'slice', 'split', 'substring',
'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toUpperCase', 'trim', 'trimLeft', 'trimRight'
];
Object.keys(theme).forEach(function(prop){
addProperty(prop, function(){
return exports[theme[prop]](this);
});
if (stringPrototypeBlacklist.indexOf(prop) !== -1) {
console.log('warn: '.red + ('String.prototype' + prop).magenta + ' is probably something you don\'t want to override. Ignoring style name');
} else {
addProperty(prop, function(){
return exports[theme[prop]](this);
});
}
});
}