or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

bindings.mdcontainer.mddependency-injection.mdindex.mdmodules.mdparsing-utilities.mdresolver.mdtesting.md
tile.json

parsing-utilities.mddocs/

String Parsing Utilities

Utility functions for parsing binding references from various formats including magic strings, class references, and lazy import references. These utilities are particularly useful for parsing route controller bindings, event listener bindings, and other string-based references in AdonisJS applications.

Capabilities

Parse Binding Reference

Parses binding references from multiple formats including magic strings, class references, and lazy import functions.

/**
 * Parses binding references from magic strings, class references, and lazy import references
 * @param binding - String reference or array with class/lazy import and optional method
 * @returns Promise resolving to parsed module path and method name
 */
function parseBindingReference(
  binding: string | [LazyImport<Constructor<any>> | Constructor<any>, any?]
): Promise<{ moduleNameOrPath: string; method: string }>;

Usage Examples:

import { parseBindingReference } from "@adonisjs/fold";

// Magic strings
const result1 = await parseBindingReference('#controllers/home_controller');
// Returns: { moduleNameOrPath: '#controllers/home_controller', method: 'handle' }

const result2 = await parseBindingReference('#controllers/home_controller.index');
// Returns: { moduleNameOrPath: '#controllers/home_controller', method: 'index' }

const result3 = await parseBindingReference('#controllers/home.controller.index');
// Returns: { moduleNameOrPath: '#controllers/home.controller', method: 'index' }

// Class reference
class HomeController {
  handle() { /* implementation */ }
  index() { /* implementation */ }
}

const result4 = await parseBindingReference([HomeController]);
// Returns: { moduleNameOrPath: 'HomeController', method: 'handle' }

const result5 = await parseBindingReference([HomeController, 'index']);
// Returns: { moduleNameOrPath: 'HomeController', method: 'index' }

// Lazy import reference
const HomeControllerImport = () => import('#controllers/home_controller');

const result6 = await parseBindingReference([HomeControllerImport]);
// Returns: { moduleNameOrPath: '#controllers/home_controller', method: 'handle' }

const result7 = await parseBindingReference([HomeControllerImport, 'index']);
// Returns: { moduleNameOrPath: '#controllers/home_controller', method: 'index' }

Magic String Format

The function supports magic strings with the following formats:

  • Simple module path: #controllers/home_controller → defaults to handle method
  • Module with method: #controllers/home_controller.index → uses index method
  • Nested module with method: #controllers/home.controller.index → handles dotted module names

Class Reference Format

The function accepts an array with:

  • First element: Class constructor or lazy import function
  • Second element (optional): Method name (defaults to handle)

Lazy Import Support

For lazy imports, the function:

  1. Parses the import function to extract the module specifier
  2. Uses the found import path as the moduleNameOrPath
  3. Applies the provided method name or defaults to handle

Types

type LazyImport<T> = () => Promise<{ default: T }>;
type Constructor<T = {}> = new (...args: any[]) => T;