@mic-rexjs/usecases
Version:
Usecases of Clean Architecture
36 lines (35 loc) • 1.2 kB
JavaScript
import { arrayUseCase } from '../arrayUseCase';
import { getDataKeyValue } from './methods/getDataKeyValue';
export const dataListUseCase = () => {
const arrayReducers = arrayUseCase();
const { extractEntity } = arrayReducers;
const extractEntityBy = (entity, target, expect = false) => {
return extractEntity(entity, (item) => {
const value = getDataKeyValue(item);
return (value === target) === expect;
});
};
const filterEntityBy = function* (entity, target, expect) {
return yield extractEntityBy(entity, target, expect);
};
const replaceEntity = function* (entity, newItem, target = getDataKeyValue(newItem)) {
const index = entity.findIndex((item) => {
const value = getDataKeyValue(item);
return value === target;
});
if (index === -1) {
return null;
}
const targetItem = entity[index];
const newEntity = entity.slice();
newEntity[index] = newItem;
yield newEntity;
return targetItem;
};
return {
...arrayReducers,
extractEntityBy,
filterEntityBy,
replaceEntity,
};
};