Просмотр исходного кода

refactor: replaces native getter to functional getters

e22m4u 3 недель назад
Родитель
Сommit
699bfc970d
5 измененных файлов с 79 добавлено и 22 удалено
  1. 17 4
      dist/cjs/index.cjs
  2. 8 4
      src/branch/router-branch.d.ts
  3. 18 4
      src/branch/router-branch.js
  4. 35 9
      src/branch/router-branch.spec.js
  5. 1 1
      src/trie-router.spec.js

+ 17 - 4
dist/cjs/index.cjs

@@ -1968,7 +1968,7 @@ var _RouterBranch = class _RouterBranch extends DebuggableService {
    *
    *
    * @type {TrieRouter}
    * @type {TrieRouter}
    */
    */
-  get router() {
+  getRouter() {
     return this._router;
     return this._router;
   }
   }
   /**
   /**
@@ -1982,7 +1982,7 @@ var _RouterBranch = class _RouterBranch extends DebuggableService {
    *
    *
    * @type {RouterBranchDefinition}
    * @type {RouterBranchDefinition}
    */
    */
-  get definition() {
+  getDefinition() {
     return this._definition;
     return this._definition;
   }
   }
   /**
   /**
@@ -1991,12 +1991,25 @@ var _RouterBranch = class _RouterBranch extends DebuggableService {
    * @type {RouterBranch|undefined}
    * @type {RouterBranch|undefined}
    */
    */
   _parentBranch;
   _parentBranch;
+  /**
+   * Has parent branch.
+   *
+   * @returns {boolean}
+   */
+  hasParentBranch() {
+    return Boolean(this._parentBranch);
+  }
   /**
   /**
    * Get parent branch.
    * Get parent branch.
    *
    *
    * @returns {RouterBranch|undefined}
    * @returns {RouterBranch|undefined}
    */
    */
-  get parentBranch() {
+  getParentBranch() {
+    if (!this._parentBranch) {
+      throw new import_js_format20.InvalidArgumentError(
+        "Parent branch does not exist in the router branch."
+      );
+    }
     return this._parentBranch;
     return this._parentBranch;
   }
   }
   /**
   /**
@@ -2024,7 +2037,7 @@ var _RouterBranch = class _RouterBranch extends DebuggableService {
     this._parentBranch = parentBranch;
     this._parentBranch = parentBranch;
     if (parentBranch) {
     if (parentBranch) {
       const mergedDef = mergeRouterBranchDefinitions(
       const mergedDef = mergeRouterBranchDefinitions(
-        parentBranch.definition,
+        parentBranch.getDefinition(),
         branchDef
         branchDef
       );
       );
       this._definition = cloneDeep(mergedDef);
       this._definition = cloneDeep(mergedDef);

+ 8 - 4
src/branch/router-branch.d.ts

@@ -1,5 +1,4 @@
 import {TrieRouter} from '../trie-router.js';
 import {TrieRouter} from '../trie-router.js';
-import {ServiceContainer} from '@e22m4u/js-service';
 import {DebuggableService} from '../debuggable-service.js';
 import {DebuggableService} from '../debuggable-service.js';
 
 
 import {
 import {
@@ -27,17 +26,22 @@ export declare class RouterBranch extends DebuggableService {
   /**
   /**
    * Get router.
    * Get router.
    */
    */
-  get router(): TrieRouter;
+  getRouter(): TrieRouter;
 
 
   /**
   /**
    * Get definition.
    * Get definition.
    */
    */
-  get definition(): RouterBranchDefinition;
+  getDefinition(): RouterBranchDefinition;
+
+  /**
+   * Has parent branch.
+   */
+  hasParentBranch(): boolean;
 
 
   /**
   /**
    * Get parent branch.
    * Get parent branch.
    */
    */
-  get parentBranch(): RouterBranch | undefined;
+  getParentBranch(): RouterBranch;
 
 
   /**
   /**
    * Constructor.
    * Constructor.

+ 18 - 4
src/branch/router-branch.js

@@ -36,7 +36,7 @@ export class RouterBranch extends DebuggableService {
    *
    *
    * @type {TrieRouter}
    * @type {TrieRouter}
    */
    */
-  get router() {
+  getRouter() {
     return this._router;
     return this._router;
   }
   }
 
 
@@ -52,7 +52,7 @@ export class RouterBranch extends DebuggableService {
    *
    *
    * @type {RouterBranchDefinition}
    * @type {RouterBranchDefinition}
    */
    */
-  get definition() {
+  getDefinition() {
     return this._definition;
     return this._definition;
   }
   }
 
 
@@ -63,12 +63,26 @@ export class RouterBranch extends DebuggableService {
    */
    */
   _parentBranch;
   _parentBranch;
 
 
+  /**
+   * Has parent branch.
+   *
+   * @returns {boolean}
+   */
+  hasParentBranch() {
+    return Boolean(this._parentBranch);
+  }
+
   /**
   /**
    * Get parent branch.
    * Get parent branch.
    *
    *
    * @returns {RouterBranch|undefined}
    * @returns {RouterBranch|undefined}
    */
    */
-  get parentBranch() {
+  getParentBranch() {
+    if (!this._parentBranch) {
+      throw new InvalidArgumentError(
+        'Parent branch does not exist in the router branch.',
+      );
+    }
     return this._parentBranch;
     return this._parentBranch;
   }
   }
 
 
@@ -98,7 +112,7 @@ export class RouterBranch extends DebuggableService {
     this._parentBranch = parentBranch;
     this._parentBranch = parentBranch;
     if (parentBranch) {
     if (parentBranch) {
       const mergedDef = mergeRouterBranchDefinitions(
       const mergedDef = mergeRouterBranchDefinitions(
-        parentBranch.definition,
+        parentBranch.getDefinition(),
         branchDef,
         branchDef,
       );
       );
       this._definition = cloneDeep(mergedDef);
       this._definition = cloneDeep(mergedDef);

+ 35 - 9
src/branch/router-branch.spec.js

@@ -16,28 +16,54 @@ describe('RouterBranch', function () {
       const router = new TrieRouter();
       const router = new TrieRouter();
       const parent = router.createBranch({path: 'foo'});
       const parent = router.createBranch({path: 'foo'});
       const S = new RouterBranch(router, {path: 'bar'}, parent);
       const S = new RouterBranch(router, {path: 'bar'}, parent);
-      expect(S.definition.path).to.be.eq('/foo/bar');
+      expect(S.getDefinition().path).to.be.eq('/foo/bar');
     });
     });
+  });
 
 
-    it('should set a given router to the "router" property', function () {
+  describe('getRouter', function () {
+    it('should return the router instance that was provided to the constructor', function () {
       const router = new TrieRouter();
       const router = new TrieRouter();
       const S = new RouterBranch(router, {path: ROOT_PATH});
       const S = new RouterBranch(router, {path: ROOT_PATH});
-      expect(S.router).to.be.eq(router);
+      expect(S.getRouter()).to.be.eq(router);
     });
     });
+  });
 
 
-    it('should set a given definition to the "definition" property as a copy', function () {
+  describe('getDefinition', function () {
+    it('should return the branch definition that was provided to the constructor', function () {
       const router = new TrieRouter();
       const router = new TrieRouter();
       const branchDef = {path: ROOT_PATH};
       const branchDef = {path: ROOT_PATH};
       const S = new RouterBranch(router, branchDef);
       const S = new RouterBranch(router, branchDef);
-      expect(S.definition).to.be.eql(branchDef);
-      expect(S.definition).to.be.not.eq(branchDef);
+      expect(S.getDefinition()).to.be.eql(branchDef);
+      expect(S.getDefinition()).to.be.not.eq(branchDef);
+    });
+  });
+
+  describe('hasParentBranch', function () {
+    it('should return true if a parent branch does exist', function () {
+      const router = new TrieRouter();
+      const parent = router.createBranch({path: ROOT_PATH});
+      const branch1 = new RouterBranch(router, {path: ROOT_PATH});
+      expect(branch1.hasParentBranch()).to.be.false;
+      const branch2 = new RouterBranch(router, {path: ROOT_PATH}, parent);
+      expect(branch2.hasParentBranch()).to.be.true;
     });
     });
+  });
 
 
-    it('should set a parent branch to the "parentBranch" property', function () {
+  describe('getParentBranch', function () {
+    it('should return a parent branch that was provided to the constructor', function () {
       const router = new TrieRouter();
       const router = new TrieRouter();
       const parent = router.createBranch({path: ROOT_PATH});
       const parent = router.createBranch({path: ROOT_PATH});
       const S = new RouterBranch(router, {path: ROOT_PATH}, parent);
       const S = new RouterBranch(router, {path: ROOT_PATH}, parent);
-      expect(S.parentBranch).to.be.eq(parent);
+      expect(S.getParentBranch()).to.be.eq(parent);
+    });
+
+    it('should throw an error if a parent branch does not exist', function () {
+      const router = new TrieRouter();
+      const S = new RouterBranch(router, {path: ROOT_PATH});
+      const throwable = () => S.getParentBranch();
+      expect(throwable).to.throw(
+        'Parent branch does not exist in the router branch.',
+      );
     });
     });
   });
   });
 
 
@@ -77,7 +103,7 @@ describe('RouterBranch', function () {
       const router = new TrieRouter();
       const router = new TrieRouter();
       const S = new RouterBranch(router, {path: 'foo'});
       const S = new RouterBranch(router, {path: 'foo'});
       const res = S.createBranch({path: 'bar'});
       const res = S.createBranch({path: 'bar'});
-      expect(res.definition.path).to.be.eq('/foo/bar');
+      expect(res.getDefinition().path).to.be.eq('/foo/bar');
     });
     });
   });
   });
 });
 });

+ 1 - 1
src/trie-router.spec.js

@@ -34,7 +34,7 @@ describe('TrieRouter', function () {
       const router = new TrieRouter();
       const router = new TrieRouter();
       const branchDef = {path: 'foo'};
       const branchDef = {path: 'foo'};
       const res = router.createBranch(branchDef);
       const res = router.createBranch(branchDef);
-      expect(res.definition.path).to.be.eq(branchDef.path);
+      expect(res.getDefinition().path).to.be.eq(branchDef.path);
     });
     });
   });
   });