UNPKG

@moonsong-labs/moonwall-util

Version:

Testing framework for the Moon family of projects

108 lines (106 loc) 3.41 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/functions/common.ts var common_exports = {}; __export(common_exports, { Perbill: () => Perbill, Percent: () => Percent, getObjectMethods: () => getObjectMethods, sortObjectByKeys: () => sortObjectByKeys }); module.exports = __toCommonJS(common_exports); var import_api_augment = require("@moonbeam-network/api-augment"); var import_util = require("@polkadot/util"); function sortObjectByKeys(o) { return Object.keys(o).sort().reduce((r, k) => (r[k] = o[k], r), {}); } var Perthing = class { unit; perthing; constructor(unit, numerator, denominator) { if (!(numerator instanceof import_util.BN)) { numerator = new import_util.BN(numerator.toString()); } if (denominator && !(denominator instanceof import_util.BN)) { denominator = new import_util.BN(denominator.toString()); } this.unit = unit; if (denominator) { this.perthing = numerator.mul(unit).div(denominator); } else { this.perthing = numerator; } } value() { return this.perthing; } of(value) { return this.divNearest(this.perthing.mul(value), this.unit); } ofCeil(value) { return this.divCeil(this.perthing.mul(value), this.unit); } toString() { return `${this.perthing.toString()}`; } divCeil(a, num) { var dm = a.divmod(num); if (dm.mod.isZero()) return dm.div; return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1); } divNearest(a, num) { var dm = a.divmod(num); if (dm.mod.isZero()) return dm.div; var mod = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod; var half = num.ushrn(1); var r2 = num.andln(1); var cmp = mod.cmp(half); if (cmp <= 0 || r2 === new import_util.BN(1) && cmp === 0) return dm.div; return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1); } }; var Perbill = class extends Perthing { constructor(numerator, denominator) { super(new import_util.BN(1e9), numerator, denominator); } }; var Percent = class extends Perthing { constructor(numerator, denominator) { super(new import_util.BN(100), numerator, denominator); } }; function getObjectMethods(obj) { let properties = /* @__PURE__ */ new Set(); let currentObj = obj; do { Object.getOwnPropertyNames(currentObj).map((item) => properties.add(item)); } while (currentObj = Object.getPrototypeOf(currentObj)); return [...properties.keys()].filter( (item) => typeof obj[item] === "function" ); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Perbill, Percent, getObjectMethods, sortObjectByKeys });