UNPKG

cerceis-lib

Version:

Contains list of quality of life functions that is written in TypeScript and es6

188 lines (187 loc) 5.24 kB
"use strict"; 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/number/index.ts var number_exports = {}; __export(number_exports, { FromNum: () => FromNum }); module.exports = __toCommonJS(number_exports); var FromNum = { /** * A generic roll. Anything larger than 100 will be true, * lesser than 0 will be false. * @param percentage 0~100 * @returns Fail(false)/Success(true) */ roll(percentage) { const fixed = percentage / 100; if (fixed >= Math.random()) return true; return false; }, /** * Dice roll for your typical TRPG game. * Ex) FromNum.diceRoll(3).D(20) = Roll 3 D20 Dice. * Ex) FromNum.diceRoll(1).D(6) = Roll 1 D6 Dice. * @param num Number of dice to roll */ diceRoll(num) { const _roll = (sides) => Math.floor(Math.random() * sides) + 1; return { /** * @param diceType Type of dice (e.g. 6, 10, 20). * @returns number[] */ D: (diceType) => { const rs = []; for (let i = 0; i < num; i++) rs.push(_roll(diceType)); return rs; } }; }, /** * Scale number down to 0 ~ 1. * @param x Input number * @param min Min number * @param max Max number */ minMaxScale(x, min, max) { return (x - min) / (max - min); }, /** * Return scaled down number back to normal. * @param x Input number * @param min Min number * @param max Max number */ unMinMaxScale(x, min, max) { return x * (max - min) + min; }, /** * Return the sum of the values. * @param nums Array of numbers */ sum(nums) { return nums.reduce((a, b) => a + b); }, /** * SoftMax / normalized exponential function. * Returns a probability distribution of K possible outcomes. * @param input Array of numbers */ softMax(input) { if (input.length === 0) return []; const total = input.reduce((a, b) => a + Math.exp(b), 0); return input.map((n) => Math.exp(n) / total); }, /** * Return the arithmetic mean of a list of numbers. * @param input Array of numbers */ mean(input) { if (input.length === 0) return 0; return input.reduce((a, b) => a + b, 0) / input.length; }, /** * Convert numeric values into Roman Numeral string. * @param number Input number. */ toRomanNumeral(number) { const strMap = { M: 1e3, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1 }; let input = number; let rs = ""; for (const n in strMap) { const tmp = Math.floor(input / strMap[n]); for (let i = 0; i < tmp; i++) rs += n; input -= tmp * strMap[n]; } return rs; }, /** * Sigmoid function — bounded, differentiable, defined for all real inputs. * @param x number or number[] */ sigmoid(x) { const _f = (_x) => 1 / (1 + Math.exp(-_x)); if (Array.isArray(x)) return x.map((n) => _f(n)); return _f(x); }, /** * ReLU (Rectified Linear Unit) — f(x) = max(0, x). * Outputs input directly if positive, otherwise zero. * @param xs number[] */ relu(xs) { return xs.map((x) => Math.max(0, x)); }, /** * Softplus — smooth version of ReLU. f(x) = log(1 + exp(x)) * @param xs number[] */ softPlus(xs) { return xs.map((x) => Math.log(1 + Math.exp(x))); }, /** * Round number to nearest specified multiple. * @param n Number to round * @param r Multiple to round to */ toNearest(n, r) { return Math.round(n / r) * r; }, /** * Convert number into a short readable string. * Does not round when truncating decimals. * @param n Input number * @param decimal Default = 2, number of decimal places to retain. * @param maxLen Default = 3, maximum digit length (overwrites decimal). */ toShortReadable(n, decimal = 2, maxLen = 3) { const symbols = { "": 0, k: 1e3, m: 1e6 }; let symbol = ""; for (const s in symbols) { if (symbols[s] > n) break; symbol = s; } if (symbol === "") return `${n}`; const divided = (n / symbols[symbol]).toFixed(decimal).split(""); while (divided.length > maxLen + 1) divided.pop(); while (divided.at(-1) === "0") divided.pop(); if (divided.at(-1) === ".") divided.pop(); return `${divided.join("")}${symbol}`; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { FromNum }); //# sourceMappingURL=index.js.map