WIP: try to convert to ES modules

This commit is contained in:
j 2026-02-09 18:59:12 +01:00
commit 299a08b6a3
29 changed files with 3003 additions and 2014 deletions

View file

@ -1,5 +1,17 @@
'use strict';
import * as OxObject from './Object.js';
import * as OxConstants from './Constants.js';
import * as OxMath from './Math.js';
const Ox = {};
Object.assign(Ox,
OxObject,
OxConstants,
OxMath
);
/*@
Ox.encodeBase26 <b> Encode a number as bijective base26
See <a href="http://en.wikipedia.org/wiki/Bijective_numeration">
@ -15,7 +27,7 @@ Ox.encodeBase26 <b> Encode a number as bijective base26
> Ox.encodeBase26(4461)
'FOO'
@*/
Ox.encodeBase26 = function(number) {
export function encodeBase26(number) {
var string = '';
while (number) {
string = String.fromCharCode(65 + (number - 1) % 26) + string;
@ -31,7 +43,7 @@ Ox.decodeBase26 <f> Decodes a bijective base26-encoded number
> Ox.decodeBase26('foo')
4461
@*/
Ox.decodeBase26 = function(string) {
export function decodeBase26(string) {
return string.toUpperCase().split('').reverse().reduce(function(p, c, i) {
return p + (c.charCodeAt(0) - 64) * Math.pow(26, i);
}, 0);
@ -45,7 +57,7 @@ Ox.encodeBase32 <b> Encode a number as base32
> Ox.encodeBase32(33819)
'110V'
@*/
Ox.encodeBase32 = function(number) {
export function encodeBase32(number) {
return Ox.map(number.toString(32), function(char) {
return Ox.BASE_32_DIGITS[parseInt(char, 32)];
});
@ -61,7 +73,7 @@ Ox.decodeBase32 <f> Decodes a base32-encoded number
> Ox.decodeBase32('?').toString()
'NaN'
@*/
Ox.decodeBase32 = function(string) {
export function decodeBase32(string) {
return parseInt(Ox.map(string.toUpperCase(), function(char) {
var index = Ox.BASE_32_DIGITS.indexOf(
Ox.BASE_32_ALIASES[char] || char
@ -75,7 +87,7 @@ Ox.encodeBase64 <f> Encode a number as base64
> Ox.encodeBase64(32394)
'foo'
@*/
Ox.encodeBase64 = function(number) {
export function encodeBase64(number) {
return btoa(Ox.encodeBase256(number)).replace(/=/g, '');
};
@ -84,7 +96,7 @@ Ox.decodeBase64 <f> Decodes a base64-encoded number
> Ox.decodeBase64('foo')
32394
@*/
Ox.decodeBase64 = function(string) {
export function decodeBase64(string) {
return Ox.decodeBase256(atob(string));
};
@ -93,7 +105,7 @@ Ox.encodeBase128 <f> Encode a number as base128
> Ox.encodeBase128(1685487)
'foo'
@*/
Ox.encodeBase128 = function(number) {
export function encodeBase128(number) {
var string = '';
while (number) {
string = Ox.char(number & 127) + string;
@ -107,7 +119,7 @@ Ox.decodeBase128 <f> Decode a base128-encoded number
> Ox.decodeBase128('foo')
1685487
@*/
Ox.decodeBase128 = function(string) {
export function decodeBase128(string) {
return string.split('').reverse().reduce(function(p, c, i) {
return p + (c.charCodeAt(0) << i * 7);
}, 0);
@ -118,7 +130,7 @@ Ox.encodeBase256 <f> Encode a number as base256
> Ox.encodeBase256(6713199)
'foo'
@*/
Ox.encodeBase256 = function(number) {
export function encodeBase256(number) {
var string = '';
while (number) {
string = Ox.char(number & 255) + string;
@ -132,7 +144,7 @@ Ox.decodeBase256 <f> Decode a base256-encoded number
> Ox.decodeBase256('foo')
6713199
@*/
Ox.decodeBase256 = function(string) {
export function decodeBase256(string) {
return string.split('').reverse().reduce(function(p, c, i) {
return p + (c.charCodeAt(0) << i * 8);
}, 0);
@ -149,7 +161,7 @@ Ox.encodeDeflate <f> Encodes a string, using deflate
> Ox.decodeDeflate(Ox.encodeDeflate('foo'), function(str) { Ox.test(str, 'foo'); })
undefined
@*/
Ox.encodeDeflate = function(string, callback) {
export function encodeDeflate(string, callback) {
// Make sure we can encode the full unicode range of characters.
string = Ox.encodeUTF8(string);
// We can only safely write to RGB, so we need 1 pixel for 3 bytes.
@ -198,7 +210,7 @@ Ox.decodeDeflate <f> Decodes an deflate-encoded string
str <s> The decoded string
@*/
Ox.decodeDeflate = function(string, callback) {
export function decodeDeflate(string, callback) {
var image = new Image(),
// PNG file signature and IHDR chunk
data = '\u0089PNG\r\n\u001A\n\u0000\u0000\u0000\u000DIHDR'
@ -292,7 +304,7 @@ Ox.encodeUTF8 <f> Encodes a string as UTF-8
> Ox.encodeUTF8("¥€$")
"\u00C2\u00A5\u00E2\u0082\u00AC\u0024"
@*/
Ox.encodeUTF8 = function(string) {
export function encodeUTF8(string) {
return Ox.map(string, function(char) {
var code = char.charCodeAt(0),
string = '';
@ -320,7 +332,7 @@ Ox.decodeUTF8 <f> Decodes an UTF-8-encoded string
> Ox.decodeUTF8('\u00C2\u00A5\u00E2\u0082\u00AC\u0024')
'¥€$'
@*/
Ox.decodeUTF8 = function(string) {
export function decodeUTF8(string) {
var code, i = 0, length = string.length, ret = '';
function error(byte, position) {
throw new RangeError(