Skip to content

options

Functions

getOptionArray()

function getOptionArray(
   options, 
   key, 
   defaultValue?, 
   separator?): string[];

Returns a string array option from a separator-delimited value.

Empty items are removed and each entry is trimmed.

Missing values return the provided default.

Parameters

options

Plugin options record.

Record\<string, string> | undefined

key

string

Option name to read.

defaultValue?

string[] = []

Value returned when the option is missing.

separator?

string = ","

Delimiter used to split the raw string.

Returns

string[]

A trimmed array of non-empty entries, an empty array for blank values, or the default when the key is missing.

Example

// For options: { "features": "feature1, feature2, feature3" }
getOptionArray(options, "features", [], ",")
// returns ["feature1", "feature2", "feature3"]

getOptionBool()

function getOptionBool(
   options, 
   key, 
   defaultValue): boolean;

Returns a boolean option using common truthy and falsy string values.

Accepted truthy values: true, 1, yes, on, enable, enabled, y.

Accepted falsy values: false, 0, no, off, disable, disabled, n.

Invalid values fall back to the provided default.

Parameters

options

Plugin options record.

Record\<string, string> | undefined

key

string

Option name to read.

defaultValue

boolean

Value returned when the option is missing or invalid.

Returns

boolean

The parsed boolean value, or the default when parsing fails.

Example

getOptionBool({ watch: "yes" }, "watch", false);
// returns true

getOptionEnum()

function getOptionEnum<T>(
   options, 
   key, 
   allowedValues, 
   defaultValue): T;

Returns a string option constrained to a known set of allowed values.

Missing, blank, and unsupported values fall back to the provided default.

Type Parameters

T

T extends string

Parameters

options

Plugin options record.

Record\<string, string> | undefined

key

string

Option name to read.

allowedValues

readonly T[]

Supported string values for the option.

defaultValue

T

Value returned when the option is missing or invalid.

Returns

T

A valid enum-like string from allowedValues.

Example

getOptionEnum({ format: "esm" }, "format", ["cjs", "esm"] as const, "cjs");
// returns "esm"

getOptionNumber()

function getOptionNumber(
   options, 
   key, 
   defaultValue): number;

Returns a numeric option or the provided fallback when parsing fails.

Empty, invalid, and non-finite values fall back to the default.

Parameters

options

Plugin options record.

Record\<string, string> | undefined

key

string

Option name to read.

defaultValue

number

Value returned when the option is missing or invalid.

Returns

number

The parsed finite number, or the default when parsing fails.

Example

getOptionNumber({ retries: "3" }, "retries", 0);
// returns 3

getOptionString()

function getOptionString(
   options, 
   key, 
   defaultValue): string;

Returns a string option or the provided fallback when the key is missing.

Unlike the numeric and enum helpers, this function preserves the raw option value exactly as provided, including empty strings.

Parameters

options

Plugin options record.

Record\<string, string> | undefined

key

string

Option name to read.

defaultValue

string

Value returned when the option is missing.

Returns

string

The raw option string, or the default when the key is missing.

Example

getOptionString({ prefix: "Api" }, "prefix", "Model");
// returns "Api"