Files
CommunitySolidServer/src/pods/generate/SuffixIdentifierGenerator.ts
Joachim Van Herwegen b3da9c9fcf refactor: Restructure source code folder
This way the location of certain classes should make more sense
2021-10-12 12:51:02 +02:00

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) };
}
}