Skip to content

Commit

Permalink
Use a stable sort to further ensure largest common subset of nodes ar…
Browse files Browse the repository at this point in the history
…e emitted
  • Loading branch information
danielstjules committed Mar 23, 2017
1 parent 27a58bd commit e9b0e2f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var parse = require('./parser').parse;
var Match = require('./match');
var nodeUtils = require('./nodeUtils');
var crypto = require('crypto');
var stable = require('stable');

class Inspector extends EventEmitter {
/**
Expand Down Expand Up @@ -167,9 +168,14 @@ class Inspector extends EventEmitter {
_analyze() {
var key, match, i, groups, nodeArrays;

var sortedKeys = Object.keys(this._map)
.filter(key => this._map[key].length >= this._minInstances)
.sort((a, b) => this._map[b].length - this._map[a].length);
var keys = Object.keys(this._map)
.filter(key => this._map[key].length >= this._minInstances);

// Need to use a stable sort to ensure parent nodes are traversed
// before children when lengths are equal
var sortedKeys = stable(keys, (a, b) => {
return this._map[b].length - this._map[a].length;
});

for (key of sortedKeys) {
if (!this._map[key] || this._map[key].length < this._minInstances) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"chalk": "*",
"commander": "*",
"node-filepaths": "0.1.0",
"stable": "0.1.6",
"strip-indent": "^1.0.0",
"strip-json-comments": "1.0.2"
},
Expand Down

0 comments on commit e9b0e2f

Please sign in to comment.