UNPKG

@rr0/data

Version:
96 lines (95 loc) 3.69 kB
import { Gender } from "@rr0/common"; import { Level2Date as EdtfDate, Level2Duration as Duration } from "@rr0/time"; import { RR0Data } from "../RR0Data.js"; import { StringUtil } from "../util/string/StringUtil.js"; export class People extends RR0Data { constructor(firstNames = [], lastName = "", pseudonyms = [], /** * @deprecated */ occupations = [], /** * @deprecated */ countries = [], /** * The people has been caught lying or has confessed a hoax. */ discredited = false, /** * @deprecated Use a "birth"-typed sub-data instead. */ gender = Gender.male, id = StringUtil.textToCamel(lastName) + firstNames.join(""), dirName = "", image, url, events = [], qualifier = "", surname = undefined) { super(); this.firstNames = firstNames; this.lastName = lastName; this.pseudonyms = pseudonyms; this.occupations = occupations; this.countries = countries; this.discredited = discredited; this.gender = gender; this.qualifier = qualifier; this.type = "people"; /** * The people actually doesn't exist. */ this.hoax = false; this.lastAndFirstName = this.getLastAndFirstName(); this.title = this.firstAndLastName; this.name = this.lastName; this.id = id; this.dirName = dirName; this.image = image; this.events = events; this.url = url; this.surname = surname; } get birthTime() { var _a; return (_a = this.events.find(event => event.eventType === "birth")) === null || _a === void 0 ? void 0 : _a.time; } get deathTime() { var _a; return (_a = this.events.find(event => event.eventType === "death")) === null || _a === void 0 ? void 0 : _a.time; } get firstAndLastName() { const { lastNameStr, firstNameStr } = this.getLastAndFirstNames(); return lastNameStr && firstNameStr ? firstNameStr + " " + lastNameStr : lastNameStr || firstNameStr; } getLastAndFirstName() { const { lastNameStr, firstNameStr } = this.getLastAndFirstNames(); return lastNameStr && firstNameStr ? lastNameStr + ", " + firstNameStr : lastNameStr || firstNameStr; } isDeceased(from) { return this.deathTime ? true : this.birthTime ? this.isProbablyDeceased(this.birthTime, from) : false; } getAge(from) { if (this.birthTime) { let timeDelta; if (this.deathTime) { timeDelta = Duration.between(this.birthTime, this.deathTime); } else if (!this.isProbablyDeceased(this.birthTime)) { const now = from !== null && from !== void 0 ? from : new EdtfDate(); timeDelta = Duration.between(this.birthTime, now); } else { return undefined; } return timeDelta.toSpec().years.value; } } isProbablyDeceased(birth, at) { const now = at !== null && at !== void 0 ? at : new EdtfDate(); const timeDelta = Duration.between(birth, now); return timeDelta.toSpec().years.value > 120; } clone() { return new People(this.firstNames, this.lastName, this.pseudonyms, this.occupations, this.countries, this.discredited, this.gender, this.id, this.dirName, this.image, this.url, this.events); } getLastAndFirstNames() { const lastNameStr = StringUtil.camelToText(this.lastName); const firstNameStr = this.firstNames.length > 0 ? this.firstNames.join(" ") : ""; return { lastNameStr, firstNameStr }; } }