From 3743509b7aa4e71d43c5f7d8a8a26450783f8ef5 Mon Sep 17 00:00:00 2001 From: insanity Date: Thu, 13 Sep 2018 14:47:22 +0900 Subject: [PATCH] sort pipe --- @overflow/commons/ui/pipe/array-sort.ts | 17 +++++++++++ @overflow/commons/ui/pipe/index.ts | 10 +++++-- @overflow/commons/ui/pipe/ip-sort.ts | 28 +++++++++++++++++++ .../component/host-detail.component.html | 2 +- .../scanner-setting-dropdown.component.ts | 7 ++++- .../component/zone-detail.component.html | 2 +- 6 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 @overflow/commons/ui/pipe/array-sort.ts create mode 100644 @overflow/commons/ui/pipe/ip-sort.ts 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 @@
    -
  • +
  • {{host.address}} ({{host.name}})