mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
987 B
42 lines
987 B
/*
|
|
* Squidex Headless CMS
|
|
*
|
|
* @license
|
|
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
|
|
*/
|
|
|
|
import { Pipe, PipeTransform } from '@angular/core';
|
|
import { StringHelper } from '@app/framework/internal';
|
|
|
|
@Pipe({
|
|
name: 'sqxDisplayName',
|
|
pure: true
|
|
})
|
|
export class DisplayNamePipe implements PipeTransform {
|
|
public transform(value: any, field1 = 'label', field2 = 'name'): any {
|
|
if (!value) {
|
|
return '';
|
|
}
|
|
|
|
return StringHelper.firstNonEmpty(this.valueOf(value, field1), this.valueOf(value, field2));
|
|
}
|
|
|
|
private valueOf(o: any, s: string): any {
|
|
s = s.replace(/\[(\w+)\]/g, '.$1');
|
|
s = s.replace(/^\./, '');
|
|
|
|
const parts = s.split('.');
|
|
|
|
for (let i = 0, n = parts.length; i < n; ++i) {
|
|
const k = parts[i];
|
|
|
|
if (k in o) {
|
|
o = o[k];
|
|
} else {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
return o;
|
|
}
|
|
}
|