# Capture

Capture values while matching templates.

Using an instance of this class within a Matcher will capture the matching value.
The `as*()` APIs on the instance can be used to get the captured value.

Extends: Matcher › Capture

- Source: [`assertions/lib/capture.ts`](https://github.com/aws/aws-cdk/blob/v2.274.0/packages/aws-cdk-lib/assertions/lib/capture.ts#L12)
- Type last changed: v2.1.0 · 8 Dec 2021

## Example

```ts
// Given a template -
// {
//   "Resources": {
//     "MyBar": {
//       "Type": "Foo::Bar",
//       "Properties": {
//         "Fred": "Flob",
//       }
//     },
//     "MyBaz": {
//       "Type": "Foo::Bar",
//       "Properties": {
//         "Fred": "Quib",
//       }
//     }
//   }
// }

const fredCapture = new Capture();
template.hasResourceProperties('Foo::Bar', {
  Fred: fredCapture,
});

fredCapture.asString(); // returns "Flob"
fredCapture.next();     // returns true
fredCapture.asString(); // returns "Quib"
```

## Initializer

```ts
new Capture(pattern?: any)
```

## Properties

- `name`: string (required) — A name for the matcher.

  This is collected as part of the result and may be presented to the user.

## Methods

- `asArray(): any[]` — Retrieve the captured value as an array.

  An error is generated if no value is captured or if the value is not an array.

- `asBoolean(): boolean` — Retrieve the captured value as a boolean.

  An error is generated if no value is captured or if the value is not a boolean.

- `asNumber(): number` — Retrieve the captured value as a number.

  An error is generated if no value is captured or if the value is not a number.

- `asObject(): { [key: string]: any }` — Retrieve the captured value as a JSON object.

  An error is generated if no value is captured or if the value is not an object.

- `asString(): string` — Retrieve the captured value as a string.

  An error is generated if no value is captured or if the value is not a string.

- `isMatcher(x: any): boolean` (static) — Check whether the provided object is a subtype of the `IMatcher`.

- `next(): boolean` — When multiple results are captured, move the iterator to the next result.

  **Returns:** true if another capture is present, false otherwise

- `test(actual: any): MatchResult` — Test whether a target matches the provided pattern.

  Every Matcher must implement this method.
  This method will be invoked by the assertions framework. Do not call this method directly.
