Created
July 16, 2015 19:06
-
-
Save dcabines/17022650137e0032ebc5 to your computer and use it in GitHub Desktop.
array stuff.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.Utils.Array = (function () { | |
'use strict'; | |
var utils = { | |
// TESTED | |
// check to see if a value is one of any of the supplied values | |
anyValue: function (value, values) { | |
return this.any(values, function (item) { | |
return item === value; | |
}); | |
}, | |
// TESTED | |
// replace an item in an array by matching properties | |
replaceItem: function (arr, idProp, data) { | |
var added = false; | |
for (var j = 0; j < arr.length; j += 1) { | |
if (arr[j][idProp] === data[idProp]) { | |
arr[j] = data; | |
added = true; | |
} | |
} | |
if (!added) { | |
arr.push(data); | |
} | |
}, | |
// TESTED | |
// filter an array by using a function to test each item | |
filter: function (arr, test) { | |
var results = []; | |
Ext.each(arr, function (item) { | |
if (test(item)) { | |
results.push(item); | |
} | |
}); | |
return results; | |
}, | |
// TESTED | |
// filter an array by checking to see if a property | |
// is either included or excluded from a list of values | |
filterProp: function (arr, prop, list, inclusive) { | |
return this.filter(arr, function (item) { | |
var hasValue = list.indexOf(item[prop]) !== -1; | |
return inclusive ? hasValue : !hasValue; | |
}); | |
}, | |
// TESTED | |
// transform an array into a new array by running a function on each element | |
map: function map(array, transform) { | |
var mapped = []; | |
Ext.each(array, function (item) { | |
mapped.push(transform(item)); | |
}); | |
return mapped; | |
}, | |
// TESTED | |
// reduce an array to a single value by running a function on each element | |
// previousValue, currentValue, index, array | |
reduce: function reduce(arr, combine) { | |
var value = arr[0]; | |
for (var i = 1; i < arr.length; i += 1) { | |
value = combine(value, arr[i], i, arr); | |
} | |
return value; | |
}, | |
// TESTED | |
// flatten an array | |
// the first item must be an array | |
flatten: function (arr) { | |
return this.reduce(arr, function (prev, curr) { | |
return prev.concat(curr); | |
}); | |
}, | |
// TESTED | |
// run a function on each value in an array and see if any return true | |
any: function (arr, test) { | |
var map = this.map(arr, function (item) { | |
return test(item); | |
}); | |
return this.reduce(map, function (prev, curr) { | |
return prev || curr; | |
}); | |
}, | |
// TESTED | |
// run a function on each value in an array and see if they all return true | |
all: function (arr, test) { | |
var map = this.map(arr, function (item) { | |
return test(item); | |
}); | |
return this.reduce(map, function (prev, curr) { | |
return prev && curr; | |
}); | |
}, | |
// TESTED | |
// build an array by conditionally including values | |
// the resulting array will be flattened | |
conditionalArray: function (arr) { | |
var result = []; | |
Ext.each(arr, function (item) { | |
if (item.condition) { | |
result.push(item.value); | |
} | |
}); | |
return this.flatten(result); | |
} | |
}; | |
return utils; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment