Skip to content

Commit

Permalink
style(): Code re-order
Browse files Browse the repository at this point in the history
  • Loading branch information
danrevah committed Dec 15, 2016
1 parent bb656d4 commit 571970a
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 40 deletions.
7 changes: 5 additions & 2 deletions src/app/pipes/array/flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
, []);
}
}
8 changes: 6 additions & 2 deletions src/app/pipes/array/intersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
11 changes: 6 additions & 5 deletions src/app/pipes/array/reverse.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
15 changes: 8 additions & 7 deletions src/app/pipes/array/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/pipes/boolean/is-defined.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 2 additions & 1 deletion src/app/pipes/boolean/is-function.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 2 additions & 1 deletion src/app/pipes/boolean/is-object.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 2 additions & 1 deletion src/app/pipes/boolean/is-string.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
15 changes: 7 additions & 8 deletions src/app/pipes/math/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
Expand Down
4 changes: 3 additions & 1 deletion src/app/pipes/math/pow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
}
}
4 changes: 3 additions & 1 deletion src/app/pipes/math/sqrt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
6 changes: 3 additions & 3 deletions src/app/pipes/math/sum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
11 changes: 5 additions & 6 deletions src/app/pipes/object/invert-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]});
}, {});
}
}
2 changes: 1 addition & 1 deletion src/app/pipes/string/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 2 additions & 0 deletions src/app/pipes/string/shorten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 571970a

Please sign in to comment.