WIP: try to convert to ES modules
This commit is contained in:
parent
ec5b050496
commit
299a08b6a3
29 changed files with 3003 additions and 2014 deletions
|
|
@ -1,5 +1,22 @@
|
|||
'use strict';
|
||||
|
||||
import * as OxCore from './Core.js';
|
||||
import * as OxFunction from './Function.js';
|
||||
import * as OxConstants from './Constants.js';
|
||||
import * as OxType from './Type.js';
|
||||
import * as OxObject from './Object.js';
|
||||
|
||||
const Ox = {};
|
||||
|
||||
Object.assign(Ox,
|
||||
OxCore,
|
||||
OxConstants,
|
||||
OxType,
|
||||
OxObject,
|
||||
OxFunction,
|
||||
);
|
||||
|
||||
|
||||
/*@
|
||||
Ox.avg <f> Returns the average of an array's values, or an object's properties
|
||||
(collection) -> <n> Average value
|
||||
|
|
@ -11,8 +28,8 @@ Ox.avg <f> Returns the average of an array's values, or an object's properties
|
|||
> Ox.avg('avg is 0.1')
|
||||
0.1
|
||||
@*/
|
||||
Ox.avg = function(collection) {
|
||||
return Ox.sum(collection) / Ox.len(collection);
|
||||
export function avg(collection) {
|
||||
return Ox.sum(collection) / len(collection);
|
||||
};
|
||||
|
||||
/*@
|
||||
|
|
@ -28,13 +45,13 @@ Ox.clone <f> Returns a (shallow or deep) copy of an array or object
|
|||
> (function() { var a = [[0, 1]], b = Ox.clone(a, true); a[0][0] = null; return b[0]; }())
|
||||
[0, 1]
|
||||
@*/
|
||||
Ox.clone = function(collection, deep) {
|
||||
export function clone(collection, deep) {
|
||||
var ret, type = Ox.typeOf(collection);
|
||||
if (type != 'array' && type != 'object') {
|
||||
ret = collection;
|
||||
} else if (deep) {
|
||||
ret = type == 'array' ? [] : {};
|
||||
Ox.forEach(collection, function(value, key) {
|
||||
forEach(collection, function(value, key) {
|
||||
type = Ox.typeOf(value);
|
||||
ret[key] = type == 'array' || type == 'object'
|
||||
? Ox.clone(value, true) : value;
|
||||
|
|
@ -59,7 +76,7 @@ Ox.contains <f> Tests if a collection contains a value
|
|||
> Ox.contains('foobar', 'bar')
|
||||
true
|
||||
@*/
|
||||
Ox.contains = function(collection, value) {
|
||||
export function contains(collection, value) {
|
||||
var type = Ox.typeOf(collection);
|
||||
return (
|
||||
type == 'nodelist' || type == 'object'
|
||||
|
|
@ -84,9 +101,9 @@ Ox.count <f> Counts the occurences of values in a collection
|
|||
> Ox.count('foo', 'x')
|
||||
0
|
||||
@*/
|
||||
Ox.count = function(collection, value) {
|
||||
export function count(collection, value) {
|
||||
var count = {};
|
||||
Ox.forEach(collection, function(value) {
|
||||
forEach(collection, function(value) {
|
||||
count[value] = (count[value] || 0) + 1;
|
||||
});
|
||||
return value ? count[value] || 0 : count;
|
||||
|
|
@ -111,42 +128,13 @@ Ox.every <f> Tests if every element of a collection satisfies a given condition
|
|||
> Ox.every([true, true, true])
|
||||
true
|
||||
@*/
|
||||
Ox.every = function(collection, iterator, that) {
|
||||
export function every(collection, iterator, that) {
|
||||
iterator = iterator || Ox.identity;
|
||||
return Ox.forEach(collection, function(value, key, collection) {
|
||||
return forEach(collection, function(value, key, collection) {
|
||||
return !!iterator.call(that, value, key, collection);
|
||||
}) == Ox.len(collection);
|
||||
}) == len(collection);
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.filter <f> Filters a collection by a given condition
|
||||
Unlike `Array.prototype.filter`, `Ox.filter` works for arrays, objects and
|
||||
strings.
|
||||
> Ox.filter([2, 1, 0], function(v, i) { return v == i; })
|
||||
[1]
|
||||
> Ox.filter({a: 'c', b: 'b', c: 'a'}, function(v, k) { return v == k; })
|
||||
{b: 'b'}
|
||||
> Ox.filter(' foo bar ', function(v) { return v != ' '; })
|
||||
'foobar'
|
||||
@*/
|
||||
Ox.filter = function(collection, iterator, that) {
|
||||
var ret, type = Ox.typeOf(collection);
|
||||
iterator = iterator || Ox.identity;
|
||||
if (type == 'object' || type == 'storage') {
|
||||
ret = {};
|
||||
Ox.forEach(collection, function(value, key) {
|
||||
if (iterator.call(that, value, key, collection)) {
|
||||
ret[key] = value;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
ret = Ox.slice(collection).filter(iterator, that);
|
||||
if (type == 'string') {
|
||||
ret = ret.join('');
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.forEach <f> forEach loop
|
||||
|
|
@ -171,7 +159,7 @@ Ox.forEach <f> forEach loop
|
|||
> Ox.forEach({a: 'f', b: 'o', c: 'o'}, function(v, k) { return v != 'o' });
|
||||
1
|
||||
@*/
|
||||
Ox.forEach = function(collection, iterator, that) {
|
||||
export function forEach(collection, iterator, that) {
|
||||
var i = 0, key, type = Ox.typeOf(collection);
|
||||
if (type == 'object' || type == 'storage') {
|
||||
for (key in collection) {
|
||||
|
|
@ -197,25 +185,6 @@ Ox.forEach = function(collection, iterator, that) {
|
|||
return i;
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.indexOf <f> Returns the first index of a collection element that passes a test
|
||||
> Ox.indexOf([1, 2, 3], function(val) { return val % 2 == 0; })
|
||||
1
|
||||
> Ox.indexOf({a: 1, b: 2, c: 3}, function(val) { return val % 2 == 0; })
|
||||
'b'
|
||||
> Ox.indexOf('FooBar', function(val) { return val == val.toUpperCase(); })
|
||||
0
|
||||
> Ox.indexOf([1, 2, 3], function(val) { return val == 0; })
|
||||
-1
|
||||
@*/
|
||||
Ox.indexOf = function(collection, test) {
|
||||
var index = Ox.forEach(collection, function(value) {
|
||||
return !test(value); // break if test succeeds
|
||||
});
|
||||
return Ox.isObject(collection) ? Object.keys(collection)[index] || null
|
||||
: index == collection.length ? -1 : index;
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.indicesOf <f> Returns all indices of collection elements that pass a test
|
||||
> Ox.indicesOf([1, 2, 3], function(val) { return val % 2 == 1; })
|
||||
|
|
@ -227,9 +196,9 @@ Ox.indicesOf <f> Returns all indices of collection elements that pass a test
|
|||
> Ox.indicesOf([1, 2, 3], function(val) { return val == 0; })
|
||||
[]
|
||||
@*/
|
||||
Ox.indicesOf = function(collection, test) {
|
||||
export function indicesOf(collection, test) {
|
||||
var ret = [];
|
||||
Ox.forEach(collection, function(value, index) {
|
||||
forEach(collection, function(value, index) {
|
||||
test(value) && ret.push(index);
|
||||
});
|
||||
return ret;
|
||||
|
|
@ -256,7 +225,7 @@ Ox.len <f> Returns the length of an array, nodelist, object, storage or string
|
|||
> Ox.len(function(a, b, c) {})
|
||||
undefined
|
||||
@*/
|
||||
Ox.len = function(collection) {
|
||||
export function len(collection) {
|
||||
var ret, type = Ox.typeOf(collection);
|
||||
if (
|
||||
type == 'arguments' || type == 'array'
|
||||
|
|
@ -282,11 +251,11 @@ Ox.map <f> Transforms the values of an array, object or string
|
|||
> Ox.map([,], function(v, i) { return i; })
|
||||
[,]
|
||||
@*/
|
||||
Ox.map = function(collection, iterator, that) {
|
||||
export function map(collection, iterator, that) {
|
||||
var ret, type = Ox.typeOf(collection);
|
||||
if (type == 'object' || type == 'storage') {
|
||||
ret = {};
|
||||
Ox.forEach(collection, function(value, key) {
|
||||
forEach(collection, function(value, key) {
|
||||
ret[key] = iterator.call(that, value, key, collection);
|
||||
});
|
||||
} else {
|
||||
|
|
@ -309,7 +278,7 @@ Ox.max <f> Returns the maximum value of a collection
|
|||
> Ox.max([])
|
||||
-Infinity
|
||||
@*/
|
||||
Ox.max = function(collection) {
|
||||
export function max(collection) {
|
||||
var ret, values = Ox.values(collection);
|
||||
if (values.length < Ox.STACK_LENGTH) {
|
||||
ret = Math.max.apply(null, values);
|
||||
|
|
@ -332,7 +301,7 @@ Ox.min <f> Returns the minimum value of a collection
|
|||
> Ox.min([])
|
||||
Infinity
|
||||
@*/
|
||||
Ox.min = function(collection) {
|
||||
export function min(collection) {
|
||||
var ret, values = Ox.values(collection);
|
||||
if (values.length < Ox.STACK_LENGTH) {
|
||||
ret = Math.min.apply(null, values);
|
||||
|
|
@ -359,8 +328,8 @@ Ox.numberOf <f> Returns the number of elements in a collection that pass a test
|
|||
> Ox.numberOf('foo', function(v, k, c) { return v == c[k - 1]; })
|
||||
1
|
||||
@*/
|
||||
Ox.numberOf = function(collection, test) {
|
||||
return Ox.len(Ox.filter(collection, test));
|
||||
export function numberOf(collection, test) {
|
||||
return len(Ox.filter(collection, test));
|
||||
};
|
||||
|
||||
/*@
|
||||
|
|
@ -381,7 +350,7 @@ Ox.remove <f> Removes an element from an array or object and returns it
|
|||
> Ox.test.collection
|
||||
[['a', 'c'], {a: 0, c: 2}]
|
||||
@*/
|
||||
Ox.remove = function(collection, element) {
|
||||
export function remove(collection, element) {
|
||||
var ret, key;
|
||||
if (Ox.isArray(collection)) {
|
||||
key = collection.indexOf(element);
|
||||
|
|
@ -405,7 +374,7 @@ Ox.reverse <f> Reverses an array or string
|
|||
> Ox.reverse('foobar')
|
||||
'raboof'
|
||||
@*/
|
||||
Ox.reverse = function(collection) {
|
||||
export function reverse(collection) {
|
||||
return Ox.isArray(collection)
|
||||
? Ox.clone(collection).reverse()
|
||||
: collection.toString().split('').reverse().join('');
|
||||
|
|
@ -420,7 +389,7 @@ Ox.shuffle <f> Randomizes the order of values within a collection
|
|||
> Ox.shuffle('123').split('').sort().join('')
|
||||
'123'
|
||||
@*/
|
||||
Ox.shuffle = function(collection) {
|
||||
export function shuffle(collection) {
|
||||
var keys, ret, type = Ox.typeOf(collection), values;
|
||||
if (type == 'object' || type == 'storage') {
|
||||
keys = Object.keys(collection);
|
||||
|
|
@ -443,57 +412,6 @@ Ox.shuffle = function(collection) {
|
|||
return ret;
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.slice <f> Alias for `Array.prototype.slice.call`
|
||||
(collection[, start[, stop]]) -> <a> Array
|
||||
collection <a|o|s> Array-like
|
||||
start <n> Start position
|
||||
stop <n> Stop position
|
||||
> (function() { return Ox.slice(arguments); }(1, 2, 3))
|
||||
[1, 2, 3]
|
||||
> Ox.slice('foo', 0, 1);
|
||||
['f']
|
||||
> Ox.slice({0: 'f', 1: 'o', 2: 'o', length: 3}, -2)
|
||||
['o', 'o']
|
||||
@*/
|
||||
// FIXME: remove toArray alias
|
||||
Ox.slice = Ox.toArray = function(collection, start, stop) {
|
||||
return Array.prototype.slice.call(collection, start, stop);
|
||||
};
|
||||
// IE8 can't apply slice to NodeLists, returns an empty array if undefined is
|
||||
// passed as stop and returns an array of null values if a string is passed as
|
||||
// value. Firefox 3.6 returns an array of undefined values if a string is passed
|
||||
// as value.
|
||||
if (
|
||||
Ox.slice([0]).length == 0
|
||||
|| Ox.slice('0')[0] === null
|
||||
|| Ox.slice('0')[0] === void 0
|
||||
|| !(function() {
|
||||
try {
|
||||
return Ox.slice(document.getElementsByTagName('a'));
|
||||
} catch (error) {}
|
||||
}())
|
||||
) {
|
||||
// FIXME: remove toArray alias
|
||||
Ox.slice = Ox.toArray = function(collection, start, stop) {
|
||||
var args = stop === void 0 ? [start] : [start, stop],
|
||||
array = [], index, length, ret;
|
||||
if (Ox.typeOf(collection) == 'string') {
|
||||
collection = collection.split('');
|
||||
}
|
||||
try {
|
||||
ret = Array.prototype.slice.apply(collection, args);
|
||||
} catch (error) {
|
||||
length = collection.length;
|
||||
for (index = 0; index < length; index++) {
|
||||
array[index] = collection[index];
|
||||
}
|
||||
ret = Array.prototype.slice.apply(array, args);
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
}
|
||||
|
||||
/*@
|
||||
Ox.some <f> Tests if one or more elements of a collection meet a given condition
|
||||
Unlike `Array.prototype.some`, `Ox.some` works for arrays, objects and
|
||||
|
|
@ -507,11 +425,11 @@ Ox.some <f> Tests if one or more elements of a collection meet a given condition
|
|||
> Ox.some([false, null, 0, '', void 0])
|
||||
false
|
||||
@*/
|
||||
Ox.some = function(collection, iterator, that) {
|
||||
export function some(collection, iterator, that) {
|
||||
iterator = iterator || Ox.identity;
|
||||
return Ox.forEach(collection, function(value, key, collection) {
|
||||
return forEach(collection, function(value, key, collection) {
|
||||
return !iterator.call(that, value, key, collection);
|
||||
}) < Ox.len(collection);
|
||||
}) < len(collection);
|
||||
};
|
||||
|
||||
/*@
|
||||
|
|
@ -529,10 +447,10 @@ Ox.sum <f> Returns the sum of the values of a collection
|
|||
> Ox.sum('08', -2, 'foo')
|
||||
6
|
||||
@*/
|
||||
Ox.sum = function(collection) {
|
||||
export function sum(collection) {
|
||||
var ret = 0;
|
||||
collection = arguments.length > 1 ? Ox.slice(arguments) : collection;
|
||||
Ox.forEach(collection, function(value) {
|
||||
forEach(collection, function(value) {
|
||||
value = +value;
|
||||
ret += isFinite(value) ? value : 0;
|
||||
});
|
||||
|
|
@ -541,7 +459,7 @@ Ox.sum = function(collection) {
|
|||
|
||||
/* FIXME: do we need this kind of zip functionality?
|
||||
|
||||
Ox.arrayToObject = function(array, key) {
|
||||
export function arrayToObject(array, key) {
|
||||
var ret = {};
|
||||
array.forEach(function(v) {
|
||||
ret[v[key]] = v;
|
||||
|
|
@ -549,9 +467,9 @@ Ox.arrayToObject = function(array, key) {
|
|||
return ret;
|
||||
};
|
||||
|
||||
Ox.objectToArray = function(object, key) {
|
||||
export function objectToArray(object, key) {
|
||||
var ret = [];
|
||||
Ox.forEach(object, function(v, k) {
|
||||
forEach(object, function(v, k) {
|
||||
ret.push(Ox.extend(v, key, k));
|
||||
});
|
||||
return ret;
|
||||
|
|
@ -574,13 +492,13 @@ Ox.values <f> Returns the values of a collection
|
|||
> Ox.values([1,,3])
|
||||
[1,,3]
|
||||
@*/
|
||||
Ox.values = function(collection) {
|
||||
export function values(collection) {
|
||||
var ret, type = Ox.typeOf(collection);
|
||||
if (type == 'array' || type == 'nodelist') {
|
||||
ret = Ox.slice(collection);
|
||||
} else if (type == 'object' || type == 'storage') {
|
||||
ret = [];
|
||||
Ox.forEach(collection, function(value) {
|
||||
forEach(collection, function(value) {
|
||||
ret.push(value);
|
||||
});
|
||||
} else if (type == 'string') {
|
||||
|
|
@ -613,9 +531,9 @@ Ox.walk <f> Iterates over a nested data structure
|
|||
> Ox.test.array
|
||||
[['a'], ['b', 'c'], ['b', 'd']]
|
||||
@*/
|
||||
Ox.walk = function(collection, iterator, that, keys) {
|
||||
export function walk(collection, iterator, that, keys) {
|
||||
keys = keys || [];
|
||||
Ox.forEach(collection, function(value, key) {
|
||||
forEach(collection, function(value, key) {
|
||||
var keys_ = keys.concat(key);
|
||||
iterator.call(that, value, keys_, collection);
|
||||
Ox.walk(collection[key], iterator, that, keys_);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue