Skip to content

Commit

Permalink
Merge pull request #419 from jpbackman/fix-variable-order
Browse files Browse the repository at this point in the history
Use lodash to sort parsed variables
  • Loading branch information
hannu committed Jan 19, 2015
2 parents 5548cf1 + c79b2ed commit 0a231f0
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions lib/modules/variable-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
var gonzales = require('gonzales-pe'),
gonzo = require('gonzales-ast'),
path = require('path'),
Q = require('q');
Q = require('q'),
_ = require('lodash');

function astToSrc(ast, syntax) {
return gonzales.astToSrc({
Expand Down Expand Up @@ -108,40 +109,23 @@ function findVariables(string, syntax) {
return out;
}

function byFileName(a, b) {
if (a.file < b.file) {
return -1;
}
if (a.file > b.file) {
return 1;
}
return 0;
}

function parseVariableDeclarationsFromFiles(files) {
var variables = [],
filePromises = [];

Object.keys(files).forEach(function(filePath) {
var filePromises = Object.keys(files).map(function(filePath) {
var contents = files[filePath],
syntax = path.extname(filePath).substring(1);

filePromises.push(Q.promise(function(resolve) {
return Q.promise(function(resolve) {
var fileVariables = parseVariableDeclarations(contents, syntax);

// Map correct file name to every variable
fileVariables.forEach(function(variable) {
variable.file = filePath;
});

variables = variables.concat(fileVariables);
resolve();
}));
resolve(fileVariables);
});
});

return Q.all(filePromises).then(function() {
variables.sort(byFileName);
return variables;
return _.chain(arguments).flatten().sortBy('file').value();
});
}

Expand Down

0 comments on commit 0a231f0

Please sign in to comment.