Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

findSubDevice utility, tests

  • Loading branch information...
commit e31fbcf69655791ecec080199e2e947658ed524b 1 parent a300b92
@thatguydan thatguydan authored
Showing with 67 additions and 8 deletions.
  1. +2 −1  index.js
  2. +27 −6 lib/utils.js
  3. +38 −1 test/utils.js
View
3  index.js
@@ -310,6 +310,7 @@ exports.app = function(opts) {
else if (b.result===1) cb(null, utils.filter(filter,b.data))
else cb({statusCode:b.id||200,error:b.error})
});
- }
+ },
+ utils:utils
}
}
View
33 lib/utils.js
@@ -1,14 +1,14 @@
-exports.filter = function(filter,set) {
+var filter = exports.filter = function(filterObj,set) {
// If an empty object is passed in either argument, return the set
- if (Object.keys(filter).length===0 || Object.keys(set).length===0) return set;
+ if (Object.keys(filterObj).length===0 || Object.keys(set).length===0) return set;
var out={};
for (var i in set) {
if (set.hasOwnProperty(i)) {
var filterMatched = 0;
- var filterLength = Object.keys(filter).length;
- for (var f in filter) {
- if (filter.hasOwnProperty(f)) {
- if (set[i].hasOwnProperty(f) && set[i][f] == filter[f])
+ var filterLength = Object.keys(filterObj).length;
+ for (var f in filterObj) {
+ if (filterObj.hasOwnProperty(f)) {
+ if (set[i].hasOwnProperty(f) && set[i][f] == filterObj[f])
filterMatched++;
}
}
@@ -16,4 +16,25 @@ exports.filter = function(filter,set) {
}
}
return out;
+};
+
+exports.findSubDevice = function(filterObj,set) {
+ var out={};
+ for (var i in set) {
+ if (set[i].hasOwnProperty('subDevices')) {
+ var subDevices = set.subDevices;
+ var filterMatched = 0;
+ var filterLength = Object.keys(filterObj).length;
+ var returned = filter(filterObj,set[i].subDevices);
+ if (Object.keys(returned).length>0) {
+ for (var r in returned) {
+ if (returned.hasOwnProperty(r)) {
+ out[r] = returned[r];
+ out[r].guid = i;
+ }
+ }
+ }
+ }
+ }
+ return out;
};
View
39 test/utils.js
@@ -13,6 +13,23 @@ describe('utils',function() {
'device_type' : 'pop_reference',
'shortName' : 'spirit of jazz',
'vid' : 0
+ },
+ 'RAGEAGAINSTTHESEWINGMACHINE_0_0_2' : {
+ 'device_type' : 'pop_reference',
+ 'shortName' : 'z sides and demos',
+ 'vid' : 1,
+ 'subDevices': {
+ 'a' : {
+ 'shortName':'86 Tram',
+ 'type' : 'transit',
+ 'data' : 'so hungover'
+ },
+ 'b' : {
+ 'shortName' : '870',
+ 'type' : 'transit',
+ 'data' : 'takes forever'
+ }
+ }
}
};
describe('filter',function() {
@@ -44,7 +61,7 @@ describe('utils',function() {
Object
.keys(utils.filter({'device_type':'pop_reference'},set))
.should
- .eql(['INEEDABOUTTHREEFIFTY_0_0_1','MYHATSONFIRE_0_0_2']);
+ .eql(['INEEDABOUTTHREEFIFTY_0_0_1','MYHATSONFIRE_0_0_2','RAGEAGAINSTTHESEWINGMACHINE_0_0_2']);
});
it('should return multiple elements with 2 filters',function() {
Object
@@ -53,4 +70,24 @@ describe('utils',function() {
.eql(['INEEDABOUTTHREEFIFTY_0_0_1','MYHATSONFIRE_0_0_2']);
});
});
+ describe('findSubDevice',function() {
+ it('should return the correct sub device with 1 filter',function() {
+ Object
+ .keys(utils.findSubDevice({shortName:'86 Tram'},set))
+ .should
+ .eql(['a']);
+ });
+ it('should return the correct sub device with 2 filters',function() {
+ Object
+ .keys(utils.findSubDevice({shortName:'86 Tram',type:'transit'},set))
+ .should
+ .eql(['a']);
+ });
+ it('should return multiple sub devices with 1 filter',function() {
+ Object
+ .keys(utils.findSubDevice({type:'transit'},set))
+ .should
+ .eql(['a','b']);
+ });
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.