From 3f33c9767a73d62b763bd18587d1bcecee023855 Mon Sep 17 00:00:00 2001 From: danrevah Date: Sat, 3 Dec 2016 18:05:49 +0200 Subject: [PATCH] add more validations instead of throwing JS errors --- package.json | 2 +- src/app/pipes/array/diff.ts | 4 ++-- src/app/pipes/array/flatten.spec.ts | 8 ++++++++ src/app/pipes/array/flatten.ts | 10 +++++++--- src/app/pipes/array/initial.ts | 5 ++--- src/app/pipes/array/intersection.spec.ts | 8 ++++++++ src/app/pipes/array/intersection.ts | 4 ++-- src/app/pipes/array/pluck.spec.ts | 8 ++++++++ src/app/pipes/array/pluck.ts | 2 +- src/app/pipes/array/some.spec.ts | 8 ++++++++ src/app/pipes/array/some.ts | 2 +- src/app/pipes/array/tail.spec.ts | 8 ++++++++ src/app/pipes/array/tail.ts | 2 +- src/app/pipes/array/truthify.spec.ts | 8 ++++++++ src/app/pipes/array/truthify.ts | 6 ++++-- src/app/pipes/array/union.spec.ts | 8 ++++++++ src/app/pipes/array/union.ts | 20 +++++++++++--------- src/app/pipes/array/unique.ts | 6 ++++-- src/app/pipes/array/without.spec.ts | 8 ++++++++ src/app/pipes/array/without.ts | 6 ++++-- src/app/pipes/string/ltrim.ts | 5 ++++- src/app/pipes/string/repeat.ts | 5 ++++- src/app/pipes/string/rtrim.ts | 5 ++++- src/app/pipes/string/scan.ts | 6 ++++-- src/app/pipes/string/shorten.spec.ts | 7 +++++++ src/app/pipes/string/shorten.ts | 6 +++++- src/app/pipes/string/slugify.ts | 4 +++- src/app/pipes/string/trim.ts | 5 +++-- src/app/pipes/string/ucfirst.ts | 7 +++++-- src/app/pipes/string/ucwords.ts | 9 ++++++--- 30 files changed, 149 insertions(+), 43 deletions(-) diff --git a/package.json b/package.json index a16c9a2d..0c50c0f2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ng2-pipes", - "version": "0.4.18", + "version": "0.4.19", "author": "Dan Revah", "description": "Useful angular2 pipes", "license": "MIT", diff --git a/src/app/pipes/array/diff.ts b/src/app/pipes/array/diff.ts index ea56ee3b..5f77676b 100644 --- a/src/app/pipes/array/diff.ts +++ b/src/app/pipes/array/diff.ts @@ -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); } diff --git a/src/app/pipes/array/flatten.spec.ts b/src/app/pipes/array/flatten.spec.ts index bcdc9958..626aaca5 100644 --- a/src/app/pipes/array/flatten.spec.ts +++ b/src/app/pipes/array/flatten.spec.ts @@ -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); diff --git a/src/app/pipes/array/flatten.ts b/src/app/pipes/array/flatten.ts index bb308f83..f106b085 100644 --- a/src/app/pipes/array/flatten.ts +++ b/src/app/pipes/array/flatten.ts @@ -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[] { diff --git a/src/app/pipes/array/initial.ts b/src/app/pipes/array/initial.ts index a7e7c385..d1dc2d6a 100644 --- a/src/app/pipes/array/initial.ts +++ b/src/app/pipes/array/initial.ts @@ -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; } diff --git a/src/app/pipes/array/intersection.spec.ts b/src/app/pipes/array/intersection.spec.ts index f5995d0a..9fc8ac50 100644 --- a/src/app/pipes/array/intersection.spec.ts +++ b/src/app/pipes/array/intersection.spec.ts @@ -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]); }); diff --git a/src/app/pipes/array/intersection.ts b/src/app/pipes/array/intersection.ts index ad5f184d..591718ec 100644 --- a/src/app/pipes/array/intersection.ts +++ b/src/app/pipes/array/intersection.ts @@ -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); } diff --git a/src/app/pipes/array/pluck.spec.ts b/src/app/pipes/array/pluck.spec.ts index 4591017e..660dd710 100644 --- a/src/app/pipes/array/pluck.spec.ts +++ b/src/app/pipes/array/pluck.spec.ts @@ -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]); diff --git a/src/app/pipes/array/pluck.ts b/src/app/pipes/array/pluck.ts index 3d7ce1bb..a01dd375 100644 --- a/src/app/pipes/array/pluck.ts +++ b/src/app/pipes/array/pluck.ts @@ -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; diff --git a/src/app/pipes/array/some.spec.ts b/src/app/pipes/array/some.spec.ts index 25a26f83..e6d98e99 100644 --- a/src/app/pipes/array/some.spec.ts +++ b/src/app/pipes/array/some.spec.ts @@ -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(); diff --git a/src/app/pipes/array/some.ts b/src/app/pipes/array/some.ts index 867a47a8..ead06373 100644 --- a/src/app/pipes/array/some.ts +++ b/src/app/pipes/array/some.ts @@ -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; } } diff --git a/src/app/pipes/array/tail.spec.ts b/src/app/pipes/array/tail.spec.ts index 22c9f91e..1adb86ef 100644 --- a/src/app/pipes/array/tail.spec.ts +++ b/src/app/pipes/array/tail.spec.ts @@ -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); diff --git a/src/app/pipes/array/tail.ts b/src/app/pipes/array/tail.ts index 60994f3b..cf2fad86 100644 --- a/src/app/pipes/array/tail.ts +++ b/src/app/pipes/array/tail.ts @@ -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; } } diff --git a/src/app/pipes/array/truthify.spec.ts b/src/app/pipes/array/truthify.spec.ts index db90348c..3874fcda 100644 --- a/src/app/pipes/array/truthify.spec.ts +++ b/src/app/pipes/array/truthify.spec.ts @@ -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]); }); diff --git a/src/app/pipes/array/truthify.ts b/src/app/pipes/array/truthify.ts index b2470680..8f64a501 100644 --- a/src/app/pipes/array/truthify.ts +++ b/src/app/pipes/array/truthify.ts @@ -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; } } diff --git a/src/app/pipes/array/union.spec.ts b/src/app/pipes/array/union.spec.ts index 4cc5f1b6..d9b78ccd 100644 --- a/src/app/pipes/array/union.spec.ts +++ b/src/app/pipes/array/union.spec.ts @@ -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]); }); diff --git a/src/app/pipes/array/union.ts b/src/app/pipes/array/union.ts index d9961130..25666b7e 100644 --- a/src/app/pipes/array/union.ts +++ b/src/app/pipes/array/union.ts @@ -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); } } diff --git a/src/app/pipes/array/unique.ts b/src/app/pipes/array/unique.ts index f3422ae4..5da8e52d 100644 --- a/src/app/pipes/array/unique.ts +++ b/src/app/pipes/array/unique.ts @@ -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; } } diff --git a/src/app/pipes/array/without.spec.ts b/src/app/pipes/array/without.spec.ts index 3951768f..7cdb3bcf 100644 --- a/src/app/pipes/array/without.spec.ts +++ b/src/app/pipes/array/without.spec.ts @@ -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}]); diff --git a/src/app/pipes/array/without.ts b/src/app/pipes/array/without.ts index c32aa307..29b0f68e 100644 --- a/src/app/pipes/array/without.ts +++ b/src/app/pipes/array/without.ts @@ -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; } } diff --git a/src/app/pipes/string/ltrim.ts b/src/app/pipes/string/ltrim.ts index b7653f3e..3d00d4e2 100644 --- a/src/app/pipes/string/ltrim.ts +++ b/src/app/pipes/string/ltrim.ts @@ -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; } } diff --git a/src/app/pipes/string/repeat.ts b/src/app/pipes/string/repeat.ts index 03a325f9..f148caa3 100644 --- a/src/app/pipes/string/repeat.ts +++ b/src/app/pipes/string/repeat.ts @@ -1,4 +1,5 @@ import {PipeTransform, Pipe} from '@angular/core'; +import GeneralHelper from '../helpers/helpers'; @Pipe({name: 'repeat'}) export class RepeatPipe implements PipeTransform { @@ -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; } } diff --git a/src/app/pipes/string/rtrim.ts b/src/app/pipes/string/rtrim.ts index 8478b5ee..ef40eb6f 100644 --- a/src/app/pipes/string/rtrim.ts +++ b/src/app/pipes/string/rtrim.ts @@ -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; } } diff --git a/src/app/pipes/string/scan.ts b/src/app/pipes/string/scan.ts index 68a5b2d1..5a781b86 100644 --- a/src/app/pipes/string/scan.ts +++ b/src/app/pipes/string/scan.ts @@ -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; } } diff --git a/src/app/pipes/string/shorten.spec.ts b/src/app/pipes/string/shorten.spec.ts index 489396c3..b0dbbf47 100644 --- a/src/app/pipes/string/shorten.spec.ts +++ b/src/app/pipes/string/shorten.spec.ts @@ -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'); diff --git a/src/app/pipes/string/shorten.ts b/src/app/pipes/string/shorten.ts index f9734551..f8795f7a 100644 --- a/src/app/pipes/string/shorten.ts +++ b/src/app/pipes/string/shorten.ts @@ -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; diff --git a/src/app/pipes/string/slugify.ts b/src/app/pipes/string/slugify.ts index b42cd024..6d0e72eb 100644 --- a/src/app/pipes/string/slugify.ts +++ b/src/app/pipes/string/slugify.ts @@ -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; } } diff --git a/src/app/pipes/string/trim.ts b/src/app/pipes/string/trim.ts index c7ff9864..741f95b6 100644 --- a/src/app/pipes/string/trim.ts +++ b/src/app/pipes/string/trim.ts @@ -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; } } diff --git a/src/app/pipes/string/ucfirst.ts b/src/app/pipes/string/ucfirst.ts index a3af9bcd..2f8af8cd 100644 --- a/src/app/pipes/string/ucfirst.ts +++ b/src/app/pipes/string/ucfirst.ts @@ -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; } } diff --git a/src/app/pipes/string/ucwords.ts b/src/app/pipes/string/ucwords.ts index fcf1c0e3..e4e9ceb2 100644 --- a/src/app/pipes/string/ucwords.ts +++ b/src/app/pipes/string/ucwords.ts @@ -1,11 +1,14 @@ import {PipeTransform, Pipe} from '@angular/core'; +import GeneralHelper from '../helpers/helpers'; @Pipe({name: 'ucwords'}) export class UcWordsPipe implements PipeTransform { transform(text: string): string { - return text.split(' ') - .map(sub => sub.slice(0, 1).toUpperCase() + sub.slice(1)) - .join(' '); + return GeneralHelper.isString(text) + ? text.split(' ') + .map(sub => sub.slice(0, 1).toUpperCase() + sub.slice(1)) + .join(' ') + : text; } }