graphql-composer
Version:
Create your GraphQL API using composition!
56 lines • 1.73 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArrayHelper = void 0;
class ArrayHelper {
static remove(itemOrIndexOrName, array) {
const arrayCopy = [...array];
const indexes = itemOrIndexOrName
.reduce((prev, rm) => {
const index = ArrayHelper.find(rm, arrayCopy);
if (index) {
prev.push(index.index);
}
return prev;
}, [])
.sort();
while (indexes.length) {
arrayCopy.splice(indexes.pop(), 1);
}
return arrayCopy;
}
static find(itemOrIndexOrName, array) {
let index;
switch (typeof itemOrIndexOrName) {
case "number":
index = itemOrIndexOrName;
break;
case "string":
index = array.findIndex((item) => item.name === itemOrIndexOrName);
break;
default:
index = array.indexOf(itemOrIndexOrName);
break;
}
if (index > -1) {
return { index, ref: array[index] };
}
}
static set(itemOrIndexOrName, value, array) {
const arrayCopy = [...array];
const found = ArrayHelper.find(itemOrIndexOrName, array);
if (found) {
arrayCopy[found.index] = value;
}
return arrayCopy;
}
static addWithoutDuplication(name, value, array) {
const arrayCopy = [...array];
const found = ArrayHelper.find(name, array);
if (!found) {
arrayCopy.push(value);
}
return arrayCopy;
}
}
exports.ArrayHelper = ArrayHelper;
//# sourceMappingURL=ArrayHelper.js.map
;