Just wondering if anyone knows of a simple and elegant way to do this: I have a method that optionally accepts options that will overide default options using Object.extend. What I would like to do is simply prune out any properties from the new options object that are undefined after this step. The method code looks something like: someMethod: function(opts) { var options = Object.extend({ someProp1: this.defaults.someProp1, someProp2: this.defaults.someProp2, someProp3: this.defaults.someProp3, someProp4: this.defaults.someProp4, someProp5: this.defaults.someProp5 }, opts || {}); // prune undefined poperties before moving on // do something with options here... } for pruning, ive got to this point: var str = Object.keys(options).findAll( function(p) { return options[p] != undefined; }).map ( function(p) { return p + '': \'''' + options[p] + ''\''''; }); which returns a string representing a hash... which i need to make into a new object. I would hope there is a more elegant approach to this than that... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Hey there, sliver a écrit :> var options = Object.extend({ > someProp1: this.defaults.someProp1, > someProp2: this.defaults.someProp2, > someProp3: this.defaults.someProp3, > someProp4: this.defaults.someProp4, > someProp5: this.defaults.someProp5 > }, opts || {});Just how could there be undefined properties? That would mean that either you have undefined defaults (which is nonsensical), or that the user of your code intentionally creates undefined-valued properties in the options object it passes (which is malicious). Also, your code could go like this: // defaults might better be in the namespace, not this, btw) var options = Object.clone(this.defaults); Object.extend(options, opts || {}); -- Christophe Porteneuve aka TDD tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Either I''m totally missing something, or you are :-) If a property is undefined... it''s undefined. No need to do this "pruning" you''re talking about as the properties you''re trying to prune (if truly undefined) will not exist in the object already (again, they are undefined). Now, if you''re looking to remove null properties, that''s completely different. In that case I think maybe proto''s hash operations? Isn''t there an equivalent to .reject() for Hashes? But I think you have your terms confused. Again, unless I''m completely missing this boat. On 3/22/07, sliver <sliver2502-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Just wondering if anyone knows of a simple and elegant way to do this: > > I have a method that optionally accepts options that will overide > default options using Object.extend. What I would like to do is simply > prune out any properties from the new options object that are > undefined after this step. > > The method code looks something like: > > someMethod: function(opts) { > var options = Object.extend({ > someProp1: this.defaults.someProp1, > someProp2: this.defaults.someProp2, > someProp3: this.defaults.someProp3, > someProp4: this.defaults.someProp4, > someProp5: this.defaults.someProp5 > }, opts || {}); > > // prune undefined poperties before moving on > > // do something with options here... > } > > for pruning, ive got to this point: > > var str = Object.keys(options).findAll( function(p) { > return options[p] != undefined; > }).map ( function(p) { > return p + '': \'''' + options[p] + ''\''''; > }); > > which returns a string representing a hash... which i need to make > into a new object. I would hope there is a more elegant approach to > this than that... > > > > >-- Ryan Gahl Application Development Consultant Athena Group, Inc. Inquire: 1-920-955-1457 Blog: http://www.someElement.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the quick response! This method is basically a small wrapper for calling Prototypes setStyle() method, which if you send an undefined property to it, FireFox''s Javascript Console will Error with a parse error for that property. The last line in the my method call should be obj.setStyle(options); where obj is the obj i am modifying of course. Unfortunately, I can''t clone the whole "defaults" object since their are more default properties than I need for this method. Ive tried using the $H().findAll but, it unfortunately pulls all of Prototypes hash values into the new object then. Does this make sense? On Mar 22, 10:30 am, Christophe Porteneuve <t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote:> Hey there, > > sliver a écrit : > > > var options = Object.extend({ > > someProp1: this.defaults.someProp1, > > someProp2: this.defaults.someProp2, > > someProp3: this.defaults.someProp3, > > someProp4: this.defaults.someProp4, > > someProp5: this.defaults.someProp5 > > }, opts || {}); > > Just how could there be undefined properties? That would mean that > either you have undefined defaults (which is nonsensical), or that the > user of your code intentionally creates undefined-valued properties in > the options object it passes (which is malicious). > > Also, your code could go like this: > > // defaults might better be in the namespace, not this, btw) > var options = Object.clone(this.defaults); > Object.extend(options, opts || {}); > > -- > Christophe Porteneuve aka TDD > t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---