Skip to content

Commit

Permalink
Add coverage to orderby
Browse files Browse the repository at this point in the history
  • Loading branch information
danrevah committed Jan 28, 2017
1 parent ca5df5d commit c305f35
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/app/pipes/array/order-by.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ describe('OrderByPipe', () => {
]);
});

it('should order by property if there a stringy value of `+property` or `property`', () => {
expect(pipe.transform(testArray, 'name')).toEqual([
{id: 3, name: 'Dan', amount: 1},
{id: 4, name: 'Dave', amount: 2},
{id: 1, name: 'John', amount: 1337},
{id: 2, name: 'Michael', amount: 42}
]);
});

it('should reverse order by property if there a stringy value of `-property`', () => {
expect(pipe.transform(testArray, '-amount')).toEqual([
{id: 1, name: 'John', amount: 1337},
Expand All @@ -84,6 +93,21 @@ describe('OrderByPipe', () => {
]);
});


it('should reverse order by multiple properties if there an array values', () => {
expect(pipe.transform([
{id: 4, name: 'Dave', amount: 2},
{id: 2, name: 'Michael', amount: 2},
{id: 3, name: 'Dan', amount: 1},
{id: 1, name: 'John', amount: 1}
], ['amount'])).toEqual([
{id: 3, name: 'Dan', amount: 1},
{id: 1, name: 'John', amount: 1},
{id: 4, name: 'Dave', amount: 2},
{id: 2, name: 'Michael', amount: 2}
]);
});

it('should reverse order by multiple properties if there an array values', () => {
expect(pipe.transform([
{id: 2, name: 'b', amount: 2},
Expand Down
9 changes: 5 additions & 4 deletions src/app/pipes/array/order-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ export class OrderByPipe implements PipeTransform {
}

static orderCompare(prop, asc, a, b) {
if (GeneralHelper.isString(a) && GeneralHelper.isString(b)) {
return a.toLowerCase().localeCompare(b.toLowerCase());
}

const fir = GeneralHelper.extractDeepPropertyByMapKey(a, prop);
const sec = GeneralHelper.extractDeepPropertyByMapKey(b, prop);

if (fir === sec) {
return 0;
}

if (GeneralHelper.isString(fir) && GeneralHelper.isString(sec)) {
const pos = fir.toLowerCase().localeCompare(sec.toLowerCase());
return asc ? pos : -pos;
}

const pos = fir < sec ? -1 : 1;
return asc ? pos : -pos;
}
Expand Down

0 comments on commit c305f35

Please sign in to comment.