@uifabric/utilities
Version:
Fluent UI React utilities for building components.
124 lines • 3.87 kB
JavaScript
/**
* Helper to find the index of an item within an array, using a callback to
* determine the match.
*
* @public
* @param array - Array to search.
* @param cb - Callback which returns true on matches.
* @param fromIndex - Optional index to start from (defaults to 0)
*/
export function findIndex(array, cb, fromIndex) {
if (fromIndex === void 0) { fromIndex = 0; }
var index = -1;
for (var i = fromIndex; array && i < array.length; i++) {
if (cb(array[i], i)) {
index = i;
break;
}
}
return index;
}
/**
* Helper to find the first item within an array that satisfies the callback.
* @param array - Array to search
* @param cb - Callback which returns true on matches
*/
export function find(array, cb) {
var index = findIndex(array, cb);
if (index < 0) {
return undefined;
}
return array[index];
}
/**
* Creates an array of a given size and helper method to populate.
*
* @public
* @param size - Size of array.
* @param getItem - Callback to populate given cell index.
*/
export function createArray(size, getItem) {
var array = [];
for (var i = 0; i < size; i++) {
array.push(getItem(i));
}
return array;
}
/**
* Convert the given array to a matrix with columnCount number
* of columns.
*
* @public
* @param items - The array to convert
* @param columnCount - The number of columns for the resulting matrix
* @returns A matrix of items
*/
export function toMatrix(items, columnCount) {
return items.reduce(function (rows, currentValue, index) {
if (index % columnCount === 0) {
rows.push([currentValue]);
}
else {
rows[rows.length - 1].push(currentValue);
}
return rows;
}, []);
}
/**
* Given an array, it returns a new array that does not contain the item at the given index.
* @param array - The array to operate on
* @param index - The index of the element to remove
*/
export function removeIndex(array, index) {
return array.filter(function (_, i) { return index !== i; });
}
/**
* Given an array, this function returns a new array where the element at a given index has been replaced.
* @param array - The array to operate on
* @param newElement - The element that will be placed in the new array
* @param index - The index of the element that should be replaced
*/
export function replaceElement(array, newElement, index) {
var copy = array.slice();
copy[index] = newElement;
return copy;
}
/**
* Given an array, this function returns a new array where an element has been inserted at the given index.
* @param array - The array to operate on
* @param index - The index where an element should be inserted
* @param itemToAdd - The element to insert
*/
export function addElementAtIndex(array, index, itemToAdd) {
var copy = array.slice();
copy.splice(index, 0, itemToAdd);
return copy;
}
/**
* Given an array where each element is of type T or T[], flatten it into an array of T
* @param array - The array where each element can optionally also be an array
*/
export function flatten(array) {
var result = [];
array.forEach(function (item) { return (result = result.concat(item)); });
return result;
}
/**
* Returns a boolean indicating if the two given arrays are equal in length and values.
*
* @param array1 - First array to compare
* @param array2 - Second array to compare
* @returns True if the arrays are the same length and have the same values in the same positions, false otherwise.
*/
export function arraysEqual(array1, array2) {
if (array1.length !== array2.length) {
return false;
}
for (var i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) {
return false;
}
}
return true;
}
//# sourceMappingURL=array.js.map