Skip to content

Commit

Permalink
feat(module:core): add pipe module
Browse files Browse the repository at this point in the history
  • Loading branch information
chensimeng authored and 思熳 committed Sep 16, 2020
1 parent a39edb7 commit ee60ab8
Show file tree
Hide file tree
Showing 16 changed files with 694 additions and 0 deletions.
137 changes: 137 additions & 0 deletions components/core/pipe/nz-bytes.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
68 changes: 68 additions & 0 deletions components/core/pipe/nz-bytes.pipe.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
33 changes: 33 additions & 0 deletions components/core/pipe/nz-css-unit.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
// });
});
33 changes: 33 additions & 0 deletions components/core/pipe/nz-ellipsis.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
39 changes: 39 additions & 0 deletions components/core/pipe/nz-ellipsis.pipe.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit ee60ab8

Please sign in to comment.