Captain is currently in ALPHA. We are happy to get your feedback

Module: string

Table of contents

Functions

Functions

getActionArguments

getActionArguments(action): Object

Parses a given action string into its constituent components: command, captainId, value, and options.

The action string is structured to command actions within the application, following the format:

  • "command:captainId" for actions without additional data.
  • "command:captainId:value" for actions with data but without options.
  • "command:captainId:value|options" for actions with both data and options.

Here, "captainId" refers to the data-captainid attribute of an HTML element, identifying the target for the command.

Parameters

NameTypeDescription
actionstringThe action string to be parsed, incorporating the command, target element's captainId, optional value, and optional options.

Returns

Object

An object containing the parsed parts of the action: { command, captainId, value, options }.

  • command: The action to perform (e.g., focus, click, type).
  • captainId: Identifier for the target element (matches data-captainid attribute).
  • value: Optional data relevant to the command (e.g., text to type).
  • options: Additional parameters or instructions (e.g., "submit" to indicate form submission after typing).
NameType
captainIdstring
commandstring
optionsundefined | string
valueundefined | string

Example

// Example 1: Focus on an element with specific captainId
getActionArguments("focus:color-mode-settings");
// Returns: { command: "focus", captainId: "color-mode-settings", value: undefined, options: undefined }

Example

// Example 2: Click an element with specific captainId
getActionArguments("click:language-settings");
// Returns: { command: "click", captainId: "language-settings", value: undefined, options: undefined }

Example

// Example 3: Type into a form and submit
getActionArguments("type:feedback-form:this app is amazing|submit");
// Returns: { command: "type", captainId: "feedback-form", value: "this app is amazing", options: "submit" }

Defined in

string.js:34


injectPrompt

injectPrompt(prompt, template): string

Replaces a placeholder for a prompt within a given template string with the specified prompt text.

This function searches for a placeholder in the format { prompt } within the provided template string and replaces it with the provided prompt text. The replacement is case-insensitive and matches the placeholder regardless of surrounding whitespace.

Parameters

NameTypeDescription
promptstringThe text to insert into the template string where the placeholder { prompt } is found.
templatestringThe template string containing the placeholder { prompt } to be replaced.

Returns

string

  • The modified template string with the { prompt } placeholder replaced by the specified prompt text.

Example

const template = "a photo of { prompt }, highres, bokeh";
const prompt = "a cute puppy playing with a red ball";
injectPrompt(prompt, template);
// Returns: "a photo of a cute puppy playing with a red ball, highres, bokeh"

Defined in

string.js:101


localFile

localFile(filePath, options?): string

Constructs a URI for a local file using a specified protocol, based on the absolute path of the file on the disk. This function facilitates the creation of URIs that adhere to specific protocol schemes for accessing files directly from the file system.

Parameters

NameTypeDefault valueDescription
filePathanyundefinedThe absolute path to the file on the disk. This should include the full path from the root directory of the file system to the file itself, ensuring precise location for URI construction.
optionsObject{}An optional object to specify additional settings: - localProtocol: The protocol scheme to use for the URI. This defines the method of access for the file. Commonly used protocols include 'file', but custom protocols can be specified to suit particular needs or applications. If not specified, defaults to a predefined LOCAL_PROTOCOL variable.
options.localProtocolundefined | stringLOCAL_PROTOCOL-

Returns

string

A string representing the URI constructed with the specified local protocol and the absolute file path.

Example

// Generate a URI for a file with default protocol, assuming LOCAL_PROTOCOL is 'file'
console.log(localFile('/Users/example/path/to/myFile.txt'));
// Output: 'file:///Users/example/path/to/myFile.txt'

Example

// Generate a URI for a file with a custom protocol
console.log(localFile('/Users/example/path/to/myFile.txt', { localProtocol: 'customProtocol' }));
// Output: 'customProtocol:///Users/example/path/to/myFile.txt'

Defined in

string.js:79