> For the complete documentation index, see [llms.txt](https://text.angular-package.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://text.angular-package.dev/tag-variable/variable/instance-methods.md).

# Instance methods

## Public

### `Variable.prototype.getValue()`

Gets the value of the variable by returning the private [`#value`](/tag-variable/variable/instance-properties.md#value)  property of a specified [`Variable`](/tag-variable/variable.md) object.

{% code title="variable.class.ts" %}

```typescript
public getValue(): Value | undefined {
  return this.#value;
}
```

{% endcode %}

#### Returns

The **return value** is the value of a generic type variable [`Value`](/tag-variable/variable/generic-type-variables.md#attribute-name-1) if set, otherwise `undefined`.

#### Example usage

```typescript
// Example usage.
import { Variable } from '@angular-package/text';

// Returns Variable {'{fix}'} of Variable<"fix", string>.
const fix = new Variable('fix');

// Returns undefined of string | undefined.
fix.getValue();

// Returns Variable {'{fix}'} of Variable<"fix", "Open the link https://duckduckgo.com/">.
const fixed = new Variable('fix', 'Open the link https://duckduckgo.com/');

// Returns Open the link https://duckduckgo.com/ of "Open the link https://duckduckgo.com/" | undefined.
fixed.getValue();
```

### `Variable.prototype.replaceVariable()`

Replaces variable in the format `{variable name}` with the value of a specified [`Variable`](/tag-variable/variable.md) object or with a provided `replaceValue`, in a given `text`.

{% hint style="info" %}
If `replaceValue` is not provided, ​private [`#value`](/tag-variable/variable/instance-properties.md#value) property is used, and if the [`#value`](/tag-variable/variable/instance-properties.md#value) is `undefined` an empty `string` is used.
{% endhint %}

{% code title="variable.class.ts" %}

```typescript
public replaceVariable<ReplaceValue extends Value>(
  text: string,
  replaceValue?: ReplaceValue
): string {
  return this.replaceTag(text, replaceValue || this.#value || '');
}
```

{% endcode %}

| Generic type variables                                                                                                                                                                                                                                                                                                                                     |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><strong><code>ReplaceValue extends Value</code></strong><br>​A generic type variable <code>ReplaceValue</code> constrained by a generic type variable <a href="/pages/uX6MIyaORkdFJdrWMOS9#attribute-name-1"><code>Value</code></a>, by default of the value captured from the provided <code>replaceValue</code> indicates replacement value type.</p> |

#### Parameters

<table><thead><tr><th width="173.44477578337126">Name: type</th><th>Description</th></tr></thead><tbody><tr><td><code>text: string</code></td><td>The text of a <code>string</code> type in which replace the variable with the value from the private <a href="/pages/GgriRAcA7pu9C1HL0HBn#value"><code>#value</code></a> property or a given <code>replaceValue</code>.</td></tr><tr><td><code>replaceValue?: ReplaceValue</code></td><td>Optional value of a generic type variable <code>ReplaceValue</code> to replace the variable in the form <code>{variable name}</code> in a given <code>text</code>.</td></tr></tbody></table>

#### Returns

The **return value** is the text of a `string` type with a replaced variable by value.

#### Example usage

```typescript
// Example usage.
import { Variable } from '@angular-package/text';

// Returns Variable {'{fix}'} of Variable<"fix", string>.
const fix = new Variable('fix');

// Returns 'There is a' of string.
fix.replaceVariable(`There is a ${fix}`);

// Returns 'There is a real fix for an error.' of string.
fix.replaceVariable(`There is a ${fix}`, 'real fix for an error.');

// Returns Variable {'{fix}'} of Variable<"fix", "Open the link https://duckduckgo.com/">.
const fixed = new Variable('fix', 'Open the link https://duckduckgo.com/');

// Returns 'There is a Open the link https://duckduckgo.com/' of string.
fixed.replaceVariable(`There is a ${fixed}`);
```

### `Variable.prototype.toArray()`

Returns converted variable to a read-only `array` where the first element is the name, and the second is the value if set, otherwise `undefined`.

{% code title="variable.class.ts" %}

```typescript
public toArray(): readonly [Name, Value | undefined] {
  return [this.name, this.#value];
}
```

{% endcode %}

#### Returns

The **return value** is a read-only `array` of the variable name and value.

#### Example usage

```typescript
// Example usage.
import { Variable } from '@angular-package/text';

// Returns Variable {'{fix}'} of Variable<"fix", string>.
const fix = new Variable('fix');

// Returns ['fix', undefined] of readonly ["fix", string | undefined].
fix.toArray();

// Returns Variable {'{fix}'} of Variable<"fix", "Open the link https://duckduckgo.com/">.
const fixed = new Variable('fix', 'Open the link https://duckduckgo.com/');

// Returns ['fix', 'Open the link https://duckduckgo.com/'] of readonly ["fix", "Open the link https://duckduckgo.com/" | undefined]
fixed.toArray();
```

### `Variable.prototype.toObject()`

Returns converted variable to a read-only `object` where the key is the variable name.

{% code title="variable.class.ts" %}

```typescript
public toObject(): Readonly<{ [K in Name]: Value | undefined }> {
  return Object.freeze({ [this.name]: this.#value }) as {
    [K in Name]: Value | undefined;
  };
}
```

{% endcode %}

#### Returns

The **return value** is a read-only `object` where the property name is the variable name, and the property value is equal to the variable value.

#### Example usage

```typescript
// Example usage.
import { Variable } from '@angular-package/text';

// Returns Variable {'{fix}'} of Variable<"fix", string>.
const fix = new Variable('fix');

// Returns {fix: undefined} of Readonly<{fix: string | undefined;}>.
fix.toObject();

// Returns Variable {'{fix}'} of Variable<"fix", "Open the link https://duckduckgo.com/">.
const fixed = new Variable('fix', 'Open the link https://duckduckgo.com/');

// Returns {fix: 'Open the link https://duckduckgo.com/'} of Readonly<{fix: "Open the link https://duckduckgo.com/" | undefined;}>.
fixed.toObject();
```

### `Variable.prototype.toString()`

Returns the variable in form `{name}`, a primitive value of the specified [`Variable`](/tag-variable/variable.md) object.

{% code title="variable.class.ts" %}

```typescript
public toString(): `{${Name}}` {
  return this.valueOf();
}
```

{% endcode %}

#### Returns

The **return value** is the variable of a generic type variable [`Name`](/tag-variable/variable/generic-type-variables.md#attribute-name) on the template `{${Name}}`.

#### Example usage

```typescript
// Example usage.
import { Variable } from '@angular-package/text';

// Returns Variable {'{fix}'} of Variable<"fix", string>.
const fix = new Variable('fix');

// Returns {fix} of "{fix}".
fix.toString();

// Returns Variable {'{fix}'} of Variable<"fix", "Open the link https://duckduckgo.com/">.
const fixed = new Variable('fix', 'Open the link https://duckduckgo.com/');

// Returns {fix} of "{fix}".
fixed.toString();
```

### `Variable.prototype.valueOf()`

Returns the variable in form `{name}`, a primitive value of the specified [`Variable`](/tag-variable/variable.md) object.

{% code title="variable.class.ts" %}

```typescript
public valueOf(): `{${Name}}` {
  return super.valueOf();
}
```

{% endcode %}

#### Returns

The **return value** is the variable of a generic type variable [`Name`](/tag-variable/variable/generic-type-variables.md#attribute-name) on the template `{${Name}}`.

#### Example usage

```typescript
// Example usage.
import { Variable } from '@angular-package/text';

// Returns Variable {'{fix}'} of Variable<"fix", string>.
const fix = new Variable('fix');

// Returns {fix} of "{fix}".
fix.valueOf();

// Returns Variable {'{fix}'} of Variable<"fix", "Open the link https://duckduckgo.com/">.
const fixed = new Variable('fix', 'Open the link https://duckduckgo.com/');

// Returns {fix} of "{fix}".
fixed.valueOf();
```
