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,8 +1,20 @@
"use strict";
/**
* @class
* @constructor
* @param {string|Array.<string>} [alphabet]
* @returns {void}
* @access public
*/
export const ErrorsManager = (function(){
/**
* @constructs ErrorsManager
* @param {string|Array.<string>} [alphabet]
* @returns {void}
* @access private
*/
const ErrorsManager = function(alphabet){
/** @type {ErrorsManager} */
@ -14,7 +26,7 @@ const ErrorsManager = function(alphabet){
/**
* @returns {void}
* @access public
* @access private
*/
const constructor = () => {
@ -32,7 +44,7 @@ const ErrorsManager = function(alphabet){
error = (
new_alphabet === undefined ? 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;
if(!error){
@ -42,7 +54,7 @@ const ErrorsManager = function(alphabet){
const original_length = new_alphabet.length,
/** @type {number} */
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;
error |= (
@ -55,7 +67,7 @@ const ErrorsManager = function(alphabet){
alphabet = error || !new_alphabet ? (
alphabet && alphabet.length ? alphabet :
ErrorsManager.prototype.BASE64) : new_alphabet;
ErrorsManager.BASE64) : new_alphabet;
// re_hexa_error = new RegExp("[^" + alphabet[0] + "]");
return error;
@ -72,14 +84,14 @@ const ErrorsManager = function(alphabet){
/** @type {Array.<number>} */
let array = [];
if(ErrorsManager.prototype.is_string(code))
if(ErrorsManager.is_string(code))
array = code.split("").map(hexa => alphabet.indexOf(hexa));
else if(ErrorsManager.prototype.is_integer(code)){
else if(ErrorsManager.is_integer(code)){
while(code){
array.push(code & 0x3F);
code >>>= 6;
};
}else if(ErrorsManager.prototype.is_array(code))
}else if(ErrorsManager.is_array(code))
array = [].concat(code);
while(array.length < length)
@ -131,12 +143,12 @@ const ErrorsManager = function(alphabet){
* @access public
*/
this.bits = code => {
if(ErrorsManager.prototype.is_integer(code))
if(ErrorsManager.is_integer(code))
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 null;
};
@ -154,11 +166,11 @@ const ErrorsManager = function(alphabet){
* @access public
*/
this.to_integer = code => {
if(ErrorsManager.prototype.is_integer(code))
if(ErrorsManager.is_integer(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;
if(!ErrorsManager.prototype.is_string(code))
if(!ErrorsManager.is_string(code))
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;
};
@ -174,15 +186,15 @@ const ErrorsManager = function(alphabet){
/** @type {string} */
let string = "";
if(ErrorsManager.prototype.is_string(code))
if(ErrorsManager.is_string(code))
string = code;
else{
if(ErrorsManager.prototype.is_integer(code)){
if(ErrorsManager.is_integer(code)){
while(code){
string += alphabet[code & 0x3F];
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];
};
@ -199,11 +211,11 @@ const ErrorsManager = function(alphabet){
* @access public
*/
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 (
ErrorsManager.prototype.is_string(code) ? code.split("").some(hexa => hexa != alphabet[0]) :
ErrorsManager.prototype.is_integer(code) ? !!code :
ErrorsManager.prototype.is_array(code) ? code.some(hexa => !!hexa) :
ErrorsManager.is_string(code) ? code.split("").some(hexa => hexa != alphabet[0]) :
ErrorsManager.is_integer(code) ? !!code :
ErrorsManager.is_array(code) ? code.some(hexa => !!hexa) :
null);
/** @type {Array.<number>} */
@ -212,7 +224,7 @@ const ErrorsManager = function(alphabet){
l = error.length * 6;
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;
};
@ -229,18 +241,18 @@ const ErrorsManager = function(alphabet){
* @access public
*/
this.compact = code => {
if(ErrorsManager.prototype.is_string(code)){
if(ErrorsManager.is_string(code)){
while(code && code[code.length - 1] == alphabet[0])
code = code.substr(0, code.length - 1);
return code || alphabet[0];
};
if(ErrorsManager.prototype.is_array(code)){
if(ErrorsManager.is_array(code)){
code = [].concat(code);
while(code.length && !code[code.length - 1])
code.pop();
return code.length ? code : [0];
};
if(ErrorsManager.prototype.is_integer(code))
if(ErrorsManager.is_integer(code))
return code;
return 0;
};
@ -264,7 +276,7 @@ const ErrorsManager = function(alphabet){
/** @type {number} */
rest = bits % 6,
/** @type {string} */
type_method = "to_" + ErrorsManager.prototype.type(code);
type_method = "to_" + ErrorsManager.type(code);
if(reverse){
@ -294,8 +306,10 @@ const ErrorsManager = function(alphabet){
if(rest){
/** @type {number} */
const r = 6 - rest,
/** @type {Array.<number>} */
const block = [~-(1 << (r = 6 - rest)), ~-(1 << rest) << r],
block = [~-(1 << r), ~-(1 << rest) << r],
/** @type {number} */
l = code.length - 1,
/** @type {number} */
@ -425,9 +439,9 @@ const ErrorsManager = function(alphabet){
code = self.bitwise(code, -_from);
return (
ErrorsManager.prototype.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.prototype.is_integer(code) ? code & ~-(1 << _to) :
ErrorsManager.is_string(code) ? code.slice(0, _to / 6 >> 0) + (rest ? alphabet[alphabet.indexOf(code.slice(-1)) & ~-(1 << rest)] : "") :
ErrorsManager.is_array(code) ? code.slice(0, _to / 6 >> 0).concat(rest ? [code[code.length - 1] & ~-(1 << rest)] : []) :
ErrorsManager.is_integer(code) ? code & ~-(1 << _to) :
null);
};
@ -444,8 +458,8 @@ const ErrorsManager = function(alphabet){
};
/** @type {Array.<string>} */
ErrorsManager.prototype.BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".split("");
// /** @type {Array.<string>} */
// ErrorsManager.BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".split("");
/**
* @param {?any} value
@ -453,7 +467,7 @@ ErrorsManager.prototype.BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst
* @access public
* @static
*/
ErrorsManager.prototype.is_string = value => typeof value == "string";
ErrorsManager.is_string = value => typeof value == "string";
/**
* @param {?any} value
@ -461,7 +475,7 @@ ErrorsManager.prototype.is_string = value => typeof value == "string";
* @access public
* @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
@ -469,7 +483,7 @@ ErrorsManager.prototype.is_integer = value => typeof value == "number" && value
* @access public
* @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
@ -477,13 +491,16 @@ ErrorsManager.prototype.is_array = value => typeof value == "object" && value in
* @access public
* @static
*/
ErrorsManager.prototype.type = code => {
ErrorsManager.type = code => {
/** @type {Array.<string>} */
const types = ["string", "integer", "array"];
for(let i = 0; i < 3; i ++)
if(ErrorsManager.prototype["is_" + types[i]](code))
if(ErrorsManager["is_" + types[i]](code))
return types[i];
return "unknown";
};
return ErrorsManager;
})();

View File

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

View File

@ -1 +1 @@
0.0.1.25
0.0.1.27