Repository: yagoferrer/map-tools (js)

Issue:
Allow markerFilter() to pass first argument as string (dimension) to fetch all.

Pull request:
Allow markerFilter() to pass first argument as string (dimension) to fet...
...ch all. Fixes #190

Patch:
diff --git a/README.md b/README.md
index 8689331..e1861b4 100644
--- a/README.md
+++ b/README.md
@@ -228,7 +228,8 @@ The default **event** is `click` but you can change it with the `event` property
 ```
 Now you can use the power of Crossfilter to update Markers. In this example it finds the city with larger population, Madrid, and makes the marker to bounce.
 ```javascript
-map.updateMarker(map.markers.filter.population.top(1), {move: 'bounce'});
+var marker = map.filterMarker('population', {limit: 1});
+map.updateMarker(marker, {move: 'bounce'});
 ```
 
 You can also pass custom Crossfilter dimensions to the `filters` option:
@@ -292,7 +293,8 @@ You can also update the `style` of a group of features by a mapping function.
 You can use a Crossfilter result to update features. In this example it finds the State named: 'Texas' and updates the background color.
 
 ```javascript
-map.updateFeature(map.json.filter.NAME.filter('Colorado').top(1), {style: {fillColor:'black'}})
+var feature = map.filterFeature({NAME:'Colorado'}, {limit: 1});
+map.updateFeature(feature, {style: {fillColor:'black'}})
 ```
 
 Add more [Style Options](https://developers.google.com/maps/documentation/javascript/reference#Data.StyleOptions)
diff --git a/examples/code.snippet/crossfilter.snippet.html b/examples/code.snippet/crossfilter.snippet.html
index 1b874cd..70837ac 100644
--- a/examples/code.snippet/crossfilter.snippet.html
+++ b/examples/code.snippet/crossfilter.snippet.html
@@ -2,7 +2,7 @@
 <pre class="language-javascript">
 <code class="language-javascript">
   // Click event action
-  var filter = map.markers.filter.population.top(1);
+  var filter = map.filterMarker('population', {limit: 1});
   map.updateMarker(filter, {move: 'bounce'});
 
   // Adding markers
diff --git a/examples/crossfilter.html b/examples/crossfilter.html
index 7f311c9..79f89ec 100644
--- a/examples/crossfilter.html
+++ b/examples/crossfilter.html
@@ -28,7 +28,8 @@
 		    position: 'right center',
 		    events: {
 			    '.top click': function (e) {
-				    map.updateMarker(map.markers.filter.population.top(1), {move: 'bounce'});
+            var marker = map.filterMarker('population', {limit: 1});
+				    map.updateMarker(marker, {move: 'bounce'});
 				    e.preventDefault();
 			    }
 		    }
diff --git a/lib/map-tools/filter.js b/lib/map-tools/filter.js
index 5cbc939..51aaa71 100644
--- a/lib/map-tools/filter.js
+++ b/lib/map-tools/filter.js
@@ -7,10 +7,14 @@ module.exports = function(global, that, type) {
   };
 
   function filter(args, options) {
+    var dimension, order, limit, query, result;
 
-    var order, limit;
-    var dimension = Object.keys(args)[0];
-    var query = args[dimension];
+    if (typeof args === 'string') {
+      dimension = args;
+    } else {
+      dimension = Object.keys(args)[0];
+      query = args[dimension];
+    }
 
     if (that[type].filter[dimension]) {
 
@@ -20,7 +24,12 @@ module.exports = function(global, that, type) {
 
       limit = (options && options.limit) ? options.limit : Infinity;
 
-      var result = that[type].filter[dimension].filter(query)[order](limit);
+      if (query) {
+        result = that[type].filter[dimension].filter(query)[order](limit);
+      } else {
+        result = that[type].filter[dimension][order](limit);
+      }
+
 
       if (limit == 1) {
         return result[0];


Test patch:
diff --git a/test/unit/filter.Spec.js b/test/unit/filter.Spec.js
index 43320e9..9d402e6 100644
--- a/test/unit/filter.Spec.js
+++ b/test/unit/filter.Spec.js
@@ -18,11 +18,22 @@ describe('Given the filterFeature Module', function () {
     filterFeature = require('map-tools/filter')({}, that, 'json');
   });
 
-  it('should filter exact values', function () {
+  it('should filter exact values when passing an object', function () {
     var result = filterFeature({NAME: 'Peter'});
     expect(result).to.eql([data[2], data[1]]);
   });
 
+  it('should return all elements order by the dimension, when passing the dimension name', function () {
+    var result = filterFeature('NAME');
+    expect(result).to.eql([data[2], data[1], data[3], data[0]]);
+  });
+
+
+  it('should return all elements order by the dimension, when passing the dimension name and order DESC', function () {
+    var result = filterFeature('NAME', {order: 'DESC'});
+    expect(result).to.eql([data[0], data[3], data[1], data[2]]);
+  });
+
   it('should limit the results when using limit', function () {
     var result = filterFeature({NAME: 'Peter'}, {limit: 1});
     expect(result).to.eql(data[2]);