@intuitionrobotics/ts-common
Version:
116 lines • 3.59 kB
JavaScript
/*
* ts-common is the basic building blocks of our typescript projects
*
* Copyright (C) 2020 Intuition Robotics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export function removeItemFromArray(array, item) {
const index = array.indexOf(item);
return removeFromArrayByIndex(array, index);
}
export function removeFromArray(array, item) {
const index = array.findIndex(item);
return removeFromArrayByIndex(array, index);
}
export function removeFromArrayByIndex(array, index) {
if (index > -1)
array.splice(index, 1);
return array;
}
export function addAllItemToArray(array, items) {
array.push(...items);
return array;
}
export function addItemToArray(array, item) {
array.push(item);
return array;
}
export function addItemToArrayAtIndex(array, item, index) {
array.splice(index, 0, item);
return array;
}
export function toggleElementInArray(array, item) {
const index = array.indexOf(item);
if (index > -1)
array.splice(index, 1);
else
array.push(item);
return array;
}
export function filterDuplicates(array) {
return Array.from(new Set(array));
}
export function filterInstances(array) {
return array.filter(item => !!item);
}
export function arrayToMap(array, getKey, map) {
return array.reduce((toRet, element) => {
toRet[getKey(element)] = element;
return toRet;
}, map || {});
}
// updateProperty<T extends object>(map: { [k: string]: T }, getKey: (element: T) => string, elements: T[]) {
// }
export function sortArray(array, map, invert = false) {
const compareFn = (a, b) => {
const _a = map(a);
const _b = map(b);
return (_a > _b ? -1 : (_a === _b ? 0 : 1)) * (invert ? -1 : 1);
};
return array.sort(compareFn);
}
export async function batchAction(arr, chunk, action) {
if (chunk <= 0)
return [];
const result = [];
for (let i = 0, j = arr.length; i < j; i += chunk) {
const items = await action(arr.slice(i, i + chunk), i / chunk);
if (Array.isArray(items))
addAllItemToArray(result, items);
else
addItemToArray(result, items);
}
return result;
}
export async function batchActionParallel(arr, chunk, action) {
if (chunk <= 0)
return [];
const promises = [];
for (let i = 0, j = arr.length; i < j; i += chunk) {
addItemToArray(promises, action(arr.slice(i, i + chunk), i / chunk));
}
const toRet = [];
const results = await Promise.all(promises);
for (const items of results) {
if (Array.isArray(items))
addAllItemToArray(toRet, items);
else
addItemToArray(toRet, items);
}
return toRet;
}
export function flatArray(arr, result = []) {
for (let i = 0, length = arr.length; i < length; i++) {
const value = arr[i];
if (Array.isArray(value)) {
flatArray(value, result);
}
else {
result.push(value);
}
}
return result;
}
;
//# sourceMappingURL=array-tools.js.map