Skip to content

Commit

Permalink
bug(#25): fix issue with filterBy
Browse files Browse the repository at this point in the history
  • Loading branch information
danrevah committed Feb 12, 2017
1 parent 085d164 commit 3c5c100
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
11 changes: 11 additions & 0 deletions src/app/pipes/array/order-by.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ describe('OrderByPipe', () => {
pipe = new OrderByPipe();
});

it ('should return dates in order asc', () => {
const a = new Date();
const b = new Date();
expect(pipe.transform([a, b], '+')).toEqual([a, b]);
});

it ('should return numbers in order asc', () => {
const numbers = [0, -1, 345, 1234, 1337, -3];
expect(pipe.transform(numbers, '+')).toEqual([-3, -1, 0, 345, 1234, 1337]);
});

it('should not do anything in-case of not an array', () => {
expect(pipe.transform('foo')).toEqual('foo');
expect(pipe.transform(null)).toEqual(null);
Expand Down
20 changes: 11 additions & 9 deletions src/app/pipes/array/order-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,25 @@ export class OrderByPipe implements PipeTransform {

if (config.length === 1) {
switch (sign) {
case '+': return out.sort();
case '-': return out.sort().reverse();
case '+': return out.sort(OrderByPipe.simpleSort.bind(this));
case '-': return out.sort(OrderByPipe.simpleSort.bind(this)).reverse();
}
}

return out.sort(OrderByPipe.orderCompare.bind(this, prop, asc));
}

// default sort by value
return out.sort((a, b) => {
return isString(a) && isString(b)
? a.toLowerCase().localeCompare(b.toLowerCase())
: a - b;
});
return out.sort(OrderByPipe.simpleSort.bind(this));
}

static orderCompare(prop: string, asc: boolean, a: any, b: any) {
private static simpleSort(a: any, b: any) {
return isString(a) && isString(b)
? a.toLowerCase().localeCompare(b.toLowerCase())
: a - b;
}

private static orderCompare(prop: string, asc: boolean, a: any, b: any) {
const first = extractDeepPropertyByMapKey(a, prop),
second = extractDeepPropertyByMapKey(b, prop);

Expand All @@ -65,7 +67,7 @@ export class OrderByPipe implements PipeTransform {
: second - first;
}

static extractFromConfig(config: any) {
private static extractFromConfig(config: any) {
const sign = config.substr(0, 1);
const prop = config.replace(/^[-+]/, '');
const asc = sign !== '-';
Expand Down

0 comments on commit 3c5c100

Please sign in to comment.