# Emil docxtemplater-util

Helper library to simplify interaction with docxtemplater.

This package provides a list of filters you can apply to your parser using the builder, so you do not need to copy paste all the formatters between repositories.

### How do I get set up?

#### Install the package:

`npm install @emilgroup/docxtemplater-util --save`

#### Setup configuration for dayjs:

```typescript
import { setupDayjs } from '@emilgroup/docxtemplater-util';

setupDayjs();
```

This one applies default options for dayjs.

You can also change the options for dayjs with this function call, you can pass the defaultTimezone or locale to use. Take a look at the following example:

```typescript
import { setupDayjs } from '@emilgroup/docxtemplater-util';

setupDayjs({
  defaultTimezone: 'Europe/Berlin',
  locales: ['de', 'fr', 'it'],
});
```

#### Then you can use the package in your project like so:

```typescript
import angularParser from 'docxtemplater/expressions';
import { FiltersBuilder } from '@emilgroup/docxtemplater-util';

FiltersBuilder.default(angularParser).applyFilters();
```

This code snippet applies all the pre-defined filters to your angularParser.

You also have ability to add new filters using the builder.

```typescript
import angularParser from 'docxtemplater/expressions';
import { FiltersBuilder } from '@emilgroup/docxtemplater-util';

const builder = new FiltersBuilder(angularParser);

builder.addFilter('foo', (str: string): string => {
  return str.concat('[foo]');
});
builder.addFilter('reverse', (str: string): string => {
  return str.split('').reduce((acc, char) => char + acc, '');
});

builder.applyFilters();
```

This one creates a new builder instance add adds custom filters to it

## How to maintain?

In case if you want to add a new filter just create new file with the following naming [filter-name]-filters.ts in the filters folder. If you update the exiting one, you will not need to do anything else, otherwise you will be need to update index.ts and filters-builder.ts to apply new filters.

## License

MIT
