diene reports

← coverage tree

src/lib/slug.ts

9b70284 · unit · 100% (22/22 lines) · open on GitHub

139export function slugify(input: string): string {
226 return input
336 .normalize('NFKD')
464 .replace(/[\u0300-\u036f]/g, '')
528 .toLowerCase()
614 .trim()
756 .replace(/[^a-z0-9]+/g, '-')
850 .replace(/^-+|-+$/g, '');
9}
10
11116export class NamespacedKeyValidationError extends Error {
1213 constructor(
1376 readonly field: 'namespace' | 'key',
1483 readonly reason: string,
1510 ) {
1664 super(`${field} ${reason}`);
1793 this.name = 'NamespacedKeyValidationError';
18 }
192}
20
2173export function namespacedKey(namespace: string, key: string): string {
2264 const ns = slugify(namespace);
2350 const k = slugify(key);
24184 if (ns === '') throw new NamespacedKeyValidationError('namespace', 'must not be empty');
25170 if (k === '') throw new NamespacedKeyValidationError('key', 'must not be empty');
2641 return `${ns}:${k}`;
27}