archive/v1/client/findByName.js
function findByNameEx(args, matches) {

		// create a variable to store the matches
		matches = matches || [];
		
		// set option defaults
		args.name       = Array.isArray(args.name) ? args.name : [args.name];
		args.recursive  = (typeof(args.recursive) === 'boolean') ? args.recursive : false;
		args.limit      = !isNaN(args.limit) ? args.limit : 1;
		args.properties = args.properties || [];
		
		// check for a starting point, if one doesnt exist begin at the datacenter level
		if (!args.parent) {
			return rootSearch(args)
			.then(function(parents) {
				return findByNameEx({
					type       : args.type,
					name       : args.name,
					properties : args.properties,
					parent     : parents,
					recursive  : args.recursive,
					limit      : args.limit
				}, matches);
			});
		}
		
		// convert the parent to an array if it is not one
		args.parent = Array.isArray(args.parent) ? args.parent : [args.parent];
		
		// perform the search
		return Promise.each(args.parent, function(parent) {
			
			// check if the match limit is met
			if (matches.length >= args.limit) {
				return;
			}
			
			// perform the search on each name
			return Promise.each(args.name, function(name) {
				
				// check if the match limit is met
				if (matches.length >= args.limit) {
					return;
				}
				
				// perform the search
				return _client.getServiceContent({
					method: 'FindChild',
					service: 'searchIndex',
					params: {
						entity: util.moRef(parent.type, parent.id),
						name: name
					}
				})
				.then(function(result) {

					var id = _.get(result, '$value');
					
					if (id) {
						matches.push(id);
					}					
				});
			})
			.then(function() {
				
				// check if the match limit is met, if not and recursive
				// attempt to descend the child objects
				if (matches.length < args.limit && args.recursive) {
					
					// get the children of the current parent
					return _client.retrieve({
						type: parent.type,
						id: parent.id,
						properties: ['childEntity']
					})
					.then(function(mo) {

						var parents = [];
						
						// get all of the child folders
						_.forEach(_.get(mo, '[0].childEntity'), function(child) {
							if (child.match(/^group-.*/)) {
								parents.push({
									type: 'Folder',
									id: child
								});
							}
						});
						
						// if there were folders, recurse
						if (parents.length > 0) {
							return findByNameEx({
								type       : args.type,
								name       : args.name,
								properties : args.properties,
								parent     : parents,
								recursive  : args.recursive,
								limit      : args.limit
							}, matches);
						}
					});
				}
			});
		})
		.then(function() {
			return matches;
		});
	}
function findByNameEx(args, matches) {

		// create a variable to store the matches
		matches = matches || [];
		
		// set option defaults
		args.name       = Array.isArray(args.name) ? args.name : [args.name];
		args.recursive  = (typeof(args.recursive) === 'boolean') ? args.recursive : false;
		args.limit      = !isNaN(args.limit) ? args.limit : 1;
		args.properties = args.properties || [];
		
		// check for a starting point, if one doesnt exist begin at the datacenter level
		if (!args.parent) {
			return rootSearch(args)
			.then(function(parents) {
				return findByNameEx({
					type       : args.type,
					name       : args.name,
					properties : args.properties,
					parent     : parents,
					recursive  : args.recursive,
					limit      : args.limit
				}, matches);
			});
		}
		
		// convert the parent to an array if it is not one
		args.parent = Array.isArray(args.parent) ? args.parent : [args.parent];
		
		// perform the search
		return Promise.each(args.parent, function(parent) {
			
			// check if the match limit is met
			if (matches.length >= args.limit) {
				return;
			}
			
			// perform the search on each name
			return Promise.each(args.name, function(name) {
				
				// check if the match limit is met
				if (matches.length >= args.limit) {
					return;
				}
				
				// perform the search
				return _client.getServiceContent({
					method: 'FindChild',
					service: 'searchIndex',
					params: {
						entity: util.moRef(parent.type, parent.id),
						name: name
					}
				})
				.then(function(result) {

					var id = _.get(result, '$value');
					
					if (id) {
						matches.push(id);
					}					
				});
			})
			.then(function() {
				
				// check if the match limit is met, if not and recursive
				// attempt to descend the child objects
				if (matches.length < args.limit && args.recursive) {
					
					// get the children of the current parent
					return _client.retrieve({
						type: parent.type,
						id: parent.id,
						properties: ['childEntity']
					})
					.then(function(mo) {

						var parents = [];
						
						// get all of the child folders
						_.forEach(_.get(mo, '[0].childEntity'), function(child) {
							if (child.match(/^group-.*/)) {
								parents.push({
									type: 'Folder',
									id: child
								});
							}
						});
						
						// if there were folders, recurse
						if (parents.length > 0) {
							return findByNameEx({
								type       : args.type,
								name       : args.name,
								properties : args.properties,
								parent     : parents,
								recursive  : args.recursive,
								limit      : args.limit
							}, matches);
						}
					});
				}
			});
		})
		.then(function() {
			return matches;
		});
	}
function findByNameEx ( args , matches ) { // create a variable to store the matches matches = matches || [ ] ; // set option defaults args . name = Array . isArray ( args . name ) ? args . name : [ args . name ] ; args . recursive = ( typeof ( args . recursive ) === 'boolean' ) ? args . recursive : false ; args . limit = ! isNaN ( args . limit ) ? args . limit : 1 ; args . properties = args . properties || [ ] ; // check for a starting point, if one doesnt exist begin at the datacenter level if ( ! args . parent ) { return rootSearch ( args ) . then ( function ( parents ) { return findByNameEx ( { type : args . type , name : args . name , properties : args . properties , parent : parents , recursive : args . recursive , limit : args . limit } , matches ) ; } ) ; } // convert the parent to an array if it is not one args . parent = Array . isArray ( args . parent ) ? args . parent : [ args . parent ] ; // perform the search return Promise . each ( args . parent , function ( parent ) { // check if the match limit is met if ( matches . length >= args . limit ) { return ; } // perform the search on each name return Promise . each ( args . name , function ( name ) { // check if the match limit is met if ( matches . length >= args . limit ) { return ; } // perform the search return _client . getServiceContent ( { method : 'FindChild' , service : 'searchIndex' , params : { entity : util . moRef ( parent . type , parent . id ) , name : name } } ) . then ( function ( result ) { var id = _ . get ( result , '$value' ) ; if ( id ) { matches . push ( id ) ; } } ) ; } ) . then ( function ( ) { // check if the match limit is met, if not and recursive // attempt to descend the child objects if ( matches . length < args . limit && args . recursive ) { // get the children of the current parent return _client . retrieve ( { type : parent . type , id : parent . id , properties : [ 'childEntity' ] } ) . then ( function ( mo ) { var parents = [ ] ; // get all of the child folders _ . forEach ( _ . get ( mo , '[0].childEntity' ) , function ( child ) { if ( child . match ( / ^group-.* / ) ) { parents . push ( { type : 'Folder' , id : child } ) ; } } ) ; // if there were folders, recurse if ( parents . length > 0 ) { return findByNameEx ( { type : args . type , name : args . name , properties : args . properties , parent : parents , recursive : args . recursive , limit : args . limit } , matches ) ; } } ) ; } } ) ; } ) . then ( function ( ) { return matches ; } ) ; }
recursive function to find matches
recursive function to find matches