Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
danrevah committed Jan 28, 2017
1 parent c305f35 commit d64e70c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 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 @@ -45,6 +45,17 @@ describe('OrderByPipe', () => {
expect(pipe.transform(['c', 'b', 'a'], '+')).toEqual(['a', 'b', 'c']);
});

it('should order by property with a single character name', () => {
expect(pipe.transform([{a: 1}, {a: 3}, {a: 42}, {a: 2}], 'a'))
.toEqual([{a: 1}, {a: 2}, {a: 3}, {a: 42}]);

expect(pipe.transform([{a: 1}, {a: 3}, {a: 42}, {a: 2}], '+a'))
.toEqual([{a: 1}, {a: 2}, {a: 3}, {a: 42}]);

expect(pipe.transform([{a: 1}, {a: 3}, {a: 42}, {a: 2}], '-a'))
.toEqual([{a: 42}, {a: 3}, {a: 2}, {a: 1}]);
});

it('should order by property if there a stringy value of `+property` or `property`', () => {
expect(pipe.transform(testArray, 'amount')).toEqual([
{id: 3, name: 'Dan', amount: 1},
Expand Down
30 changes: 19 additions & 11 deletions src/app/pipes/array/order-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class OrderByPipe implements PipeTransform {

const out = [...arr];

// sort by multiple properties
if (Array.isArray(config)) {
return out.sort((a, b) => {
for (let i=0, l=config.length; i<l; ++i) {
Expand All @@ -24,35 +25,42 @@ export class OrderByPipe implements PipeTransform {
});
}

// sort by a single property value
if (GeneralHelper.isString(config)) {
const [prop, asc] = OrderByPipe.extractFromConfig(config);
const [prop, asc, sign] = OrderByPipe.extractFromConfig(config);

if (config.length === 1) {
return asc ? out.sort() : out.sort().reverse();
switch (sign) {
case '+': return out.sort();
case '-': return out.sort().reverse();
}
}

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

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

static orderCompare(prop, asc, a, b) {
const fir = GeneralHelper.extractDeepPropertyByMapKey(a, prop);
const sec = GeneralHelper.extractDeepPropertyByMapKey(b, prop);
const first = GeneralHelper.extractDeepPropertyByMapKey(a, prop);
const second = GeneralHelper.extractDeepPropertyByMapKey(b, prop);

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

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

const pos = fir < sec ? -1 : 1;
const pos = first < second ? -1 : 1;
return asc ? pos : -pos;
}

Expand All @@ -61,6 +69,6 @@ export class OrderByPipe implements PipeTransform {
const prop = config.replace(/^[-+]/, '');
const asc = sign !== '-';

return [prop, asc];
return [prop, asc, sign];
}
}
14 changes: 5 additions & 9 deletions src/app/pipes/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@ export default class GeneralHelper {
static extractDeepPropertyByMapKey(obj: Object, map: string) {
const keys = map.split('.');
const key = keys.shift();
let prop = obj[key];

for (let key of keys) {
if (GeneralHelper.isUndefined(prop[key])) {
return prop[key];
}
prop = prop[key];
}

return prop;
return keys.reduce((prop, key) => {
return !GeneralHelper.isUndefined(prop) && !GeneralHelper.isUndefined(prop[key])
? prop[key]
: undefined;
}, obj[key]);
}
}

0 comments on commit d64e70c

Please sign in to comment.