Add stuff
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
import { checkSlug, parseSlug, type SlugRecord } from './slugcheck';
|
||||
|
||||
describe('slugcheck', () => {
|
||||
it('parseSlug', () => {
|
||||
expect(parseSlug('')).toBeNull();
|
||||
expect(parseSlug('aaaa-333')).toMatchObject({ id: 333 });
|
||||
expect(parseSlug('aaaa-333')).toMatchObject({ id: 333, name: 'Aaaa' });
|
||||
expect(parseSlug('my-favorite-record-34234')).toMatchObject({
|
||||
id: 34234,
|
||||
name: 'My Favorite Record'
|
||||
});
|
||||
expect(parseSlug('33')).toMatchObject({ id: 33 });
|
||||
});
|
||||
it('checkSlug success', () => {
|
||||
const params = {
|
||||
nameSlug: 'my-favorite-record-34234',
|
||||
teamSlug: 'best_team-43',
|
||||
article: '33',
|
||||
userSlug: '44'
|
||||
};
|
||||
const slugs = checkSlug(params);
|
||||
expect(slugs).not.toBeNull();
|
||||
expect(slugs).toHaveProperty('nameSlug');
|
||||
|
||||
const s = slugs as SlugRecord;
|
||||
expect(s.nameSlug).toBeObject();
|
||||
expect(s.nameSlug?.name).toEqual('My Favorite Record');
|
||||
expect(s.nameSlug?.id).toEqual(34234);
|
||||
expect(s).toHaveProperty('teamSlug');
|
||||
expect(s).toHaveProperty('userSlug');
|
||||
expect(s).not.toHaveProperty('article');
|
||||
});
|
||||
it('checkSlug failure', () => {
|
||||
const params = {
|
||||
nameSlug: 'my-favorite-record'
|
||||
};
|
||||
const slugs = checkSlug(params);
|
||||
expect(slugs).toBeNull();
|
||||
});
|
||||
it('checkSlug empty', () => {
|
||||
const params = {
|
||||
article: '33'
|
||||
};
|
||||
|
||||
const slugs = checkSlug(params);
|
||||
expect(slugs).not.toBeNull();
|
||||
expect(slugs).toBeObject();
|
||||
expect(Object.keys(slugs as SlugRecord)).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,40 @@
|
||||
import type { RouteParamsGeneric } from '#vue-router';
|
||||
|
||||
export type Slug = { name: string; id: number };
|
||||
export type SlugRecord = Record<string, Slug>;
|
||||
|
||||
export function parseSlug(slug: string): null | Slug {
|
||||
const result = /^(?<name>(?:\w+-)*)(?<id>\d*)$/.exec(slug);
|
||||
if (
|
||||
result === null ||
|
||||
result.groups === undefined ||
|
||||
result.groups.id === undefined ||
|
||||
result.groups.id.length <= 0
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const { name, id } = result.groups!;
|
||||
|
||||
return {
|
||||
name: name
|
||||
.substring(0, name.length - 1)
|
||||
.split('-')
|
||||
.map((w) => `${w.substring(0, 1).toUpperCase()}${w.substring(1)}`)
|
||||
.join(' '),
|
||||
id: parseInt(id)
|
||||
};
|
||||
}
|
||||
|
||||
export function checkSlug(routeParams: RouteParamsGeneric): null | SlugRecord {
|
||||
const slugs = Object.keys(routeParams).filter(
|
||||
(k) => k.endsWith('Slug') && typeof routeParams[k] === 'string'
|
||||
);
|
||||
|
||||
const parsed = slugs.map((s) => parseSlug(routeParams[s] as string));
|
||||
|
||||
if (parsed.some((s) => s === null)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Object.fromEntries(slugs.map((slugKey, i) => [slugKey, parsed[i]!]));
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
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
|
||||
}) + '-';
|
||||
}
|
||||
|
||||
string += `${id}`;
|
||||
|
||||
return string;
|
||||
}
|
||||
Reference in New Issue
Block a user