WIP: use es-modules

This commit is contained in:
j 2026-02-16 22:16:19 +01:00
commit baef49f116
143 changed files with 738 additions and 55 deletions

65
source/Ox/index.js Normal file
View file

@ -0,0 +1,65 @@
'use strict';
import Ox from './js/Namespace.js';
import * as OxCore from './js/Core.js';
import * as OxFunction from './js/Function.js';
import * as OxPolyfill from './js/Polyfill.js';
import * as OxArray from './js/Array.js';
import * as OxString from './js/String.js';
import * as OxCollection from './js/Collection.js';
import * as OxMath from './js/Math.js';
import * as OxAsync from './js/Async.js';
import * as OxColor from './js/Color.js';
import * as OxConstants from './js/Constants.js';
import * as OxDate from './js/Date.js';
import * as OxDOM from './js/DOM.js';
import * as OxEncoding from './js/Encoding.js';
import * as OxFormat from './js/Format.js';
import * as OxGeo from './js/Geo.js';
import * as OxHash from './js/Hash.js';
import * as OxHTML from './js/HTML.js';
import * as OxJavaScript from './js/JavaScript.js';
import * as OxLocale from './js/Locale.js';
import * as OxObject from './js/Object.js';
import * as OxRegExp from './js/RegExp.js';
import * as OxRequest from './js/Request.js';
import * as OxType from './js/Type.js';
import * as OxVideo from './js/Video.js';
export default Ox;
export { Ox };
// For backward compatibility with global usage
if (typeof globalThis !== 'undefined') {
globalThis.Ox = Ox;
/*@
Ox.documentReady <function> Calls a callback function once the DOM is ready
(callback) -> <b> If true, the document was ready
callback <f> Callback function
@*/
Ox.documentReady = (function() {
var callbacks = [];
document.onreadystatechange = globalThis.onload = function() {
if (document.readyState == 'complete') {
callbacks.forEach(function(callback) {
callback();
});
document.onreadystatechange = globalThis.onload = null;
}
};
return function(callback) {
if (document.readyState == 'complete') {
callback();
return true;
} else {
callbacks.push(callback);
return false;
}
};
}());
}

View file

