Skip to content

Commit

Permalink
add more validations instead of throwing JS errors
Browse files Browse the repository at this point in the history
  • Loading branch information
danrevah committed Dec 3, 2016
1 parent 835b561 commit 3f33c97
Show file tree
Hide file tree
Showing 30 changed files with 149 additions and 43 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng2-pipes",
"version": "0.4.18",
"version": "0.4.19",
"author": "Dan Revah",
"description": "Useful angular2 pipes",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions src/app/pipes/array/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'diff'})
export class DiffPipe implements PipeTransform {

transform(arr: any[], ...args: any[]): any[] {
return args.reduce((diffArr, currArr) => {
transform(arr: any, ...args: any[]): any[] {
return !Array.isArray(arr) ? arr : args.reduce((diffArr, currArr) => {
return diffArr.filter(elm => !~currArr.indexOf(elm))
}, arr);
}
Expand Down
8 changes: 8 additions & 0 deletions src/app/pipes/array/flatten.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ describe('FlattenPipe', () => {
pipe = new FlattenPipe();
});

it('should not change anything if not array', () => {
expect(pipe.transform('foo')).toEqual('foo');
expect(pipe.transform(null)).toEqual(null);
expect(pipe.transform(undefined)).toEqual(undefined);
expect(pipe.transform(42)).toEqual(42);
expect(pipe.transform({foo: 1, bar: 2})).toEqual({foo: 1, bar: 2});
});

it('should deep flatten array', () => {
let deepArray = [1,2,3,[4,5,6,[7,8,9],[10,11,12,13,[14],[15],[16, [17]]]]];
let result = pipe.transform(deepArray);
Expand Down
10 changes: 7 additions & 3 deletions src/app/pipes/array/flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'flatten'})
export class FlattenPipe implements PipeTransform {

transform(array: any[], shallow: boolean = false): any[] {
transform(arr: any, shallow: boolean = false): any[] {
if (!Array.isArray(arr)) {
return arr;
}

return shallow
? [].concat.apply([], array)
: this.flatten(array);
? [].concat.apply([], arr)
: this.flatten(arr);
}

private flatten(array: any[]): any[] {
Expand Down
5 changes: 2 additions & 3 deletions src/app/pipes/array/initial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'initial'})
export class InitialPipe implements PipeTransform {

transform(arr: any[], num: number = 0): any[]
{
return arr instanceof Array
transform(arr: any, num: number = 0): any[] {
return Array.isArray(arr)
? arr.slice(0, arr.length - num)
: arr;
}
Expand Down
8 changes: 8 additions & 0 deletions src/app/pipes/array/intersection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ describe('IntersectionPipe', () => {
pipe = new IntersectionPipe();
});

it('should not change anything if not array', () => {
expect(pipe.transform('foo')).toEqual('foo');
expect(pipe.transform(null)).toEqual(null);
expect(pipe.transform(undefined)).toEqual(undefined);
expect(pipe.transform(42)).toEqual(42);
expect(pipe.transform({foo: 1, bar: 2})).toEqual({foo: 1, bar: 2});
});

it('should not change when calling pipe without arguments', () => {
expect(pipe.transform([1, 2, 3])).toEqual([1, 2, 3]);
});
Expand Down
4 changes: 2 additions & 2 deletions src/app/pipes/array/intersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'intersection'})
export class IntersectionPipe implements PipeTransform {

transform(arr: any[], ...args: any[]): any[] {
return args.reduce((newArr, currArr) => {
transform(arr: any, ...args: any[]): any[] {
return !Array.isArray(arr) ? arr : args.reduce((newArr, currArr) => {
return newArr.filter(elm => !!~currArr.indexOf(elm))
}, arr);
}
Expand Down
8 changes: 8 additions & 0 deletions src/app/pipes/array/pluck.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ describe('PluckPipe', () => {
pipe = new PluckPipe();
});

it('should not change anything if not array', () => {
expect(pipe.transform('foo', '')).toEqual('foo');
expect(pipe.transform(null, '')).toEqual(null);
expect(pipe.transform(undefined, '')).toEqual(undefined);
expect(pipe.transform(42, '')).toEqual(42);
expect(pipe.transform({foo: 1, bar: 2}, '')).toEqual({foo: 1, bar: 2});
});

it('should return only the properties of the array of object', () => {
const arr = [{foo: 'bar', a: 1, b: {c: 4}},{foo: 'bar2', a: 2, b: {c: 5}}, {foo: 'bar3', a: 3, b: {c: 6}}];
expect(pipe.transform(arr, 'a')).toEqual([1, 2, 3]);
Expand Down
2 changes: 1 addition & 1 deletion src/app/pipes/array/pluck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import GeneralHelper from '../helpers';
@Pipe({name: 'pluck'})
export class PluckPipe implements PipeTransform {

transform(arr: any[], map: string): any[] {
transform(arr: any, map: string): any[] {
return Array.isArray(arr)
? arr.map(e => GeneralHelper.extractDeepPropertyByMapKey(e, map))
: arr;
Expand Down
8 changes: 8 additions & 0 deletions src/app/pipes/array/some.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ describe('SomePipe', () => {
pipe = new SomePipe();
});

it('should not change anything if not array', () => {
expect(pipe.transform('foo', GeneralHelper.isNumber)).toEqual('foo');
expect(pipe.transform(null, GeneralHelper.isNumber)).toEqual(null);
expect(pipe.transform(undefined, GeneralHelper.isNumber)).toEqual(undefined);
expect(pipe.transform(42, GeneralHelper.isNumber)).toEqual(42);
expect(pipe.transform({foo: 1, bar: 2}, GeneralHelper.isNumber)).toEqual({foo: 1, bar: 2});
});

it('should check if some elements of the array fits the predicate', () => {
expect(pipe.transform([1, 2, 3, 4], GeneralHelper.isNumber)).toBeTruthy();
expect(pipe.transform([1, 2, 3, 'a'], GeneralHelper.isNumber)).toBeTruthy();
Expand Down
2 changes: 1 addition & 1 deletion src/app/pipes/array/some.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'some'})
export class SomePipe implements PipeTransform {

transform(arr: any[], predicate: (value: any, index: number, array: any[]) => boolean): boolean {
transform(arr: any, predicate: (value: any, index: number, array: any[]) => boolean): boolean {
return Array.isArray(arr) ? arr.some(predicate) : arr;
}
}
8 changes: 8 additions & 0 deletions src/app/pipes/array/tail.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ describe('TailPipe', () => {
pipe = new TailPipe();
});

it('should not change anything if not array', () => {
expect(pipe.transform('foo')).toEqual('foo');
expect(pipe.transform(null)).toEqual(null);
expect(pipe.transform(undefined)).toEqual(undefined);
expect(pipe.transform(42)).toEqual(42);
expect(pipe.transform({foo: 1, bar: 2})).toEqual({foo: 1, bar: 2});
});

it('should check that nothing happens if there are no parameters', () => {
let arr = [1, 2, 3];
let result = pipe.transform(arr);
Expand Down
2 changes: 1 addition & 1 deletion src/app/pipes/array/tail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'tail'})
export class TailPipe implements PipeTransform {

transform(arr: any[], num: number = 0): any[] {
transform(arr: any, num: number = 0): any[] {
return Array.isArray(arr) ? arr.slice(num) : arr;
}
}
8 changes: 8 additions & 0 deletions src/app/pipes/array/truthify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ describe('TrurthifyPipe', () => {
pipe = new TrurthifyPipe();
});

it('should not change anything if not array', () => {
expect(pipe.transform('foo')).toEqual('foo');
expect(pipe.transform(null)).toEqual(null);
expect(pipe.transform(undefined)).toEqual(undefined);
expect(pipe.transform(42)).toEqual(42);
expect(pipe.transform({foo: 1, bar: 2})).toEqual({foo: 1, bar: 2});
});

it('should not change array without falsy values', () => {
expect(pipe.transform([1,2,3])).toEqual([1, 2, 3]);
});
Expand Down
6 changes: 4 additions & 2 deletions src/app/pipes/array/truthify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'truthify'})
export class TrurthifyPipe implements PipeTransform {

transform(arr: any[]): any[] {
return arr.filter(elm => !!elm);
transform(arr: any): any[] {
return Array.isArray(arr)
? arr.filter(elm => !!elm)
: arr;
}
}
8 changes: 8 additions & 0 deletions src/app/pipes/array/union.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ describe('UnionPipe', () => {
pipe = new UnionPipe();
});

it('should not change anything if not array', () => {
expect(pipe.transform('foo')).toEqual('foo');
expect(pipe.transform(null)).toEqual(null);
expect(pipe.transform(undefined)).toEqual(undefined);
expect(pipe.transform(42)).toEqual(42);
expect(pipe.transform({foo: 1, bar: 2})).toEqual({foo: 1, bar: 2});
});

it('should check if array is not changed when calling pipe without arguments', () => {
expect(pipe.transform([1, 2, 3])).toEqual([1, 2, 3]);
});
Expand Down
20 changes: 11 additions & 9 deletions src/app/pipes/array/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'union'})
export class UnionPipe implements PipeTransform {

transform(arr: any[], args: any[] = []): any[] {
return args.reduce((newArr, currArr) => {
return newArr.concat(currArr.reduce((noDupArr, curr) => {
if (!~noDupArr.indexOf(curr) && !~newArr.indexOf(curr)) {
noDupArr.push(curr);
}
return noDupArr;
}, []));
}, arr);
transform(arr: any, args: any[] = []): any[] {
return (!Array.isArray(arr) || !Array.isArray(args))
? arr
: args.reduce((newArr, currArr) => {
return newArr.concat(currArr.reduce((noDupArr, curr) => {
if (!~noDupArr.indexOf(curr) && !~newArr.indexOf(curr)) {
noDupArr.push(curr);
}
return noDupArr;
}, []));
}, arr);
}
}
6 changes: 4 additions & 2 deletions src/app/pipes/array/unique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'unique'})
export class UniquePipe implements PipeTransform {

transform(arr: any[]): any[] {
return arr.filter((elm, index) => arr.indexOf(elm) === index);
transform(arr: any): any[] {
return Array.isArray(arr)
? arr.filter((elm, index) => arr.indexOf(elm) === index)
: arr;
}
}
8 changes: 8 additions & 0 deletions src/app/pipes/array/without.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ describe('WithoutPipe', () => {
pipe = new WithoutPipe();
});

it('should not change anything if not array', () => {
expect(pipe.transform('foo')).toEqual('foo');
expect(pipe.transform(null)).toEqual(null);
expect(pipe.transform(undefined)).toEqual(undefined);
expect(pipe.transform(42)).toEqual(42);
expect(pipe.transform({foo: 1, bar: 2})).toEqual({foo: 1, bar: 2});
});

it('should keep the array the same way if it doesn\'t have any without arguments', () => {
expect(pipe.transform([1, 2, 3])).toEqual([1, 2, 3]);
expect(pipe.transform([1, 2, 3, {id: 1}])).toEqual([1, 2, 3, {id: 1}]);
Expand Down
6 changes: 4 additions & 2 deletions src/app/pipes/array/without.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'without'})
export class WithoutPipe implements PipeTransform {

transform(arr: any[], args: any[] = []): any[] {
return arr.filter(elm => !~args.indexOf(elm));
transform(arr: any, args: any[] = []): any[] {
return Array.isArray(arr)
? arr.filter(elm => !~args.indexOf(elm))
: arr;
}
}
5 changes: 4 additions & 1 deletion src/app/pipes/string/ltrim.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {PipeTransform, Pipe} from '@angular/core';
import GeneralHelper from '../helpers/helpers';

@Pipe({name: 'ltrim'})
export class LeftTrimPipe implements PipeTransform {

transform(text: string, chars: string = '\\s'): string {
return text.replace(new RegExp(`^[${chars}]+`), '');
return GeneralHelper.isString(text)
? text.replace(new RegExp(`^[${chars}]+`), '')
: text;
}
}
5 changes: 4 additions & 1 deletion src/app/pipes/string/repeat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {PipeTransform, Pipe} from '@angular/core';
import GeneralHelper from '../helpers/helpers';

@Pipe({name: 'repeat'})
export class RepeatPipe implements PipeTransform {
Expand All @@ -11,6 +12,8 @@ export class RepeatPipe implements PipeTransform {
}

private repeat(str: string, n: number, separator: string): string {
return n == 0 ? str : (str + separator + this.repeat(str, n - 1, separator));
return GeneralHelper.isString(str)
? (n == 0 ? str : (str + separator + this.repeat(str, n - 1, separator)))
: str;
}
}
5 changes: 4 additions & 1 deletion src/app/pipes/string/rtrim.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {PipeTransform, Pipe} from '@angular/core';
import GeneralHelper from '../helpers/helpers';

@Pipe({name: 'rtrim'})
export class RightTrimPipe implements PipeTransform {

transform(text: string, chars: string = '\\s'): string {
return text.replace(new RegExp(`[${chars}]+$`), '');
return GeneralHelper.isString(text)
? text.replace(new RegExp(`[${chars}]+$`), '')
: text;
}
}
6 changes: 4 additions & 2 deletions src/app/pipes/string/scan.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {PipeTransform, Pipe} from '@angular/core';
import GeneralHelper from '../helpers/helpers';

@Pipe({name: 'scan'})
export class ScanPipe implements PipeTransform {

transform(text: string, args: string[] = []): string {
return text.replace(/\{(\d+)}/g, (match, index) =>
typeof (args[index]) !== 'undefined' ? args[index] : match);
return GeneralHelper.isString(text)
? text.replace(/\{(\d+)}/g, (match, index) => typeof (args[index]) !== 'undefined' ? args[index] : match)
: text;
}
}
7 changes: 7 additions & 0 deletions src/app/pipes/string/shorten.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ describe('ShortenPipe Tests', () => {
pipe = new ShortenPipe();
});

it('should not do anything when not a string', () => {
expect(pipe.transform(null)).toEqual(null);
expect(pipe.transform(undefined)).toEqual(undefined);
expect(pipe.transform(42)).toEqual(42);
expect(pipe.transform({foo: 1, bar: 2})).toEqual({foo: 1, bar: 2});
});

it('should not change the string if the length is more than the string size', () => {
expect(pipe.transform('lorem ipsum', 20)).toEqual('lorem ipsum');
expect(pipe.transform('lorem ipsum', 20, '..')).toEqual('lorem ipsum');
Expand Down
6 changes: 5 additions & 1 deletion src/app/pipes/string/shorten.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import {PipeTransform, Pipe} from '@angular/core';
import GeneralHelper from '../helpers/helpers';

@Pipe({name: 'shorten'})
export class ShortenPipe implements PipeTransform {

transform(text: string, length: number = 0, suffix: string = '', wordBreak: boolean = true): string {
transform(text: any, length: number = 0, suffix: string = '', wordBreak: boolean = true): string {
if (!GeneralHelper.isString(text)) {
return text;
}
if (text.length > length) {
if (wordBreak) {
return text.slice(0, length) + suffix;
Expand Down
4 changes: 3 additions & 1 deletion src/app/pipes/string/slugify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import GeneralHelper from '../helpers';
export class SlugifyPipe implements PipeTransform {

transform(str: string): string {
return GeneralHelper.isString(str) ? str.toLowerCase().replace(/\s+/g, '-') : str;
return GeneralHelper.isString(str)
? str.toLowerCase().replace(/\s+/g, '-')
: str;
}
}
5 changes: 3 additions & 2 deletions src/app/pipes/string/trim.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {PipeTransform, Pipe} from '@angular/core';
import GeneralHelper from '../helpers/helpers';

@Pipe({name: 'trim'})
export class TrimPipe implements PipeTransform {

transform(text: string, chars: string = '\\s'): string {
return text.replace(new RegExp(
return GeneralHelper.isString(text) ? text.replace(new RegExp(
`^[${chars}]+|[${chars}]+$`, 'g'
), '');
), '') : text;
}
}
7 changes: 5 additions & 2 deletions src/app/pipes/string/ucfirst.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {PipeTransform, Pipe} from '@angular/core';
import GeneralHelper from '../helpers/helpers';

@Pipe({name: 'ucfirst'})
export class UcFirstPipe implements PipeTransform {

transform(text:string):string {
return text.slice(0, 1).toUpperCase() + text.slice(1);
transform(text: string):string {
return GeneralHelper.isString(text)
? (text.slice(0, 1).toUpperCase() + text.slice(1))
: text;
}
}
Loading

0 comments on commit 3f33c97

Please sign in to comment.