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,19 @@
'use strict';
import * as OxCore from './Core.js';
import * as OxBase from './Base.js';
import * as OxFunction from './Function.js';
import * as OxType from './Type.js';
const Ox = {};
Object.assign(Ox,
OxCore,
OxBase,
OxFunction,
OxType,
);
/*@
Ox.api <f> Turns an array into a list API
`Ox.api` takes an array and returns a function that allows you to run
@ -163,7 +177,7 @@ Ox.api <f> Turns an array into a list API
> Ox.test.apiResults[9].data
{items: [{name: 'John Cale'}, {name: 'Brian Eno'}]}
@*/
Ox.api = function(items, options) {
export function api(items, options) {
options = options || {};
@ -442,7 +456,7 @@ Ox.compact <f> Removes `null` or `undefined` values from an array
> Ox.compact([null,,1,,2,,3])
[1, 2, 3]
@*/
Ox.compact = function(array) {
export function compact(array) {
return array.filter(function(value) {
return value != null;
});
@ -461,7 +475,7 @@ Ox.find <f> Returns array elements that match a string
> Ox.find(['Bar', 'Barfoo', 'Foo', 'Foobar'], 'foo', true)
['Foo', 'Foobar']
@*/
Ox.find = function(array, string, leading) {
export function find(array, string, leading) {
var matches = [[], []];
string = string.toLowerCase();
array.forEach(function(value) {
@ -480,7 +494,7 @@ Ox.flatten <f> Flattens an array
> Ox.flatten([1, [2, [3], 2], 1])
[1, 2, 3, 2, 1]
@*/
Ox.flatten = function(array) {
export function flatten(array) {
var ret = [];
array.forEach(function(value) {
if (Ox.isArray(value)) {
@ -493,7 +507,7 @@ Ox.flatten = function(array) {
};
// FIXME: add docs and tests
Ox.getIndex = function(array, key, value) {
export function getIndex(array, key, value) {
return Ox.indexOf(array, function(obj) {
return obj[key] === value;
});
@ -509,13 +523,13 @@ Ox.getIndexById <f> Returns the first array index of an object with a given id
> Ox.getIndexById([{id: 'foo', str: 'Foo'}, {id: 'bar', str: 'Bar'}], 'baz')
-1
@*/
Ox.getIndexById = function(array, id) {
return Ox.getIndex(array, 'id', id);
export function getIndexById(array, id) {
return getIndex(array, 'id', id);
};
// FIXME: add docs and tests
Ox.getObject = function(array, key, value) {
var index = Ox.getIndex(array, key, value);
export function getObject(array, key, value) {
var index = getIndex(array, key, value);
return index > -1 ? array[index] : null;
};
@ -529,12 +543,12 @@ Ox.getObjectById <f> Returns the first object in an array with a given id
> Ox.getObjectById([{id: 'foo', str: 'Foo'}, {id: 'bar', str: 'Bar'}], 'baz')
null
@*/
Ox.getObjectById = function(array, id) {
return Ox.getObject(array, 'id', id);
export function getObjectById(array, id) {
return getObject(array, 'id', id);
};
/*
Ox.indexOf = function(arr) {
export function indexOf(arr) {
// indexOf for primitives, test for function, deep equal for others
};
*/
@ -555,7 +569,7 @@ Ox.last <f> Gets or sets the last element of an array
> Ox.last('123')
'3'
@*/
Ox.last = function(array, value) {
export function last(array, value) {
var ret;
if (arguments.length == 1) {
ret = array[array.length - 1];
@ -576,7 +590,7 @@ Ox.makeArray <f> Wraps any non-array in an array.
['foo']
@*/
// FIXME: rename to toArray
Ox.makeArray = function(value) {
export function makeArray(value) {
var ret, type = Ox.typeOf(value);
if (type == 'arguments' || type == 'nodelist') {
ret = Ox.slice(value);
@ -599,7 +613,7 @@ Ox.nextValue <f> Next value, given an array of numbers, a number and a direction
> Ox.nextValue([], 1, 1)
void 0
@*/
Ox.nextValue = function(array, value, direction) {
export function nextValue(array, value, direction) {
var found = false, nextValue;
direction = direction || 1;
direction == -1 && array.reverse();
@ -641,7 +655,7 @@ Ox.range <f> Python-style range
> Ox.range(-1, -2, -0.5)
[-1, -1.5]
@*/
Ox.range = function() {
export function range() {
var array = [];
Ox.loop.apply(null, Ox.slice(arguments).concat(function(index) {
array.push(index);
@ -768,7 +782,7 @@ Ox.unique <f> Removes duplicate values from an array
> Ox.unique('foo')
'fo'
@*/
Ox.unique = function(array) {
export function unique(array) {
return Ox.filter(array, function(value, index) {
return array.indexOf(value) == index;
});
@ -781,7 +795,7 @@ Ox.zip <f> Zips an array of arrays
> Ox.zip([0, 1, 2], [3, 4, 5])
[[0, 3], [1, 4], [2, 5]]
@*/
Ox.zip = function() {
export function zip() {
var args = arguments.length == 1 ? arguments[0] : Ox.slice(arguments),
array = [];
args[0].forEach(function(value, index) {