# @theorchard/suite-utils

Common utilities for Orchard suite apps.

## Usage

### `createHref(route:string, routeArgs: {}): string`

Creates a url from a route. The route is defined using a pattern of mandatory and optional segments.

A mandatory segment is defined like: `:[name]`. e.g `:page`.
This requires a `page` property to be passed into the `routeArgs`. Empty or undefined segments will throw an exception.

An optional segment is defined using a question mark `?` at the end. e.g `:page?`.
A missing optional segment is removed from the final url.

Properties passed into the `routeArgs` that is not defined in the route pattern will be appended as query parameters.

#### Examples

```ts
let url = createHref('/:page/:arg?', { page: 'test' });
// url => '/test'

url = createHref('/:page/:arg?', { page: 'test', arg: 'value' });
// url => '/test/value'

url = createHref('/:page/:arg?', {
    page: 'test',
    something: 'else',
    other: 'stuff',
});
// url => '/test?something=else&other=stuff'

url = createHref('/:page/:view', { page: 'test', view: 'value' });
// url => '/test/view'

url = createHref('/:page/:arg?', { view: 'test' });
// throws exception
```

### `parseQueryParams(search:string, config: {}): {}`

Parses the given location search string using a configuration object.
The configuration needs to specify each parameter to be returned.

#### Examples

```ts
const params = parseQueryParams('?page=test&countries=NO,SE&flag=true&other=10&ignored=yes', {
    page: { type: 'string' },
    countries: { type: 'string[]' },
    flag: { type: 'boolean' },
    dimension: { type: 'string', default: 'all' },
    alias: { name: 'other', type: 'number' },
});

params ===
    {
        page: 'test',
        countries: ['NO', 'SE'],
        flag: true,
        dimension: 'all',
        alias: 10,
    };
```
