-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
65 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string>} | ||
*/ | ||
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; | ||
module.exports = Numesis; |