@zohodesk/utils
Version:
DOT Utils Collection
76 lines (72 loc) • 1.62 kB
JavaScript
import { DUMMY_ARRAY } from "../constantProps/constant";
import { deepEqual } from "../deepEqual";
export const appendToArray = _ref => {
let {
array,
item
} = _ref;
return [...array, item];
};
export const updateArrayOfObjectItemById = _ref2 => {
let {
arrayOfObject,
id,
newItem
} = _ref2;
let found = false;
return arrayOfObject.map(object => {
if (!found && id !== undefined && object.id === id) {
found = true;
return Array.isArray(newItem) ? [...newItem] : typeof newItem === 'object' ? { ...object,
...newItem
} : newItem;
}
return object;
});
};
export const updateArrayItemByItem = _ref3 => {
let {
array,
currentItem,
newItem
} = _ref3;
let found = false;
return array.map(arrayItem => {
if (!found && currentItem !== undefined && deepEqual(arrayItem, currentItem)) {
found = true;
return newItem;
}
return arrayItem;
});
};
export const deleteFromArrayOfObjectById = _ref4 => {
let {
arrayOfObject,
id
} = _ref4;
let found = false;
return arrayOfObject.filter(object => {
if (!found && id !== undefined && object.id === id) {
found = true;
return false;
}
return true;
});
};
export const deleteFromArrayByItem = _ref5 => {
let {
array,
currentItem
} = _ref5;
let found = false;
return array.filter(arrayItem => {
if (!found && currentItem !== undefined && deepEqual(arrayItem, currentItem)) {
found = true;
return false;
}
return true;
});
};
export const emptyArray = () => {
return DUMMY_ARRAY;
};