old-money
Version:
Pounds, shillings, and pence
49 lines (48 loc) • 1.29 kB
JavaScript
// src/index.ts
var lsd = class {
constructor(p = 0, shillings, pence) {
this.totalPence = 0;
this.SHILLINGS_PER_POUND = 20;
this.PENCE_PER_SHILLING = 12;
this.PENCE_PER_POUND = 240;
if (shillings === void 0 && pence === void 0) {
this.totalPence = parseInt(p);
return;
}
this.totalPence = parseInt(pence);
this.addShillings(shillings);
this.addPounds(p);
}
get pounds() {
return parseInt(this.totalPence / this.PENCE_PER_POUND);
}
get shillings() {
return parseInt(this.totalPence / this.PENCE_PER_SHILLING) % this.SHILLINGS_PER_POUND;
}
get pence() {
return this.totalPence % this.PENCE_PER_SHILLING;
}
get totalShillings() {
return parseInt(this.totalPence / this.PENCE_PER_SHILLING);
}
addPence(pence) {
this.totalPence += pence;
}
addShillings(shillings) {
this.totalPence += shillings * this.PENCE_PER_SHILLING;
}
addPounds(pounds) {
this.totalPence += pounds * this.PENCE_PER_POUND;
}
add(money) {
this.totalPence += money.totalPence;
}
toString(format) {
if (!format)
return `\xA3${this.pounds}/${this.shillings}/${this.pence}`;
return format.replace("$l", this.pounds).replace("$s", this.shillings).replace("$d", this.pence);
}
};
export {
lsd
};