esamattis / esamattis/underscore.string
Allow conflicting functions via type detection
- Dominant language
- JavaScript
- Stars
- 3.4k
- Forks
- 367
- PR merge metrics
- No merged PRs in 30d
Description
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!
Contributor guide
Assessment
This issue has not been assessed yet.