diff --git a/@overflow/commons/ui/pipe/array-sort.ts b/@overflow/commons/ui/pipe/array-sort.ts new file mode 100644 index 0000000..a1d273d --- /dev/null +++ b/@overflow/commons/ui/pipe/array-sort.ts @@ -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; + } +} diff --git a/@overflow/commons/ui/pipe/index.ts b/@overflow/commons/ui/pipe/index.ts index 1899a16..219abe9 100644 --- a/@overflow/commons/ui/pipe/index.ts +++ b/@overflow/commons/ui/pipe/index.ts @@ -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 ]; diff --git a/@overflow/commons/ui/pipe/ip-sort.ts b/@overflow/commons/ui/pipe/ip-sort.ts new file mode 100644 index 0000000..1053e51 --- /dev/null +++ b/@overflow/commons/ui/pipe/ip-sort.ts @@ -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; + }); + } +} diff --git a/src/commons/component/host-detail.component.html b/src/commons/component/host-detail.component.html index 8ade5c1..2b773f2 100644 --- a/src/commons/component/host-detail.component.html +++ b/src/commons/component/host-detail.component.html @@ -68,7 +68,7 @@ diff --git a/src/commons/component/scanner-setting-dropdown.component.ts b/src/commons/component/scanner-setting-dropdown.component.ts index 8b6700a..7a8cb85 100644 --- a/src/commons/component/scanner-setting-dropdown.component.ts +++ b/src/commons/component/scanner-setting-dropdown.component.ts @@ -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) { diff --git a/src/commons/component/zone-detail.component.html b/src/commons/component/zone-detail.component.html index afd2319..759e946 100644 --- a/src/commons/component/zone-detail.component.html +++ b/src/commons/component/zone-detail.component.html @@ -48,7 +48,7 @@