diff --git a/source/Ox.UI/js/Form/OptionGroup.js b/source/Ox.UI/js/Form/OptionGroup.js index c0065356..fe5614a3 100644 --- a/source/Ox.UI/js/Form/OptionGroup.js +++ b/source/Ox.UI/js/Form/OptionGroup.js @@ -10,6 +10,8 @@ Ox.OptionGroup OptionGroup property property to check @*/ +// FIXME: Should be moved to Ox.js + Ox.OptionGroup = function(items, min, max, property) { var length = items.length; diff --git a/source/Ox/js/Array.js b/source/Ox/js/Array.js index f65a7e28..fc18b42b 100644 --- a/source/Ox/js/Array.js +++ b/source/Ox/js/Array.js @@ -457,6 +457,33 @@ Ox.indexOf = function(arr) { }; */ +/*@ +Ox.last Gets or sets the last element of an array + Unlike `arrayWithALongName[arrayWithALongName.length - 1]`, + `Ox.last(arrayWithALongName)` is short. + + > Ox.last(Ox.test.array) + 3 + > Ox.last(Ox.test.array, 4) + [1, 2, 4] + > Ox.test.array + [1, 2, 4] + > Ox.last('123') + '3' +@*/ +Ox.last = function(array, value) { + var ret; + if (arguments.length == 1) { + ret = array[array.length - 1]; + } else { + array[array.length - 1] = value; + ret = array; + } + return ret; +}; + /*@ Ox.makeArray Wraps any non-array in an array. (value) -> Array diff --git a/source/Ox/js/Async.js b/source/Ox/js/Async.js index a16e92b4..badbb702 100644 --- a/source/Ox/js/Async.js +++ b/source/Ox/js/Async.js @@ -31,8 +31,16 @@ }; /*@ - Ox.nonblockingForEach Non-blocking forEach with synchronous iterator + Ox.nonblockingForEach Non-blocking `forEach` with synchronous iterator (col, iterator[, that], callback[, ms]) -> undefined + collection Collection + iterator Iterator function + value <*> Value + key Key + collection The collection + that The iterator's `this` binding + callback Callback function + ms Number of milliseconds after which to insert a `setTimeout` call @*/ Ox.nonblockingForEach = function(collection, iterator, that, callback, ms) { var i = 0, keys, last = Ox.last(arguments), @@ -76,12 +84,13 @@ }; /*@ - Ox.nonblockingMap Non-blocking map with synchronous iterator - (collection, iterator[, that]) -> undefined + Ox.nonblockingMap Non-blocking `map` with synchronous iterator + (collection, iterator[, that], callback[, ms]) -> undefined collection Collection iterator Iterator function - that The iterator's this binding + that The iterator's `this` binding callback Callback function + ms Number of milliseconds after which to insert a `setTimeout` call > Ox.test.string "012abcfoo" + > Ox.forEach({a: 'f', b: 'o', c: 'o'}, function(v, k) { return v != 'o' }); + 1 @*/ Ox.forEach = function(collection, iterator, that) { - var i, key, type = Ox.typeOf(collection); + var i = 0, key, type = Ox.typeOf(collection); if (type != 'array' && type != 'object') { collection = Ox.toArray(collection); } @@ -166,6 +186,7 @@ Ox.forEach = function(collection, iterator, that) { break; } } + i++; } } else { for (i = 0; i < collection.length; i++) { @@ -183,7 +204,7 @@ Ox.forEach = function(collection, iterator, that) { throw error; } } - return type == 'object' ? key : i; + return i; }; /*@ @@ -201,7 +222,8 @@ Ox.indexOf = function(collection, test) { var index = Ox.forEach(collection, function(value) { test(value) && Ox.Break(); }); - return index == collection.length ? -1 : index; + return Ox.isObject(collection) ? Object.keys(collection)[index] || null + : index == collection.length ? -1 : index; }; /*@ @@ -248,33 +270,6 @@ Ox.isEmpty = function(value) { return Ox.len(value) === 0; }; -/*@ -Ox.last Gets or sets the last element of an array - Unlike `arrayWithALongName[arrayWithALongName.length - 1]`, - `Ox.last(arrayWithALongName)` is short. - - > Ox.last(Ox.test.array) - 3 - > Ox.last(Ox.test.array, 4) - [1, 2, 4] - > Ox.test.array - [1, 2, 4] - > Ox.last('123') - '3' -@*/ -Ox.last = function(array, value) { - var ret; - if (arguments.length == 1) { - ret = array[array.length - 1]; - } else { - array[array.length - 1] = value; - ret = array; - } - return ret; -}; - /*@ Ox.len Returns the length of an array, node list, object or string Not to be confused with `Ox.length`, which is the `length` property of the @@ -310,7 +305,8 @@ Ox.len = function(collection) { /*@ Ox.map Transforms the values of an array, object or string - Unlike `[].map()`, `Ox.map()` works for arrays, objects and strings. + Unlike `Array.prototype.map`, `Ox.map` works for arrays, objects and + strings. > Ox.map([2, 1, 0], function(v, i) { return v == i; }) [false, true, false] > Ox.map({a: 'b', b: 'b', c: 'b'}, function(v, k) { return v == k; }) @@ -391,36 +387,6 @@ Ox.reverse = function(collection) { : collection.toString().split('').reverse().join(''); }; -/*@ -Ox.setPropertyOnce Sets a property once - Given a array of objects, each of which has a property with a boolean - value, this sets exactly one of these to true, and returns the index - of the object whose property is true. - > Ox.setPropertyOnce([{selected: false}, {selected: false}], 'selected') - 0 - > Ox.setPropertyOnce([{selected: false}, {selected: true}], 'selected') - 1 - > Ox.setPropertyOnce([{selected: true}, {selected: true}], 'selected') - 0 -@*/ -// fixme: strange name, and shouldn't it return the full array? -// fixme: unused, there is OptionGroup (which should be part of Ox.js) -Ox.setPropertyOnce = function(arr, str) { - var pos = -1; - Ox.forEach(arr, function(v, i) { - if (pos == -1 && arr[i][str]) { - pos = i; - } else if (pos > -1 && arr[i][str]) { - delete arr[i][str]; - } - }); - if (pos == -1) { - arr[0][str] = true; - pos = 0; - } - return pos; -}; - /*@ Ox.shuffle Randomizes the order of values within a collection > Ox.shuffle([1, 2, 3]).length @@ -484,7 +450,8 @@ if ( /*@ Ox.some Tests if one or more elements of a collection meet a given condition - Unlike `[].some()`, `Ox.some()` works for arrays, objects and strings. + Unlike `Array.prototype.some`, `Ox.some` works for arrays, objects and + strings. > Ox.some([2, 1, 0], function(i, v) { return i == v; }) true > Ox.some({a: 1, b: 2, c: 3}, function(v) { return v == 1; }) @@ -545,6 +512,8 @@ Ox.objectToArray = function(object, key) { /*@ Ox.values Returns the values of a collection + (collection) -> Array of values + collection Collection > Ox.values([1, 2, 3]) [1, 2, 3] > Ox.values({a: 1, b: 2, c: 3}) @@ -571,6 +540,13 @@ Ox.values = function(collection) { /*@ Ox.walk Iterates over a nested data structure + (collection, iterator[, that]) -> undefined + collection Collection + iterator Iterator + value <*> Value + keys Array of keys + collection The collection + that The iterator's `this` binding