mwbot-ts - v1.2.7
    Preparing search index...

    Interface TemplateBase<T>Protected

    The instance members of the TemplateBase class. For static members, see TemplateBaseStatic (defined separately due to TypeScript limitations).

    interface TemplateBase<T extends string | Title> {
        params: Record<string, TemplateParameter>;
        title: T;
        deleteParam(key: string, resolveHierarchy?: boolean): boolean;
        getParam(key: string, resolveHierarchy?: boolean): null | TemplateParameter;
        hasParam(key: string | RegExp, value?: string | RegExp): boolean;
        hasParam(predicate: (param: TemplateParameter) => boolean): boolean;
        insertParam(
            key: string,
            value: string,
            overwrite?: boolean,
            position?: { before: string } | { after: string } | "start" | "end",
        ): this;
        updateParam(key: string, value: string): this;
    }

    Type Parameters

    Hierarchy (View Summary)

    Index

    Properties

    params: Record<string, TemplateParameter>

    The template's parameters.

    This property is read-only. To update it, use insertParam, updateParam, or deleteParam as per your needs.

    title: T

    The template's title.

    This property is read-only. To update it, use setTitle.

    Methods

    • Deletes a parameter from the template.

      Parameters

      • key: string

        The parameter key to delete.

      • OptionalresolveHierarchy: boolean

        Whether to consider hierarchies when resolving the key. If true, this method checks the params object for any parameter whose key is an alias of key and deletes it if found. (Default: false)

      Returns boolean

      true if a matching parameter was deleted; otherwise, false.

    • Gets a parameter object by key.

      Caution: The returned object is mutable.

      Parameters

      • key: string

        The parameter key.

      • OptionalresolveHierarchy: boolean

        Whether to consider hierarchies when searching for a matching parameter. If true, this method first checks whether key belongs to a hierarchy array. If params contains a parameter with a higher-priority key in that hierarchy, that parameter is returned instead. (Default: false).

        Example:

        • Given key = '1', hierarchies = [['1', 'user', 'User']], and params containing a parameter keyed 'user', this method returns the 'user' parameter.

        If key does not belong to any hierarchy array, the method behaves the same as when resolveHierarchy is false. This also applies if no template parameter hierarchies have been provided via the constructor or ParseTemplatesConfig.hierarchies.

      Returns null | TemplateParameter

      The parameter object if found; otherwise, null.

    • Checks if a template parameter with the specified key exists, optionally matching its value.

      Parameters

      • key: string | RegExp

        The parameter key to match, either as an exact string or a regular expression.

      • Optionalvalue: string | RegExp

        The optional value matcher.

        • If a string, checks for an exact value match.
        • If a regular expression, tests the parameter value against the pattern.
        • If omitted, only the parameter key is checked.

      Returns boolean

      true if a matching parameter exists; otherwise, false.

      hasParam("user"); // Checks if a parameter named "user" exists.
      hasParam(/^data-/); // Checks for parameters starting with "data-".
      hasParam("id", "123"); // Checks if "id" exists and equals "123".
    • Checks if a template parameter exists based on a custom predicate function.

      Parameters

      • predicate: (param: TemplateParameter) => boolean

        A function that tests each parameter.

      Returns boolean

      true if a matching parameter exists; otherwise, false.

      hasParam((param) => param.key.startsWith("meta") && param.value.length > 10);
      
    • Inserts a template parameter into the list.

      By default, if the parameter is new:

      • It is inserted at the end of the list.
      • If position is specified, the insertion position is controlled accordingly.

      If a parameter with the same key already exists:

      • If overwrite is true (default):
        • If position is not specified, the parameter’s value is updated in place (its position remains unchanged).
        • If position is specified, the parameter’s value is updated and moved to the specified position.
      • If overwrite is false, the existing parameter is left unchanged and no insertion occurs.

      If parameter hierarchies are set, key is automatically resolved to the higher-priority key, if applicable.

      Note that the order of parameters in the final output can also be controlled via stringify, using TemplateOutputConfig.sortPredicate, regardless of the insertion order tracked by this instance.

      Parameters

      • key: string

        The key of the parameter. This can be an empty string for unnamed parameters.

      • value: string

        The value of the parameter.

      • Optionaloverwrite: boolean

        Whether to overwrite an existing parameter if it exists. (Default: true)

      • Optionalposition: { before: string } | { after: string } | "start" | "end"

        Where to insert or move the parameter:

        • 'start': Insert at the beginning.
        • 'end': Insert at the end (default for new parameters).
        • { before: referenceKey }: Insert before the given key.
        • { after: referenceKey }: Insert after the given key.

        If referenceKey does not exist:

        • For new parameters, insertion falls back to 'end'.
        • For existing parameters, the value is updated in place without moving it (i.e., behaves as if position is not specified).

        When specifying a referenceKey, it is best practice to verify its existence beforehand to ensure expected behavior. See hasParam for the relevant utility.

      Returns this

      The current instance for chaining.

    • Updates the value of an existing parameter without changing its position.

      • If no parameter with the given key exists, this method does nothing.
      • If parameter hierarchies are set, key is automatically resolved to the higher-priority key, if applicable.

      This is functionally equivalent to calling insertParam with overwrite = true and no position, except that it performs no operation if the parameter does not already exist.

      Use this method when you want to safely update a parameter only if it exists, without affecting the order of parameters or accidentally inserting new ones.

      Parameters

      • key: string

        The key of the parameter. This can be an empty string for unnamed parameters.

      • value: string

        The new value of the parameter.

      Returns this

      The current instance for chaining.