Skip to content

Commit

Permalink
Add support for "or" connective
Browse files Browse the repository at this point in the history
Closes #23.
  • Loading branch information
bebraw committed Mar 19, 2014
1 parent 4a50907 commit 73ef90e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,9 @@ Get hosted files for a selected version
http://api.jsdelivr.com/v1/jsdelivr/libraries/jquery/2.0.3
```

Get libraries matching to any part of query (defaults to `and`)
```
http://api.jsdelivr.com/v1/jsdelivr/libraries?name=jquery&zip=anything*&op=or
```

Built with [rest-sugar](https://github.com/bebraw/rest-sugar)
36 changes: 26 additions & 10 deletions lib/get_all.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,41 @@ module.exports = function(model, query, cb) {
var fields = query.fields;
var limit = query.limit;
var offset = query.offset || 0;
var op = query.op;

delete query.fields;
delete query.limit;
delete query.offset;
delete query.op;

var zq = zip(query);
var ret = model._data;

if(fp.count(query)) {
ret = ret.filter(function(o) {
return zq.map(function(p) {
var a = is.string(o[p[0]])? o[p[0]].toLowerCase(): '';
var b = is.string(p[1])? p[1].toLowerCase(): '';

return minimatch(a, b, {
matchBase: true
});
}).filter(fp.id).length === zq.length;
});
if(op === 'or') {
ret = ret.filter(function(o) {
return zq.map(function(p) {
var a = is.string(o[p[0]])? o[p[0]].toLowerCase(): '';
var b = is.string(p[1])? p[1].toLowerCase(): '';

return minimatch(a, b, {
matchBase: true
});
}).filter(fp.id).length > 0;
});
}
else {
ret = ret.filter(function(o) {
return zq.map(function(p) {
var a = is.string(o[p[0]])? o[p[0]].toLowerCase(): '';
var b = is.string(p[1])? p[1].toLowerCase(): '';

return minimatch(a, b, {
matchBase: true
});
}).filter(fp.id).length === zq.length;
});
}
}

if(fields) {
Expand Down

0 comments on commit 73ef90e

Please sign in to comment.