From 8ffcc2e7bb633239de45f89bd76bc2ac24fe0167 Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 14 Jan 2021 23:30:35 -0600 Subject: [PATCH] refactor: gather config --- packages/core/src/gather.js | 22 ++++++++++++++++++---- packages/core/src/index.js | 1 + packages/core/src/string.js | 3 +++ packages/socket/package.json | 5 +---- packages/socket/src/index.js | 8 ++++---- 5 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 packages/core/src/string.js diff --git a/packages/core/src/gather.js b/packages/core/src/gather.js index 2b88781..739047f 100644 --- a/packages/core/src/gather.js +++ b/packages/core/src/gather.js @@ -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); diff --git a/packages/core/src/index.js b/packages/core/src/index.js index a43ace7..181cd16 100644 --- a/packages/core/src/index.js +++ b/packages/core/src/index.js @@ -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 {}; diff --git a/packages/core/src/string.js b/packages/core/src/string.js new file mode 100644 index 0000000..68f7eca --- /dev/null +++ b/packages/core/src/string.js @@ -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(''); diff --git a/packages/socket/package.json b/packages/socket/package.json index 91b4ab1..3be6635 100644 --- a/packages/socket/package.json +++ b/packages/socket/package.json @@ -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" } } diff --git a/packages/socket/src/index.js b/packages/socket/src/index.js index d49f2b1..44b7326 100644 --- a/packages/socket/src/index.js +++ b/packages/socket/src/index.js @@ -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) => ({