@intuitionrobotics/ts-common
Version:
145 lines • 5.09 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.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeItemFromArray = removeItemFromArray;
exports.removeFromArray = removeFromArray;
exports.removeFromArrayByIndex = removeFromArrayByIndex;
exports.addAllItemToArray = addAllItemToArray;
exports.addItemToArray = addItemToArray;
exports.addItemToArrayAtIndex = addItemToArrayAtIndex;
exports.toggleElementInArray = toggleElementInArray;
exports.filterDuplicates = filterDuplicates;
exports.filterInstances = filterInstances;
exports.arrayToMap = arrayToMap;
exports.sortArray = sortArray;
exports.batchAction = batchAction;
exports.batchActionParallel = batchActionParallel;
exports.flatArray = flatArray;
function removeItemFromArray(array, item) {
const index = array.indexOf(item);
return removeFromArrayByIndex(array, index);
}
function removeFromArray(array, item) {
const index = array.findIndex(item);
return removeFromArrayByIndex(array, index);
}
function removeFromArrayByIndex(array, index) {
if (index > -1)
array.splice(index, 1);
return array;
}
function addAllItemToArray(array, items) {
array.push(...items);
return array;
}
function addItemToArray(array, item) {
array.push(item);
return array;
}
function addItemToArrayAtIndex(array, item, index) {
array.splice(index, 0, item);
return array;
}
function toggleElementInArray(array, item) {
const index = array.indexOf(item);
if (index > -1)
array.splice(index, 1);
else
array.push(item);
return array;
}
function filterDuplicates(array) {
return Array.from(new Set(array));
}
function filterInstances(array) {
return array.filter(item => !!item);
}
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[]) {
// }
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);
}
function batchAction(arr, chunk, action) {
return __awaiter(this, void 0, void 0, function* () {
if (chunk <= 0)
return [];
const result = [];
for (let i = 0, j = arr.length; i < j; i += chunk) {
const items = yield action(arr.slice(i, i + chunk), i / chunk);
if (Array.isArray(items))
addAllItemToArray(result, items);
else
addItemToArray(result, items);
}
return result;
});
}
function batchActionParallel(arr, chunk, action) {
return __awaiter(this, void 0, void 0, function* () {
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 = yield Promise.all(promises);
for (const items of results) {
if (Array.isArray(items))
addAllItemToArray(toRet, items);
else
addItemToArray(toRet, items);
}
return toRet;
});
}
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