# Helpers

> Functions exported alongside the components, for building tables and panels that match the chart.

The package exports the functions its own components use, so the schedule or side panel you build beside a chart can format and name things the same way.

```ts
import { assignFills, formatAmount, formatPercent, layerKey, layerTerms, towerTop } from '@vinsa/towers';
```

## Formatting

### `formatAmount(n)`

Shortens an amount: thousands as `K`, millions as `M`, billions as `B`. Amounts under a thousand stay exact.

```ts
formatAmount(2500000); // "2.5M"
formatAmount(750000); // "750K"
formatAmount(480); // "480"
```

### `formatPercent(n)`

```ts
formatPercent(62.5); // "62.5%"
```

### `layerTerms(layer)`

The terms of a layer as an underwriter would write them, or `null` for a layer type that has none. It accepts a layer from your request or a block from the response.

```ts
layerTerms({ layerType: 'XOL', lowerLimit: 2500000, upperLimit: 5000000, accumulateBasis: 'CLM' });
// "2.5M xs 2.5M per claim"
```

### Code names

`LAYER_TYPE_NAMES`, `BASIS_NAMES` and `PROTECTION_NAMES` map the API's codes to words:

```ts
LAYER_TYPE_NAMES.XOL; // "Excess of loss"
BASIS_NAMES.EVT; // "per event"
PROTECTION_NAMES.RET; // "Retention only"
```

## Working with towers

### `layerKey(layer)`

The key the components use for selection: the layer's `layerRef`, or its `layerName` when it has none. Use it to match the `key` from `onSelect` to a layer in your own data.

```ts
const layers = program.contracts.filter((c) => risk.contractRefs.includes(c.contractRef)).flatMap((c) => c.layers);
const layer = layers.find((l) => layerKey(l) === selectedKey);
```

### `isLayerCedant(block)` (0.3.1 and later)

Returns `true` for a cedant block that belongs to a Layer in the reinsurer viewpoint, including a Layer drawn entirely as cedant. It returns `false` for retention fillers.

```ts
import { isLayerCedant } from '@vinsa/towers';

const layerBlocks = band.verticalBlocks.filter((block) => !block.isRetention || isLayerCedant(block));
```

### `assignFills(towers)`

Returns a `Map` from each `colorKey` to a fill colour, honouring any `colorOverride`. Pass the result to several [`<TowerChart />`](https://towers.vinsa.dev/docs/react/tower-chart.md)s, or use it to colour rows of your own table to match.

```ts
const fills = assignFills(risk.towers);
fills.get(block.colorKey); // "#cfe0f5"
```

### `towerTop(tower)`

The highest limit drawn in a tower, or `0` when it holds only unlimited layers. Useful for the upper bound of a loss slider.

## Lower level

`towerShapes(tower)`, `layerCaptions(shapes)` and `blockLines(block)` are what `<TowerChart />` uses to turn bands and blocks into rectangles and labels. They are exported for anyone drawing towers on a different surface, such as canvas or a PDF.

In the [reinsurer viewpoint](https://towers.vinsa.dev/docs/api/viewpoints.md), `blockLines(block)` labels anonymous blocks Other reinsurers and includes the status for quoted or indication Lines.
