import numbro from 'numbro';

export default class Numbro {
	static defaultString = '0,0';

	static format(value, format = null) {
		const numericValue = typeof value === 'number' ? value : Number.parseFloat(value);

		return (numericValue === 0 || numericValue === null)
			? numbro(0).format(format || Numbro.defaultFormatString)
			: Number.isFinite(numericValue)
				? numericValue >= 100000
					? numbro(numericValue).format(format || '0,0[.]0a')
					: numbro(numericValue).format(format || Numbro.defaultFormatString)
				: '—';
	}

	static formatCurrency(value, format = '0,0a') {
		return numbro(value).formatCurrency(format);
	}

	static unformat(value) {
		return numbro().unformat(value);
	}

	static nth(value, skipNumber) {
		// https://stackoverflow.com/a/39466341
		const nth = ['st', 'nd', 'rd'][((((Math.round(value) + 90) % 100) - 10) % 10) - 1] || 'th';

		return skipNumber ? nth : `${Math.round(value)}${nth}`;
	}
}
