catalog-react: humanizeEntity add defaultName
Signed-off-by: Vincenzo Scamporlino <vincenzos@spotify.com>
This commit is contained in:
@@ -215,9 +215,12 @@ describe('humanizeEntityRef', () => {
|
||||
describe('humanizeEntity', () => {
|
||||
it('gives a readable name when one is provided at metadata.title', () => {
|
||||
expect(
|
||||
humanizeEntity({
|
||||
metadata: { name: 'my-entity', title: 'My Title' },
|
||||
} as Entity),
|
||||
humanizeEntity(
|
||||
{
|
||||
metadata: { name: 'my-entity', title: 'My Title' },
|
||||
} as Entity,
|
||||
'default',
|
||||
),
|
||||
).toBe('My Title');
|
||||
});
|
||||
|
||||
@@ -257,13 +260,16 @@ describe('humanizeEntity', () => {
|
||||
])(
|
||||
'gives a readable name for kind %s when one is provided at spec.profile.displayName',
|
||||
(_, entity: Entity, expected) => {
|
||||
expect(humanizeEntity(entity)).toBe(expected);
|
||||
expect(humanizeEntity(entity, 'default')).toBe(expected);
|
||||
},
|
||||
);
|
||||
|
||||
it('should pass through to humanizeEntityRef when nothing matches', () => {
|
||||
expect(
|
||||
humanizeEntity({ kind: 'Group', metadata: { name: 'test' } } as Entity),
|
||||
).toBe('group:test');
|
||||
humanizeEntity(
|
||||
{ kind: 'Group', metadata: { name: 'test' } } as Entity,
|
||||
'default',
|
||||
),
|
||||
).toBe('default');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -25,7 +25,8 @@ import get from 'lodash/get';
|
||||
* @param defaultNamespace - if set to false then namespace is never omitted,
|
||||
* if set to string which matches namespace of entity then omitted
|
||||
*
|
||||
* @public */
|
||||
* @public
|
||||
**/
|
||||
export function humanizeEntityRef(
|
||||
entityRef: Entity | CompoundEntityRef,
|
||||
opts?: {
|
||||
@@ -73,27 +74,19 @@ export function humanizeEntityRef(
|
||||
* If an entity is either User or Group, this will be its `spec.profile.displayName`.
|
||||
* Otherwise, this is `metadata.title`.
|
||||
*
|
||||
* If neither of those are found or populated, fallback to `humanizeEntityRef`.
|
||||
* If neither of those are found or populated, fallback to `defaultName`.
|
||||
*
|
||||
* @param entity - Entity to convert.
|
||||
* @param opts - If entity readable name is not available, opts will be used to specify humanizeEntityRef options.
|
||||
* @returns Readable name, defaults to unique identifier.
|
||||
* @param defaultName - If entity readable name is not available, `defaultName` will be returned.
|
||||
* @returns Readable name, defaults to `defaultName`.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export function humanizeEntity(
|
||||
entity: Entity,
|
||||
opts?: {
|
||||
defaultKind?: string;
|
||||
defaultNamespace?: string | false;
|
||||
},
|
||||
) {
|
||||
export function humanizeEntity(entity: Entity, defaultName: string) {
|
||||
for (const path of ['spec.profile.displayName', 'metadata.title']) {
|
||||
const value = get(entity, path);
|
||||
if (value && typeof value === 'string') {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
return humanizeEntityRef(entity, opts);
|
||||
return defaultName;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user