Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(module:core): add pipe module #4206

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions components/core/pipe/humanize/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @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 { Type, ɵstringify as stringify } from '@angular/core';

// tslint:disable-next-line no-any
export function InvalidPipeArgumentError(type: Type<any>, value: {}): Error {
return Error(`InvalidPipeArgument: '${value}' for pipe '${stringify(type)}'`);
}
29 changes: 29 additions & 0 deletions components/core/pipe/humanize/humanize-bytes.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @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 { kbn } from './kbn';

import { InvalidPipeArgumentError } from './errors';

@Pipe({
name: 'nzHumanizeBytes'
})
export class NzHumanizeBytesPipe implements PipeTransform {
transform(value: number | string, decimals: number = 2, scaledDecimals: number | null = null): string {
if (value == null) {
return '';
}

try {
return kbn.bytes(+value, decimals, scaledDecimals);
} catch (err) {
throw InvalidPipeArgumentError(NzHumanizeBytesPipe, err.message);
}
}
}
22 changes: 22 additions & 0 deletions components/core/pipe/humanize/humanize-check-null.pipe.ts
Original file line number Diff line number Diff line change
@@ -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';

@Pipe({
name: 'nzHumanizeCheckNull'
})
export class NzHumanizeCheckNullPipe implements PipeTransform {
transform(value: number | string, nullReplacement: string = '-'): string | number | null {
if (value == null) {
return nullReplacement;
}

return value;
}
}
18 changes: 18 additions & 0 deletions components/core/pipe/humanize/humanize-duration.pipe.ts
Original file line number Diff line number Diff line change
@@ -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: 'nzHumanizeDuration'
})
export class NzHumanizeDurationPipe implements PipeTransform {
transform(): string {
return '';
}
}
23 changes: 23 additions & 0 deletions components/core/pipe/humanize/humanize.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @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 { NzHumanizeBytesPipe } from './humanize-bytes.pipe';
import { NzHumanizeCheckNullPipe } from './humanize-check-null.pipe';
import { NzHumanizeDurationPipe } from './humanize-duration.pipe';

const pipes = [NzHumanizeBytesPipe, NzHumanizeCheckNullPipe, NzHumanizeDurationPipe];

@NgModule({
imports: [CommonModule],
declarations: [...pipes],
exports: [...pipes]
})
export class NzHumanizeModule {}
Loading