diff --git a/source/Ox/js/Array.js b/source/Ox/js/Array.js index a4956dc9..f65a7e28 100644 --- a/source/Ox/js/Array.js +++ b/source/Ox/js/Array.js @@ -363,8 +363,8 @@ Ox.api = function(items, options) { }; /*@ -Ox.compact Removes `null` or `undefined` values - (array) -> Array +Ox.compact Removes `null` or `undefined` values from an array + (array) -> Array without `null` or `undefined` values > Ox.compact([null,,1,,2,,3]) [1, 2, 3] @*/ @@ -378,27 +378,31 @@ Ox.compact = function(array) { Ox.find Returns array elements that match a string Returns an array of case-insensitive matches: exact match first, then leading matches, then other matches + (array, query[, leading]) -> Array of matches + array <[s]> Array of strings + query Query string + leading If true, returns leading matches only > Ox.find(['Bar', 'Barfoo', 'Foo', 'Foobar'], 'foo') ['Foo', 'Foobar', 'Barfoo'] > Ox.find(['Bar', 'Barfoo', 'Foo', 'Foobar'], 'foo', true) ['Foo', 'Foobar'] @*/ Ox.find = function(array, string, leading) { - var ret = [[], []]; + var matches = [[], []]; string = string.toLowerCase(); array.forEach(function(value) { var lowerCase = value.toLowerCase(), index = lowerCase.indexOf(string); - index > -1 && ret[index == 0 ? 0 : 1][ + index > -1 && matches[index == 0 ? 0 : 1][ lowerCase == string ? 'unshift' : 'push' ](value); }) - return leading ? ret[0] : ret[0].concat(ret[1]); + return leading ? matches[0] : matches[0].concat(matches[1]); }; /*@ Ox.flatten Flattens an array - (arr) -> Array + (array) -> Flattened array > Ox.flatten([1, [2, [3], 2], 1]) [1, 2, 3, 2, 1] @*/ @@ -418,6 +422,9 @@ Ox.flatten = function(array) { /*@ Ox.getIndexById Returns the first array index of an object with a given id + (array, id) -> Index (or `-1`) + array <[o]> Array of objects with a unique `'id'` property + id Id > Ox.getIndexById([{id: 'foo', str: 'Foo'}, {id: 'bar', str: 'Bar'}], 'bar') 1 > Ox.getIndexById([{id: 'foo', str: 'Foo'}, {id: 'bar', str: 'Bar'}], 'baz') @@ -431,6 +438,9 @@ Ox.getIndexById = function(array, id) { /*@ Ox.getObjectById Returns the first object in an array with a given id + (array, id) -> Object (or `null`) + array <[o]> Array of objects with a unique `'id'` property + id Id > Ox.getObjectById([{id: 'foo', str: 'Foo'}, {id: 'bar', str: 'Bar'}], 'bar') {id: "bar", str: "Bar"} > Ox.getObjectById([{id: 'foo', str: 'Foo'}, {id: 'bar', str: 'Bar'}], 'baz') @@ -449,6 +459,8 @@ Ox.indexOf = function(arr) { /*@ Ox.makeArray Wraps any non-array in an array. + (value) -> Array + value <*> Any value > Ox.makeArray('foo') ['foo'] > Ox.makeArray(['foo']) @@ -646,7 +658,7 @@ Ox.toArray = function(collection) { try { Array.prototype.slice.call(document.getElementsByTagName('a')); } catch (error) { - // Handle MSIE NodeLists + // Handle IE NodeLists Ox.toArray = function(collection) { var i, length, ret = []; try { @@ -662,8 +674,8 @@ try { } /*@ -Ox.unique Returns an array without duplicate values - (array) -> Array +Ox.unique Removes duplicate values from an array + (array) -> Array without duplicate values > Ox.unique([1, 2, 3, 2, 1]) [1, 2, 3] > Ox.unique([NaN, NaN])