Skip to content

Commit 3c5c100

Browse files
committed
bug(#25): fix issue with filterBy
1 parent 085d164 commit 3c5c100

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/app/pipes/array/order-by.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ describe('OrderByPipe', () => {
1414
pipe = new OrderByPipe();
1515
});
1616

17+
it ('should return dates in order asc', () => {
18+
const a = new Date();
19+
const b = new Date();
20+
expect(pipe.transform([a, b], '+')).toEqual([a, b]);
21+
});
22+
23+
it ('should return numbers in order asc', () => {
24+
const numbers = [0, -1, 345, 1234, 1337, -3];
25+
expect(pipe.transform(numbers, '+')).toEqual([-3, -1, 0, 345, 1234, 1337]);
26+
});
27+
1728
it('should not do anything in-case of not an array', () => {
1829
expect(pipe.transform('foo')).toEqual('foo');
1930
expect(pipe.transform(null)).toEqual(null);

src/app/pipes/array/order-by.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,25 @@ export class OrderByPipe implements PipeTransform {
3131

3232
if (config.length === 1) {
3333
switch (sign) {
34-
case '+': return out.sort();
35-
case '-': return out.sort().reverse();
34+
case '+': return out.sort(OrderByPipe.simpleSort.bind(this));
35+
case '-': return out.sort(OrderByPipe.simpleSort.bind(this)).reverse();
3636
}
3737
}
3838

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

4242
// default sort by value
43-
return out.sort((a, b) => {
44-
return isString(a) && isString(b)
45-
? a.toLowerCase().localeCompare(b.toLowerCase())
46-
: a - b;
47-
});
43+
return out.sort(OrderByPipe.simpleSort.bind(this));
4844
}
4945

50-
static orderCompare(prop: string, asc: boolean, a: any, b: any) {
46+
private static simpleSort(a: any, b: any) {
47+
return isString(a) && isString(b)
48+
? a.toLowerCase().localeCompare(b.toLowerCase())
49+
: a - b;
50+
}
51+
52+
private static orderCompare(prop: string, asc: boolean, a: any, b: any) {
5153
const first = extractDeepPropertyByMapKey(a, prop),
5254
second = extractDeepPropertyByMapKey(b, prop);
5355

@@ -65,7 +67,7 @@ export class OrderByPipe implements PipeTransform {
6567
: second - first;
6668
}
6769

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

0 commit comments

Comments
 (0)