refactor: gather config

This commit is contained in:
cha0s 2021-01-14 23:30:35 -06:00
parent 0a020b63d2
commit 8ffcc2e7bb
5 changed files with 27 additions and 12 deletions

View File

@ -2,10 +2,16 @@ import D from 'debug';
import {basename, extname} from 'path';
const capitalize = (token) => token.substring(0, 1).toUpperCase() + token.substring(1);
import {camelCase, capitalize} from './string';
const debug = D('@latus/core/gather');
export const gatherWithLatus = (context) => (latus) => (
export const gatherWithLatus = (
context,
{
transformer = camelCase,
},
) => (latus) => (
Object.fromEntries(context.keys().map((path) => {
const M = context(path).default;
if ('function' !== typeof M) {
@ -14,13 +20,21 @@ export const gatherWithLatus = (context) => (latus) => (
);
}
return [
basename(path, extname(path)).split('-').map(capitalize).join(''),
transformer(basename(path, extname(path))),
M(latus),
];
}))
);
export default (latus, type, idAttribute = 'id', typeAttribute = 'type', check = () => {}) => {
export default (
latus,
{
type,
idAttribute = 'id',
typeAttribute = 'type',
check = () => {},
},
) => {
const gathered = latus.invokeReduce(type);
check(gathered, type);
const composed = latus.invokeComposed(`${type}.decorate`, gathered);

View File

@ -11,6 +11,7 @@ export {compose, fastApply} from './function';
export {gatherWithLatus, default as gather} from './gather';
export {default as Middleware} from './middleware';
export {default as Latus} from './latus';
export * from './string';
export const Class = class {};

View File

@ -0,0 +1,3 @@
export const capitalize = (string) => string.substring(0, 1).toUpperCase() + string.substring(1);
export const camelCase = (string) => string.split(/[_-]/).map(capitalize).join('');

View File

@ -30,6 +30,7 @@
"dependencies": {
"@latus/core": "2.0.0",
"@latus/http": "2.0.0",
"@latus/react": "2.0.0",
"buffer": "^6.0.3",
"debug": "4.3.1",
"msgpack-lite": "^0.1.26",
@ -39,7 +40,6 @@
"socket.io-client": "2.3.0"
},
"devDependencies": {
"@latus/react": "2.0.0",
"@neutrinojs/airbnb-base": "^9.4.0",
"@neutrinojs/copy": "9.4.0",
"@neutrinojs/mocha": "^9.4.0",
@ -52,8 +52,5 @@
"neutrino": "^9.4.0",
"webpack": "^4",
"webpack-cli": "^3"
},
"peerDependencies": {
"@latus/react": "2.0.0"
}
}

View File

@ -19,10 +19,10 @@ export default {
// eslint-disable-next-line no-param-reassign
latus.config['%packets'] = gather(
latus,
'@latus/socket/packets',
'id',
'type',
badPacketsCheck,
{
type: '@latus/socket/packets',
check: badPacketsCheck,
},
);
},
'@latus/socket/packets': (latus) => ({