From 3558c8fd1dbedef8484c329e117390d1ffdc119f Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 15 Mar 2024 12:28:34 -0300 Subject: src/hero.mjs: Add comboForLabel() --- src/hero.mjs | 5 +++++ tests/js/hero.mjs | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/hero.mjs b/src/hero.mjs index 20c7900..6838473 100644 --- a/src/hero.mjs +++ b/src/hero.mjs @@ -158,6 +158,11 @@ assert.ok(HTTP_METHODS.has(WEBSOCKET_METHOD)); export const isValidLabel = name => HTTP_METHODS.has(name) || name === WEBSOCKET_LABEL; +export const comboForLabel = (label, keyword) => + label === WEBSOCKET_LABEL ? + [ WEBSOCKET_KEYWORD, WEBSOCKET_METHOD ] : + [ keyword, label ]; + export const addRoute = (table, methods, path, handlerFn) => { if (methods === "*") { return addRoute(table, HTTP_METHODS_ARR, path, handlerFn); diff --git a/tests/js/hero.mjs b/tests/js/hero.mjs index d9c01d4..171e01f 100644 --- a/tests/js/hero.mjs +++ b/tests/js/hero.mjs @@ -21,6 +21,7 @@ import { pathToSegments, hasPathParams, isValidLabel, + comboForLabel, addRoute, findStaticHandler, firstParamMatch, @@ -594,6 +595,24 @@ const test_isValidLabel = async t => { }); }; +const test_comboForLabel = async t => { + t.start("comboForLabel()"); + + await t.test("websocket gets its own combo", () => { + assert.deepEqual( + comboForLabel("WEBSOCKET", "IGNORED"), + [ "websocket", "GET" ], + ); + }); + + await t.test("otherwise we get what pass", () => { + assert.deepEqual( + comboForLabel("not-websocket", "a-keyword"), + [ "a-keyword", "not-websocket" ], + ); + }); +}; + const test_addRoute = async t => { t.start("addRoute()"); @@ -1806,6 +1825,7 @@ await runner.runTests([ test_pathToSegments, test_hasPathParams, test_isValidLabel, + test_comboForLabel, test_addRoute, test_findStaticHandler, test_firstParamMatch, -- cgit v1.2.3