Skip to content

Commit

Permalink
Merge pull request #22 from Budibase/move-to-non-mutating-sort-reverse-2
Browse files Browse the repository at this point in the history
Move to using non-mutating variants of sort and reverse.
  • Loading branch information
shogunpurple authored Oct 4, 2024
2 parents 3f11685 + ebe5cb6 commit 123a41b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
18 changes: 8 additions & 10 deletions lib/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,11 @@ helpers.pluck = function(array, prop) {
* @api public
* @example {{reverse [1, 2, 3]}} -> [3, 2, 1]
*/

helpers.reverse = function(array) {
if (util.isUndefined(array)) return '';
array = util.result(array);
if (Array.isArray(array)) {
array.reverse();
return array;
return [...array].reverse();
}
if (array && typeof array === 'string') {
return array.split('').reverse().join('');
Expand Down Expand Up @@ -607,9 +605,9 @@ helpers.sort = function(array, options) {
array = util.result(array);
if (Array.isArray(array)) {
if (getValue(options, 'hash.reverse')) {
return array.sort().reverse();
return [...array].sort().reverse();
}
return array.sort();
return [...array].sort();
}
return '';
};
Expand Down Expand Up @@ -641,14 +639,14 @@ helpers.sortBy = function(array, prop, options) {
args.pop();

if (!util.isString(prop) && typeof prop !== 'function') {
return array.sort();
return [...array].sort();
}

if (typeof prop === 'function') {
return array.sort(prop);
return [...array].sort(prop);
}

return array.sort((a, b) => (a[prop] > b[prop] ? 1 : -1));
return [...array].sort((a, b) => (a[prop] > b[prop] ? 1 : -1));
}
return '';
};
Expand Down Expand Up @@ -879,7 +877,7 @@ helpers.withSort = function(array, prop, options) {
if (util.isUndefined(prop)) {
options = prop;

array = array.sort();
array = [...array].sort();
if (getValue(options, 'hash.reverse')) {
array = array.reverse();
}
Expand All @@ -890,7 +888,7 @@ helpers.withSort = function(array, prop, options) {
return result;
}

array.sort(function(a, b) {
array = [...array].sort(function(a, b) {
a = getValue(a, prop);
b = getValue(b, prop);
return a > b ? 1 : a < b ? -1 : 0;
Expand Down
12 changes: 12 additions & 0 deletions test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ describe('array', function() {
assert.equal(res, 'a,b,c');
});

it('should sort the items in a frozen array', function() {
var fn = hbs.compile('{{sort array}}');
var res = fn({array: Object.freeze(['c', 'a', 'b'])});
assert.equal(res, 'a,b,c');
});

it('should return all items in an array sorted in lexicographical order', function() {
var fn = hbs.compile('{{sort array}}');
assert.equal(fn(context), 'a,b,c,d,e,f,g,h');
Expand All @@ -348,6 +354,12 @@ describe('array', function() {
var res = fn({array: ['c', 'a', 'b']});
assert.equal(res, 'c,b,a');
});

it('should sort the items in a frozen array in reverse order:', function() {
var fn = hbs.compile('{{sort array reverse="true"}}');
var res = fn({array: Object.freeze(['c', 'a', 'b'])});
assert.equal(res, 'c,b,a');
});
});

describe('sortBy', function() {
Expand Down

0 comments on commit 123a41b

Please sign in to comment.