Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
fix(sortBy): compare whole prefix instead of first character (#702)
Browse files Browse the repository at this point in the history
* fix(sortBy): compare whole prefix instead of first character

This was noticed in #690 (comment) by @samouss, so I took the time to rewrite this function a little bit to be more clear (IMO)

* test(formatSort): add UT

* refactor: change conditions to lower nesting

* clearer flow (slightly slower, but clearer)
  • Loading branch information
Haroenv authored May 10, 2019
1 parent c78bd0f commit 93298e0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/functions/formatSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,32 @@ var find = require('./find');
/**
* Transform sort format from user friendly notation to lodash format
* @param {string[]} sortBy array of predicate of the form "attribute:order"
* @param {string[]} [defaults] array of predicate of the form "attribute:order"
* @return {array.<string[]>} array containing 2 elements : attributes, orders
*/
module.exports = function formatSort(sortBy, defaults) {
var defaultInstructions = (defaults || []).map(function(sort) {
return sort.split(':');
});

return sortBy.reduce(
function preparePredicate(out, sortInstruction) {
var sortInstructions = sortInstruction.split(':');
if (defaults && sortInstructions.length === 1) {
var similarDefault = find(defaults, function(predicate) {
return predicate[0] === sortInstruction[0];
});
if (similarDefault) {
sortInstructions = similarDefault.split(':');
}
function preparePredicate(out, sort) {
var sortInstruction = sort.split(':');

var matchingDefault = find(defaultInstructions, function(
defaultInstruction
) {
return defaultInstruction[0] === sortInstruction[0];
});

if (sortInstruction.length > 1 || !matchingDefault) {
out[0].push(sortInstruction[0]);
out[1].push(sortInstruction[1]);
return out;
}
out[0].push(sortInstructions[0]);
out[1].push(sortInstructions[1]);

out[0].push(matchingDefault[0]);
out[1].push(matchingDefault[1]);
return out;
},
[[], []]
Expand Down
41 changes: 41 additions & 0 deletions test/spec/functions/formatSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

var formatSort = require('../../../src/functions/formatSort');

it('splits into attribute & direction', function() {
expect(formatSort(['isRefined:desc', 'isNotRefined:desc'])).toEqual([
['isRefined', 'isNotRefined'],
['desc', 'desc']
]);
});

it('leaves direction empty if no direction was given', function() {
expect(formatSort(['isRefined:desc', 'isNotRefined'])).toEqual([
['isRefined', 'isNotRefined'],
['desc', undefined]
]);
});

it('takes from defaults if no direction was given', function() {
expect(
formatSort(
['isRefined:desc', 'isNotRefined'],
['books:asc', 'isRefined:desc', 'isNotRefined:asc']
)
).toEqual([
['isRefined', 'isNotRefined'],
['desc', 'asc']
]);
});

it('leaves direction empty if no direction was given & no default matches', function() {
expect(
formatSort(
['isRefined:desc', 'isNotRefined'],
['books:asc', 'isRefined:desc']
)
).toEqual([
['isRefined', 'isNotRefined'],
['desc', undefined]
]);
});

0 comments on commit 93298e0

Please sign in to comment.