Skip to content

Commit

Permalink
Compare by number of dependents and alphabetically
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarchena committed May 25, 2015
1 parent 53724de commit e6f844f
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/dependency.js
Expand Up @@ -65,16 +65,43 @@ Dependency.prototype = {
return this.name == dependency.name;
},

/**
* This method is used for sorting.
* This dependency instance is bigger than another:
* - if it is dependent of the given one,
* - if the passed dependency is deeper in the tree
* - comparing their names alphabetically
*
* @param {Dependency} dependency The dependency to compare with
* @return {Integer} This method will return 1 if the instance is bigger than the given one,
* '0' if they are equivalent and -1 in any other case.
*/
compare: function compare(dependency) {
if (this.hasDependency(dependency, true)) {
return 1;
}
if (dependency.hasDependency(this, true)) {
return -1;
}
if (this.dependents.length < dependency.dependents.length) {
return 1;
}
if (this.dependents.length > dependency.dependents.length) {
return -1;
}
if (this.name > dependency.name) {
return 1;
}
if (this.name < dependency.name) {
return -1;
}
return 0;
},

/**
* This method returns the complete list dependencies. Not only the direct ones.
* @return {Array} Full dependency list of this instance
*/
getDependencyArray: function toArray() {
var arr = [];
this.dependencies.forEach(function(d){
Expand Down

0 comments on commit e6f844f

Please sign in to comment.