epeli/underscore.string — Issue #62: Allow conflicting functions via type detection
Author: ryanbutterfield
Created: 2011-11-12 04:38:56+00:00
State: open

Maybe you could suggest (or provide a helper function) that conflicting functions could override the underscore equivalents via wrappers that detect whether the argument passed is a String or other Object.

This would avoid potential bugs when someone uses the wrong function for the wrong type.

This is what I have done in my project and it seems to work well:

```javascript
_.mixin(_.str.exports());

// functions that conflict with _ and _.prototype
_.mixin(_.reduce(['include', 'contains'], function(memo, f) {
var str = _.str[f], und = _[f];
memo[f] = function(obj) {
return (_.isString(obj) ? str : und).apply(this, arguments);
};
return memo;
}, {}));

// functions that just conflict with _.prototype
_.each(['reverse'], function(f) {
var wstr, str = _.str[f], wund = _.prototype[f];
_.mixin({__tmp: str}); // get access to addToWrapper
wstr = _.prototype.__tmp;
_[f] = str;
_.prototype[f] = function() {
return (_.isString(this._wrapped) ? wstr : wund).apply(this, arguments);
};
});
```

Thanks for a great library!