first commit
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { LimitToPipe } from './limit-to.pipe';
|
||||
|
||||
describe('LimitToPipe', () => {
|
||||
it('create an instance', () => {
|
||||
const pipe = new LimitToPipe();
|
||||
expect(pipe).toBeTruthy();
|
||||
});
|
||||
|
||||
it('returns same value when length is shorter than specified', () => {
|
||||
const pipe = new LimitToPipe();
|
||||
const result = pipe.transform('some text', '20');
|
||||
expect(result).toBe('some text');
|
||||
});
|
||||
|
||||
it('returns limited value when length is longer than specified', () => {
|
||||
const pipe = new LimitToPipe();
|
||||
const result = pipe.transform('some text', '3');
|
||||
expect(result).toBe('som..');
|
||||
});
|
||||
|
||||
it('returns empty string when value is empty', () => {
|
||||
const pipe = new LimitToPipe();
|
||||
const result = pipe.transform('', '3');
|
||||
expect(result).toBe('');
|
||||
});
|
||||
|
||||
it('returns empty string when value is null', () => {
|
||||
const pipe = new LimitToPipe();
|
||||
const result = pipe.transform(null, '3');
|
||||
expect(result).toBe('');
|
||||
});
|
||||
|
||||
it('returns empty string when value is undefined', () => {
|
||||
const pipe = new LimitToPipe();
|
||||
const result = pipe.transform(undefined, '3');
|
||||
expect(result).toBe('');
|
||||
});
|
||||
|
||||
it('returns empty string when limit to is 0', () => {
|
||||
const pipe = new LimitToPipe();
|
||||
const result = pipe.transform(undefined, '0');
|
||||
expect(result).toBe('');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
|
||||
@Pipe({
|
||||
name: 'limitTo'
|
||||
})
|
||||
export class LimitToPipe implements PipeTransform {
|
||||
|
||||
transform(value: string, limitTo: string): string {
|
||||
|
||||
if (value === undefined || value === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const limit = limitTo ? parseInt(limitTo, 10) : 10;
|
||||
const trail = '..';
|
||||
|
||||
return value.length > limit ? value.substring(0, limit) + trail : value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { LocalDatePipe } from './local-date.pipe';
|
||||
|
||||
describe('LocalDatePipe', () => {
|
||||
it('create an instance', () => {
|
||||
const pipe = new LocalDatePipe();
|
||||
expect(pipe).toBeTruthy();
|
||||
});
|
||||
|
||||
it('returns valid date given utc', () => {
|
||||
const date = new Date('2018-05-04T08:17:57.8979116Z');
|
||||
|
||||
const pipe = new LocalDatePipe();
|
||||
const result = pipe.transform(date, 'DD MMM YYYY HH:mm');
|
||||
expect(result).toBe('04 May 2018 09:17');
|
||||
});
|
||||
|
||||
it('returns empty string when date is null', () => {
|
||||
const date = null;
|
||||
|
||||
const pipe = new LocalDatePipe();
|
||||
const result = pipe.transform(date, 'DD MMM YYYY HH:mm');
|
||||
expect(result).toBe('');
|
||||
});
|
||||
|
||||
it('returns empty string when format is null', () => {
|
||||
const date = new Date('2018-05-04T08:17:57.8979116Z');
|
||||
|
||||
const pipe = new LocalDatePipe();
|
||||
const result = pipe.transform(date, null);
|
||||
expect(result).toBe('');
|
||||
});
|
||||
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
import * as moment from 'moment';
|
||||
|
||||
@Pipe({
|
||||
name: 'localDate'
|
||||
})
|
||||
export class LocalDatePipe implements PipeTransform {
|
||||
|
||||
transform(value: Date, args: string): string {
|
||||
if (!value || !args) {
|
||||
return '';
|
||||
}
|
||||
return moment.utc(value).local().format(args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { YesNoPipe } from './yes-no.pipe';
|
||||
|
||||
describe('YesNoPipe', () => {
|
||||
it('create an instance', () => {
|
||||
const pipe = new YesNoPipe();
|
||||
expect(pipe).toBeTruthy();
|
||||
});
|
||||
|
||||
it('returns Yes given true', () => {
|
||||
const pipe = new YesNoPipe();
|
||||
|
||||
const result = pipe.transform(true);
|
||||
|
||||
expect(result).toBe('Yes');
|
||||
});
|
||||
|
||||
it('returns No given false', () => {
|
||||
const pipe = new YesNoPipe();
|
||||
|
||||
const result = pipe.transform(false);
|
||||
|
||||
expect(result).toBe('No');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
|
||||
@Pipe({
|
||||
name: 'yesNo'
|
||||
})
|
||||
export class YesNoPipe implements PipeTransform {
|
||||
|
||||
transform(value: boolean): string {
|
||||
return value ? 'Yes' : 'No';
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user