aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib.ts217
1 files changed, 82 insertions, 135 deletions
diff --git a/src/lib.ts b/src/lib.ts
index 52413bb..53ce401 100644
--- a/src/lib.ts
+++ b/src/lib.ts
@@ -244,29 +244,6 @@ export class HistDate {
return intToOrdinal(this.day);
}
}
- toDisplayString(){
- if (this.year <= -1e4){ // N.NNN billion/million/thousand years ago
- if (this.year <= -1e9){
- return `${Math.round(-this.year / 1e6) / 1e3} billion years ago`;
- } else if (this.year <= -1e6){
- return `${Math.round(-this.year / 1e3) / 1e3} million years ago`;
- } else {
- return `${Math.round(-this.year / 1e3)} thousand years ago`;
- }
- } else if (this.gcal == null){
- if (this.year < 0){ // eg: 1,000 BC
- return `${(-this.year).toLocaleString()} BC`;
- } else if (this.year < 1500){ // eg: 10 AD
- return `${this.year} AD`;
- } else {
- return `${this.year}`; // eg: 2010
- }
- } else { // eg: 2nd Mar 1710 BC (O.S.)
- const bcSuffix = this.year < 0 ? ' BC' : (this.year < 1500 ? ' AD' : '');
- const calStr = this.gcal ? '' : ' (O.S.)';
- return `${intToOrdinal(this.day)} ${MONTH_NAMES[this.month-1]} ${Math.abs(this.year)}${bcSuffix}${calStr}`;
- }
- }
toInt(){ // Used for v-for keys
return this.day + this.month * 50 + this.year * 1000;
}
@@ -296,13 +273,92 @@ export class CalDate extends HistDate {
}
}
export const MIN_CAL_DATE = new CalDate(MIN_CAL_YEAR, 1, 1);
+
+// For event representation
+export class HistEvent {
+ id: number;
+ title: string;
+ start: HistDate;
+ startUpper: HistDate | null;
+ end: HistDate | null;
+ endUpper: HistDate | null;
+ ctg: string;
+ imgId: number;
+ pop: number;
+ constructor(
+ id: number, title: string, start: HistDate, startUpper: HistDate | null = null,
+ end: HistDate | null = null, endUpper: HistDate | null = null, ctg='', imgId=0, pop=0){
+ this.id = id;
+ this.title = title;
+ this.start = start;
+ this.startUpper = startUpper;
+ this.end = end;
+ this.endUpper = endUpper;
+ this.ctg = ctg;
+ this.imgId = imgId;
+ this.pop = pop;
+ }
+}
+export class ImgInfo {
+ url: string;
+ license: string;
+ artist: string;
+ credit: string;
+ constructor(url: string, license: string, artist: string, credit: string){
+ this.url = url;
+ this.license = license;
+ this.artist = artist;
+ this.credit = credit;
+ }
+}
+export class EventInfo {
+ event: HistEvent;
+ desc: string | null;
+ wikiId: number;
+ imgInfo: ImgInfo;
+ constructor(event: HistEvent, desc: string, wikiId: number, imgInfo: ImgInfo){
+ this.event = event;
+ this.desc = desc;
+ this.wikiId = wikiId;
+ this.imgInfo = imgInfo;
+ }
+}
+export function cmpHistEvent(event: HistEvent, event2: HistEvent){
+ const cmp = event.start.cmp(event2.start);
+ return cmp != 0 ? cmp : event.id - event2.id;
+}
+
+// For date display
+export function dateToDisplayStr(date: HistDate){
+ if (date.year <= -1e4){ // N.NNN billion/million/thousand years ago
+ if (date.year <= -1e9){
+ return `${Math.round(-date.year / 1e6) / 1e3} billion years ago`;
+ } else if (date.year <= -1e6){
+ return `${Math.round(-date.year / 1e3) / 1e3} million years ago`;
+ } else {
+ return `${Math.round(-date.year / 1e3)} thousand years ago`;
+ }
+ } else if (date.gcal == null){
+ if (date.year < 0){ // eg: 1,000 BC
+ return `${(-date.year).toLocaleString()} BC`;
+ } else if (date.year < 1500){ // eg: 10 AD
+ return `${date.year} AD`;
+ } else {
+ return `${date.year}`; // eg: 2010
+ }
+ } else { // eg: 2nd Mar 1710 BC (O.S.)
+ const bcSuffix = date.year < 0 ? ' BC' : (date.year < 1500 ? ' AD' : '');
+ const calStr = date.gcal ? '' : ' (O.S.)';
+ return `${intToOrdinal(date.day)} ${MONTH_NAMES[date.month-1]} ${Math.abs(date.year)}${bcSuffix}${calStr}`;
+ }
+}
export function boundedDateToStr(start: HistDate, end: HistDate | null) : string {
// Converts a date with uncertain end bound to string for display
if (end == null){
- return start.toDisplayString();
+ return dateToDisplayStr(start);
}
- const startStr = start.toDisplayString();
- const endStr = end.toDisplayString();
+ const startStr = dateToDisplayStr(start);
+ const endStr = dateToDisplayStr(end);
if (startStr == endStr){
return startStr;
}
@@ -354,116 +410,7 @@ export function boundedDateToStr(start: HistDate, end: HistDate | null) : string
}
return `${startStr} to ${endStr}`;
}
-export function eventDatesToStrings(
- start: HistDate, startUpper: HistDate | null, end: HistDate | null, endUpper: HistDate | null)
- : [string] | [string, string] {
- if (end == null){
- return [boundedDateToStr(start, startUpper)];
- }
- const startStr = boundedDateToStr(start, startUpper);
- const endStr = boundedDateToStr(end, endUpper);
- if (startStr == endStr){
- return [startStr];
- }
- if (startStr.includes(' to ') || startStr.startsWith('About ')
- || endStr.includes(' to ') || endStr.startsWith('About ')){
- return [startStr, endStr];
- }
- let startMatch: null | string[];
- let endMatch: null | string[];
- if (start.gcal == null && end.gcal == null){
- if (startStr.endsWith(' years ago') && endStr.endsWith(' years ago')){
- const yaRegex = /^(.*) (.*) years ago$/;
- startMatch = yaRegex.exec(startStr)!;
- endMatch = yaRegex.exec(endStr)!;
- if (startMatch[2] == endMatch[2]){ // Same billion/million/thousand scale
- return [`${startMatch[1]} to ${endMatch[1]} ${startMatch[2]} years ago`];
- } else {
- return [`${startMatch[1]} ${startMatch[2]} to ${endMatch[1]} ${endMatch[2]} years ago`];
- }
- }
- const mcRegex = /^(.*) (millenium|century( (BC|AD))?)$/;
- startMatch = mcRegex.exec(startStr);
- endMatch = mcRegex.exec(endStr);
- if (startMatch != null && endMatch != null && startMatch[2] == endMatch[2]){
- return [`${startMatch[1]} to ${endMatch[1]} ${startMatch[2]}`];
- }
- const bcRegex = /^(.*) (BC|AD)$/;
- startMatch = bcRegex.exec(startStr);
- endMatch = bcRegex.exec(endStr);
- if (startMatch != null && endMatch != null && startMatch[2] == endMatch[2]){
- return [`${startMatch[1]} to ${endStr}`];
- }
- } else if (start.gcal != null && end.gcal != null){
- const calRegex = /^(\S+ )?(\S+) (.*)$/; // Matches day (optional), month, and suffix
- startMatch = calRegex.exec(startStr);
- endMatch = calRegex.exec(endStr);
- if (startMatch != null && endMatch != null && startMatch[3] == endMatch[3]){ // Same suffix
- if (startMatch[2] == endMatch[2]){ // Same month
- return [`${startMatch[1]}to ${endMatch[1]}${startMatch[2]} ${startMatch[3]}`];
- }
- if (startMatch[1] == null){
- return [`${startMatch[2]} to ${endMatch[2]} ${startMatch[3]}`];
- }
- return [`${startMatch[1]}${startMatch[2]} to ${endMatch[1]}${endMatch[2]} ${startMatch[3]}`];
- }
- }
- return [startStr, endStr];
-}
-// For event representation
-export class HistEvent {
- id: number;
- title: string;
- start: HistDate;
- startUpper: HistDate | null;
- end: HistDate | null;
- endUpper: HistDate | null;
- ctg: string;
- imgId: number;
- pop: number;
- constructor(
- id: number, title: string, start: HistDate, startUpper: HistDate | null = null,
- end: HistDate | null = null, endUpper: HistDate | null = null, ctg='', imgId=0, pop=0){
- this.id = id;
- this.title = title;
- this.start = start;
- this.startUpper = startUpper;
- this.end = end;
- this.endUpper = endUpper;
- this.ctg = ctg;
- this.imgId = imgId;
- this.pop = pop;
- }
-}
-export class ImgInfo {
- url: string;
- license: string;
- artist: string;
- credit: string;
- constructor(url: string, license: string, artist: string, credit: string){
- this.url = url;
- this.license = license;
- this.artist = artist;
- this.credit = credit;
- }
-}
-export class EventInfo {
- event: HistEvent;
- desc: string | null;
- wikiId: number;
- imgInfo: ImgInfo;
- constructor(event: HistEvent, desc: string, wikiId: number, imgInfo: ImgInfo){
- this.event = event;
- this.desc = desc;
- this.wikiId = wikiId;
- this.imgInfo = imgInfo;
- }
-}
-export function cmpHistEvent(event: HistEvent, event2: HistEvent){
- const cmp = event.start.cmp(event2.start);
- return cmp != 0 ? cmp : event.id - event2.id;
-}
// For server requests
const SERVER_DATA_URL = (new URL(window.location.href)).origin + '/data/'