mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
21 lines
713 B
TypeScript
21 lines
713 B
TypeScript
import type { ResourceIdentifier } from '../../http/representation/ResourceIdentifier';
|
|
import { ensureTrailingSlash } from '../../util/PathUtil';
|
|
import type { IdentifierGenerator } from './IdentifierGenerator';
|
|
|
|
/**
|
|
* Generates identifiers by appending the name to a stored base identifier.
|
|
* Non-alphanumeric characters will be replaced with `-`.
|
|
*/
|
|
export class SuffixIdentifierGenerator implements IdentifierGenerator {
|
|
private readonly base: string;
|
|
|
|
public constructor(base: string) {
|
|
this.base = base;
|
|
}
|
|
|
|
public generate(name: string): ResourceIdentifier {
|
|
const cleanName = name.replace(/\W/gu, '-');
|
|
return { path: ensureTrailingSlash(new URL(cleanName, this.base).href) };
|
|
}
|
|
}
|