sort pipe

This commit is contained in:
insanity 2018-09-13 14:47:22 +09:00
parent efc9a0cfc4
commit 3743509b7a
6 changed files with 60 additions and 6 deletions

View File

@ -0,0 +1,17 @@
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'sort' })
export class Sort implements PipeTransform {
transform(array: any[], field: string): any[] {
array.sort((a: any, b: any) => {
if (a[field] < b[field]) {
return -1;
} else if (a[field] > b[field]) {
return 1;
} else {
return 0;
}
});
return array;
}
}

View File

@ -1,7 +1,11 @@
import { ObjectKeys } from "./object-keys";
import { stringPrettify } from "./string-prettify";
import { ObjectKeys } from './object-keys';
import { stringPrettify } from './string-prettify';
import { IPSort } from './ip-sort';
import { Sort } from './array-sort';
export const PIPES = [
ObjectKeys,
stringPrettify
stringPrettify,
IPSort,
Sort
];

View File

@ -0,0 +1,28 @@
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'ipSort' })
export class IPSort implements PipeTransform {
transform(array: any[], field: string): any[] {
array.sort((a: any, b: any) => {
a = this.ip2num(a[field]);
b = this.ip2num(b[field]);
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
});
return array;
}
ip2num(ip: string): number {
return ip.split('.').map((octet, index, array) => {
// tslint:disable-next-line:radix
return parseInt(octet) * Math.pow(256, (array.length - index - 1));
}).reduce((prev, curr) => {
return prev + curr;
});
}
}

View File

@ -68,7 +68,7 @@
<p-tabPanel header="Ports" *ngIf="host.portList">
<ul class="key-value">
<li *ngFor="let port of host.portList">
<li *ngFor="let port of host.portList | sort: 'portNumber'">
<span class="meta-value">{{port.portNumber}} ({{port.metaPortType.key}})</span>
</li>
</ul>

View File

@ -101,7 +101,12 @@ export class ScannerSettingDropdownComponent implements OnInit {
}
ipToNum(ip: string): number {
return ip.split('.').reduce(function (ipInt, octet) { return (ipInt << 8) + parseInt(octet, 10); }, 0) >>> 0;
return ip.split('.').map((octet, index, array) => {
// tslint:disable-next-line:radix
return parseInt(octet) * Math.pow(256, (array.length - index - 1));
}).reduce((prev, curr) => {
return prev + curr;
});
}
validatePort(value: string, idx: number) {

View File

@ -48,7 +48,7 @@
<p-tabPanel header="Hosts">
<ul class="key-value">
<li *ngFor="let host of zone.hostList">
<li *ngFor="let host of zone.hostList | ipSort: 'address'">
<span class="meta-value">{{host.address}}</span> <span *ngIf="host.name" class="meta-value">
({{host.name}})</span>
</li>