diff --git a/components/core/pipe/index.ts b/components/core/pipe/index.ts new file mode 100644 index 00000000000..97717c1c837 --- /dev/null +++ b/components/core/pipe/index.ts @@ -0,0 +1,6 @@ +/** + * 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 + */ + +export * from './public-api'; diff --git a/components/core/pipe/nz-pipe.module.ts b/components/core/pipe/nz-pipe.module.ts new file mode 100644 index 00000000000..2ced3306c62 --- /dev/null +++ b/components/core/pipe/nz-pipe.module.ts @@ -0,0 +1,16 @@ +/** + * 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 { NzTimeRangePipe } from './time-range.pipe'; + +@NgModule({ + imports: [CommonModule], + exports: [NzTimeRangePipe], + declarations: [NzTimeRangePipe] +}) +export class NzPipesModule {} diff --git a/components/core/pipe/package.json b/components/core/pipe/package.json new file mode 100644 index 00000000000..ded1e7a9fdf --- /dev/null +++ b/components/core/pipe/package.json @@ -0,0 +1,7 @@ +{ + "ngPackage": { + "lib": { + "entryFile": "public-api.ts" + } + } +} diff --git a/components/core/pipe/public-api.ts b/components/core/pipe/public-api.ts new file mode 100644 index 00000000000..6dde7902397 --- /dev/null +++ b/components/core/pipe/public-api.ts @@ -0,0 +1,7 @@ +/** + * 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 + */ + +export * from './nz-pipe.module'; +export * from './time-range.pipe'; diff --git a/components/pipes/time-range.pipe.ts b/components/core/pipe/time-range.pipe.ts similarity index 99% rename from components/pipes/time-range.pipe.ts rename to components/core/pipe/time-range.pipe.ts index 8faf1f5b60c..1eea6ec4378 100644 --- a/components/pipes/time-range.pipe.ts +++ b/components/core/pipe/time-range.pipe.ts @@ -4,7 +4,6 @@ */ import { Pipe, PipeTransform } from '@angular/core'; - import { timeUnits } from 'ng-zorro-antd/core/time'; import { padStart } from 'ng-zorro-antd/core/util'; diff --git a/components/pipes/time-range.pipe.spec.ts b/components/core/pipe/time-range.spec.ts similarity index 96% rename from components/pipes/time-range.pipe.spec.ts rename to components/core/pipe/time-range.spec.ts index fb35989c40c..84efa6f1c7f 100644 --- a/components/pipes/time-range.pipe.spec.ts +++ b/components/core/pipe/time-range.spec.ts @@ -2,7 +2,7 @@ import { CommonModule } from '@angular/common'; import { Component } from '@angular/core'; import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { NzPipesModule } from 'ng-zorro-antd/pipes'; +import { NzPipesModule } from 'ng-zorro-antd/core/pipe'; @Component({ template: ` diff --git a/components/core/util/number.ts b/components/core/util/number.ts index 24458317465..7c3376f0350 100644 --- a/components/core/util/number.ts +++ b/components/core/util/number.ts @@ -2,6 +2,7 @@ * 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 { NzSafeAny } from 'ng-zorro-antd/core/types'; export function getPercent(min: number, max: number, value: number): number { return ((value - min) / (max - min)) * 100; @@ -23,8 +24,7 @@ export function ensureNumberInRange(num: number, min: number, max: number): numb } } -// tslint:disable-next-line:no-any -export function isNumberFinite(value: any): boolean { +export function isNumberFinite(value: NzSafeAny): boolean { return typeof value === 'number' && isFinite(value); } diff --git a/components/pipes/demo/math.md b/components/pipes/demo/aggregate.md similarity index 78% rename from components/pipes/demo/math.md rename to components/pipes/demo/aggregate.md index ed97a4d471a..5b60ddabe88 100644 --- a/components/pipes/demo/math.md +++ b/components/pipes/demo/aggregate.md @@ -1,8 +1,8 @@ --- order: 4 title: - zh-CN: nzMath - en-US: nzMath + zh-CN: nzAggregate + en-US: nzAggregate --- ## zh-CN diff --git a/components/pipes/demo/aggregate.ts b/components/pipes/demo/aggregate.ts new file mode 100644 index 00000000000..46d8a0d810f --- /dev/null +++ b/components/pipes/demo/aggregate.ts @@ -0,0 +1,22 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'nz-demo-pipes-aggregate', + template: ` + + + + + + + + + + + + + + + ` +}) +export class NzDemoPipesAggregateComponent {} diff --git a/components/pipes/demo/css-unit.ts b/components/pipes/demo/css-unit.ts index ed5d4fd2b31..0e61ad57f2b 100644 --- a/components/pipes/demo/css-unit.ts +++ b/components/pipes/demo/css-unit.ts @@ -3,11 +3,32 @@ import { Component } from '@angular/core'; @Component({ selector: 'nz-demo-pipes-css-unit', template: ` -
- + + +
+
Default
+
%
+
rem
-
-
- ` + `, + styles: [ + ` + .wrap { + display: flex; + } + .box { + margin-top: 20px; + margin-right: 20px; + text-align: center; + line-height: 50px; + color: #fff; + width: 50px; + height: 50px; + background: #4183c4; + } + ` + ] }) -export class NzDemoPipesCssUnitComponent {} +export class NzDemoPipesCssUnitComponent { + radiusValue = 0; +} diff --git a/components/pipes/demo/ellipsis.ts b/components/pipes/demo/ellipsis.ts index 9dc4c9e55ea..fb1e434d7fb 100644 --- a/components/pipes/demo/ellipsis.ts +++ b/components/pipes/demo/ellipsis.ts @@ -3,13 +3,18 @@ import { Component } from '@angular/core'; @Component({ selector: 'nz-demo-pipes-ellipsis', template: ` - {{ 'Hello World' | nzEllipsis: 4 }} +
- {{ 'Hello World' | nzEllipsis: 4:'':true }} -
- {{ 'Hello World' | nzEllipsis: 4:'...':true }} -
- {{ 'Hello World, how is it going?' | nzEllipsis: 14:'...':true }} - ` +

{{ str | nzEllipsis: 36:'...' }}

+ `, + styles: [ + ` + p { + padding: 8px 12px; + } + ` + ] }) -export class NzDemoPipesEllipsisComponent {} +export class NzDemoPipesEllipsisComponent { + str = 'Ant Design, a design language for background applications'; +} diff --git a/components/pipes/demo/math.ts b/components/pipes/demo/math.ts deleted file mode 100644 index 03ba5b72eb0..00000000000 --- a/components/pipes/demo/math.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'nz-demo-pipes-math', - template: ` - {{ [7, 8, 2, 3] | nzMath: 'min' }} -
- {{ [7, 8, 2, 3] | nzMath: 'max' }} -
- {{ [7, 8, 2, 3] | nzMath: 'sum' }} -
- {{ [7, 8, 2, 3] | nzMath: 'avg' }} -
- ` -}) -export class NzDemoPipesMathComponent {} diff --git a/components/pipes/demo/module b/components/pipes/demo/module index 3bf72e40703..eac35820234 100644 --- a/components/pipes/demo/module +++ b/components/pipes/demo/module @@ -1,3 +1,8 @@ +import { NzGridModule } from 'ng-zorro-antd/grid'; +import { NzInputModule } from 'ng-zorro-antd/input'; import { NzPipesModule } from 'ng-zorro-antd/pipes'; +import { NzSliderModule } from 'ng-zorro-antd/slider'; +import { NzStatisticModule } from 'ng-zorro-antd/statistic'; +import { NzTableModule } from 'ng-zorro-antd/table'; -export const moduleList = [ NzPipesModule ]; +export const moduleList = [ NzPipesModule, NzTableModule, NzSliderModule, NzInputModule, NzStatisticModule, NzGridModule ]; diff --git a/components/pipes/demo/safe-null.ts b/components/pipes/demo/safe-null.ts index 414bd987a96..1b35f8c4a80 100644 --- a/components/pipes/demo/safe-null.ts +++ b/components/pipes/demo/safe-null.ts @@ -3,10 +3,38 @@ import { Component } from '@angular/core'; @Component({ selector: 'nz-demo-pipes-safe-null', template: ` - + + + + Name + Age + Address + + + + + {{ data.name }} + {{ data.age }} + {{ data.address | nzSafeNull: '-' }} + + + ` }) -export class NzDemoPipesSafeNullComponent {} +export class NzDemoPipesSafeNullComponent { + listOfData = [ + { + name: 'John Brown', + age: 32 + }, + { + name: 'Jim Green', + age: 42 + }, + { + name: 'Joe Black', + age: 32, + address: 'Sidney No. 1 Lake Park' + } + ]; +} diff --git a/components/pipes/demo/sanitizer.md b/components/pipes/demo/sanitizer.md index cbfe5364939..a61d17a6194 100644 --- a/components/pipes/demo/sanitizer.md +++ b/components/pipes/demo/sanitizer.md @@ -9,6 +9,21 @@ title: DomSanitizer 的 Pipe 实现 +```html +
+
+ + +``` + ## en-US Pipe implementation of DomSanitizer + + +```html +
+
+ + +``` \ No newline at end of file diff --git a/components/pipes/demo/sanitizer.ts b/components/pipes/demo/sanitizer.ts index 237c9b838e2..cbf5027fad0 100644 --- a/components/pipes/demo/sanitizer.ts +++ b/components/pipes/demo/sanitizer.ts @@ -3,21 +3,9 @@ import { Component } from '@angular/core'; @Component({ selector: 'nz-demo-pipes-sanitizer', template: ` -
-
-
我不会报警
-
-
-
-
- -
+
` }) export class NzDemoPipesSanitizerComponent { - htmlSnippet: string = '

hello world

'; - scriptSnippet: string = ''; - styleSnippet: string = "{'height': '20px'}"; - urlSnippet: string = 'https://ng.ant.design/assets/img/logo.svg'; - resourceUrlSnippet: string = 'https://ng.ant.design/docs/introduce/zh'; + html = `I am innerHTML`; } diff --git a/components/pipes/demo/some.md b/components/pipes/demo/some.md deleted file mode 100644 index ea6bba8d19f..00000000000 --- a/components/pipes/demo/some.md +++ /dev/null @@ -1,14 +0,0 @@ ---- -order: 7 -title: - zh-CN: nzSome - en-US: nzSome ---- - -## zh-CN - -集合中的至少一项通过,则返回true。 - -## en-US - -Returns true if at least one of the item in the collections pass the method. \ No newline at end of file diff --git a/components/pipes/demo/some.ts b/components/pipes/demo/some.ts deleted file mode 100644 index 3f1eb40da82..00000000000 --- a/components/pipes/demo/some.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'nz-demo-pipes-some', - template: ` - {{ [5, 6, 7, 8, 9] | nzSome: method }} -
- {{ [6, 7, 8, 9, 10] | nzSome: method }} -
- ` -}) -export class NzDemoPipesSomeComponent { - method = (item: number) => { - return item === 5; - }; -} diff --git a/components/pipes/demo/time-range.md b/components/pipes/demo/time-range.md deleted file mode 100644 index 8b4a63c06ed..00000000000 --- a/components/pipes/demo/time-range.md +++ /dev/null @@ -1,14 +0,0 @@ ---- -order: 9 -title: - zh-CN: nzTimeRange - en-US: nzTimeRange ---- - -## zh-CN - -按自定义格式返回间隔时间 - -## en-US - -Return interval time in custom format \ No newline at end of file diff --git a/components/pipes/demo/time-range.ts b/components/pipes/demo/time-range.ts deleted file mode 100644 index e1c0285d70d..00000000000 --- a/components/pipes/demo/time-range.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'nz-demo-pipes-time-range', - template: ` - {{ diff | nzTimeRange: format1 }} -
- {{ diff | nzTimeRange: format2 }} -
- {{ diff | nzTimeRange: format3 }} -
- {{ diff1 | nzTimeRange }} -
- ` -}) -export class NzDemoPipesTimeRangeComponent { - diff = 1000 * 60 * 60 * 24 * 2 + 1000 * 30; - diff1 = 0; - format1 = 'HH:mm:ss'; - format2 = 'HH:mm'; - format3 = 'D 天 H 时 m 分 s 秒'; -} diff --git a/components/pipes/demo/trim.md b/components/pipes/demo/trim.md index 6c5f9e6e97e..eb9039e508f 100644 --- a/components/pipes/demo/trim.md +++ b/components/pipes/demo/trim.md @@ -7,8 +7,8 @@ title: ## zh-CN -去除字符串左右指定字符 +去除字符串左右空字符串 ## en-US -Strip left and right string \ No newline at end of file +Strip left and right empty string \ No newline at end of file diff --git a/components/pipes/demo/trim.ts b/components/pipes/demo/trim.ts index 65f6b4f5e09..a8faa2dc3eb 100644 --- a/components/pipes/demo/trim.ts +++ b/components/pipes/demo/trim.ts @@ -3,12 +3,27 @@ import { Component } from '@angular/core'; @Component({ selector: 'nz-demo-pipes-trim', template: ` - {{ 'hello' | nzTrim }} +
- {{ 'hello ' | nzTrim }} -
- {{ ' hello ' | nzTrim }} -
- ` +
+
{{ str }}
+
+
+
{{ str | nzTrim }}
+
+ `, + styles: [ + ` + div { + padding: 8px 12px; + } + pre { + display: inline-block; + background: #eee; + } + ` + ] }) -export class NzDemoPipesTrimComponent {} +export class NzDemoPipesTrimComponent { + str = ' Ant Design '; +} diff --git a/components/pipes/doc/index.en-US.md b/components/pipes/doc/index.en-US.md index 36d6e47dc1c..656a77fd8b7 100755 --- a/components/pipes/doc/index.en-US.md +++ b/components/pipes/doc/index.en-US.md @@ -1,7 +1,7 @@ --- category: Components type: General -title: Pipe +title: Pipes cols: 1 experimental: true --- @@ -14,23 +14,19 @@ Common Pipe Collections in Projects - After introducing Pipe, use it like angular's default Pipe ```ts -import { NzPipesModule } from 'ng-zorro-antd/pipe'; +import { NzPipesModule } from 'ng-zorro-antd/pipes'; ``` ## API -```html -
{{ value | nzSafeNull : '-' }}
-``` - -### nz-safe-null +### __nz-safe-null__ | Property | Description | Type | Default | | -------- | ----------- | ---- | ------- | | `replace` | Replace character | `string` | '' | -### nz-bytes +### __nz-bytes__ | Property | Description | Type | Default | | -------- | ----------- | ---- | ------- | @@ -38,48 +34,28 @@ import { NzPipesModule } from 'ng-zorro-antd/pipe'; | `from` | Unit of current value | `string` | 'B' | | `to` | Units converted to target value | `string` | '' | -### nz-css-unit +### __nz-css-unit__ | Property | Description | Type | Default | | -------- | ----------- | ---- | ------- | | `defaultUnit` | Default Unit | `string` | 'px' | -### nz-ellipsis +### __nz-ellipsis__ | Property | Description | Type | Default | | -------- | ----------- | ---- | ------- | | `length` | Truncate length | `number` | '' | | `suffix` | Replace character | `string` | '' | -| `preserve` | Whether to filter the length of spaces | `boolean` | false | -### nz-math +### __nz-aggregate__ | Property | Description | Type | Default | | -------- | ----------- | ---- | ------- | -| `method` | Aggregation | `string` | '' | +| `type` | Aggregation | `'sum' \| 'max' \| 'min' \| 'avg'` | '' | -### nz-sanitizer +### __nz-sanitizer__ | Property | Description | Type | Default | | -------- | ----------- | ---- | ------- | | `type` | sanitizer type | `string` | 'html' | - - -### nz-some - -| Property | Description | Type | Default | -| -------- | ----------- | ---- | ------- | -| `predicate` | function | `object` | '' | - -### nz-trim - -| Property | Description | Type | Default | -| -------- | ----------- | ---- | ------- | -| `chars` | Regular expression | `string` | '\\s' | - -### nz-time-range - -| Property | Description | Type | Default | -| -------- | ----------- | ---- | ------- | -| `format` | format | `string` | 'HH:mm:ss' | diff --git a/components/pipes/doc/index.zh-CN.md b/components/pipes/doc/index.zh-CN.md index 158340d0e31..58733c8b446 100755 --- a/components/pipes/doc/index.zh-CN.md +++ b/components/pipes/doc/index.zh-CN.md @@ -1,7 +1,7 @@ --- category: Components type: 通用 -title: Pipe +title: Pipes cols: 1 experimental: true --- @@ -14,22 +14,18 @@ experimental: true - 引入 Pipe 后,像 angular 的默认 Pipe 一样使用 ```ts -import { NzPipesModule } from 'ng-zorro-antd/pipe'; +import { NzPipesModule } from 'ng-zorro-antd/pipes'; ``` ## API -```html -
{{ value | nzSafeNull }}
-``` - -### nz-safe-null +### __nz-safe-null__ | 参数 | 说明 | 类型 | 默认值 | | --- | --- | --- | --- | | `replace` | 替换字符 | `string` | '' | -### nz-bytes +### __nz-bytes__ | 参数 | 说明 | 类型 | 默认值 | | --- | --- | --- | --- | @@ -37,48 +33,29 @@ import { NzPipesModule } from 'ng-zorro-antd/pipe'; | `from` | 当前值的单位 | `string` | 'B' | | `to` | 转换到目标值的单位 | `string` | '' | -### nz-css-unit +### __nz-css-unit__ | 参数 | 说明 | 类型 | 默认值 | | --- | --- | --- | --- | | `defaultUnit` | 默认单位 | `string` | 'px' | -### nz-ellipsis +### __nz-ellipsis__ | 参数 | 说明 | 类型 | 默认值 | | --- | --- | --- | --- | | `length` | 截取长度 | `number` | '' | | `suffix` | 替换字符 | `string` | '' | -| `preserve` | 是否过滤空格的长度 | `boolean` | false | - - -### nz-math - -| 参数 | 说明 | 类型 | 默认值 | -| --- | --- | --- | --- | -| `method` | 聚合方式 | `string` | '' | -### nz-sanitizer -| 参数 | 说明 | 类型 | 默认值 | -| --- | --- | --- | --- | -| `type` | sanitizer类型 | `string` | 'html' | - - -### nz-some +### __nz-aggregate__ | 参数 | 说明 | 类型 | 默认值 | | --- | --- | --- | --- | -| `predicate` | 函数 | `object` | '' | +| `method` | 聚合方式 | `'sum' \| 'max' \| 'min' \| 'avg'` | '' | -### nz-trim +### __nz-sanitizer__ | 参数 | 说明 | 类型 | 默认值 | | --- | --- | --- | --- | -| `chars` | 正则表达式 | `string` | '\\s' | +| `type` | sanitizer 类型 | `string` | 'html' | -### nz-time-range - -| 参数 | 说明 | 类型 | 默认值 | -| --- | --- | --- | --- | -| `format` | 格式 | `string` | 'HH:mm:ss' | diff --git a/components/pipes/nz-aggregate.pipe.spec.ts b/components/pipes/nz-aggregate.pipe.spec.ts new file mode 100644 index 00000000000..5d5eea8b8ac --- /dev/null +++ b/components/pipes/nz-aggregate.pipe.spec.ts @@ -0,0 +1,72 @@ +import { NzAggregatePipe } from './nz-aggregate.pipe'; + +describe('NzAggregatePipe', () => { + let nzAggregatePipe: NzAggregatePipe; + + beforeEach(() => { + nzAggregatePipe = new NzAggregatePipe(); + }); + it('Should return 4', () => { + expect(nzAggregatePipe.transform([1, 2, 3, 4], 'max')).toEqual(4); + }); + + it('Should return 1', () => { + expect(nzAggregatePipe.transform([1], 'max')).toEqual(1); + }); + + it('Should return 1', () => { + expect(nzAggregatePipe.transform([1, 1], 'max')).toEqual(1); + }); + + it('Should return undefined', () => { + expect(nzAggregatePipe.transform([], 'max')).toBeUndefined(); + }); + + it('Should return 1', () => { + expect(nzAggregatePipe.transform([1, 2, 3, 4], 'min')).toEqual(1); + }); + + it('Should return 2', () => { + expect(nzAggregatePipe.transform([4, 3, 2, 5], 'min')).toEqual(2); + }); + + it('Should return 1', () => { + expect(nzAggregatePipe.transform([1], 'min')).toEqual(1); + }); + + it('Should return 1', () => { + expect(nzAggregatePipe.transform([1, 1], 'min')).toEqual(1); + }); + + it('Should return undefined', () => { + expect(nzAggregatePipe.transform([], 'min')).toBeUndefined(); + }); + + it('Should return 10', () => { + expect(nzAggregatePipe.transform([1, 2, 3, 4], 'sum')).toEqual(10); + }); + + it('Should return 1', () => { + expect(nzAggregatePipe.transform([1], 'sum')).toEqual(1); + }); + + it('Should return 2', () => { + expect(nzAggregatePipe.transform([1, 1], 'sum')).toEqual(2); + }); + + it('Should return 2.5', () => { + expect(nzAggregatePipe.transform([1, 2, 3, 4], 'avg')).toEqual(2.5); + }); + + it('Should return 1', () => { + expect(nzAggregatePipe.transform([1], 'avg')).toEqual(1); + }); + + it('Should return 1', () => { + expect(nzAggregatePipe.transform([1, 1], 'avg')).toEqual(1); + }); + + it('Should return undefined', () => { + expect(nzAggregatePipe.transform([], 'avg')).toBeUndefined(); + }); +}); diff --git a/components/pipes/nz-aggregate.pipe.ts b/components/pipes/nz-aggregate.pipe.ts new file mode 100644 index 00000000000..5a9ed414a86 --- /dev/null +++ b/components/pipes/nz-aggregate.pipe.ts @@ -0,0 +1,37 @@ +/** + * 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/core/util'; + +export type AggregateMethod = 'sum' | 'max' | 'min' | 'avg'; + +@Pipe({ + name: 'nzAggregate' +}) +export class NzAggregatePipe implements PipeTransform { + transform(value: number[], method: AggregateMethod): undefined | number { + if (!Array.isArray(value)) { + return value; + } + + if (value.length === 0) { + return undefined; + } + + switch (method) { + case 'sum': + return sum(value); + case 'avg': + return sum(value) / value.length; + case 'max': + return Math.max(...value); + case 'min': + return Math.min(...value); + default: + throw Error(`Invalid Pipe Arguments: Aggregate pipe doesn't support this type`); + } + } +} diff --git a/components/pipes/nz-ellipsis.pipe.spec.ts b/components/pipes/nz-ellipsis.pipe.spec.ts index 0aa5d4092c0..020f0aa38a0 100644 --- a/components/pipes/nz-ellipsis.pipe.spec.ts +++ b/components/pipes/nz-ellipsis.pipe.spec.ts @@ -8,23 +8,7 @@ describe('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'); + expect(pipe.transform('Hello World', 4, '')).toEqual('Hell'); }); it('Should return the input', () => { diff --git a/components/pipes/nz-ellipsis.pipe.ts b/components/pipes/nz-ellipsis.pipe.ts index 7eb4c0fa70d..14c02cfd76f 100644 --- a/components/pipes/nz-ellipsis.pipe.ts +++ b/components/pipes/nz-ellipsis.pipe.ts @@ -10,7 +10,7 @@ import { NzSafeAny } from 'ng-zorro-antd/core/types'; name: 'nzEllipsis' }) export class NzEllipsisPipe implements PipeTransform { - transform(value: NzSafeAny, length?: number, suffix: string = '', preserve: boolean = false): NzSafeAny { + transform(value: NzSafeAny, length?: number, suffix: string = ''): NzSafeAny { if (typeof value !== 'string') { return value; } @@ -21,16 +21,6 @@ export class NzEllipsisPipe implements PipeTransform { 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; + return value.substring(0, len) + suffix; } } diff --git a/components/pipes/nz-math.pipe.spec.ts b/components/pipes/nz-math.pipe.spec.ts deleted file mode 100644 index 282962e601a..00000000000 --- a/components/pipes/nz-math.pipe.spec.ts +++ /dev/null @@ -1,72 +0,0 @@ -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 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 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 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 undefined', () => { - expect(nzMathPipe.transform([], EMathMethod.AVG)).toBeUndefined(); - }); -}); diff --git a/components/pipes/nz-math.pipe.ts b/components/pipes/nz-math.pipe.ts deleted file mode 100644 index e5da623d980..00000000000 --- a/components/pipes/nz-math.pipe.ts +++ /dev/null @@ -1,40 +0,0 @@ -/** - * 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/core/util'; - -export enum EMathMethod { - SUM = 'sum', - MAX = 'max', - MIN = 'min', - AVG = 'avg' -} - -@Pipe({ - name: 'nzMath' -}) -export class NzMathPipe implements PipeTransform { - private getMethodResult(data: number[], method: string): number { - return method === EMathMethod.MIN ? Math.min(...data) : Math.max(...data); - } - transform(value: number[], method: string): 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/pipes/nz-pipes.module.ts b/components/pipes/nz-pipes.module.ts index 48ae3d03383..a5429fd1051 100644 --- a/components/pipes/nz-pipes.module.ts +++ b/components/pipes/nz-pipes.module.ts @@ -6,27 +6,15 @@ import { CommonModule } from '@angular/common'; import { NgModule } from '@angular/core'; +import { NzAggregatePipe } from './nz-aggregate.pipe'; 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'; -const pipes = [ - NzTimeRangePipe, - NzToCssUnitPipe, - NzSafeNullPipe, - NzSanitizerPipe, - NzTrimPipe, - NzBytesPipe, - NzSomePipe, - NzMathPipe, - NzEllipsisPipe -]; +const pipes = [NzToCssUnitPipe, NzSafeNullPipe, NzSanitizerPipe, NzTrimPipe, NzBytesPipe, NzAggregatePipe, NzEllipsisPipe]; @NgModule({ imports: [CommonModule], diff --git a/components/pipes/nz-sanitizer.pipe.spec.ts b/components/pipes/nz-sanitizer.pipe.spec.ts index b251a4c0735..634ce2455ec 100644 --- a/components/pipes/nz-sanitizer.pipe.spec.ts +++ b/components/pipes/nz-sanitizer.pipe.spec.ts @@ -4,7 +4,6 @@ import { NzSanitizerPipe } from './nz-sanitizer.pipe'; describe('NzSanitizerPipe', () => { const htmlSnippet = '

hello world

'; - const scriptSnippet = ""; const styleSnippet = 'height:50px;background-color: red'; const urlSnippet = 'https://img.alicdn.com/tfs/TB1Ly5oS3HqK1RjSZFPXXcwapXa-238-54.png'; const responseUrlSnippet = 'https://www.aliyun.com/'; @@ -24,11 +23,6 @@ describe('NzSanitizerPipe', () => { expect(pipe.transform(htmlSnippet, 'html')).toBeTruthy(); })); - it('Should sanitizer but type is script', inject([DomSanitizer], (domSanitizer: DomSanitizer) => { - const pipe: NzSanitizerPipe = new NzSanitizerPipe(domSanitizer); - expect(pipe.transform(scriptSnippet, 'script')).toBeTruthy(); - })); - it('Should sanitizer but type is style', inject([DomSanitizer], (domSanitizer: DomSanitizer) => { const pipe: NzSanitizerPipe = new NzSanitizerPipe(domSanitizer); expect(pipe.transform(styleSnippet, 'style')).toBeTruthy(); @@ -43,9 +37,4 @@ describe('NzSanitizerPipe', () => { const pipe: NzSanitizerPipe = new NzSanitizerPipe(domSanitizer); expect(pipe.transform(responseUrlSnippet, 'resourceUrl')).toBeTruthy(); })); - - // it('Should sanitizer but type is other', inject([DomSanitizer], (domSanitizer: DomSanitizer) => { - // const pipe: NzSanitizerPipe = new NzSanitizerPipe(domSanitizer); - // expect(pipe.transform(htmlSnippet, 'html2')).toBeFalsy(); - // })); }); diff --git a/components/pipes/nz-sanitizer.pipe.ts b/components/pipes/nz-sanitizer.pipe.ts index 62e6df80fbf..b8411f68a9a 100644 --- a/components/pipes/nz-sanitizer.pipe.ts +++ b/components/pipes/nz-sanitizer.pipe.ts @@ -4,22 +4,26 @@ */ import { Pipe, PipeTransform } from '@angular/core'; -import { DomSanitizer, SafeHtml, SafeResourceUrl, SafeScript, SafeStyle, SafeUrl } from '@angular/platform-browser'; +import { DomSanitizer, SafeHtml, SafeResourceUrl, SafeStyle, SafeUrl } from '@angular/platform-browser'; import { NzSafeAny } from 'ng-zorro-antd/core/types'; +type DomSanitizerType = 'html' | 'style' | 'url' | 'resourceUrl'; + @Pipe({ name: 'nzSanitizer' }) export class NzSanitizerPipe implements PipeTransform { constructor(protected sanitizer: DomSanitizer) {} - transform(value: NzSafeAny, type: string = 'html'): SafeHtml | SafeStyle | SafeScript | SafeUrl | SafeResourceUrl { + transform(value: NzSafeAny, type: 'html'): SafeHtml; + transform(value: NzSafeAny, type: 'style'): SafeStyle; + transform(value: NzSafeAny, type: 'url'): SafeUrl; + transform(value: NzSafeAny, type: 'resourceUrl'): SafeResourceUrl; + transform(value: NzSafeAny, type: DomSanitizerType = 'html'): SafeHtml | SafeStyle | 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': diff --git a/components/pipes/nz-some.pipe.spec.ts b/components/pipes/nz-some.pipe.spec.ts deleted file mode 100644 index 8631c047767..00000000000 --- a/components/pipes/nz-some.pipe.spec.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { NzSafeAny } from 'ng-zorro-antd/core/types'; -import { NzSomePipe } from './nz-some.pipe'; - -describe('NzSomePipe', () => { - let pipe: NzSomePipe; - - const fn = (item: NzSafeAny) => { - 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); - }); -}); diff --git a/components/pipes/nz-some.pipe.ts b/components/pipes/nz-some.pipe.ts deleted file mode 100644 index 57fb7226c26..00000000000 --- a/components/pipes/nz-some.pipe.ts +++ /dev/null @@ -1,25 +0,0 @@ -/** - * 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/pipes/nz-trim.pipe.spec.ts b/components/pipes/nz-trim.pipe.spec.ts index 332588369b2..a6f9e7d5f19 100644 --- a/components/pipes/nz-trim.pipe.spec.ts +++ b/components/pipes/nz-trim.pipe.spec.ts @@ -11,9 +11,4 @@ describe('NzTrimPipe', () => { 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/pipes/nz-trim.pipe.ts b/components/pipes/nz-trim.pipe.ts index dc4b8d0c32a..df3a30f57d6 100644 --- a/components/pipes/nz-trim.pipe.ts +++ b/components/pipes/nz-trim.pipe.ts @@ -9,7 +9,8 @@ import { Pipe, PipeTransform } from '@angular/core'; 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; + // TODO(chensimeng) trimEnd, trimStart + transform(text: string): string { + return text.trim(); } } diff --git a/components/pipes/public-api.ts b/components/pipes/public-api.ts index 85d2ce8146a..f84935f5060 100644 --- a/components/pipes/public-api.ts +++ b/components/pipes/public-api.ts @@ -4,12 +4,10 @@ */ export * from './nz-pipes.module'; -export * from './time-range.pipe'; export * from './nz-css-unit.pipe'; export * from './nz-bytes.pipe'; export * from './nz-ellipsis.pipe'; -export * from './nz-math.pipe'; +export * from './nz-aggregate.pipe'; export * from './nz-safe-null.pipe'; export * from './nz-sanitizer.pipe'; -export * from './nz-some.pipe'; export * from './nz-trim.pipe'; diff --git a/components/statistic/statistic.module.ts b/components/statistic/statistic.module.ts index 6bbd44bf262..1bdfb9e3725 100644 --- a/components/statistic/statistic.module.ts +++ b/components/statistic/statistic.module.ts @@ -7,14 +7,14 @@ import { CommonModule } from '@angular/common'; import { NgModule } from '@angular/core'; import { NzOutletModule } from 'ng-zorro-antd/core/outlet'; -import { NzPipesModule } from 'ng-zorro-antd/pipes'; +import { NzPipesModule as NzPipesModuleFormCore } from 'ng-zorro-antd/core/pipe'; import { NzCountdownComponent } from './countdown.component'; import { NzStatisticNumberComponent } from './statistic-number.component'; import { NzStatisticComponent } from './statistic.component'; @NgModule({ - imports: [CommonModule, PlatformModule, NzOutletModule, NzPipesModule], + imports: [CommonModule, PlatformModule, NzOutletModule, NzPipesModuleFormCore], declarations: [NzStatisticComponent, NzCountdownComponent, NzStatisticNumberComponent], exports: [NzStatisticComponent, NzCountdownComponent, NzStatisticNumberComponent] })