@ -1,5 +1,7 @@
'use strict';
import Ox from './Namespace.js';
/*@
Ox.api <f> Turns an array into a list API
`Ox.api` takes an array and returns a function that allows you to run

View file

@ -1,5 +1,7 @@
'use strict';
import Ox from './Namespace.js';
(function() {
function asyncMap(forEach, collection, iterator, that, callback) {

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
/*@
Ox.avg <f> Returns the average of an array's values, or an object's properties

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
/*@
Ox.hsl <f> Takes RGB values and returns HSL values

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
//@ Ox.AMPM <[s]> ['AM', 'PM']
Ox.AMPM = ['AM', 'PM'];
@ -101,6 +102,8 @@ Ox.PATH = (function() {
return src.replace(regexp, '');
}
}
// FIXME: fix path detection
return './source/';
}());
//@ Ox.MODE <s> Mode ('dev' or 'min')
Ox.MODE = Ox.PATH.slice(0, -1).split('/').pop();

View file

@ -2,15 +2,7 @@
'use strict';
/*@
Ox <f> The `Ox` object
See `Ox.wrap` for details.
(value) -> <o> wrapped value
value <*> Any value
@*/
this.Ox = function(value) {
return Ox.wrap(value);
};
import Ox from './Namespace.js';
/*@
Ox.load <f> Loads OxJS and, optionally, one or more modules
@ -76,20 +68,17 @@ Ox.load = function() {
if (!length) {
callback(true);
} else {
Ox.forEach(modules, function(options, module) {
Ox.getFile(
Ox.PATH + module + '/' + module + '.js?' + Ox.VERSION,
function() {
Ox.load[module](options, function(success) {
succeeded += success;
if (++loaded == length) {
Ox.setLocale(Ox.LOCALE, function() {
callback(succeeded == length);
});
}
Ox.forEach(modules, async function(options, module) {
console.log("load module!", module, options)
const obj = await import('../../' + module + '/index.js?' + Ox.VERSION);
Ox.load[module](options, function(success) {
succeeded += success;
if (++loaded == length) {
Ox.setLocale(Ox.LOCALE, function() {
callback(succeeded == length);
});
}
);
});
});
}
});
@ -114,7 +103,7 @@ Ox.localStorage = function(namespace) {
var localStorage;
try {
// this will fail if third party cookies/storage is not allowed
localStorage = window.localStorage || {};
localStorage = globalThis.localStorage || {};
// FF 3.6 can't assign to or iterate over localStorage
for (var key in localStorage) {}
// In Safari (OS X or iOS) is in private browsing mode,
@ -206,7 +195,7 @@ Ox.Log = (function() {
args.unshift(
Ox.formatDate(date, '%H:%M:%S.') + (+date).toString().slice(-3)
);
window.console && window.console.log && window.console.log.apply(window.console, args);
globalThis.console && globalThis.console.log && globalThis.console.log.apply(globalThis.console, args);
ret = args.join(' ');
}
return ret;
@ -268,7 +257,7 @@ Ox.print = function() {
args.unshift(
date.toString().split(' ')[4] + '.' + (+date).toString().slice(-3)
);
window.console && window.console.log.apply(window.console, args);
globalThis.console && globalThis.console.log.apply(globalThis.console, args);
return args.join(' ');
};

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
/*@
Ox.$ <f> Generic HTML element, mimics jQuery
@ -828,12 +829,12 @@ Ox.documentReady <function> Calls a callback function once the DOM is ready
@*/
Ox.documentReady = (function() {
var callbacks = [];
document.onreadystatechange = window.onload = function() {
document.onreadystatechange = globalThis.onload = function() {
if (document.readyState == 'complete') {
callbacks.forEach(function(callback) {
callback();
});
document.onreadystatechange = window.onload = null;
document.onreadystatechange = globalThis.onload = null;
}
};
return function(callback) {

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
//@ Ox.getDate <f> Get the day of a date, optionally UTC
// see Ox.setSeconds for source code

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
/*@
Ox.encodeBase26 <b> Encode a number as bijective base26

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
/*@
Ox.formatArea <f> Formats a number of meters as square meters or kilometers

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
/*@
Ox.cache <f> Memoize a function

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
(function() {

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
(function() {

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
/*@
Ox.oshash <f> Calculates oshash for a given file or blob object. Async.

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
/*@
Ox.doc <f> Generates documentation for annotated JavaScript

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
(function() {

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
/*@
Ox.acosh <f> Inverse hyperbolic cosine

14
source/Ox/js/Namespace.js Normal file
View file

@ -0,0 +1,14 @@
'use strict';
/*@
Ox <f> The `Ox` object
See `Ox.wrap` for details.
(value) -> <o> wrapped value
value <*> Any value
@*/
export const Ox = function(value) {
return Ox.wrap(value)
};
export default Ox;

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
/*@
Ox.extend <function> Extends an object with one or more other objects

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
(function(window) {
@ -427,4 +428,4 @@
}
}
}(this));
}(globalThis));

View file

@ -1,3 +1,5 @@
import Ox from './Namespace.js';
/*@
Ox.escapeRegExp <f> Escapes a string for use in a regular expression
(str) -> <r> Escaped string
@ -10,4 +12,4 @@ Ox.escapeRegExp <f> Escapes a string for use in a regular expression
// see https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions
Ox.escapeRegExp = function(string) {
return (string + '').replace(/([\/\\^$*+?.\-|(){}[\]])/g, '\\$1');
};
};

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
/*@
Ox.get <f> Get a remote resource

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
/*@
Ox.char <f> Alias for String.fromCharCode
@ -265,17 +266,30 @@ Ox.parseURL <f> Takes a URL, returns its components
'?a=0&b=1'
@*/
Ox.parseURL = (function() {
var a = document.createElement('a'),
keys = ['hash', 'host', 'hostname', 'origin',
'pathname', 'port', 'protocol', 'search'];
return function(string) {
var ret = {};
a.href = string;
keys.forEach(function(key) {
ret[key] = a[key];
});
return ret;
};
const keys = [
'hash', 'host', 'hostname', 'origin',
'pathname', 'port', 'protocol', 'search'
];
if (typeof document == 'undefined') {
return function(string) {
const a = new URL(string);
var ret = {};
keys.forEach(function(key) {
ret[key] = a[key];
});
return ret;
}
} else {
var a = document.createElement('a');
return function(string) {
var ret = {};
a.href = string;
keys.forEach(function(key) {
ret[key] = a[key];
});
return ret;
};
}
}());
// FIXME: can we get rid of this?

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
/*@
Ox.checkType <f> Throws a TypeError if a value is not of a given type

View file

@ -1,4 +1,5 @@
'use strict';
import Ox from './Namespace.js';
/*@
Ox.getVideoFormat <f> Get supported video format