Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

does not support deep properties #10

Closed
esteban-url opened this issue May 1, 2017 · 0 comments
Closed

does not support deep properties #10

esteban-url opened this issue May 1, 2017 · 0 comments
Assignees
Milestone

Comments

@esteban-url
Copy link
Contributor

it will not work on complex objects such as

var testObj = {
  a: 'b',
  'hyph"en': 10,
  "hy[ph]en": 11,
  b: {
    c: [1, 2, 3],
    d: ['h', 'ch'],
    e: [{}, {f: 'g'}],
    f: 'i'
  }
};

expressions such as 'b.c[0]' or b.e[2].f would fail

I've tried to add tests and run them on my fork but couldn't get it to work, or spend more time on it. so i fixed it on my project by adding lodash, this is what my pipe looks like now:

import { Pipe, PipeTransform } from '@angular/core';
import * as lodash from 'lodash';
declare var _: any;
_ = lodash;

@Pipe({
  name: 'orderBy'
})
export class OrderPipe implements PipeTransform {

    transform(value: any[], prop?: any, reverse?: boolean, option?: string): any {

      if (!value) {
        return value;
      }

      let array: any[] = value.sort((a: any, b: any): number => {
        let aa = _.get(a, prop);
        let bb = _.get(b, prop);
        if (option && option.localeCompare('case-insensitive')==0 &&
            this.isString(aa) && this.isString(bb)) {
          return aa.localeCompare(bb);
        }
        return aa > bb ? 1 : -1;
      });

      if (reverse) {
        return array.reverse();
      }

      return array;
    }

    isString(value: any) {
      return typeof value === 'string' || value instanceof String;
    }
  }
VadimDez added a commit that referenced this issue May 25, 2017
@VadimDez VadimDez self-assigned this May 26, 2017
@VadimDez VadimDez added this to the 0.1.5 milestone May 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants