@rr0/data
Version:
RR0 data model
38 lines (37 loc) • 1.9 kB
JavaScript
import path from "path";
import { AbstractDataService } from "../AbstractDataService.js";
import { StringUtil } from "../util/string/StringUtil.js";
export class PeopleService extends AbstractDataService {
constructor(dataService, factory, config) {
super(dataService, factory, config.files);
this.config = config;
}
getUrl(lastName, firstNames) {
const normalizedLastName = StringUtil.textToCamel(lastName);
const normalizedFirstNames = firstNames.map(StringUtil.textToCamel);
const firstLetter = (normalizedLastName + normalizedFirstNames.join("")).charAt(0).toLowerCase();
return path.join(this.config.rootDir, `${firstLetter}/${normalizedLastName}${normalizedFirstNames.join("")}`);
}
createFromTitle(title) {
const peopleFactory = this.factory;
const { lastName, firstNames, qualifier } = peopleFactory.namesFromTitle(title);
const key = this.cacheKey(lastName, title);
const dirName = lastName ? this.dirNameFromNames(lastName, firstNames, title) : this.getUrl(StringUtil.textToCamel(title), []);
const created = peopleFactory.parse({ title, firstNames, lastName, dirName });
if (this.files.indexOf(dirName) < 0) {
console.warn(`Could not find dirName "${dirName}" in PeopleService files; clearing dirName`);
Object.assign(created, { dirName: undefined });
}
this.cache.set(key, created);
return created;
}
cacheKey(lastName, title) {
const titleKey = StringUtil.textToCamel(title);
return StringUtil.textToCamel(lastName) || titleKey;
}
dirNameFromNames(lastName, firstNames, title) {
var _a;
const cacheKey = this.cacheKey(lastName, title);
return ((_a = this.cache.get(cacheKey)) === null || _a === void 0 ? void 0 : _a.dirName) || this.getUrl(lastName, firstNames);
}
}