Files
pichunt/shared/utils/sluggy.ts
T

38 lines
793 B
TypeScript

import slugify from 'slugify';
export function sluggy(object: {
name?: string | null | undefined;
title?: string | null | undefined;
id: number | string;
}) {
const { id, name, title } = object;
let string = '';
if (!!name && name.length > 0) {
string =
slugify(name.substring(0, 25), {
replacement: '-',
lower: true,
strict: true,
trim: true
}) + '-';
} else if (!!title && title.length > 0) {
string =
slugify(title.substring(0, 25), {
replacement: '-',
lower: true,
strict: true,
trim: true
}) + '-';
}
const stringId = `${id}`;
// prevent '-' strings on weird Unicode names
if (string.length === 1) {
return stringId;
}
string += stringId;
return string;
}