@subsocial/utils
Version:
JavaScript utils for Subsocial blockchain.
39 lines (38 loc) • 1.26 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.pluralize = void 0;
const util_1 = require("@polkadot/util");
const bn_js_1 = __importDefault(require("bn.js"));
const ZERO = new bn_js_1.default(0);
const ONE = new bn_js_1.default(1);
function pluralize({ count, singularText, pluralText, hideCount }) {
if (!count) {
count = ZERO;
}
else if (typeof count === 'string') {
if (count.startsWith('0x')) {
count = (0, util_1.hexToBn)(count);
}
else if (count.startsWith('-0x')) {
count = (0, util_1.hexToBn)(count.substring(1), { isNegative: true });
}
else {
count = new bn_js_1.default(count);
}
}
else if (typeof count === 'number') {
count = new bn_js_1.default(count);
}
const plural = () => !pluralText
? singularText + 's'
: pluralText;
const text = count.eq(ONE)
? singularText
: plural();
const countMsg = hideCount ? '' : `${count.toNumber()} `;
return `${countMsg}${text}`;
}
exports.pluralize = pluralize;