mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
75
lib/core/pipes/file-size.pipe.spec.ts
Normal file
75
lib/core/pipes/file-size.pipe.spec.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
/*!
|
||||
* @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 { FileSizePipe } from './file-size.pipe';
|
||||
|
||||
describe('FileSizePipe', () => {
|
||||
|
||||
let pipe: FileSizePipe;
|
||||
|
||||
beforeEach(() => {
|
||||
pipe = new FileSizePipe();
|
||||
});
|
||||
|
||||
it('returns empty string with invalid input', () => {
|
||||
expect(pipe.transform(null)).toBe('');
|
||||
expect(pipe.transform(undefined)).toBe('');
|
||||
});
|
||||
|
||||
it('should convert value to Bytes', () => {
|
||||
expect(pipe.transform(0)).toBe('0 Bytes');
|
||||
expect(pipe.transform(1023)).toBe('1023 Bytes');
|
||||
});
|
||||
|
||||
it('should convert value to KB', () => {
|
||||
expect(pipe.transform(1024)).toBe('1 KB');
|
||||
expect(pipe.transform(1048575)).toBe('1024 KB');
|
||||
});
|
||||
|
||||
it('should convert value to MB', () => {
|
||||
expect(pipe.transform(1048576)).toBe('1 MB');
|
||||
expect(pipe.transform(1073741823)).toBe('1024 MB');
|
||||
});
|
||||
|
||||
it('should convert value to GB', () => {
|
||||
expect(pipe.transform(1073741824)).toBe('1 GB');
|
||||
expect(pipe.transform(1099511627775)).toBe('1024 GB');
|
||||
});
|
||||
|
||||
it('should convert value to TB and PB', () => {
|
||||
expect(pipe.transform(1099511627776)).toBe('1 TB');
|
||||
expect(pipe.transform(1125899906842623)).toBe('1 PB');
|
||||
});
|
||||
|
||||
it('should convert value with custom precision', () => {
|
||||
const tests = [
|
||||
{ size: 10, precision: 2, expectancy: '10 Bytes'},
|
||||
{ size: 1023, precision: 1, expectancy: '1023 Bytes'},
|
||||
{ size: 1025, precision: 2, expectancy: '1 KB'},
|
||||
{ size: 1499, precision: 0, expectancy: '1.46 KB'},
|
||||
{ size: 1999, precision: 0, expectancy: '1.95 KB'},
|
||||
{ size: 2000, precision: 2, expectancy: '1.95 KB'},
|
||||
{ size: 5000000, precision: 4, expectancy: '4.7684 MB'},
|
||||
{ size: 12345678901234, precision: 3, expectancy: '11.228 TB'}
|
||||
];
|
||||
|
||||
tests.forEach(({ size, precision, expectancy }) => {
|
||||
expect(pipe.transform(size, precision)).toBe(expectancy);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
42
lib/core/pipes/file-size.pipe.ts
Normal file
42
lib/core/pipes/file-size.pipe.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
/*!
|
||||
* @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: 'adfFileSize'
|
||||
})
|
||||
export class FileSizePipe implements PipeTransform {
|
||||
|
||||
transform(bytes: number, decimals: number = 2): string {
|
||||
if (bytes == null || bytes === undefined) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (bytes === 0) {
|
||||
return '0 Bytes';
|
||||
}
|
||||
|
||||
const k = 1024,
|
||||
dm = decimals || 2,
|
||||
sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
|
||||
i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
|
||||
}
|
||||
|
||||
}
|
18
lib/core/pipes/index.ts
Normal file
18
lib/core/pipes/index.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
/*!
|
||||
* @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.
|
||||
*/
|
||||
|
||||
export * from './public-api';
|
31
lib/core/pipes/mime-type-icon.pipe.ts
Normal file
31
lib/core/pipes/mime-type-icon.pipe.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
/*!
|
||||
* @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';
|
||||
import { ThumbnailService } from '../services/thumbnail.service';
|
||||
|
||||
@Pipe({
|
||||
name: 'adfMimeTypeIcon'
|
||||
})
|
||||
export class MimeTypeIconPipe implements PipeTransform {
|
||||
|
||||
constructor(private thumbnailService: ThumbnailService) { }
|
||||
|
||||
transform(text: string): string {
|
||||
return this.thumbnailService.getMimeTypeIcon(text);
|
||||
}
|
||||
}
|
145
lib/core/pipes/node-name-tooltip.pipe.spec.ts
Normal file
145
lib/core/pipes/node-name-tooltip.pipe.spec.ts
Normal file
@@ -0,0 +1,145 @@
|
||||
/*!
|
||||
* @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 { MinimalNodeEntity } from 'alfresco-js-api';
|
||||
import { NodeNameTooltipPipe } from './node-name-tooltip.pipe';
|
||||
|
||||
describe('NodeNameTooltipPipe', () => {
|
||||
|
||||
const nodeName = 'node-name';
|
||||
const nodeTitle = 'node-title';
|
||||
const nodeDescription = 'node-description';
|
||||
|
||||
let pipe: NodeNameTooltipPipe;
|
||||
|
||||
beforeEach(() => {
|
||||
pipe = new NodeNameTooltipPipe();
|
||||
});
|
||||
|
||||
it('should not transform when missing node', () => {
|
||||
expect(pipe.transform(null)).toBe(null);
|
||||
});
|
||||
|
||||
it('should not transform when missing node entry', () => {
|
||||
expect(pipe.transform(<any> {})).toBe(null);
|
||||
});
|
||||
|
||||
it('should use title and description when all fields present', () => {
|
||||
const node: any = {
|
||||
entry: {
|
||||
name: nodeName,
|
||||
properties: {
|
||||
'cm:title': nodeTitle,
|
||||
'cm:description': nodeDescription
|
||||
}
|
||||
}
|
||||
};
|
||||
let tooltip = pipe.transform(node);
|
||||
expect(tooltip).toBe(`${nodeTitle}\n${nodeDescription}`);
|
||||
});
|
||||
|
||||
it('should use name when other properties are missing', () => {
|
||||
const node = {
|
||||
entry: {
|
||||
name: nodeName
|
||||
}
|
||||
};
|
||||
let tooltip = pipe.transform(<MinimalNodeEntity> node);
|
||||
expect(tooltip).toBe(nodeName);
|
||||
});
|
||||
|
||||
it('should display name when title and description are missing', () => {
|
||||
const node: any = {
|
||||
entry: {
|
||||
name: nodeName,
|
||||
properties: {}
|
||||
}
|
||||
};
|
||||
let tooltip = pipe.transform(node);
|
||||
expect(tooltip).toBe(nodeName);
|
||||
});
|
||||
|
||||
it('should use name and description when title is missing', () => {
|
||||
const node: any = {
|
||||
entry: {
|
||||
name: nodeName,
|
||||
properties: {
|
||||
'cm:title': null,
|
||||
'cm:description': nodeDescription
|
||||
}
|
||||
}
|
||||
};
|
||||
let tooltip = pipe.transform(node);
|
||||
expect(tooltip).toBe(`${nodeName}\n${nodeDescription}`);
|
||||
});
|
||||
|
||||
it('should use name and title when description is missing', () => {
|
||||
const node: any = {
|
||||
entry: {
|
||||
name: nodeName,
|
||||
properties: {
|
||||
'cm:title': nodeTitle,
|
||||
'cm:description': null
|
||||
}
|
||||
}
|
||||
};
|
||||
let tooltip = pipe.transform(node);
|
||||
expect(tooltip).toBe(`${nodeName}\n${nodeTitle}`);
|
||||
});
|
||||
|
||||
it('should use name if name and description are the same', () => {
|
||||
const node: any = {
|
||||
entry: {
|
||||
name: nodeName,
|
||||
properties: {
|
||||
'cm:title': null,
|
||||
'cm:description': nodeName
|
||||
}
|
||||
}
|
||||
};
|
||||
let tooltip = pipe.transform(node);
|
||||
expect(tooltip).toBe(nodeName);
|
||||
});
|
||||
|
||||
it('should use name if name and title are the same', () => {
|
||||
const node: any = {
|
||||
entry: {
|
||||
name: nodeName,
|
||||
properties: {
|
||||
'cm:title': nodeName,
|
||||
'cm:description': null
|
||||
}
|
||||
}
|
||||
};
|
||||
let tooltip = pipe.transform(node);
|
||||
expect(tooltip).toBe(nodeName);
|
||||
});
|
||||
|
||||
it('should use name if all values are the same', () => {
|
||||
const node: any = {
|
||||
entry: {
|
||||
name: nodeName,
|
||||
properties: {
|
||||
'cm:title': nodeName,
|
||||
'cm:description': nodeName
|
||||
}
|
||||
}
|
||||
};
|
||||
let tooltip = pipe.transform(node);
|
||||
expect(tooltip).toBe(nodeName);
|
||||
});
|
||||
});
|
78
lib/core/pipes/node-name-tooltip.pipe.ts
Normal file
78
lib/core/pipes/node-name-tooltip.pipe.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
/*!
|
||||
* @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';
|
||||
import { MinimalNodeEntity } from 'alfresco-js-api';
|
||||
|
||||
@Pipe({
|
||||
name: 'adfNodeNameTooltip'
|
||||
})
|
||||
export class NodeNameTooltipPipe implements PipeTransform {
|
||||
|
||||
transform(node: MinimalNodeEntity): string {
|
||||
if (node) {
|
||||
return this.getNodeTooltip(node);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private containsLine(lines: string[], line: string): boolean {
|
||||
return lines.some((item: string) => {
|
||||
return item.toLowerCase() === line.toLowerCase();
|
||||
});
|
||||
}
|
||||
|
||||
private removeDuplicateLines(lines: string[]): string[] {
|
||||
const reducer = (acc: string[], line: string): string[] => {
|
||||
if (!this.containsLine(acc, line)) { acc.push(line); }
|
||||
return acc;
|
||||
};
|
||||
|
||||
return lines.reduce(reducer, []);
|
||||
}
|
||||
|
||||
private getNodeTooltip(node: MinimalNodeEntity): string {
|
||||
if (!node || !node.entry) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { entry: { properties, name } } = node;
|
||||
const lines = [ name ];
|
||||
|
||||
if (properties) {
|
||||
const {
|
||||
'cm:title': title,
|
||||
'cm:description': description
|
||||
} = properties;
|
||||
|
||||
if (title && description) {
|
||||
lines[0] = title;
|
||||
lines[1] = description;
|
||||
}
|
||||
|
||||
if (title) {
|
||||
lines[1] = title;
|
||||
}
|
||||
|
||||
if (description) {
|
||||
lines[1] = description;
|
||||
}
|
||||
}
|
||||
|
||||
return this.removeDuplicateLines(lines).join(`\n`);
|
||||
}
|
||||
}
|
58
lib/core/pipes/pipe.module.ts
Normal file
58
lib/core/pipes/pipe.module.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
/*!
|
||||
* @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 { CommonModule } from '@angular/common';
|
||||
import { NgModule } from '@angular/core';
|
||||
|
||||
import { FileSizePipe } from './file-size.pipe';
|
||||
import { MimeTypeIconPipe } from './mime-type-icon.pipe';
|
||||
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';
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
CommonModule
|
||||
],
|
||||
declarations: [
|
||||
FileSizePipe,
|
||||
HighlightPipe,
|
||||
TimeAgoPipe,
|
||||
MimeTypeIconPipe,
|
||||
InitialUsernamePipe,
|
||||
NodeNameTooltipPipe
|
||||
],
|
||||
providers: [
|
||||
FileSizePipe,
|
||||
HighlightPipe,
|
||||
TimeAgoPipe,
|
||||
MimeTypeIconPipe,
|
||||
InitialUsernamePipe,
|
||||
NodeNameTooltipPipe
|
||||
],
|
||||
exports: [
|
||||
FileSizePipe,
|
||||
HighlightPipe,
|
||||
TimeAgoPipe,
|
||||
MimeTypeIconPipe,
|
||||
InitialUsernamePipe,
|
||||
NodeNameTooltipPipe
|
||||
]
|
||||
})
|
||||
export class PipeModule {
|
||||
}
|
25
lib/core/pipes/public-api.ts
Normal file
25
lib/core/pipes/public-api.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
/*!
|
||||
* @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.
|
||||
*/
|
||||
|
||||
export * from './file-size.pipe';
|
||||
export * from './mime-type-icon.pipe';
|
||||
export * from './node-name-tooltip.pipe';
|
||||
export * from './text-highlight.pipe';
|
||||
export * from './time-ago.pipe';
|
||||
export * from './user-initial.pipe';
|
||||
|
||||
export * from './pipe.module';
|
32
lib/core/pipes/text-highlight.pipe.ts
Normal file
32
lib/core/pipes/text-highlight.pipe.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
/*!
|
||||
* @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';
|
||||
import { HighlightTransformService, HightlightTransformResult } from '../services/highlight-transform.service';
|
||||
|
||||
@Pipe({
|
||||
name: 'highlight'
|
||||
})
|
||||
export class HighlightPipe implements PipeTransform {
|
||||
|
||||
constructor(private highlightTransformService: HighlightTransformService) { }
|
||||
|
||||
transform(text: string, search: string): string {
|
||||
const result: HightlightTransformResult = this.highlightTransformService.highlight(text, search);
|
||||
return result.text;
|
||||
}
|
||||
}
|
42
lib/core/pipes/time-ago.pipe.spec.ts
Normal file
42
lib/core/pipes/time-ago.pipe.spec.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
/*!
|
||||
* @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 { TimeAgoPipe } from './time-ago.pipe';
|
||||
|
||||
describe('TimeAgoPipe', () => {
|
||||
|
||||
let pipe: TimeAgoPipe;
|
||||
|
||||
beforeEach(() => {
|
||||
pipe = new TimeAgoPipe();
|
||||
});
|
||||
|
||||
it('should return time difference for a given date', () => {
|
||||
let date = new Date();
|
||||
expect(pipe.transform(date)).toBe('a few seconds ago');
|
||||
});
|
||||
|
||||
it('should return exact date if given date is more than seven days ', () => {
|
||||
let date = new Date('1990-11-03T15:25:42.749');
|
||||
expect(pipe.transform(date)).toBe('03/11/1990 15:25');
|
||||
});
|
||||
|
||||
it('should return empty string if given date is empty', () => {
|
||||
expect(pipe.transform(null)).toBe('');
|
||||
expect(pipe.transform(undefined)).toBe('');
|
||||
});
|
||||
});
|
35
lib/core/pipes/time-ago.pipe.ts
Normal file
35
lib/core/pipes/time-ago.pipe.ts
Normal 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 * as moment from 'moment';
|
||||
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
|
||||
@Pipe({
|
||||
name: 'adfTimeAgo'
|
||||
})
|
||||
export class TimeAgoPipe implements PipeTransform {
|
||||
|
||||
transform(value: Date) {
|
||||
if (value !== null && value !== undefined ) {
|
||||
const then = moment(value);
|
||||
const diff = moment().diff(then, 'days');
|
||||
return diff > 7 ? then.format('DD/MM/YYYY HH:mm') : then.fromNow();
|
||||
}
|
||||
return '';
|
||||
}
|
||||
}
|
95
lib/core/pipes/user-initial.pipe.spec.ts
Normal file
95
lib/core/pipes/user-initial.pipe.spec.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
/*!
|
||||
* @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 { DomSanitizer } from '@angular/platform-browser';
|
||||
import { UserProcessModel } from '../models/user-process.model';
|
||||
import { InitialUsernamePipe } from './user-initial.pipe';
|
||||
|
||||
class FakeSanitazer extends DomSanitizer {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
sanitize(html) {
|
||||
return html;
|
||||
}
|
||||
|
||||
bypassSecurityTrustHtml(value: string): any {
|
||||
return value;
|
||||
}
|
||||
|
||||
bypassSecurityTrustStyle(value: string): any {
|
||||
return null;
|
||||
}
|
||||
|
||||
bypassSecurityTrustScript(value: string): any {
|
||||
return null;
|
||||
}
|
||||
|
||||
bypassSecurityTrustUrl(value: string): any {
|
||||
return null;
|
||||
}
|
||||
|
||||
bypassSecurityTrustResourceUrl(value: string): any {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
describe('UserInitialPipe', () => {
|
||||
|
||||
let pipe: InitialUsernamePipe;
|
||||
let fakeUser: UserProcessModel;
|
||||
|
||||
beforeEach(() => {
|
||||
pipe = new InitialUsernamePipe(new FakeSanitazer());
|
||||
fakeUser = new UserProcessModel();
|
||||
});
|
||||
|
||||
it('should return a div with the user initials', () => {
|
||||
fakeUser.firstName = 'FAKE-NAME';
|
||||
fakeUser.lastName = 'FAKE-SURNAME';
|
||||
let result = pipe.transform(fakeUser);
|
||||
expect(result).toBe('<div id="user-initials-image" class="">FF</div>');
|
||||
});
|
||||
|
||||
it('should apply the style class passed in input', () => {
|
||||
fakeUser.firstName = 'FAKE-NAME';
|
||||
fakeUser.lastName = 'FAKE-SURNAME';
|
||||
let result = pipe.transform(fakeUser, 'fake-class-to-check');
|
||||
expect(result).toBe('<div id="user-initials-image" class="fake-class-to-check">FF</div>');
|
||||
});
|
||||
|
||||
it('should return a single letter into div when lastname is undefined', () => {
|
||||
fakeUser.firstName = 'FAKE-NAME';
|
||||
fakeUser.lastName = undefined;
|
||||
let result = pipe.transform(fakeUser);
|
||||
expect(result).toBe('<div id="user-initials-image" class="">F</div>');
|
||||
});
|
||||
|
||||
it('should return a single letter into div when firstname is null', () => {
|
||||
fakeUser.firstName = undefined;
|
||||
fakeUser.lastName = 'FAKE-SURNAME';
|
||||
let result = pipe.transform(fakeUser);
|
||||
expect(result).toBe('<div id="user-initials-image" class="">F</div>');
|
||||
});
|
||||
|
||||
it('should return an empty string when user is null', () => {
|
||||
let result = pipe.transform(null);
|
||||
expect(result).toBe('');
|
||||
});
|
||||
});
|
43
lib/core/pipes/user-initial.pipe.ts
Normal file
43
lib/core/pipes/user-initial.pipe.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
/*!
|
||||
* @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';
|
||||
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
|
||||
import { UserProcessModel } from '../models/user-process.model';
|
||||
|
||||
@Pipe({
|
||||
name: 'usernameInitials'
|
||||
})
|
||||
export class InitialUsernamePipe implements PipeTransform {
|
||||
|
||||
constructor(private sanitized: DomSanitizer) {}
|
||||
|
||||
transform(user: UserProcessModel, className: string = '', delimiter: string = ''): SafeHtml {
|
||||
let result: SafeHtml = '';
|
||||
if (user) {
|
||||
let initialResult = this.getInitialUserName(user.firstName, user.lastName, delimiter);
|
||||
result = this.sanitized.bypassSecurityTrustHtml(`<div id="user-initials-image" class="${className}">${initialResult}</div>`);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
getInitialUserName(firstName: string, lastName: string, delimiter: string) {
|
||||
firstName = (firstName ? firstName[0] : '');
|
||||
lastName = (lastName ? lastName[0] : '');
|
||||
return firstName + delimiter + lastName;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user