getIdsOfType now returns all ids if type undefined

This convenience method is used to extract IDs from an
ActionResult object. Often, the caller is only interested in
modified items of a certain type, but sometimes it is useful
to simply get a list of all IDs in the ActionResult.

Partially-Implements: blueprint angular-registry
Change-Id: I7847c24cbc7a6c4039ce0e195083cc619491e62d
This commit is contained in:
Tyr Johanson 2016-05-25 13:55:21 -06:00
parent b0d1c88bbd
commit e17935b198
2 changed files with 14 additions and 8 deletions

View File

@ -88,18 +88,18 @@
getIdsOfType: getIdsOfType
};
// Given a list of objects (items) that each have an 'id' property,
// return a list of those id values for objects whose 'type' property
// matches the 'type' parameter.
// This is a convenience method used for extracting IDs from action
// result objects. For example, if you wanted to know the IDs of
// the deleted images (but didn't want to know about other deleted types),
// you'd use this function.
/**
* Returns an array of ids that match the given resource type (e.g. "OS::Glance::Image")
*
* @param items - items to filter
* @param type - type to filter in, or 'undefined' to match all types)
* @returns [] - array of ids that match the requested type, or an empty array
*/
function getIdsOfType(items, type) {
return items ? items.reduce(typeIdReduce, []) : [];
function typeIdReduce(accumulator, item) {
if (item.type === type) {
if (angular.isUndefined(type) || item.type === type) {
accumulator.push(item.id);
}
return accumulator;

View File

@ -41,6 +41,12 @@
{type: 'OS::Glance::Image', id: 2}];
expect(service.getIdsOfType(items, 'OS::Glance::Image')).toEqual([1, 2]);
});
it('returns all items if undefined type', function() {
var items = [{type: 'OS::Cinder::Volume', id: 1},
{type: 'OS::Glance::Image', id: 2}];
expect(service.getIdsOfType(items)).toEqual([1, 2]);
});
});
it('has getActionResult', function() {