Add fullName pipe and change example (#3946)

This commit is contained in:
Maurizio Vitale
2018-11-07 19:26:13 +00:00
committed by Eugenio Romano
parent c885f6d303
commit 0ee592607a
9 changed files with 96 additions and 4 deletions

View File

@@ -0,0 +1,48 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { FullNamePipe } from './full-name.pipe';
import { async } from '@angular/core/testing';
describe('FullNamePipe', () => {
let pipe: FullNamePipe;
beforeEach(async(() => {
pipe = new FullNamePipe();
}));
it('should return empty string when there is no name', () => {
const user = {};
expect(pipe.transform(user)).toBe('');
});
it('should return only firstName as fullName when there is no lastName ', () => {
const user = {firstName : 'Abc'};
expect(pipe.transform(user)).toBe('Abc');
});
it('should return only lastName as fullName when there is no firstName ', () => {
const user = {lastName : 'Xyz'};
expect(pipe.transform(user)).toBe('Xyz');
});
it('should return fullName when firstName and lastName are available', () => {
const user = {firstName : 'Abc', lastName : 'Xyz'};
expect(pipe.transform(user)).toBe('Abc Xyz');
});
});

View File

@@ -0,0 +1,35 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'fullName' })
export class FullNamePipe implements PipeTransform {
transform(user: any): string {
let fullName = '';
if (user) {
if (user.firstName) {
fullName += user.firstName;
}
if (user.lastName) {
fullName += fullName.length > 0 ? ' ' : '';
fullName += user.lastName;
}
}
return fullName;
}
}

View File

@@ -24,6 +24,7 @@ import { NodeNameTooltipPipe } from './node-name-tooltip.pipe';
import { HighlightPipe } from './text-highlight.pipe';
import { TimeAgoPipe } from './time-ago.pipe';
import { InitialUsernamePipe } from './user-initial.pipe';
import { FullNamePipe } from './full-name.pipe';
@NgModule({
imports: [
@@ -35,6 +36,7 @@ import { InitialUsernamePipe } from './user-initial.pipe';
TimeAgoPipe,
MimeTypeIconPipe,
InitialUsernamePipe,
FullNamePipe,
NodeNameTooltipPipe
],
providers: [
@@ -51,6 +53,7 @@ import { InitialUsernamePipe } from './user-initial.pipe';
TimeAgoPipe,
MimeTypeIconPipe,
InitialUsernamePipe,
FullNamePipe,
NodeNameTooltipPipe
]
})

View File

@@ -21,5 +21,6 @@ export * from './node-name-tooltip.pipe';
export * from './text-highlight.pipe';
export * from './time-ago.pipe';
export * from './user-initial.pipe';
export * from './full-name.pipe';
export * from './pipe.module';