From c2f6b87676e60f491df2ee5bcb28447fa0746f84 Mon Sep 17 00:00:00 2001 From: jer-echo Date: Thu, 23 Jun 2022 09:42:02 +0800 Subject: [PATCH] implement update from mod.ts --- mod.js | 90 ++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 25 deletions(-) diff --git a/mod.js b/mod.js index 0707530..96cd755 100644 --- a/mod.js +++ b/mod.js @@ -1,30 +1,70 @@ +/** + * @class Numesis + * Create custom Number System + * Compatible with: + * - Deno + * - NodeJS + * - Browser + * - Typescript + * @author Jericho Aquino + * @version 1.0.2 + * @license Apache-2.0 + * @see {@link https://github.com/eru123/numesis} Github Repository + * @see {@link https://www.npmjs.com/package/numesis} NPM Package + */ class Numesis { - c; - constructor(ch = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"){ - this.c = { - s: ch, - l: ch.length, - i: ch.length - 1, - a: ch.split("") - }; + /** + * A non-duplicate character set, that will be use in creating new number system + * @type {Set} + */ + charset; + /** + * @param {String} chartset A non-duplicate character set, that will be use in creating new number system + */ + constructor(chartset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"){ + this.charset = Array.from(new Set(chartset.split(""))); } - ep(n, c = "") { - n = Math.floor(n); - const { l , i , a } = this.c; - if (n <= i) return a[n] + c; - const m = n % l; - c = m > 0 && m <= l ? a[m] + c : a[0] + c; - const d = Math.floor(n / l); - return this.ep(d, c); + /** + * A recursive method that encodes the n variable to custom number system + * @param {number|string} number The number that will be decoded + * @param {string} current The current string in the recursion + * + * @return {String} encoded string + */ + encode_process(number, current = "") { + number = parseInt("" + number); + const length = this.charset.length; + const lastIndex = length - 1; + const set = this.charset; + if (number <= lastIndex) return "" + (set[number] + current); + const m = number % length; + current = m > 0 && m <= length ? set[m] + current : set[0] + current; + const d = parseInt(number / length + ""); + return this.encode_process(d, current); } - e(n) { - const str = String(n); - if (str.includes(".")) { - const st = str.split("."); - return `${this.ep(Number(st[0]) || 0)}.${this.ep(Number(st[1]) || 0)}`; - } - return this.ep(n); + /** + * Alias for encode_process + */ + ep = this.encode_process; + /** + * A public method that encodes n paramenter to custom number system + * @param {number|string} args numbers that will be decoded + * + * @return {String} encoded string + */ + encode(...args) { + return args.map((n)=>{ + const str = String(n); + if (/\./i.test(str)) { + const st = str.split("."); + return `${this.encode_process(+st[0] || 0)}.${this.encode_process(+st[1] || 0)}`; + } + return this.encode_process(n); + }).join(""); } + /** + * Alias for encode + */ + e = this.encode.bind(this); } - -module.exports = Numesis; \ No newline at end of file +module.exports = Numesis;