Skip to content

Commit 4bdc1f5

Browse files
authored
Merge pull request #2 from instructure/commutative-sort
reimplement noncommutative sort
2 parents 6ebdcba + 36a569a commit 4bdc1f5

File tree

4 files changed

+21
-42
lines changed

4 files changed

+21
-42
lines changed

.travis.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
before_script: 'npm install -g grunt-cli'
22
language: node_js
33
node_js:
4-
- "6"
5-
- "5"
6-
- "4"
4+
- "10"
5+
- "12"

dist/lib/pre_processor.js

+8-19
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,6 @@ function concatNode(string, tempMap) {
9797
return new SexprNode(parts);
9898
}
9999

100-
function sortBy(array, fn) {
101-
return array.map(function (item, i) {
102-
return [fn(item), i, item];
103-
}).sort(function (left, right) {
104-
var leftSort = left[0],
105-
rightSort = right[0];
106-
if (leftSort !== rightSort) {
107-
if (leftSort > rightSort) return 1;
108-
if (leftSort < rightSort) return 0;
109-
}
110-
return left[1] - right[1];
111-
}).map(function (obj) {
112-
return obj[2];
113-
});
114-
}
115-
116100
var PreProcessor = {
117101
process: function process(ast) {
118102
var statements = ast.statements,
@@ -180,9 +164,14 @@ var PreProcessor = {
180164
* https://github.com/wycats/handlebars.js/issues/767
181165
*/
182166
applySubExpressionHack: function applySubExpressionHack(node) {
183-
node.hash.pairs = sortBy(node.hash.pairs, function (pair) {
184-
var type = pair[1].type;
185-
return type === "sexpr" ? 0 : 1;
167+
node.hash.pairs = node.hash.pairs.sort(function (a, b) {
168+
if (a[1].type === b[1].type) {
169+
return 0;
170+
} else if (a[1].type === 'sexpr') {
171+
return -1;
172+
} else if (b[1].type === 'sexpr') {
173+
return 1;
174+
}
186175
});
187176
var pairs = node.hash.pairs;
188177
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");

lib/pre_processor.js

+10-19
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,6 @@ function concatNode(string, tempMap) {
8888
return new SexprNode(parts);
8989
}
9090

91-
function sortBy(array, fn) {
92-
return array.map(function(item, i) {
93-
return [fn(item), i, item];
94-
}).sort(function(left, right) {
95-
var leftSort = left[0]
96-
, rightSort = right[0];
97-
if (leftSort !== rightSort) {
98-
if (leftSort > rightSort) return 1;
99-
if (leftSort < rightSort) return 0;
100-
}
101-
return left[1] - right[1];
102-
}).map(function(obj) {
103-
return obj[2];
104-
});
105-
}
106-
10791
var PreProcessor = {
10892
process: function(ast) {
10993
var statements = ast.statements
@@ -176,9 +160,16 @@ var PreProcessor = {
176160
* https://github.com/wycats/handlebars.js/issues/767
177161
*/
178162
applySubExpressionHack: function(node) {
179-
node.hash.pairs = sortBy(node.hash.pairs, function(pair) {
180-
var type = pair[1].type;
181-
return type === "sexpr" ? 0 : 1;
163+
node.hash.pairs = node.hash.pairs.sort(function(a,b) {
164+
if (a[1].type === b[1].type) {
165+
return 0
166+
}
167+
else if (a[1].type === 'sexpr') {
168+
return -1;
169+
}
170+
else if (b[1].type === 'sexpr') {
171+
return 1;
172+
}
182173
});
183174
var pairs = node.hash.pairs;
184175
if (pairs.length > 1 && pairs[1][1].type === "sexpr")

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"grunt-contrib-clean": "~1.0.0",
2626
"grunt-contrib-copy": "~1.0.0",
2727
"gruntify-eslint": "~3.1.0",
28-
"handlebars": ">=1.3 <3",
28+
"handlebars": "~1.3.0",
2929
"i18nliner": "~0.2.0",
3030
"jsdom": "~8.5.0",
3131
"matchdep": "~1.0.1",

0 commit comments

Comments
 (0)