julesfern / spahql

A query language for Javascript data. Extracted from Spah.
MIT License
325 stars 20 forks source link

Implement eager set #23

Closed Mihailoff closed 10 years ago

Mihailoff commented 10 years ago
collection = data.select('/item/*');
// i expect that every item will have this field, but only first one is affected
collection.set('foo', 'bar'); 
// as feature request, explicitly set all items
collection.setAll('foo', 'bar'); 

Now i have to process paths in loop to modify every item.

julesfern commented 10 years ago

Hey @Mihailoff!

setAll is already present in SpahQL. Here's the source from src/SpahQL.js:

/**
  * SpahQL#setAll(key, value) -> SpahQL
  * - key (String, Number): The key to set on this result
  * - value: The value to set for the given key
  * SpahQL#setAll(dictionary) -> SpahQL
  * - dictionary (Object): A key/value hash containing multiple keys to be set.
  *
  * Just like #set, only it attempts to set the given key(s) on all items in this set:
  *
  * db.select("//foo").set("a", "a-value") // Attempt to set "a" on all "foo" objects
  *
  * Just like #set, returns self.
  **/
  "setAll": function(keyOrDict, value) {
      for(var i=0; i<this.length; i++) this.set(keyOrDict, value, this[i]);
      return this;
    },