feat: gatherFlat

This commit is contained in:
cha0s 2021-04-06 15:43:03 -05:00
parent 09d1e03da5
commit 442826f287
2 changed files with 35 additions and 9 deletions

View File

@ -13,6 +13,17 @@ const debug = D('@latus/core/gather');
export const hotGathered = new Map();
const transformPath = (transformer, path) => {
const parts = dirname(path).split('/');
if ('.' === parts[0]) {
parts.shift();
}
if ('index' === parts[parts.length - 1]) {
parts.pop();
}
return transformer(join(parts.join('-'), basename(path, extname(path))));
};
export const decorateWithLatus = (
context,
{
@ -26,20 +37,30 @@ export const decorateWithLatus = (
`decorateWithLatus: require(${path}).default is not a function (from: ${context.id})`,
);
}
const parts = dirname(path).split('/');
if ('.' === parts[0]) {
parts.shift();
}
if ('index' === parts[parts.length - 1]) {
parts.pop();
}
const key = transformer(join(parts.join('-'), basename(path, extname(path))));
const key = transformPath(transformer, path);
// eslint-disable-next-line no-param-reassign
Gathered[key] = M(Gathered[key], latus);
});
return Gathered;
};
export const gatherFlat = (
context,
{
transformer = camelCase,
} = {},
) => () => (
Object.fromEntries(
context.keys()
.map(
(path) => [
transformPath(transformer, path),
context(path).default,
],
),
)
);
export const gatherWithLatus = (
context,
{

View File

@ -11,7 +11,12 @@ export {default as ensureUniqueReduction} from './ensure-unique-reduction';
export {default as EventEmitter} from './event-emitter';
export {deflate, inflate} from './flate';
export {compose, fastApply} from './function';
export {decorateWithLatus, gatherWithLatus, default as gather} from './gather';
export {
decorateWithLatus,
gatherFlat,
gatherWithLatus,
default as gather,
} from './gather';
export {default as Middleware} from './middleware';
export {default as Latus} from './latus';
export {default as require} from './require';