Forráskód Böngészése

docs: add @linkcode to jsdoc comments

Sven 1 éve
szülő
commit
c631350642
7 módosított fájl, 43 hozzáadás és 41 törlés
  1. 4 4
      README.md
  2. 8 5
      lib/config.ts
  3. 9 9
      lib/dom.ts
  4. 14 15
      lib/math.ts
  5. 2 2
      lib/misc.ts
  6. 4 4
      lib/onSelector.ts
  7. 2 2
      lib/translation.ts

+ 4 - 4
README.md

@@ -636,10 +636,10 @@ Usage:
 ```ts
 mapRange(
   value: number,
-  range_1_min: number,
-  range_1_max: number,
-  range_2_min: number,
-  range_2_max: number
+  range1min: number,
+  range1max: number,
+  range2min: number,
+  range2max: number
 ): number
 ```
   

+ 8 - 5
lib/config.ts

@@ -38,7 +38,7 @@ export interface ConfigManagerOptions<TData> {
  * Supports migrating data from older versions of the configuration to newer ones and populating the cache with default data if no persistent data is found.  
  *   
  * ⚠️ Requires the directives `@grant GM.getValue` and `@grant GM.setValue`  
- * ⚠️ Make sure to call `loadData()` at least once after creating an instance, or the returned data will be the same as `options.defaultConfig`
+ * ⚠️ Make sure to call {@linkcode loadData()} at least once after creating an instance, or the returned data will be the same as `options.defaultConfig`
  * 
  * @template TData The type of the data that is saved in persistent storage (will be automatically inferred from `config.defaultConfig`) - this should also be the type of the data format associated with the current `options.formatVersion`
  */
@@ -54,7 +54,7 @@ export class ConfigManager<TData = any> {
    * Supports migrating data from older versions of the configuration to newer ones and populating the cache with default data if no persistent data is found.  
    *   
    * ⚠️ Requires the directives `@grant GM.getValue` and `@grant GM.setValue`  
-   * ⚠️ Make sure to call `loadData()` at least once after creating an instance, or the returned data will be the same as `options.defaultConfig`
+   * ⚠️ Make sure to call {@linkcode loadData()} at least once after creating an instance, or the returned data will be the same as `options.defaultConfig`
    * 
    * @template TData The type of the data that is saved in persistent storage (will be automatically inferred from `config.defaultConfig`) - this should also be the type of the data format associated with the current `options.formatVersion`
    * @param options The options for this ConfigManager instance
@@ -98,7 +98,10 @@ export class ConfigManager<TData = any> {
     }
   }
 
-  /** Returns a copy of the data from the in-memory cache. Use `loadData()` to get fresh data from persistent storage (usually not necessary since the cache should always exactly reflect persistent storage). */
+  /**
+   * Returns a copy of the data from the in-memory cache.  
+   * Use {@linkcode loadData()} to get fresh data from persistent storage (usually not necessary since the cache should always exactly reflect persistent storage).
+   */
   public getData(): TData {
     return this.deepCopy(this.cachedConfig);
   }
@@ -129,8 +132,8 @@ export class ConfigManager<TData = any> {
 
   /**
    * Call this method to clear all persistently stored data associated with this ConfigManager instance.  
-   * The in-memory cache will be left untouched, so you may still access the data with `getData()`.  
-   * Calling `loadData()` or `setData()` after this method was called will recreate persistent storage with the cached or default data.  
+   * The in-memory cache will be left untouched, so you may still access the data with {@linkcode getData()}  
+   * Calling {@linkcode loadData()} or {@linkcode setData()} after this method was called will recreate persistent storage with the cached or default data.  
    *   
    * ⚠️ This requires the additional directive `@grant GM.deleteValue`
    */

+ 9 - 9
lib/dom.ts

@@ -12,8 +12,8 @@ export function getUnsafeWindow() {
 }
 
 /**
- * Inserts `afterElement` as a sibling just after the provided `beforeElement`
- * @returns Returns the `afterElement`
+ * Inserts {@linkcode afterElement} as a sibling just after the provided {@linkcode beforeElement}
+ * @returns Returns the {@linkcode afterElement}
  */
 export function insertAfter(beforeElement: Element, afterElement: Element) {
   beforeElement.parentNode?.insertBefore(afterElement, beforeElement.nextSibling);
@@ -86,15 +86,15 @@ export function openInNewTab(href: string) {
 }
 
 /**
- * Intercepts the specified event on the passed object and prevents it from being called if the called `predicate` function returns a truthy value.  
+ * Intercepts the specified event on the passed object and prevents it from being called if the called {@linkcode predicate} function returns a truthy value.  
  * This function should be called as soon as possible (I recommend using `@run-at document-start`), as it will only intercept events that are added after this function is called.  
  * Calling this function will set the `Error.stackTraceLimit` to 1000 to ensure the stack trace is preserved.
  */
 export function interceptEvent<TEvtObj extends EventTarget, TPredicateEvt extends Event>(
   eventObject: TEvtObj,
   eventName: Parameters<TEvtObj["addEventListener"]>[0],
-  predicate: (event: TPredicateEvt) => boolean,
-) {  
+  predicate: (event: TPredicateEvt) => boolean = () => true,
+) {
   // default is between 10 and 100 on conventional browsers so this should hopefully be more than enough
   // @ts-ignore
   if(typeof Error.stackTraceLimit === "number" && Error.stackTraceLimit < 1000) {
@@ -119,13 +119,13 @@ export function interceptEvent<TEvtObj extends EventTarget, TPredicateEvt extend
 }
 
 /**
- * Intercepts the specified event on the window object and prevents it from being called if the called `predicate` function returns a truthy value.  
+ * Intercepts the specified event on the window object and prevents it from being called if the called {@linkcode predicate} function returns a truthy value.  
  * This function should be called as soon as possible (I recommend using `@run-at document-start`), as it will only intercept events that are added after this function is called.  
  * Calling this function will set the `Error.stackTraceLimit` to 1000 to ensure the stack trace is preserved.
  */
 export function interceptWindowEvent<TEvtKey extends keyof WindowEventMap>(
   eventName: TEvtKey,
-  predicate: (event: WindowEventMap[TEvtKey]) => boolean,
+  predicate: (event: WindowEventMap[TEvtKey]) => boolean = () => true,
 ) {  
   return interceptEvent(getUnsafeWindow(), eventName, predicate);
 }
@@ -146,7 +146,7 @@ export function interceptWindowEvent<TEvtKey extends keyof WindowEventMap>(
  * @returns Returns an object with the following properties:
  * | Property | Description |
  * | :-- | :-- |
- * | `setGain()` | Used to change the gain multiplier |
+ * | `setGain()` | Used to change the gain multiplier from the default set by {@linkcode initialMultiplier} |
  * | `getGain()` | Returns the current gain multiplier |
  * | `enable()` | Call to enable the amplification for the first time or if it was disabled before |
  * | `disable()` | Call to disable amplification |
@@ -209,7 +209,7 @@ export function amplifyMedia<TElem extends HTMLMediaElement>(mediaElement: TElem
   return props;
 }
 
-/** An object which contains the results of `amplifyMedia()` */
+/** An object which contains the results of {@linkcode amplifyMedia()} */
 export type AmplifyMediaResult = ReturnType<typeof amplifyMedia>;
 
 /** Checks if an element is scrollable in the horizontal and vertical directions */

+ 14 - 15
lib/math.ts

@@ -1,35 +1,34 @@
-/** Ensures the passed `value` always stays between `min` and `max` */
+/** Ensures the passed {@linkcode value} always stays between {@linkcode min} and {@linkcode max} */
 export function clamp(value: number, min: number, max: number) {
   return Math.max(Math.min(value, max), min);
 }
 
 /**
- * Transforms the value parameter from the numerical range `range_1_min-range_1_max` to the numerical range `range_2_min-range_2_max`  
+ * Transforms the value parameter from the numerical range `range1min─range1max` to the numerical range `range2min─range2max`  
  * For example, you can map the value 2 in the range of 0-5 to the range of 0-10 and you'd get a 4 as a result.
  */
-export function mapRange(value: number, range_1_min: number, range_1_max: number, range_2_min: number, range_2_max: number) {
-  if(Number(range_1_min) === 0.0 && Number(range_2_min) === 0.0)
-    return value * (range_2_max / range_1_max);
+export function mapRange(value: number, range1min: number, range1max: number, range2min: number, range2max: number) {
+  if(Number(range1min) === 0.0 && Number(range2min) === 0.0)
+    return value * (range2max / range1max);
 
-  return (value - range_1_min) * ((range_2_max - range_2_min) / (range_1_max - range_1_min)) + range_2_min;
+  return (value - range1min) * ((range2max - range2min) / (range1max - range1min)) + range2min;
 }
 
-/** Returns a random number between `min` and `max` (inclusive) */
+/** Returns a random number between {@linkcode min} and {@linkcode max} (inclusive) */
 export function randRange(min: number, max: number): number
-/** Returns a random number between 0 and `max` (inclusive) */
+/** Returns a random number between 0 and {@linkcode max} (inclusive) */
 export function randRange(max: number): number
-/** Returns a random number between `min` and `max` (inclusive) */
+/** Returns a random number between {@linkcode min} and {@linkcode max} (inclusive) */
 export function randRange(...args: number[]): number {
   let min: number, max: number;
 
-  if(typeof args[0] === "number" && typeof args[1] === "number") {
-    // using randRange(min, max)
+  // using randRange(min, max)
+  if(typeof args[0] === "number" && typeof args[1] === "number")
     [ min, max ] = args;
-  }
+  // using randRange(max)
   else if(typeof args[0] === "number" && typeof args[1] !== "number") {
-    // using randRange(max)
     min = 0;
-    max = args[0];
+    [ max ] = args;
   }
   else
     throw new TypeError(`Wrong parameter(s) provided - expected: "number" and "number|undefined", got: "${typeof args[0]}" and "${typeof args[1]}"`);
@@ -38,7 +37,7 @@ export function randRange(...args: number[]): number {
   max = Number(max);
 
   if(isNaN(min) || isNaN(max))
-    throw new TypeError("Parameters \"min\" and \"max\" can't be NaN");
+    return NaN;
 
   if(min > max)
     throw new TypeError("Parameter \"min\" can't be bigger than \"max\"");

+ 2 - 2
lib/misc.ts

@@ -2,7 +2,7 @@
 export type Stringifiable = string | { toString(): string };
 
 /**
- * Automatically appends an `s` to the passed `word`, if `num` is not equal to 1
+ * Automatically appends an `s` to the passed {@linkcode word}, if {@linkcode num} is not equal to 1
  * @param word A word in singular form, to auto-convert to plural
  * @param num If this is an array or NodeList, the amount of items is used
  */
@@ -20,7 +20,7 @@ export function pauseFor(time: number) {
 }
 
 /**
- * Calls the passed `func` after the specified `timeout` in ms (defaults to 300).  
+ * Calls the passed {@linkcode func} after the specified {@linkcode timeout} in ms (defaults to 300).  
  * Any subsequent calls to this function will reset the timer and discard previous calls.
  */
 export function debounce<TFunc extends (...args: TArgs[]) => void, TArgs = any>(func: TFunc, timeout = 300) { // eslint-disable-line @typescript-eslint/no-explicit-any

+ 4 - 4
lib/onSelector.ts

@@ -1,4 +1,4 @@
-/** Options for the `onSelector()` function */
+/** Options for the {@linkcode onSelector()} function */
 export type OnSelectorOpts<TElem extends Element = HTMLElement> = SelectorOptsOne<TElem> | SelectorOptsAll<TElem>;
 
 type SelectorOptsOne<TElem extends Element> = SelectorOptsCommon & {
@@ -23,12 +23,12 @@ type SelectorOptsCommon = {
 const selectorMap = new Map<string, OnSelectorOpts[]>();
 
 /**
- * Calls the `listener` as soon as the `selector` exists in the DOM.  
+ * Calls the {@linkcode listener} as soon as the {@linkcode selector} exists in the DOM.  
  * Listeners are deleted when they are called once, unless `options.continuous` is set.  
  * Multiple listeners with the same selector may be registered.
  * @param selector The selector to listen for
  * @param options Used for switching to `querySelectorAll()` and for calling the listener continuously
- * @template TElem The type of element that the listener will return as its argument (defaults to the generic HTMLElement)
+ * @template TElem The type of element that the listener will return as its argument (defaults to the generic type HTMLElement)
  */
 export function onSelector<TElem extends Element = HTMLElement>(
   selector: string,
@@ -47,7 +47,7 @@ export function onSelector<TElem extends Element = HTMLElement>(
 }
 
 /**
- * Removes all listeners registered in `onSelector()` that have the given selector
+ * Removes all listeners registered in {@linkcode onSelector()} that have the given selector
  * @returns Returns true when all listeners with the associated selector were found and removed, false otherwise
  */
 export function removeOnSelector(selector: string) {

+ 2 - 2
lib/translation.ts

@@ -5,10 +5,10 @@ const trans: Record<string, Record<string, string>> = {};
 let curLang: string;
 
 /**
- * Returns the translated text for the specified key in the current language set by `setLanguage()`  
+ * Returns the translated text for the specified key in the current language set by {@linkcode tr.setLanguage()}  
  * If the key is not found in the previously registered translation, the key itself is returned.  
  *   
- * ⚠️ Remember to register a language with `tr.addLanguage()` and set it as active with `tr.setLanguage()` before using this function, otherwise it will always return the key itself.
+ * ⚠️ Remember to register a language with {@linkcode tr.addLanguage()} and set it as active with {@linkcode tr.setLanguage()} before using this function, otherwise it will always return the key itself.
  * @param key Key of the translation to return
  * @param args Optional arguments to be passed to the translated text. They will replace placeholders in the format `%n`, where `n` is the 1-indexed argument number
  */