Skip to content

Commit

Permalink
Remove unused types in jsdoc
Browse files Browse the repository at this point in the history
Replace interfaces by types
  • Loading branch information
yoriiis committed Jun 26, 2023
1 parent beed85b commit aab257a
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 74 deletions.
48 changes: 24 additions & 24 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Location from './location'
import { RouteData, Route, HelperFunction, Fn, Component } from './interface'
import { RouteData, Route, HelperFunction, Fn, Component } from './types'
import { getDynamicSegmentsFromPath, createRegExpFromPath } from './utils'

export default class App {
Expand All @@ -14,12 +14,12 @@ export default class App {

/**
* @constructor
* @param {Object} options
* @param {String} options.mode Location mode (hash|history)
* @param {Array} options.routes Definition of routes
* @param {HTMLElement} options.target HTMLElement target
* @param {String} options.basePath Site base path
* @param {Boolean} options.silentOnNotFound Silent not found routes
* @param options
* @param options.mode Location mode (hash|history)
* @param options.routes Definition of routes
* @param options.target HTMLElement target
* @param options.basePath Site base path
* @param options.silentOnNotFound Silent not found routes
*/
constructor({
mode = 'hash',
Expand Down Expand Up @@ -67,8 +67,8 @@ export default class App {

/**
* Create routes data
* @param {Array<Route>} routes Definition of routes
* @returns {Object} Routes data parsed
* @param routes Definition of routes
* @returns Routes data parsed
*/
createRoutesData(routes: Route[]): Map<string, RouteData> {
const inValidRoutes = routes
Expand Down Expand Up @@ -123,17 +123,17 @@ export default class App {

/**
* Check if the component interface type is granted
* @param {Function} component Component
* @returns {Boolean} Interface type is granted
* @param component Component
* @returns Interface type is granted
*/
isInterfaceTypeFromComponentGranted(component: Fn | Component): boolean {
return !!(typeof component === 'function' || this.isComponentClass(component))
}

/**
* Check if the component is a class component (extends from Component)
* @param {Function} component Component
* @returns {Boolean} The component extends from the Component class
* @param component Component
* @returns The component extends from the Component class
*/
isComponentClass(component: Fn | Component): boolean {
return component.prototype ? !!component.prototype.__isComponent : false
Expand All @@ -149,7 +149,7 @@ export default class App {

/**
* On navigate event
* @param {Event} e Event data
* @param e Event data
*/
onNavigate(e: Event) {
const { to } = (<CustomEvent>e).detail
Expand All @@ -158,7 +158,7 @@ export default class App {

/**
* On click on app event
* @param {Event} e Event data
* @param e Event data
*/
onClickOnApp(e: Event) {
const target = e.target as HTMLElement
Expand All @@ -174,7 +174,7 @@ export default class App {

/**
* On route change event
* @param {Object} currentPath Current path from location
* @param currentPath Current path from location
*/
onRouteChange(currentPath: string) {
const route = this.getRouteMatch(currentPath)
Expand All @@ -199,8 +199,8 @@ export default class App {
/**
* Search the route from the path
* Path can contain transformed segments
* @param {String} path Path
* @returns {(Route|undefined)} The route that matches the path or undefined
* @param path Path
* @returns The route that matches the path or undefined
*/
getRouteMatch(path: string): RouteData | undefined {
const route = this.routes.get(path)
Expand Down Expand Up @@ -299,7 +299,7 @@ export default class App {
/**
* Get the component view
* From the render function is Component, or from the component itself
* @returns {(Node.ELEMENT_NODE|Node.DOCUMENT_FRAGMENT_NODE)} The component view
* @returns The component view
*/
getComponentView() {
if (this.currentRoute) {
Expand Down Expand Up @@ -342,8 +342,8 @@ export default class App {

/**
* Get the interface type from the component view
* @param {(Node.ELEMENT_NODE|Node.DOCUMENT_FRAGMENT_NODE)} component Component view
* @returns {(String|null)} Component type or null
* @param.ELEMENT_NODE|Node.DOCUMENT_FRAGMENT_NODE)} component Component view
* @returns Component type or null
*/
getInterfaceTypeFromView(component: string | Node): string | null {
if (typeof component === 'string') {
Expand All @@ -361,8 +361,8 @@ export default class App {

/**
* Transform links inside string component into router friendly links
* @param {(Node.ELEMENT_NODE|Node.DOCUMENT_FRAGMENT_NODE)} component Component view
* @returns {Node.DOCUMENT_FRAGMENT_NODE} View of components with links compatible with the router
* @param.ELEMENT_NODE|Node.DOCUMENT_FRAGMENT_NODE)} component Component view
* @returns View of components with links compatible with the router
*/
transformLinksInStringComponent(component: string): DocumentFragment {
const template = document.createElement('template')
Expand All @@ -387,7 +387,7 @@ export default class App {
/**
* Get component helper functions
* Function are inject as dependencies in the Component
* @returns {Object} List of helper function
* @returns List of helper function
*/
getComponentHelpers(): HelperFunction {
return {
Expand Down
10 changes: 5 additions & 5 deletions src/component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { extend } from './utils'
import { privateGetExternalStore, RouteComponent } from './interface'
import { privateGetExternalStore, RouteComponent } from './types'

class Component {
store: Map<string, object>
Expand Down Expand Up @@ -55,7 +55,7 @@ class Component {

/**
* Set the component store
* @param {Object} data Data to store
* @param data Data to store
*/
setStore(data: any) {
const keys = Object.keys(data) as string[]
Expand All @@ -75,9 +75,9 @@ class Component {
/**
* Get store from a key
* Store can be retrieved from an external Component
* @param {String} key Store key
* @param {String} path Cmponent path
* @returns {(any|null)} Content of the store key
* @param key Store key
* @param path Cmponent path
* @returns Content of the store key
*/
getStore(key: string, path?: string): object | undefined | null {
if (key) {
Expand Down
2 changes: 1 addition & 1 deletion src/jsx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ElementAttributes, createElementFunction, Constructable, Component } from './interface'
import { ElementAttributes, createElementFunction, Constructable, Component } from './types'

type ChildrenAsArray = string[] | HTMLElement[] | SVGElement[]

Expand Down
14 changes: 7 additions & 7 deletions src/link.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Attributes } from './interface'
import { Attributes } from './types'

/**
* Create the link
* @param {Object} options
* @param {Object} options.to Location path
* @param {Object} options.children The children of the element
* @param {Object} options.attrs The attributes of the element
* @param {Boolean} isHtml The function is called from HTML
* @returns {(String|HTMLElement)} Element as string or HTMLElement
* @param options
* @param options.to Location path
* @param options.children The children of the element
* @param options.attrs The attributes of the element
* @param isHtml The function is called from HTML
* @returns Element as string or HTMLElement
*/
export default function Link(
{
Expand Down
24 changes: 12 additions & 12 deletions src/location.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { onRouteChangeFunction } from './interface'
import { onRouteChangeFunction } from './types'

export default class Location {
callback: onRouteChangeFunction
Expand All @@ -9,10 +9,10 @@ export default class Location {

/**
* @constructor
* @param {Object} options
* @param {Function} options.callback On route change callback function
* @param {Boolean} options.mode Location mode (hash|history)
* @param {Boolean} options.basePath Site base path
* @param options
* @param options.callback On route change callback function
* @param options.mode Location mode (hash|history)
* @param options.basePath Site base path
*/
constructor({
callback,
Expand All @@ -36,8 +36,8 @@ export default class Location {
/**
* Normalize the site base path
* Add leading slash and remove trailing slash
* @param {String} basePath Base path
* @returns {String} Normalized base path
* @param basePath Base path
* @returns Normalized base path
*/
normalizeBasePath(basePath: string): string {
return basePath.replace(/\/+$/, '').replace(/^\/*/, '/')
Expand Down Expand Up @@ -68,7 +68,7 @@ export default class Location {
/**
* Get the current path
* In history mode, the path does not contains the base path
* @returns {String} Current path or default path
* @returns Current path or default path
*/
getPath(): string {
if (this.isHashMode) {
Expand All @@ -86,9 +86,9 @@ export default class Location {

/**
* Extract the base path from the pathname
* @param {String} pathname Loation pathname
* @param {String} basePath Normalized base path
* @returns {String} Pathname without the base path
* @param pathname Loation pathname
* @param basePath Normalized base path
* @returns Pathname without the base path
*/
stripBasePath(pathname: string, basePath: string): string {
// Default base path
Expand All @@ -105,7 +105,7 @@ export default class Location {

/**
* Set the new path
* @param {String} path New path
* @param path New path
*/
setPath(path: string) {
this.currentPath = path
Expand Down
2 changes: 1 addition & 1 deletion src/navigate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Navigate to a path
* Dispatch an event listen by the App
* Used by Component.navigate or JSX onClick function
* @param {String} to Location path
* @param to Location path
*/
export default function navigate(to: string) {
document.dispatchEvent(
Expand Down
24 changes: 9 additions & 15 deletions src/interface.ts → src/types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
export type Fn = () => void

export interface Constructable<T> {
export type Constructable<T> = {
new (...args: any): T
}

export interface RouteComponent {
params: {
[key: string]: string
}
export type RouteComponent = {
params: Record<string, string>
path: string
}

export interface Component {
export type Component = {
afterDestroy: Fn
afterRender: Fn
beforeDestroy: Fn
Expand All @@ -26,13 +24,13 @@ export interface Component {
setStore: (data: any) => void
}

export interface Route {
export type Route = {
component: Constructable<Component> | Fn | any // Multi types with constructor and function fails
path: string
props?: any
}

export interface RouteData {
export type RouteData = {
component: any
dynamicSegments: string[]
interfaceType: string | null
Expand All @@ -43,21 +41,17 @@ export interface RouteData {
props: any
}

export interface Attributes {
[key: string]: string
}
export type Attributes = Record<string, string>

export type onRouteChangeFunction = (currentPath: string) => void

export type privateGetExternalStore = (key: string, path: string) => object | undefined | null

export interface HelperFunction {
export type HelperFunction = {
__getExternalStore: privateGetExternalStore
}

export interface ElementAttributes {
[key: string]: string | Fn
}
export type ElementAttributes = Record<string, string | Fn>

export type createElementFunction = ({
children,
Expand Down
18 changes: 9 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**
* Check whether an object has the property
* @param {Object} obj Object reference
* @param {String} key Object property key inside the object
* @returns {Boolean} Object has the property key
* @param obj Object reference
* @param key Object property key inside the object
* @returns Object has the property key
*/
function hasOwn(obj: any, key: string): any {
return Object.prototype.hasOwnProperty.call(obj, key)
}

/**
* Deep clone for multiple objects
* @param {Boolean} deep Deep clone
* @param {Array<Object>} objects List of objects to merged
* @param deep Deep clone
* @param>} objects List of objects to merged
* @returns Merged object
*/
function extend(deep = false, ...objects: any[]): any {
Expand Down Expand Up @@ -48,8 +48,8 @@ const PATTERN_DYNAMIC_SEGMENT = `\/:([^\/]*)`

/**
* Get dynamic segments from path
* @param {String} path Path
* @returns {Array<String>} List of dynamic segments as key (without the "/:"")
* @param path Path
* @returns List of dynamic segments as key (without the "/:"")
*/
function getDynamicSegmentsFromPath(path: string): string[] {
const segments = path.match(new RegExp(PATTERN_DYNAMIC_SEGMENT, 'g')) || []
Expand All @@ -68,8 +68,8 @@ function getDynamicSegmentsFromPath(path: string): string[] {
/**
* Create RegExp from path
* Used to match path with dynamic segments ("/:id/:name" = "/42/john-doe")
* @param {String} path Path
* @returns {String} Path transformed in RegExp
* @param path Path
* @returns Path transformed in RegExp
*/
function createRegExpFromPath(path: string): string {
return (
Expand Down

0 comments on commit aab257a

Please sign in to comment.