Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reimplement noncommutative sort #2

Merged
merged 1 commit into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
before_script: 'npm install -g grunt-cli'
language: node_js
node_js:
- "6"
- "5"
- "4"
- "10"
- "12"
27 changes: 8 additions & 19 deletions dist/lib/pre_processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,6 @@ function concatNode(string, tempMap) {
return new SexprNode(parts);
}

function sortBy(array, fn) {
return array.map(function (item, i) {
return [fn(item), i, item];
}).sort(function (left, right) {
var leftSort = left[0],
rightSort = right[0];
if (leftSort !== rightSort) {
if (leftSort > rightSort) return 1;
if (leftSort < rightSort) return 0;
}
return left[1] - right[1];
}).map(function (obj) {
return obj[2];
});
}

var PreProcessor = {
process: function process(ast) {
var statements = ast.statements,
Expand Down Expand Up @@ -180,9 +164,14 @@ var PreProcessor = {
* https://github.com/wycats/handlebars.js/issues/767
*/
applySubExpressionHack: function applySubExpressionHack(node) {
node.hash.pairs = sortBy(node.hash.pairs, function (pair) {
var type = pair[1].type;
return type === "sexpr" ? 0 : 1;
node.hash.pairs = node.hash.pairs.sort(function (a, b) {
if (a[1].type === b[1].type) {
return 0;
} else if (a[1].type === 'sexpr') {
return -1;
} else if (b[1].type === 'sexpr') {
return 1;
}
});
var pairs = node.hash.pairs;
if (pairs.length > 1 && pairs[1][1].type === "sexpr") throw new _errors2.default.MultipleSubExpressionsError(node.firstLine, "Handlebars 1.3 doesn't support multiple sub-expressions in the options hash");
Expand Down
29 changes: 10 additions & 19 deletions lib/pre_processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,6 @@ function concatNode(string, tempMap) {
return new SexprNode(parts);
}

function sortBy(array, fn) {
return array.map(function(item, i) {
return [fn(item), i, item];
}).sort(function(left, right) {
var leftSort = left[0]
, rightSort = right[0];
if (leftSort !== rightSort) {
if (leftSort > rightSort) return 1;
if (leftSort < rightSort) return 0;
}
return left[1] - right[1];
}).map(function(obj) {
return obj[2];
});
}

var PreProcessor = {
process: function(ast) {
var statements = ast.statements
Expand Down Expand Up @@ -176,9 +160,16 @@ var PreProcessor = {
* https://github.com/wycats/handlebars.js/issues/767
*/
applySubExpressionHack: function(node) {
node.hash.pairs = sortBy(node.hash.pairs, function(pair) {
var type = pair[1].type;
return type === "sexpr" ? 0 : 1;
node.hash.pairs = node.hash.pairs.sort(function(a,b) {
if (a[1].type === b[1].type) {
return 0
}
else if (a[1].type === 'sexpr') {
return -1;
}
else if (b[1].type === 'sexpr') {
return 1;
}
});
var pairs = node.hash.pairs;
if (pairs.length > 1 && pairs[1][1].type === "sexpr")
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"grunt-contrib-clean": "~1.0.0",
"grunt-contrib-copy": "~1.0.0",
"gruntify-eslint": "~3.1.0",
"handlebars": ">=1.3 <3",
"handlebars": "~1.3.0",
"i18nliner": "~0.2.0",
"jsdom": "~8.5.0",
"matchdep": "~1.0.1",
Expand Down