From 571970a08192971529af3ec80fc81bc14c0b8cfb Mon Sep 17 00:00:00 2001 From: danrevah Date: Fri, 16 Dec 2016 01:46:30 +0200 Subject: [PATCH] style(): Code re-order --- src/app/pipes/array/flatten.ts | 7 +++++-- src/app/pipes/array/intersection.ts | 8 ++++++-- src/app/pipes/array/reverse.ts | 11 ++++++----- src/app/pipes/array/union.ts | 15 ++++++++------- src/app/pipes/boolean/is-defined.ts | 3 ++- src/app/pipes/boolean/is-function.ts | 3 ++- src/app/pipes/boolean/is-object.ts | 3 ++- src/app/pipes/boolean/is-string.ts | 3 ++- src/app/pipes/math/bytes.ts | 15 +++++++-------- src/app/pipes/math/pow.ts | 4 +++- src/app/pipes/math/sqrt.ts | 4 +++- src/app/pipes/math/sum.ts | 6 +++--- src/app/pipes/object/invert-by.ts | 11 +++++------ src/app/pipes/string/scan.ts | 2 +- src/app/pipes/string/shorten.ts | 2 ++ 15 files changed, 57 insertions(+), 40 deletions(-) diff --git a/src/app/pipes/array/flatten.ts b/src/app/pipes/array/flatten.ts index f106b085..7e83f6f2 100644 --- a/src/app/pipes/array/flatten.ts +++ b/src/app/pipes/array/flatten.ts @@ -14,7 +14,10 @@ export class FlattenPipe implements PipeTransform { } private flatten(array: any[]): any[] { - return array.reduce((arr: any[], elm: any) => elm instanceof Array ? - arr.concat(this.flatten(elm)) : arr.concat(elm), []); + return array.reduce((arr: any[], elm: any) => + Array.isArray(elm) + ? arr.concat(this.flatten(elm)) + : arr.concat(elm) + , []); } } diff --git a/src/app/pipes/array/intersection.ts b/src/app/pipes/array/intersection.ts index 591718ec..5a4fb7a4 100644 --- a/src/app/pipes/array/intersection.ts +++ b/src/app/pipes/array/intersection.ts @@ -4,8 +4,12 @@ import {PipeTransform, Pipe} from '@angular/core'; export class IntersectionPipe implements PipeTransform { transform(arr: any, ...args: any[]): any[] { - return !Array.isArray(arr) ? arr : args.reduce((newArr, currArr) => { - return newArr.filter(elm => !!~currArr.indexOf(elm)) + if (!Array.isArray(arr)) { + return arr; + } + + return args.reduce((newArr, currArr) => { + return newArr.filter(elm => !!~currArr.indexOf(elm)); }, arr); } } diff --git a/src/app/pipes/array/reverse.ts b/src/app/pipes/array/reverse.ts index fc139570..a7d943e6 100644 --- a/src/app/pipes/array/reverse.ts +++ b/src/app/pipes/array/reverse.ts @@ -1,16 +1,17 @@ import {PipeTransform, Pipe} from '@angular/core'; +import GeneralHelper from '../helpers/helpers'; @Pipe({name: 'reverse'}) export class ReversePipe implements PipeTransform { transform(value: any): any { - if (typeof value === 'string') { + if (GeneralHelper.isString(value)) { return value.split('').reverse().join(''); } - if (Array.isArray(value)) { - return value.reverse(); - } - return value; + + return Array.isArray(value) + ? value.reverse() + : value; } } diff --git a/src/app/pipes/array/union.ts b/src/app/pipes/array/union.ts index 25666b7e..f21162e6 100644 --- a/src/app/pipes/array/union.ts +++ b/src/app/pipes/array/union.ts @@ -4,14 +4,15 @@ import {PipeTransform, Pipe} from '@angular/core'; export class UnionPipe implements PipeTransform { transform(arr: any, args: any[] = []): any[] { - return (!Array.isArray(arr) || !Array.isArray(args)) - ? arr - : args.reduce((newArr, currArr) => { + if (!Array.isArray(arr) || !Array.isArray(args)) { + return arr; + } + + return args.reduce((newArr, currArr) => { return newArr.concat(currArr.reduce((noDupArr, curr) => { - if (!~noDupArr.indexOf(curr) && !~newArr.indexOf(curr)) { - noDupArr.push(curr); - } - return noDupArr; + return (!~noDupArr.indexOf(curr) && !~newArr.indexOf(curr)) + ? noDupArr.concat([curr]) + : noDupArr; }, [])); }, arr); } diff --git a/src/app/pipes/boolean/is-defined.ts b/src/app/pipes/boolean/is-defined.ts index ea296af6..5d3ccb63 100644 --- a/src/app/pipes/boolean/is-defined.ts +++ b/src/app/pipes/boolean/is-defined.ts @@ -1,9 +1,10 @@ import {PipeTransform, Pipe} from '@angular/core'; +import GeneralHelper from '../helpers/helpers'; @Pipe({name: 'isDefined'}) export class IsDefinedPipe implements PipeTransform { transform(value: any): boolean { - return typeof value !== 'undefined'; + return !GeneralHelper.isUndefined(value); } } diff --git a/src/app/pipes/boolean/is-function.ts b/src/app/pipes/boolean/is-function.ts index 17244a3e..d6d94b02 100644 --- a/src/app/pipes/boolean/is-function.ts +++ b/src/app/pipes/boolean/is-function.ts @@ -1,9 +1,10 @@ import {PipeTransform, Pipe} from '@angular/core'; +import GeneralHelper from '../helpers/helpers'; @Pipe({name: 'isFunction'}) export class IsFunctionPipe implements PipeTransform { transform(value: any): boolean { - return typeof value === 'function'; + return GeneralHelper.isFunction(value); } } diff --git a/src/app/pipes/boolean/is-object.ts b/src/app/pipes/boolean/is-object.ts index 796cfcbe..76fb92fb 100644 --- a/src/app/pipes/boolean/is-object.ts +++ b/src/app/pipes/boolean/is-object.ts @@ -1,9 +1,10 @@ import {PipeTransform, Pipe} from '@angular/core'; +import GeneralHelper from '../helpers/helpers'; @Pipe({name: 'isObject'}) export class IsObjectPipe implements PipeTransform { transform(value: any): boolean { - return value !== null && typeof value === 'object'; + return GeneralHelper.isObject(value); } } diff --git a/src/app/pipes/boolean/is-string.ts b/src/app/pipes/boolean/is-string.ts index 7ac3d2de..e0ba4210 100644 --- a/src/app/pipes/boolean/is-string.ts +++ b/src/app/pipes/boolean/is-string.ts @@ -1,9 +1,10 @@ import {PipeTransform, Pipe} from '@angular/core'; +import GeneralHelper from '../helpers/helpers'; @Pipe({name: 'isString'}) export class IsStringPipe implements PipeTransform { transform(value: any): boolean { - return typeof value === 'string'; + return GeneralHelper.isString(value); } } diff --git a/src/app/pipes/math/bytes.ts b/src/app/pipes/math/bytes.ts index 046bf2bb..345b334a 100644 --- a/src/app/pipes/math/bytes.ts +++ b/src/app/pipes/math/bytes.ts @@ -3,20 +3,19 @@ import GeneralHelper from '../helpers/helpers'; @Pipe({name: 'bytes'}) export class BytesPipe implements PipeTransform { + private dictionary: Array<{max: number, type: string}> = [ + { max: 1e3, type: 'B' }, + { max: 1e6, type: 'KB' }, + { max: 1e9, type: 'MB' }, + { max: 1e12, type: 'GB' } + ]; transform(value: number): string | number { if (!GeneralHelper.isNumberFinite(value)) { return NaN; } - const dictionary: Array<{max: number, type: string}> = [ - { max: 1e3, type: 'B' }, - { max: 1e6, type: 'KB' }, - { max: 1e9, type: 'MB' }, - { max: 1e12, type: 'GB' } - ]; - - const format = dictionary.find(d => value < d.max) || dictionary[dictionary.length - 1]; + const format = this.dictionary.find(d => value < d.max) || this.dictionary[this.dictionary.length - 1]; const num = value / (format.max / 1e3); return `${num} ${format.type}`; } diff --git a/src/app/pipes/math/pow.ts b/src/app/pipes/math/pow.ts index 6796d217..f554c945 100644 --- a/src/app/pipes/math/pow.ts +++ b/src/app/pipes/math/pow.ts @@ -4,6 +4,8 @@ import {PipeTransform, Pipe} from '@angular/core'; export class PowerPipe implements PipeTransform { transform(num: number, power: number = 2): number { - return isNaN(num) ? num : num ** power; + return !isNaN(num) + ? num ** power + : num ; } } diff --git a/src/app/pipes/math/sqrt.ts b/src/app/pipes/math/sqrt.ts index e118e45d..7fad19e9 100644 --- a/src/app/pipes/math/sqrt.ts +++ b/src/app/pipes/math/sqrt.ts @@ -4,6 +4,8 @@ import {PipeTransform, Pipe} from '@angular/core'; export class SqrtPipe implements PipeTransform { transform(num: number): number { - return isNaN(num) ? num : Math.sqrt(num); + return !isNaN(num) + ? Math.sqrt(num) + : num; } } diff --git a/src/app/pipes/math/sum.ts b/src/app/pipes/math/sum.ts index 991ba142..5a0947b8 100644 --- a/src/app/pipes/math/sum.ts +++ b/src/app/pipes/math/sum.ts @@ -4,8 +4,8 @@ import {PipeTransform, Pipe} from '@angular/core'; export class SumPipe implements PipeTransform { transform(arr: number[]): number|number[] { - return !Array.isArray(arr) - ? arr - : arr.reduce((sum, curr) => sum + curr, 0); + return Array.isArray(arr) + ? arr.reduce((sum, curr) => sum + curr, 0) + : arr; } } diff --git a/src/app/pipes/object/invert-by.ts b/src/app/pipes/object/invert-by.ts index e0aefeaa..6bd4e63a 100644 --- a/src/app/pipes/object/invert-by.ts +++ b/src/app/pipes/object/invert-by.ts @@ -9,12 +9,11 @@ export class InvertByPipe implements PipeTransform { return obj; } - return Object.keys(obj) - .reduce((o, k) => { - const key = cb ? cb(obj[k]) : obj[k]; - return Array.isArray(o[key]) - ? (o[key].push(k), o) - : Object.assign(o, {[key]: [k]}); + return Object.keys(obj).reduce((o, k) => { + const key = cb ? cb(obj[k]) : obj[k]; + return Array.isArray(o[key]) + ? (o[key].push(k), o) + : Object.assign(o, {[key]: [k]}); }, {}); } } diff --git a/src/app/pipes/string/scan.ts b/src/app/pipes/string/scan.ts index 5a781b86..6f91828e 100644 --- a/src/app/pipes/string/scan.ts +++ b/src/app/pipes/string/scan.ts @@ -6,7 +6,7 @@ export class ScanPipe implements PipeTransform { transform(text: string, args: string[] = []): string { return GeneralHelper.isString(text) - ? text.replace(/\{(\d+)}/g, (match, index) => typeof (args[index]) !== 'undefined' ? args[index] : match) + ? text.replace(/\{(\d+)}/g, (match, index) => !GeneralHelper.isUndefined(args[index]) ? args[index] : match) : text; } } diff --git a/src/app/pipes/string/shorten.ts b/src/app/pipes/string/shorten.ts index f8795f7a..94dd4c73 100644 --- a/src/app/pipes/string/shorten.ts +++ b/src/app/pipes/string/shorten.ts @@ -8,10 +8,12 @@ export class ShortenPipe implements PipeTransform { if (!GeneralHelper.isString(text)) { return text; } + if (text.length > length) { if (wordBreak) { return text.slice(0, length) + suffix; } + if (!!~text.indexOf(' ', length)) { return text.slice(0, text.indexOf(' ', length)) + suffix; }