From ee60ab81e52ece748f29f1e462364520747e0bed Mon Sep 17 00:00:00 2001 From: chensimeng Date: Mon, 24 Feb 2020 17:39:07 +0800 Subject: [PATCH] feat(module:core): add pipe module --- components/core/pipe/nz-bytes.pipe.spec.ts | 137 ++++++++++++++++++ components/core/pipe/nz-bytes.pipe.ts | 68 +++++++++ components/core/pipe/nz-css-unit.pipe.spec.ts | 33 +++++ components/core/pipe/nz-ellipsis.pipe.spec.ts | 33 +++++ components/core/pipe/nz-ellipsis.pipe.ts | 39 +++++ components/core/pipe/nz-math.pipe.spec.ts | 88 +++++++++++ components/core/pipe/nz-math.pipe.ts | 58 ++++++++ components/core/pipe/nz-pipe.module.ts | 37 +++++ .../core/pipe/nz-safe-null.pipe.spec.ts | 33 +++++ components/core/pipe/nz-safe-null.pipe.ts | 22 +++ .../core/pipe/nz-sanitizer.pipe.spec.ts | 17 +++ components/core/pipe/nz-sanitizer.pipe.ts | 35 +++++ components/core/pipe/nz-some.pipe.spec.ts | 29 ++++ components/core/pipe/nz-some.pipe.ts | 28 ++++ components/core/pipe/nz-trim.pipe.spec.ts | 19 +++ components/core/pipe/nz-trim.pipe.ts | 18 +++ 16 files changed, 694 insertions(+) create mode 100644 components/core/pipe/nz-bytes.pipe.spec.ts create mode 100644 components/core/pipe/nz-bytes.pipe.ts create mode 100644 components/core/pipe/nz-css-unit.pipe.spec.ts create mode 100644 components/core/pipe/nz-ellipsis.pipe.spec.ts create mode 100644 components/core/pipe/nz-ellipsis.pipe.ts create mode 100644 components/core/pipe/nz-math.pipe.spec.ts create mode 100644 components/core/pipe/nz-math.pipe.ts create mode 100644 components/core/pipe/nz-pipe.module.ts create mode 100644 components/core/pipe/nz-safe-null.pipe.spec.ts create mode 100644 components/core/pipe/nz-safe-null.pipe.ts create mode 100644 components/core/pipe/nz-sanitizer.pipe.spec.ts create mode 100644 components/core/pipe/nz-sanitizer.pipe.ts create mode 100644 components/core/pipe/nz-some.pipe.spec.ts create mode 100644 components/core/pipe/nz-some.pipe.ts create mode 100644 components/core/pipe/nz-trim.pipe.spec.ts create mode 100644 components/core/pipe/nz-trim.pipe.ts diff --git a/components/core/pipe/nz-bytes.pipe.spec.ts b/components/core/pipe/nz-bytes.pipe.spec.ts new file mode 100644 index 00000000000..5612517c62b --- /dev/null +++ b/components/core/pipe/nz-bytes.pipe.spec.ts @@ -0,0 +1,137 @@ +import { ByteUnit, NzBytesPipe } from './nz-bytes.pipe'; + +describe('NzBytesPipe', () => { + let pipe: NzBytesPipe; + + beforeEach(() => { + pipe = new NzBytesPipe(); + }); + + it('Should return 150 B', () => { + const result = pipe.transform(150, 0); + expect(result).toEqual('150 B'); + }); + + it('Should return 155.57 B', () => { + const result = pipe.transform(155.56791, 2); + expect(result).toEqual('155.57 B'); + }); + + it('Should return 155.5 B', () => { + const result = pipe.transform(155.5, 1); + expect(result).toEqual('155.5 B'); + }); + + it('Should return 1 kB', () => { + const result = pipe.transform(1024, 0); + expect(result).toEqual('1 kB'); + }); + + it('Should return 1 kB #2', () => { + const result = pipe.transform(1, 0, 'kB'); + expect(result).toEqual('1 kB'); + }); + + it('Should return 1 kB #3', () => { + const result = pipe.transform(1, 0, 'KB'); + expect(result).toEqual('1 kB'); + }); + + it('Should return 890 kB', () => { + const kB = 1024 * 890; + const result = pipe.transform(kB, 0); + expect(result).toEqual('890 kB'); + }); + + it('Should return 890 kB #2', () => { + const result = pipe.transform(890, 0, 'kB'); + expect(result).toEqual('890 kB'); + }); + + it('Should return 890 kB #3', () => { + const result = pipe.transform(890, 0, 'KB'); + expect(result).toEqual('890 kB'); + }); + + it('Should return 1023 kB', () => { + const kB = 1024 * 1023; + const result = pipe.transform(kB, 0); + expect(result).toEqual('1023 kB'); + }); + + it('Should return 241 MB', () => { + const mb = 1024 * 1024 * 240.5691; + const result = pipe.transform(mb, 0); + expect(result).toEqual('241 MB'); + }); + + it('Should return 241 MB', () => { + const mb = 240.5691 / 1024; + const result = pipe.transform(mb, 0, 'GB'); + expect(result).toEqual('241 MB'); + }); + + it('Should return 240.54 MB', () => { + const mb = 1024 * 1024 * 240.5411; + const result = pipe.transform(mb, 2); + expect(result).toEqual('240.54 MB'); + }); + + it('Should return 1023 MB', () => { + const mb = 1024 * 1024 * 1023; + const result = pipe.transform(mb, 2); + expect(result).toEqual('1023 MB'); + }); + + it('Should return 1023 MB #2', () => { + const kB = 1024 * 1023; + const result = pipe.transform(kB, 2, 'kB'); + expect(result).toEqual('1023 MB'); + }); + + it('Should return 1023 GB', () => { + const gb = 1024 * 1024 * 1024 * 1023; + const result = pipe.transform(gb, 2); + expect(result).toEqual('1023 GB'); + }); + + it('Should return 1.03 TB', () => { + const gb = 1024 * 1024 * 1024 * 1059; + const result = pipe.transform(gb, 2); + expect(result).toEqual('1.03 TB'); + }); + + it('Should return the input', () => { + expect(pipe.transform('a')).toEqual('a'); + }); + + it('Should return 100 TB', () => { + const bytes = 100; + const unit = 'TB'; + const result = NzBytesPipe.formatResult(bytes, unit); + expect(result).toEqual('100 TB'); + }); + + it('Should return 1', () => { + const format = { max: Math.pow(1024, 4), prev: 'MB' as ByteUnit }; + const bytes = 1024 * 1024 * 1024; + const result = NzBytesPipe.calculateResult(format, bytes); + expect(result).toEqual(1); + }); + + it('Should return 1024 MB, set output unit', () => { + const bytes = 1024 * 1024 * 1024; + const from = 'B' as ByteUnit; + const to = 'MB' as ByteUnit; + const result = pipe.transform(bytes, 0, from, to); + expect(result).toEqual('1024 MB'); + }); + + it('Should return 1024 MB, from & to units are equals', () => { + const bytes = 1024; + const from = 'MB' as ByteUnit; + const to = 'MB' as ByteUnit; + const result = pipe.transform(bytes, 0, from, to); + expect(result).toEqual('1024 MB'); + }); +}); diff --git a/components/core/pipe/nz-bytes.pipe.ts b/components/core/pipe/nz-bytes.pipe.ts new file mode 100644 index 00000000000..ea45419c378 --- /dev/null +++ b/components/core/pipe/nz-bytes.pipe.ts @@ -0,0 +1,68 @@ +/** + * @license + * Copyright Alibaba.com All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE + */ + +import { Pipe, PipeTransform } from '@angular/core'; +import { isNumberFinite, toDecimal } from 'ng-zorro-antd'; + +export type ByteUnit = 'B' | 'kB' | 'KB' | 'MB' | 'GB' | 'TB'; + +@Pipe({ + name: 'nzBytes' +}) +export class NzBytesPipe implements PipeTransform { + static formats: { [key: string]: { max: number; prev?: ByteUnit } } = { + B: { max: 1024 }, + kB: { max: Math.pow(1024, 2), prev: 'B' }, + KB: { max: Math.pow(1024, 2), prev: 'B' }, // Backward compatible + MB: { max: Math.pow(1024, 3), prev: 'kB' }, + GB: { max: Math.pow(1024, 4), prev: 'MB' }, + TB: { max: Number.MAX_SAFE_INTEGER, prev: 'GB' } + }; + + // tslint:disable-next-line:no-any + transform(input: any, decimal: number = 0, from: ByteUnit = 'B', to?: ByteUnit): any { + if (!(isNumberFinite(input) && isNumberFinite(decimal) && decimal % 1 === 0 && decimal >= 0)) { + return input; + } + + let bytes = input; + let unit = from; + while (unit !== 'B') { + bytes *= 1024; + unit = NzBytesPipe.formats[unit].prev!; + } + + if (to) { + const format = NzBytesPipe.formats[to]; + + const result = toDecimal(NzBytesPipe.calculateResult(format, bytes), decimal); + + return NzBytesPipe.formatResult(result, to); + } + + for (const key in NzBytesPipe.formats) { + if (NzBytesPipe.formats.hasOwnProperty(key)) { + const format = NzBytesPipe.formats[key]; + if (bytes < format.max) { + const result = toDecimal(NzBytesPipe.calculateResult(format, bytes), decimal); + + return NzBytesPipe.formatResult(result, key); + } + } + } + } + + static formatResult(result: number, unit: string): string { + return `${result} ${unit}`; + } + + static calculateResult(format: { max: number; prev?: ByteUnit }, bytes: number): number { + const prev = format.prev ? NzBytesPipe.formats[format.prev] : undefined; + return prev ? bytes / prev.max : bytes; + } +} diff --git a/components/core/pipe/nz-css-unit.pipe.spec.ts b/components/core/pipe/nz-css-unit.pipe.spec.ts new file mode 100644 index 00000000000..72dd6b4b86b --- /dev/null +++ b/components/core/pipe/nz-css-unit.pipe.spec.ts @@ -0,0 +1,33 @@ +import { NzToCssUnitPipe } from './nz-css-unit.pipe'; + +describe('NzToCssUnitPipe', () => { + let pipe: NzToCssUnitPipe; + + beforeEach(() => { + pipe = new NzToCssUnitPipe(); + }); + + it('Should ToCssUnit', () => { + expect(pipe.transform('100')).toEqual('100px'); + }); + + it('Should ToCssUnit but typeof value is String', () => { + expect(pipe.transform('100px')).toEqual('100px'); + }); + + it('Should ToCssUnit but defaultUnit is defined', () => { + expect(pipe.transform('100', 'pt')).toEqual('100pt'); + }); + + it('Should ToCssUnit but defaultUnit is defined and typeof value is String', () => { + expect(pipe.transform('100px', 'pt')).toEqual('100px'); + }); + + // it('Should ToCssUnit but typeof value is null', () => { + // expect(pipe.transform(null)).toEqual('0px'); + // }); + // + // it('Should ToCssUnit but typeof value is undefined', () => { + // expect(pipe.transform(undefined)).toEqual(undefined); + // }); +}); diff --git a/components/core/pipe/nz-ellipsis.pipe.spec.ts b/components/core/pipe/nz-ellipsis.pipe.spec.ts new file mode 100644 index 00000000000..0aa5d4092c0 --- /dev/null +++ b/components/core/pipe/nz-ellipsis.pipe.spec.ts @@ -0,0 +1,33 @@ +import { NzEllipsisPipe } from './nz-ellipsis.pipe'; + +describe('NzEllipsisPipe', () => { + let pipe: NzEllipsisPipe; + + beforeEach(() => { + pipe = new NzEllipsisPipe(); + }); + + it('Should truncate', () => { + expect(pipe.transform('Hello World', 4, '', false)).toEqual('Hell'); + }); + + it('Should truncate but preserve word', () => { + expect(pipe.transform('Hello World', 4, '', true)).toEqual('Hello'); + }); + + it('Should truncate but preserve word and add suffix', () => { + expect(pipe.transform('Hello World', 4, '...', true)).toEqual('Hello...'); + }); + + it('Should truncate but preserve word and add suffix', () => { + expect(pipe.transform('Hello World, how is it going?', 14, '...', true)).toEqual('Hello World, how...'); + }); + + it('Should preserve word', () => { + expect(pipe.transform('aaaa', 2, '', true)).toEqual('aaaa'); + }); + + it('Should return the input', () => { + expect(pipe.transform('Hello', 10)).toEqual('Hello'); + }); +}); diff --git a/components/core/pipe/nz-ellipsis.pipe.ts b/components/core/pipe/nz-ellipsis.pipe.ts new file mode 100644 index 00000000000..d06d84dade5 --- /dev/null +++ b/components/core/pipe/nz-ellipsis.pipe.ts @@ -0,0 +1,39 @@ +/** + * @license + * Copyright Alibaba.com All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE + */ + +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({ + name: 'nzEllipsis' +}) +export class NzEllipsisPipe implements PipeTransform { + // tslint:disable-next-line:no-any + transform(value: any, length?: number, suffix?: string, preserve?: boolean): any { + if (typeof value !== 'string') { + return value; + } + + const len = typeof length === 'undefined' ? value.length : length; + + if (value.length <= len) { + return value; + } + + let index = len; + + if (preserve) { + if (value.indexOf(' ', len) === -1) { + index = value.length; + } else { + index = value.indexOf(' ', len); + } + } + + return value.substring(0, index) + suffix; + } +} diff --git a/components/core/pipe/nz-math.pipe.spec.ts b/components/core/pipe/nz-math.pipe.spec.ts new file mode 100644 index 00000000000..6aa2cc0b6d6 --- /dev/null +++ b/components/core/pipe/nz-math.pipe.spec.ts @@ -0,0 +1,88 @@ +import { EMathMethod, NzMathPipe } from './nz-math.pipe'; + +describe('NzMathPipe', () => { + let nzMathPipe: NzMathPipe; + + beforeEach(() => { + nzMathPipe = new NzMathPipe(); + }); + it('Should return 4', () => { + expect(nzMathPipe.transform([1, 2, 3, 4], EMathMethod.MAX)).toEqual(4); + }); + + it('Should return 1', () => { + expect(nzMathPipe.transform([1], EMathMethod.MAX)).toEqual(1); + }); + + it('Should return 1', () => { + expect(nzMathPipe.transform([1, 1], EMathMethod.MAX)).toEqual(1); + }); + + it('Should return the value unchanged', () => { + expect(nzMathPipe.transform(1, EMathMethod.MAX)).toEqual(1); + }); + + it('Should return undefined', () => { + expect(nzMathPipe.transform([], EMathMethod.MAX)).toBeUndefined(); + }); + + it('Should return 1', () => { + expect(nzMathPipe.transform([1, 2, 3, 4], EMathMethod.MIN)).toEqual(1); + }); + + it('Should return 2', () => { + expect(nzMathPipe.transform([4, 3, 2, 5], EMathMethod.MIN)).toEqual(2); + }); + + it('Should return 1', () => { + expect(nzMathPipe.transform([1], EMathMethod.MIN)).toEqual(1); + }); + + it('Should return 1', () => { + expect(nzMathPipe.transform([1, 1], EMathMethod.MIN)).toEqual(1); + }); + + it('Should return the value unchanged', () => { + expect(nzMathPipe.transform(1, EMathMethod.MIN)).toEqual(1); + }); + + it('Should return undefined', () => { + expect(nzMathPipe.transform([], EMathMethod.MIN)).toBeUndefined(); + }); + + it('Should return 10', () => { + expect(nzMathPipe.transform([1, 2, 3, 4], EMathMethod.SUM)).toEqual(10); + }); + + it('Should return 1', () => { + expect(nzMathPipe.transform([1], EMathMethod.SUM)).toEqual(1); + }); + + it('Should return 2', () => { + expect(nzMathPipe.transform([1, 1], EMathMethod.SUM)).toEqual(2); + }); + + it('Should return 15', () => { + expect(nzMathPipe.transform(15, EMathMethod.SUM)).toEqual(15); + }); + + it('Should return 2.5', () => { + expect(nzMathPipe.transform([1, 2, 3, 4], EMathMethod.AVG)).toEqual(2.5); + }); + + it('Should return 1', () => { + expect(nzMathPipe.transform([1], EMathMethod.AVG)).toEqual(1); + }); + + it('Should return 1', () => { + expect(nzMathPipe.transform([1, 1], EMathMethod.AVG)).toEqual(1); + }); + + it('Should return the value unchanged', () => { + expect(nzMathPipe.transform(1, EMathMethod.AVG)).toEqual(1); + }); + + it('Should return undefined', () => { + expect(nzMathPipe.transform([], EMathMethod.AVG)).toBeUndefined(); + }); +}); diff --git a/components/core/pipe/nz-math.pipe.ts b/components/core/pipe/nz-math.pipe.ts new file mode 100644 index 00000000000..ebdaf257d9c --- /dev/null +++ b/components/core/pipe/nz-math.pipe.ts @@ -0,0 +1,58 @@ +/** + * @license + * Copyright Alibaba.com All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE + */ + +import { Pipe, PipeTransform } from '@angular/core'; +import { sum } from 'ng-zorro-antd'; + +export enum EMathMethod { + SUM = 'sum', + MAX = 'max', + MIN = 'min', + AVG = 'avg' +} + +@Pipe({ + name: 'nzMath' +}) +export class NzMathPipe implements PipeTransform { + getMethodResult(data: number[], method: EMathMethod): number { + let result: number = data[0]; + // tslint:disable-next-line:no-any + data.forEach((item: any) => { + if (method === EMathMethod.MAX) { + if (result < item) { + result = item; + } + } else if (method === EMathMethod.MIN) { + if (result > item) { + result = item; + } + } + }); + return result; + } + + // tslint:disable-next-line:no-any + transform(value: any, method: EMathMethod): undefined | number { + if (!Array.isArray(value)) { + return value; + } + if (value.length === 0) { + return undefined; + } + if (method === EMathMethod.SUM) { + return sum(value); + } else if (method === EMathMethod.AVG) { + return sum(value) / value.length; + } else if (method === EMathMethod.MAX || method === EMathMethod.MIN) { + return this.getMethodResult(value, method); + } else { + throw Error(`InvalidPipeArgument: Math pipe doesn't have this method`); + } + } +} diff --git a/components/core/pipe/nz-pipe.module.ts b/components/core/pipe/nz-pipe.module.ts new file mode 100644 index 00000000000..082973ea82c --- /dev/null +++ b/components/core/pipe/nz-pipe.module.ts @@ -0,0 +1,37 @@ +/** + * @license + * Copyright Alibaba.com All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE + */ + +import { CommonModule } from '@angular/common'; +import { NgModule } from '@angular/core'; + +import { NzBytesPipe } from './nz-bytes.pipe'; +import { NzToCssUnitPipe } from './nz-css-unit.pipe'; +import { NzEllipsisPipe } from './nz-ellipsis.pipe'; +import { NzMathPipe } from './nz-math.pipe'; +import { NzSafeNullPipe } from './nz-safe-null.pipe'; +import { NzSanitizerPipe } from './nz-sanitizer.pipe'; +import { NzSomePipe } from './nz-some.pipe'; +import { NzTrimPipe } from './nz-trim.pipe'; +import { NzTimeRangePipe } from './time-range.pipe'; + +@NgModule({ + imports: [CommonModule], + exports: [NzTimeRangePipe, NzToCssUnitPipe], + declarations: [ + NzTimeRangePipe, + NzToCssUnitPipe, + NzSafeNullPipe, + NzSanitizerPipe, + NzTrimPipe, + NzBytesPipe, + NzSomePipe, + NzMathPipe, + NzEllipsisPipe + ] +}) +export class NzPipesModule {} diff --git a/components/core/pipe/nz-safe-null.pipe.spec.ts b/components/core/pipe/nz-safe-null.pipe.spec.ts new file mode 100644 index 00000000000..0e20d739199 --- /dev/null +++ b/components/core/pipe/nz-safe-null.pipe.spec.ts @@ -0,0 +1,33 @@ +import { NzSafeNullPipe } from './nz-safe-null.pipe'; + +describe('NzSafeNullPipe', () => { + let pipe: NzSafeNullPipe; + + beforeEach(() => { + pipe = new NzSafeNullPipe(); + }); + + it('Should safeNull', () => { + expect(pipe.transform('Hello')).toEqual('Hello'); + }); + + it('Should safeNull but replace', () => { + expect(pipe.transform('Hello', '...')).toEqual('Hello'); + }); + + it('Should safeNull but value is null', () => { + expect(pipe.transform(null)).toEqual(''); + }); + + it('Should safeNull but value is null and replace', () => { + expect(pipe.transform(null, '-')).toEqual('-'); + }); + + it('Should safeNull but value is undefined', () => { + expect(pipe.transform(undefined)).toEqual(''); + }); + + it('Should safeNull but value is undefined and replace', () => { + expect(pipe.transform(undefined, '-')).toEqual('-'); + }); +}); diff --git a/components/core/pipe/nz-safe-null.pipe.ts b/components/core/pipe/nz-safe-null.pipe.ts new file mode 100644 index 00000000000..adaef96f0f7 --- /dev/null +++ b/components/core/pipe/nz-safe-null.pipe.ts @@ -0,0 +1,22 @@ +/** + * @license + * Copyright Alibaba.com All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE + */ + +import { Pipe, PipeTransform } from '@angular/core'; +import { isNil } from 'ng-zorro-antd'; + +@Pipe({ + name: 'nzSafeNull' +}) +export class NzSafeNullPipe implements PipeTransform { + transform(value: T, replace: string = ''): T | string { + if (isNil(value)) { + return replace; + } + return value; + } +} diff --git a/components/core/pipe/nz-sanitizer.pipe.spec.ts b/components/core/pipe/nz-sanitizer.pipe.spec.ts new file mode 100644 index 00000000000..5ec85a33a97 --- /dev/null +++ b/components/core/pipe/nz-sanitizer.pipe.spec.ts @@ -0,0 +1,17 @@ +import { DomSanitizer } from '@angular/platform-browser'; +import { NzSanitizerPipe } from './nz-sanitizer.pipe'; + +describe('NzSanitizerPipe', () => { + let pipe: NzSanitizerPipe; + beforeEach(() => { + let sanitizer: DomSanitizer; + // @ts-ignore + sanitizer = new DomSanitizer(); + pipe = new NzSanitizerPipe(sanitizer); + }); + + it('Should sanitizer', () => { + const htmlSnippet = ""; + expect(pipe.transform(htmlSnippet, 'html')).toBeTruthy(); + }); +}); diff --git a/components/core/pipe/nz-sanitizer.pipe.ts b/components/core/pipe/nz-sanitizer.pipe.ts new file mode 100644 index 00000000000..0ab39abff13 --- /dev/null +++ b/components/core/pipe/nz-sanitizer.pipe.ts @@ -0,0 +1,35 @@ +/** + * @license + * Copyright Alibaba.com All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE + */ + +import { Pipe, PipeTransform } from '@angular/core'; +import { DomSanitizer, SafeHtml, SafeResourceUrl, SafeScript, SafeStyle, SafeUrl } from '@angular/platform-browser'; + +@Pipe({ + name: 'nzSanitizer' +}) +export class NzSanitizerPipe implements PipeTransform { + constructor(protected sanitizer: DomSanitizer) {} + + // tslint:disable-next-line:no-any + transform(value: any, type: string = 'html'): SafeHtml | SafeStyle | SafeScript | SafeUrl | SafeResourceUrl { + switch (type) { + case 'html': + return this.sanitizer.bypassSecurityTrustHtml(value); + case 'style': + return this.sanitizer.bypassSecurityTrustStyle(value); + case 'script': + return this.sanitizer.bypassSecurityTrustScript(value); + case 'url': + return this.sanitizer.bypassSecurityTrustUrl(value); + case 'resourceUrl': + return this.sanitizer.bypassSecurityTrustResourceUrl(value); + default: + throw new Error(`Invalid safe type specified: ${type}`); + } + } +} diff --git a/components/core/pipe/nz-some.pipe.spec.ts b/components/core/pipe/nz-some.pipe.spec.ts new file mode 100644 index 00000000000..194b8b6230b --- /dev/null +++ b/components/core/pipe/nz-some.pipe.spec.ts @@ -0,0 +1,29 @@ +import { NzSomePipe } from './nz-some.pipe'; + +describe('NzSomePipe', () => { + let pipe: NzSomePipe; + + // tslint:disable-next-line:no-any + const fn = (item: any) => { + return item === 2; + }; + + beforeEach(() => { + pipe = new NzSomePipe(); + }); + + it('Should return true', () => { + const array = [0, 1, 2, 3]; + + expect(pipe.transform(array, fn)).toEqual(true); + expect(array).toEqual([0, 1, 2, 3]); // Check integrity + }); + + it('Should return false', () => { + expect(pipe.transform([1, 3], fn)).toEqual(false); + }); + + // it('Should return the value unchanged', () => { + // expect(pipe.transform('a', null)).toEqual('a'); + // }); +}); diff --git a/components/core/pipe/nz-some.pipe.ts b/components/core/pipe/nz-some.pipe.ts new file mode 100644 index 00000000000..d97273d1635 --- /dev/null +++ b/components/core/pipe/nz-some.pipe.ts @@ -0,0 +1,28 @@ +/** + * @license + * Copyright Alibaba.com All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE + */ + +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({ + name: 'nzSome' +}) +export class NzSomePipe implements PipeTransform { + // tslint:disable-next-line:ban-types + transform(value: T, predicate: Function): T | boolean { + if (!Array.isArray(value) || !predicate) { + return value; + } + let [res, i] = [false, -1]; + + while (++i < value.length && !res) { + res = predicate(value[i], i, value); + } + + return res; + } +} diff --git a/components/core/pipe/nz-trim.pipe.spec.ts b/components/core/pipe/nz-trim.pipe.spec.ts new file mode 100644 index 00000000000..332588369b2 --- /dev/null +++ b/components/core/pipe/nz-trim.pipe.spec.ts @@ -0,0 +1,19 @@ +import { NzTrimPipe } from './nz-trim.pipe'; + +describe('NzTrimPipe', () => { + let pipe: NzTrimPipe; + + beforeEach(() => { + pipe = new NzTrimPipe(); + }); + + it('Should trim whitespace from string', () => { + const result = pipe.transform(' foo bar '); + expect(result).toEqual('foo bar'); + }); + + it('Should trim other characters from string', () => { + const result = pipe.transform('42foo bar4242', '42'); + expect(result).toEqual('foo bar'); + }); +}); diff --git a/components/core/pipe/nz-trim.pipe.ts b/components/core/pipe/nz-trim.pipe.ts new file mode 100644 index 00000000000..b4eb232205b --- /dev/null +++ b/components/core/pipe/nz-trim.pipe.ts @@ -0,0 +1,18 @@ +/** + * @license + * Copyright Alibaba.com All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE + */ + +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({ + name: 'nzTrim' +}) +export class NzTrimPipe implements PipeTransform { + transform(text: string, chars: string = '\\s'): string { + return typeof text === 'string' ? text.replace(new RegExp(`^[${chars}]+|[${chars}]+$`, 'g'), '') : text; + } +}