old-money
Version:
Pounds, shillings, and pence
137 lines (93 loc) • 2.68 kB
Markdown
💷 JS/TS module for Pounds, Shillings, and Pence (lsd/£sd)
`old-money` [uses ECMAScript Modules][esm] so you'll need Node 14+
[esm]: https://blog.sindresorhus.com/hello-modules-d1010b4e777b
## Install + Build
Built with esbuild
```
npm i
npm run watch
```
or `npm run build`
## Test
Tests use ava.
```
npm test
```
## Usage
```
import { lsd } from 'old-money'
let money = new lsd(959);
console.log(money.toString());
console.log(money.pounds, money.shillings, money.pence);
```
Create a new lsd money object with a total number of pence:
```
let m = new lsd(6);
```
Create a new lsd money object using three "stacks" of coin types. These will be converted into a total number of pence and rationalised:
```
let m = new lsd(1, 41, 25);
console.log(m.totalPence);
// 757
console.log(m.pounds, m.shillings, m.pence);
// 3, 3, 1
```
Get the money amount as a string in the default format like "£3/19/11"
```
let m = new lsd(959);
console.log(m.toString());
// "£3/19/11"
```
Format your own string using `$l`, `$s`, and `$d` for pounds, shillings, and pence respectively.
```
let m = new lsd(522);
console.log(m.toString("£$l. $ss. $dd."));
// "£2. 3s. 6d."
console.log(m.toString("$l pounds, $s and $d"));
// "2 pounds, 3 and 6"
```
Get the individual, bounded number of pounds, shillings, and pence, with an upper bound on shillings (20) and pence (12):
```
let m = new lsd(959);
console.log(m.pounds, m.shillings, m.pence);
// 3, 19, 11
```
Get the whole amount as a total number of pence:
```
let m = new lsd(959);
console.log(m.totalPence);
// 959
```
Get the whole amount as a total whole number of shillings, as some shops did:
```
let m = new lsd(1176);
console.log(m.totalShillings);
// 98
```
Add the specified number of pounds, shillings, or pence. Pence can be greater than 12 and shillings greater than 20 because the amount will be rationalised into a whole number of pence:
```
let m = new lsd(12); //12d (one shilling)
m.addShillings(19); // Adds up to 20s, that is, £1
console.log(m.toString());
// "£1/0/0"
```
Add together two `lsd` objects:
```
let m = new lsd(1,2,6);
let n = new lsd(0,2,6);
m.add(n);
console.log(m.toString());
// "£1/5/0"
// n is unchanged
```