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,9 +1,21 @@
'use strict';
import * as OxCore from './Core.js';
import * as OxBase from './Base.js';
import * as OxCollection from './Collection.js';
const Ox = {};
Object.assign(Ox,
OxCore,
OxBase,
OxCollection,
);
/*@
Ox.char <f> Alias for String.fromCharCode
@*/
Ox.char = String.fromCharCode;
export const char = String.fromCharCode;
/*@
Ox.clean <f> Remove leading, trailing and double whitespace from a string
@ -18,7 +30,7 @@ Ox.clean <f> Remove leading, trailing and double whitespace from a string
> Ox.clean(' foo\tbar ')
'foo bar'
@*/
Ox.clean = function(string) {
export function clean(string) {
return Ox.filter(Ox.map(string.split('\n'), function(string) {
return string.replace(/\s+/g, ' ').trim() || '';
})).join('\n');
@ -30,7 +42,7 @@ Ox.codePointAt <f> Returns the code point at a given index
> Ox.codePointAt('\uD83D\uDCA9', 0)
0x1F4A9
@*/
Ox.codePointAt = function(string, index) {
export function codePointAt(string, index) {
var first, length = string.length, ret, second;
if (index >= 0 && index < length) {
first = string.charCodeAt(index);
@ -55,7 +67,7 @@ Ox.endsWith <f> Tests if a string ends with a given substring
> Ox.endsWith('foobar', 'foo')
false
@*/
Ox.endsWith = function(string, substring) {
export function endsWith(string, substring) {
string = string.toString();
substring = substring.toString();
return string.slice(string.length - substring.length) == substring;
@ -69,7 +81,7 @@ Ox.fromCodePoint <f> Returns a string for one or more given code points
> Ox.fromCodePoint(0x1F4A9)
'\uD83D\uDCA9'
@*/
Ox.fromCodePoint = function() {
export function fromCodePoint() {
var ret = '';
Ox.forEach(arguments, function(number) {
if (number < 0 || number > 0x10FFFF || !Ox.isInt(number)) {
@ -99,7 +111,7 @@ Ox.isValidEmail <f> Tests if a string is a valid e-mail address
> Ox.isValidEmail('foo@bar..com')
false
@*/
Ox.isValidEmail = function(string) {
export function isValidEmail(string) {
return !!/^[0-9A-Z\.\+\-_]+@(?:[0-9A-Z\-]+\.)+[A-Z]{2,64}$/i.test(string);
};
@ -140,7 +152,7 @@ Ox.pad <f> Pad a string to a given length
> Ox.pad('foo', -1)
''
@*/
Ox.pad = function(string, position, length, padding) {
export function pad(string, position, length, padding) {
var hasPosition = Ox.isString(arguments[1]),
isNumber = Ox.isNumber(arguments[0]),
last = Ox.last(arguments);
@ -166,7 +178,7 @@ Ox.parseDuration <f> Takes a formatted duration, returns seconds
> Ox.parseDuration('1::')
3600
@*/
Ox.parseDuration = function(string) {
export function parseDuration(string) {
return string.split(':').reverse().slice(0, 4).reduce(function(p, c, i) {
return p + (parseFloat(c) || 0) * (i == 3 ? 86400 : Math.pow(60, i));
}, 0);
@ -187,7 +199,7 @@ Ox.parsePath <f> Returns the components of a path
> Ox.parsePath('.foo')
{extension: '', filename: '.foo', pathname: ''}
@*/
Ox.parsePath = function(string) {
export function parsePath(string) {
var matches = /^(.+\/)?(.+?(\..+)?)?$/.exec(string);
return {
pathname: matches[1] || '',
@ -206,7 +218,7 @@ Ox.parseSRT <f> Parses an srt subtitle file
> Ox.parseSRT('1\n01:02:00,000 --> 01:02:03,400\nHello World')
[{'in': 3720, out: 3723.4, text: 'Hello World'}]
@*/
Ox.parseSRT = function(string, fps) {
export function parseSRT(string, fps) {
return string.replace(/\r\n/g, '\n').trim().split('\n\n')
.map(function(block) {
var lines = block.split('\n'), points;
@ -264,22 +276,35 @@ Ox.parseURL <f> Takes a URL, returns its components
> Ox.parseURL('http://www.foo.com:8080/bar/index.html?a=0&b=1#c').search
'?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;
};
export const parseURL = (function() {
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?
Ox.parseUserAgent = function(userAgent) {
export function parseUserAgent(userAgent) {
var aliases = {
browser: {
'Firefox': /(Fennec|Firebird|Iceweasel|Minefield|Namoroka|Phoenix|SeaMonkey|Shiretoko)/
@ -440,7 +465,7 @@ Ox.repeat <f> Repeat a value multiple times
@*/
// FIXME: see https://github.com/paulmillr/es6-shim/blob/master/es6-shim.js
// for a faster version
Ox.repeat = function(value, times) {
export function repeat(value, times) {
var ret;
if (Ox.isArray(value)) {
ret = [];
@ -458,7 +483,7 @@ Ox.splice <f> `[].splice` for strings, returns a new string
> Ox.splice('12xxxxx89', 2, 5, 3, 4, 5, 6, 7)
'123456789'
@*/
Ox.splice = function(string, index, remove) {
export function splice(string, index, remove) {
var array = string.split('');
Array.prototype.splice.apply(array, Ox.slice(arguments, 1));
return array.join('');
@ -474,7 +499,7 @@ Ox.startsWith <f> Tests if a string ends with a given substring
> Ox.startsWith('foobar', 'bar')
false
@*/
Ox.startsWith = function(string, substring) {
export function startsWith(string, substring) {
string = string.toString();
substring = substring.toString();
return string.slice(0, substring.length) == substring;
@ -489,7 +514,7 @@ Ox.toCamelCase <f> Takes a string with '-', '/' or '_', returns a camelCase stri
> Ox.toCamelCase('foo_bar_baz')
'fooBarBaz'
@*/
Ox.toCamelCase = function(string) {
export function toCamelCase(string) {
return string.replace(/[\-\/_][a-z]/g, function(string) {
return string[1].toUpperCase();
});
@ -500,7 +525,7 @@ Ox.toDashes <f> Takes a camelCase string, returns a string with dashes
> Ox.toDashes('fooBarBaz')
'foo-bar-baz'
@*/
Ox.toDashes = function(string) {
export function toDashes(string) {
return string.replace(/[A-Z]/g, function(string) {
return '-' + string.toLowerCase();
});
@ -511,7 +536,7 @@ Ox.toSlashes <f> Takes a camelCase string, returns a string with slashes
> Ox.toSlashes('fooBarBaz')
'foo/bar/baz'
@*/
Ox.toSlashes = function(string) {
export function toSlashes(string) {
return string.replace(/[A-Z]/g, function(string) {
return '/' + string.toLowerCase();
});
@ -524,7 +549,7 @@ Ox.toTitleCase <f> Returns a string with capitalized words
> Ox.toTitleCase('Apple releases iPhone, IBM stock plummets')
'Apple Releases iPhone, IBM Stock Plummets'
@*/
Ox.toTitleCase = function(string) {
export function toTitleCase(string) {
return string.split(' ').map(function(value) {
var substring = value.slice(1),
lowercase = substring.toLowerCase();
@ -540,7 +565,7 @@ Ox.toUnderscores <f> Takes a camelCase string, returns string with underscores
> Ox.toUnderscores('fooBarBaz')
'foo_bar_baz'
@*/
Ox.toUnderscores = function(string) {
export function toUnderscores(string) {
return string.replace(/[A-Z]/g, function(string) {
return '_' + string.toLowerCase();
});
@ -562,7 +587,7 @@ Ox.truncate <f> Truncate a string to a given length
> Ox.truncate('anticonstitutionellement', 'center', 16, '...')
'anticon...lement'
@*/
Ox.truncate = function(string, position, length, padding) {
export function truncate(string, position, length, padding) {
var hasPosition = Ox.isString(arguments[1]), last = Ox.last(arguments);
position = hasPosition ? arguments[1] : 'right';
length = hasPosition ? arguments[2] : arguments[1];
@ -589,7 +614,7 @@ Ox.words <f> Splits a string into words, removing punctuation
> Ox.words('Let\'s "split" array-likes into key/value pairs--okay?')
['let\'s', 'split', 'array-likes', 'into', 'key', 'value', 'pairs', 'okay']
@*/
Ox.words = function(string) {
export function words(string) {
var array = string.toLowerCase().split(/\b/),
length = array.length,
startsWithWord = /\w/.test(array[0]);
@ -635,7 +660,7 @@ Ox.wordwrap <f> Wrap a string at word boundaries
> Ox.wordwrap('These are short words', 16, true)
'These are \nshort words'
@*/
Ox.wordwrap = function(string, length) {
export function wordwrap(string, length) {
var balanced, lines, max, newline, words;
string = String(string);
length = length || 80;