add optional fragment to route paths
authorJustin Wind <justin.wind+git@gmail.com>
Sun, 17 Aug 2025 00:59:15 +0000 (17:59 -0700)
committerJustin Wind <justin.wind+git@gmail.com>
Sun, 17 Aug 2025 00:59:15 +0000 (17:59 -0700)
lib/dingus.js
test/lib/dingus.js

index adbcf019908258e26b7fd9ac69337728534d16a4..9d2655ed22c44f5f228f5607f53cc0763c56e06a 100644 (file)
@@ -223,10 +223,11 @@ class Dingus {
    * Render a named path with any parameters.
    * @param {string} name path name
    * @param {object=} params parameter substitutions
+   * @param {string=} fragment #fragment
    * @param {boolean=} asArray disable joining parts into string
    * @returns {string} rendered path
    */
-  routePath(name, params, asArray = false) {
+  routePath(name, params, fragment, asArray = false) {
     const namedPath = this.router.getNamedPath(name);
     if (!namedPath) {
       throw new DingusError(`undefined route path '${name}'`);
@@ -234,7 +235,11 @@ class Dingus {
 
     const parameterizedPath = this.constructor._parameterizePath(namedPath, params);
 
-    return asArray ? parameterizedPath : parameterizedPath.join('/');
+    if (asArray) {
+      return parameterizedPath;
+    } else {
+      return parameterizedPath.join('/') + (fragment ? `#${fragment}` : '');
+    }
   }
 
 
@@ -242,10 +247,11 @@ class Dingus {
    * Render internal path including proxyPrefix.
    * @param {string} name path name
    * @param {object=} params parameter substitutions
+   * @param {string=} fragment #fragment
    * @returns {string} rendered path
    */
-  absolutePath(name, params) {
-    return this.proxyPrefix + this.routePath(name, params);
+  absolutePath(name, params, fragment) {
+    return this.proxyPrefix + this.routePath(name, params, fragment);
   }
 
 
@@ -294,10 +300,11 @@ class Dingus {
    * Render external URL for path.
    * @param {string} name path name
    * @param {object=} params parameter substitutions
+   * @param {string=} fragment #fragment
    * @returns {string} rendered url
    */
-  externalPath(name, params) {
-    return this.selfBaseUrl + this.routePath(name, params);
+  externalPath(name, params, fragment) {
+    return this.selfBaseUrl + this.routePath(name, params, fragment);
   }
 
 
index 0e4f43c6d93b86d43b8927ecc93de163c8c5bafd..77cf5a2a8a0d7109075cb014070bf5285ad88b76 100644 (file)
@@ -338,6 +338,11 @@ describe('Dingus', function () {
       const result = dingus.routePath('pathName');
       assert.strictEqual(result, '/a/b/c');
     });
+    it('renders plain path with fragment', function () {
+      dingus.on('GET', '/a/b/c', 'pathName', () => {});
+      const result = dingus.routePath('pathName', undefined, 'foo');
+      assert.strictEqual(result, '/a/b/c#foo');
+    });
     it('renders parameterized path', function () {
       dingus.on('GET', '/a/:b/c', 'pathName', () => {});
       const result = dingus.routePath('pathName', { b: 'foo' });
@@ -345,7 +350,7 @@ describe('Dingus', function () {
     });
     it('renders parameterized path as array', function () {
       dingus.on('GET', '/a/:b/c', 'pathName', () => {});
-      const result = dingus.routePath('pathName', { b: 'foo' }, true);
+      const result = dingus.routePath('pathName', { b: 'foo' }, undefined, true);
       assert.deepStrictEqual(result, ['', 'a', 'foo', 'c']);
     });
     it('requires parameters', function () {