feat(ecma): Moduled JavaScript to native ECAMScript importing.

This commit is contained in:
KyMAN 2024-12-16 17:51:17 +01:00
parent 60f24f8bbb
commit 11bb8b558b
3 changed files with 501 additions and 471 deletions

View File

@ -1,9 +1,21 @@
"use strict";
/** /**
* @class
* @constructor
* @param {string|Array.<string>} [alphabet] * @param {string|Array.<string>} [alphabet]
* @returns {void} * @returns {void}
* @access public * @access public
*/ */
const ErrorsManager = function(alphabet){ export const ErrorsManager = (function(){
/**
* @constructs ErrorsManager
* @param {string|Array.<string>} [alphabet]
* @returns {void}
* @access private
*/
const ErrorsManager = function(alphabet){
/** @type {ErrorsManager} */ /** @type {ErrorsManager} */
const self = this; const self = this;
@ -14,7 +26,7 @@ const ErrorsManager = function(alphabet){
/** /**
* @returns {void} * @returns {void}
* @access public * @access private
*/ */
const constructor = () => { const constructor = () => {
@ -32,7 +44,7 @@ const ErrorsManager = function(alphabet){
error = ( error = (
new_alphabet === undefined ? 0 : new_alphabet === undefined ? 0 :
new_alphabet === null ? 0 : new_alphabet === null ? 0 :
!ErrorsManager.prototype.is_string(new_alphabet) && !ErrorsManager.prototype.is_array(new_alphabet) ? 1 << 2 : !ErrorsManager.is_string(new_alphabet) && !ErrorsManager.is_array(new_alphabet) ? 1 << 2 :
0) << 1; 0) << 1;
if(!error){ if(!error){
@ -42,7 +54,7 @@ const ErrorsManager = function(alphabet){
const original_length = new_alphabet.length, const original_length = new_alphabet.length,
/** @type {number} */ /** @type {number} */
final_length = (new_alphabet = (new_alphabet instanceof Array ? new_alphabet : new_alphabet.split("")).filter((character, i) => ( final_length = (new_alphabet = (new_alphabet instanceof Array ? new_alphabet : new_alphabet.split("")).filter((character, i) => (
ErrorsManager.prototype.is_string(character) && character.length == 1 && new_alphabet.indexOf(character) == i ErrorsManager.is_string(character) && character.length == 1 && new_alphabet.indexOf(character) == i
))).length; ))).length;
error |= ( error |= (
@ -55,7 +67,7 @@ const ErrorsManager = function(alphabet){
alphabet = error || !new_alphabet ? ( alphabet = error || !new_alphabet ? (
alphabet && alphabet.length ? alphabet : alphabet && alphabet.length ? alphabet :
ErrorsManager.prototype.BASE64) : new_alphabet; ErrorsManager.BASE64) : new_alphabet;
// re_hexa_error = new RegExp("[^" + alphabet[0] + "]"); // re_hexa_error = new RegExp("[^" + alphabet[0] + "]");
return error; return error;
@ -72,14 +84,14 @@ const ErrorsManager = function(alphabet){
/** @type {Array.<number>} */ /** @type {Array.<number>} */
let array = []; let array = [];
if(ErrorsManager.prototype.is_string(code)) if(ErrorsManager.is_string(code))
array = code.split("").map(hexa => alphabet.indexOf(hexa)); array = code.split("").map(hexa => alphabet.indexOf(hexa));
else if(ErrorsManager.prototype.is_integer(code)){ else if(ErrorsManager.is_integer(code)){
while(code){ while(code){
array.push(code & 0x3F); array.push(code & 0x3F);
code >>>= 6; code >>>= 6;
}; };
}else if(ErrorsManager.prototype.is_array(code)) }else if(ErrorsManager.is_array(code))
array = [].concat(code); array = [].concat(code);
while(array.length < length) while(array.length < length)
@ -131,12 +143,12 @@ const ErrorsManager = function(alphabet){
* @access public * @access public
*/ */
this.bits = code => { this.bits = code => {
if(ErrorsManager.prototype.is_integer(code)) if(ErrorsManager.is_integer(code))
return code ? Math.log2(code) : 1; return code ? Math.log2(code) : 1;
ErrorsManager.prototype.is_string(code) && (code = self.to_array(code)); ErrorsManager.is_string(code) && (code = self.to_array(code));
if(ErrorsManager.prototype.is_array(code)) if(ErrorsManager.is_array(code))
return !(code = self.compact(code)).length || !code[code.length - 1] ? 1 : (code.length - 1) * 6 + (Math.log2(code[code.length - 1]) + 1 >> 0); return !(code = self.compact(code)).length || !code[code.length - 1] ? 1 : (code.length - 1) * 6 + (Math.log2(code[code.length - 1]) + 1 >> 0);
return null; return null;
}; };
@ -154,11 +166,11 @@ const ErrorsManager = function(alphabet){
* @access public * @access public
*/ */
this.to_integer = code => { this.to_integer = code => {
if(ErrorsManager.prototype.is_integer(code)) if(ErrorsManager.is_integer(code))
return code; return code;
if(ErrorsManager.prototype.is_array(code)) if(ErrorsManager.is_array(code))
return code.length ? code.length > 1 ? code.reduce((total, hexa, i) => total | (hexa << i * 6)) : code[0] : 0; return code.length ? code.length > 1 ? code.reduce((total, hexa, i) => total | (hexa << i * 6)) : code[0] : 0;
if(!ErrorsManager.prototype.is_string(code)) if(!ErrorsManager.is_string(code))
return 0; return 0;
return code ? code.length > 1 ? code.split("").reduce((total, hexa, i) => (i > 1 ? total : alphabet.indexOf(total)) | (alphabet.indexOf(hexa) << i * 6)) : alphabet.indexOf(code) : 0; return code ? code.length > 1 ? code.split("").reduce((total, hexa, i) => (i > 1 ? total : alphabet.indexOf(total)) | (alphabet.indexOf(hexa) << i * 6)) : alphabet.indexOf(code) : 0;
}; };
@ -174,15 +186,15 @@ const ErrorsManager = function(alphabet){
/** @type {string} */ /** @type {string} */
let string = ""; let string = "";
if(ErrorsManager.prototype.is_string(code)) if(ErrorsManager.is_string(code))
string = code; string = code;
else{ else{
if(ErrorsManager.prototype.is_integer(code)){ if(ErrorsManager.is_integer(code)){
while(code){ while(code){
string += alphabet[code & 0x3F]; string += alphabet[code & 0x3F];
code >>>= 6; code >>>= 6;
}; };
}else if(ErrorsManager.prototype.is_array(code)) }else if(ErrorsManager.is_array(code))
string = code.length ? code.length > 1 ? code.reduce((total, hexa, i) => (i > 1 ? total : alphabet[total]) + alphabet[hexa]) : alphabet[code[0]] : alphabet[0]; string = code.length ? code.length > 1 ? code.reduce((total, hexa, i) => (i > 1 ? total : alphabet[total]) + alphabet[hexa]) : alphabet[code[0]] : alphabet[0];
}; };
@ -199,11 +211,11 @@ const ErrorsManager = function(alphabet){
* @access public * @access public
*/ */
this.has = (code, bits) => { this.has = (code, bits) => {
if(!ErrorsManager.prototype.is_integer(bits) && !ErrorsManager.prototype.is_array(bits)) if(!ErrorsManager.is_integer(bits) && !ErrorsManager.is_array(bits))
return ( return (
ErrorsManager.prototype.is_string(code) ? code.split("").some(hexa => hexa != alphabet[0]) : ErrorsManager.is_string(code) ? code.split("").some(hexa => hexa != alphabet[0]) :
ErrorsManager.prototype.is_integer(code) ? !!code : ErrorsManager.is_integer(code) ? !!code :
ErrorsManager.prototype.is_array(code) ? code.some(hexa => !!hexa) : ErrorsManager.is_array(code) ? code.some(hexa => !!hexa) :
null); null);
/** @type {Array.<number>} */ /** @type {Array.<number>} */
@ -212,7 +224,7 @@ const ErrorsManager = function(alphabet){
l = error.length * 6; l = error.length * 6;
return l ? ( return l ? (
AnP.prototype.is_array(bits) ? bits : [bits] AnP.is_array(bits) ? bits : [bits]
).some(bit => bit <= l && error[bit / 6 >> 0] & (1 << bit % 6)) : false; ).some(bit => bit <= l && error[bit / 6 >> 0] & (1 << bit % 6)) : false;
}; };
@ -229,18 +241,18 @@ const ErrorsManager = function(alphabet){
* @access public * @access public
*/ */
this.compact = code => { this.compact = code => {
if(ErrorsManager.prototype.is_string(code)){ if(ErrorsManager.is_string(code)){
while(code && code[code.length - 1] == alphabet[0]) while(code && code[code.length - 1] == alphabet[0])
code = code.substr(0, code.length - 1); code = code.substr(0, code.length - 1);
return code || alphabet[0]; return code || alphabet[0];
}; };
if(ErrorsManager.prototype.is_array(code)){ if(ErrorsManager.is_array(code)){
code = [].concat(code); code = [].concat(code);
while(code.length && !code[code.length - 1]) while(code.length && !code[code.length - 1])
code.pop(); code.pop();
return code.length ? code : [0]; return code.length ? code : [0];
}; };
if(ErrorsManager.prototype.is_integer(code)) if(ErrorsManager.is_integer(code))
return code; return code;
return 0; return 0;
}; };
@ -264,7 +276,7 @@ const ErrorsManager = function(alphabet){
/** @type {number} */ /** @type {number} */
rest = bits % 6, rest = bits % 6,
/** @type {string} */ /** @type {string} */
type_method = "to_" + ErrorsManager.prototype.type(code); type_method = "to_" + ErrorsManager.type(code);
if(reverse){ if(reverse){
@ -294,8 +306,10 @@ const ErrorsManager = function(alphabet){
if(rest){ if(rest){
/** @type {number} */
const r = 6 - rest,
/** @type {Array.<number>} */ /** @type {Array.<number>} */
const block = [~-(1 << (r = 6 - rest)), ~-(1 << rest) << r], block = [~-(1 << r), ~-(1 << rest) << r],
/** @type {number} */ /** @type {number} */
l = code.length - 1, l = code.length - 1,
/** @type {number} */ /** @type {number} */
@ -425,9 +439,9 @@ const ErrorsManager = function(alphabet){
code = self.bitwise(code, -_from); code = self.bitwise(code, -_from);
return ( return (
ErrorsManager.prototype.is_string(code) ? code.slice(0, _to / 6 >> 0) + (rest ? alphabet[alphabet.indexOf(code.slice(-1)) & ~-(1 << rest)] : "") : ErrorsManager.is_string(code) ? code.slice(0, _to / 6 >> 0) + (rest ? alphabet[alphabet.indexOf(code.slice(-1)) & ~-(1 << rest)] : "") :
ErrorsManager.prototype.is_array(code) ? code.slice(0, _to / 6 >> 0).concat(rest ? [code[code.length - 1] & ~-(1 << rest)] : []) : ErrorsManager.is_array(code) ? code.slice(0, _to / 6 >> 0).concat(rest ? [code[code.length - 1] & ~-(1 << rest)] : []) :
ErrorsManager.prototype.is_integer(code) ? code & ~-(1 << _to) : ErrorsManager.is_integer(code) ? code & ~-(1 << _to) :
null); null);
}; };
@ -442,48 +456,51 @@ const ErrorsManager = function(alphabet){
constructor(); constructor();
}; };
/** @type {Array.<string>} */ // /** @type {Array.<string>} */
ErrorsManager.prototype.BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".split(""); // ErrorsManager.BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".split("");
/** /**
* @param {?any} value * @param {?any} value
* @returns {boolean} * @returns {boolean}
* @access public * @access public
* @static * @static
*/ */
ErrorsManager.prototype.is_string = value => typeof value == "string"; ErrorsManager.is_string = value => typeof value == "string";
/** /**
* @param {?any} value * @param {?any} value
* @returns {boolean} * @returns {boolean}
* @access public * @access public
* @static * @static
*/ */
ErrorsManager.prototype.is_integer = value => typeof value == "number" && value == value >> 0; ErrorsManager.is_integer = value => typeof value == "number" && value == value >> 0;
/** /**
* @param {?any} value * @param {?any} value
* @returns {boolean} * @returns {boolean}
* @access public * @access public
* @static * @static
*/ */
ErrorsManager.prototype.is_array = value => typeof value == "object" && value instanceof Array; ErrorsManager.is_array = value => typeof value == "object" && value instanceof Array;
/** /**
* @param {!(string|number|Array.<number>)} code * @param {!(string|number|Array.<number>)} code
* @returns {string} * @returns {string}
* @access public * @access public
* @static * @static
*/ */
ErrorsManager.prototype.type = code => { ErrorsManager.type = code => {
/** @type {Array.<string>} */ /** @type {Array.<string>} */
const types = ["string", "integer", "array"]; const types = ["string", "integer", "array"];
for(let i = 0; i < 3; i ++) for(let i = 0; i < 3; i ++)
if(ErrorsManager.prototype["is_" + types[i]](code)) if(ErrorsManager["is_" + types[i]](code))
return types[i]; return types[i];
return "unknown"; return "unknown";
}; };
return ErrorsManager;
})();

View File

@ -1,7 +1,10 @@
<script src="ecma/ErrorsManager.ecma.js"></script> <script type="module" charset="utf-8">
<script> "use strict";
errors_manager = new ErrorsManager(); import {ErrorsManager} from "./ecma/ErrorsManager.ecma.js";
/** @type {ErrorsManager} */
const errors_manager = new ErrorsManager();
console.log(errors_manager.to_string(105)); console.log(errors_manager.to_string(105));
console.log(errors_manager.to_array(105)); console.log(errors_manager.to_array(105));
@ -13,19 +16,29 @@
console.log(errors_manager.bits("pB")); console.log(errors_manager.bits("pB"));
console.log(errors_manager.bits([41, 1])); console.log(errors_manager.bits([41, 1]));
options = [-10, -5, -3, -1, 0, 1, 3, 5, 10]; /** @type {Array.<number>} */
filler = ""; const options = [-10, -5, -3, -1, 0, 1, 3, 5, 10],
/** @type {Array.<number|string|Array.<number>} */
results = []; results = [];
/** @type {string} */
let filler = "";
while(filler.length < 30) while(filler.length < 30)
filler += " "; filler += " ";
format = (value, characters) => " " + ( /**
* @param {!string} value
* @param {!number} character
* @returns string
* @access public
*/
const format = (value, characters) => " " + (
JSON.stringify(value).replace(/,/g, ", ") + filler JSON.stringify(value).replace(/,/g, ", ") + filler
).slice(0, characters) + " "; ).slice(0, characters) + " ";
[105, "pB", [41, 1]].forEach((value, i) => options.forEach(bits => { [105, "pB", [41, 1]].forEach((value, i) => options.forEach(bits => {
/** @type {string|number|Array.<number>} */
const new_value = errors_manager.bitwise(value, bits); const new_value = errors_manager.bitwise(value, bits);
results.push([ results.push([

View File

@ -1 +1 @@
0.0.1.25 0.0.1.27