# @wazapp/helpers

Helpers are utils that are meant to be used mostly in component templates.

## API

### each(collection, onItem, onEmpty)

`each` will make your iterations in template a breeze. It takes care of unique keys for each element, as well as simplify the syntax.

It takes 3 arguments:

* `collection` - an array of something
* `onItem(item, index)` - callback which will be used for each item in the collection
* `onEmpty` - `optional` - `ReactNode` that will be returned in case the collection is empty.&#x20;

#### Example

**Before**

```typescript
import Component from "@wazapp/component";
import { each } from '@wazapp/helpers'

export default class MyComponent extends Component {
  phrases = ['Maciej', 'was', 'here'];

  template({ name }) {
    return (
      <ul>
        {this.phrases.map((phrase) => (
          <li key={phrase}>
            {phrase}
          </li>
        ))}
      </ul>
    );
  }
}
```

**After**

```typescript
import Component from "@wazapp/component";
import { each } from '@wazapp/helpers'

export default class MyComponent extends Component {
  phrases = ['Maciej', 'was', 'here'];

  template({ name }) {
    return (
      <ul>
        {each(this.phrases, (phrase) => (
          <li>
            {phrase}
          </li>
        ))}
      </ul>
    );
  }
}
```

### when(condition, ifTrue, ifFalse)

`when` is simple `if` statement which does not require to return `null` as false case.

It takes 3 arguments:

* `condition`
* `ifTrue` - value when true
* `ifFalse` - value when false

#### Example

**Before**

```typescript
import Component from "@wazapp/component";
import { when } from '@wazapp/helpers'

export default class MyComponent extends Component {
  template({ name }) {
    return (
      <div>
        {name === 'Maciej' ? (
          <h1>Hi Maciej</h1>
        ) : null}
      </div>
    );
  }
}
```

**After**

```typescript
import Component from "@wazapp/component";
import { when } from '@wazapp/helpers'

export default class MyComponent extends Component {
  template({ name }) {
    return (
      <div>
        {when(name === 'Maciej', (
          <h1>Hi Maciej</h1>)
        )}
      </div>
    );
  }
}
```

### classNames()

Exposes: <https://github.com/JedWatson/classnames>

### yieldChildren(children, ...args)

`yieldChildren` allows you to return component's `children` or in case the `children` is a function, it will allow you to call it with custom arguments.

Arguments:

* `children`: `ReactNode | Function`
* `...args`: `any[]` - your custom arguments that will be used on `children` call (if `children` is a function)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://exelord.gitbook.io/wazapp/packages/helpers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
