-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Higher-order functions accepting Arrays or Objects #93
Comments
sure! what about creating a new package // collection/filter.js
define(['../lang/isArray', '../array/filter', '../object/filter'], function(isArray, arrFilter, objFilter){
function filter(target, callback, thisObj){
return isArray(target)? arrFilter(target, callback, thisObj) : objFilter(target, callback, thisObj);
}
return filter;
}); I think we should keep it as backwards compatible as possible (diff projects rely on amd-utils) and there is a perf hit by calling This process could be automated by the build tool or by a macro. - yesterday I added the option to create new modules through the build script What do you think? |
Uhm, maybe we could abstract it into a separate module if the logic is always the same (which should be): // collection/make_.js
define(['../lang/isArray'], function(isArray){
function makeCollectionMethod(arrMethod, objMethod) {
return function(){
var args = Array.prototype.slice.call(arguments);
return isArray(args[0])? arrMethod.apply(null, args) : objMethod.apply(null, args);
};
}
return makeCollectionMethod;
});
// collection/filter.js
define(['./make_', '../array/filter', '../object/filter'], function(make, arrFilter, objFilter){
return make(arrFilter, objFilter);
}); Keep it DRY. |
Yeah, I'm on board with keeping it a separate module. The performance hit should be expected, I think, since by using it you're essentially declaring "I don't know what you're passing me!". Your comment about using a generic |
Yes, we would need to add the missing methods to the object package, I've been adding new stuff as needed but the goal is to have all the important methods from underscore.js/lodash + many more. Maybe we should start with the most common methods like I will update the build tool to generate the collection packages automatically, thinking in something like |
I started a fork with a "collection" branch for this: https://github.com/zship/amd-utils/tree/collection . Added the missing object methods. Also added a |
I added the command to automatically create the collection module To run the tests locally open the Objects have some weird behavior on IE if they contain one of these properties so it's better to use Tests and documentation takes a lot of time. Luckily these methods have a lot of things in common with the array ones so we can just copy examples/specs and add links to the original methods, see: http://millermedeiros.github.com/amd-utils/collection.html Thanks for the feedback and for your work. Keep it coming. |
Sounds great. Thanks for your fast responses to this feature request. I'll be at work until later tonight and then I'll have time to work on tests and documentation. Of course, if you get the inclination to work on that during the day, I won't complain! Otherwise, so we're not duplicating effort, I'll assume that's on me. |
Collection methods like those found in Lo-Dash/Underscore treat array-like objects as arrays too. They use a basic check of This allows for things like |
Also they iterate over objects own properties, so using your |
@jdalton makes a lot of sense. I will create proper specs and update the code accordingly. Thanks. |
…gets and array-like objects. see #93
Something's occurred to me while I've been working on the tests today: some current object methods in amd-utils (let's take There are two avenues I see here:
|
I agree with approach #2. I also added an option on |
"collection" package was released on v0.7.0 |
I'd like to see the higher-order functions under "array/" be available for Arrays or Objects, similar to Underscore's "Collection" functions. I think sometimes it's convenient to accept Array | Object in method parameters. Async.js is a good example of a library where this convention is useful. I'm currently writing this (re-using amd-utils modules where appropriate) and calling it "collection/". Would you want to pull it?
The text was updated successfully, but these errors were encountered: