npm package: @plumtreesystems/utils
Description: Utils for Plum Tree System app's
Author: Sarunas
Latest version: 1.3.3

# Plum tree systems utils

## Instalation
### Add module
```
    yarn add @plumtreesystems/utils
```
### Add types definition to `tsconfig.json` file if necessary
``` 
    "typeRoots": [
      "./node_modules/@plumtreesystems/utils/dist/esm/@types"
    ],
```

### For Jest to understand modules add to `jest.config.js` file
```
    transformIgnorePatterns: ['/node_modules/(?!@plumtreesystems)'],
```

## Utils
### Browser detector
Initiated `BrowserDetector` class.
```
import { BrowserDetector } from '@plumtreesystems/utils';
```

### Currency prefix
`CurrencyManager` uninitiated class.

```
import { CurrencyManager, CurrencyConfigType } from '@plumtreesystems/utils';

new CurrencyPrefix(config);
```
#### Params:
- `config: CurrencyConfigType[]`

```
CurrencyConfigType {
    prefix: string;
    prefixInputRegex: RegExp|null;
    countries: string[];
    currencies: string[];
    default?: boolean;
}
```

#### Types:
```
CurrencyOptionsType {
    format: {
        withCents: boolean; // default: true
    }
}
```

#### Functions:

``` typescript
// get config by country
getConfigByCountry(country: string): CurrencyConfigType

// get config by currency
getConfigByCurrency(currency: string): CurrencyConfigType

// get currency prefix by country
resolvePrefixByCountry(country: string): string

// get currency prefix by currency
resolvePrefixByCurrency(currency: string): string

// format provided sum to display string
formatThousands(sum: string|number, options?: Partial<CurrencyOptionsType>): string

// format sum for currency display by supplied config.
formatSum(config: CurrencyConfigType, sum: string|number, options?: Partial<CurrencyOptionsType>): string

// get formatted sum by provided country
formatSumByCountry(country: string, sum: number|string, options?: Partial<CurrencyOptionsType>): string

// get formatted sum by provided currency
formatSumByCurrency(currency: string, sum: number|string, options?: Partial<CurrencyOptionsType>): string
```

### Date manager
`DateManager` uninitiated class.

```
import { DateManager } from '@plumtreesystems/utils';

new DateManager(dateFormat, timeFormat);
```

#### Params:
- `dateFormat: string = 'YYYY-MM-DD'` 
- `timeFormat: string = 'HH:mm'`

#### Functions:

``` typescript
// normalize date string to contain T separator between date and time
formatDate(val: string)

// get time zone RegExp e.g. +02:00
getRegExp(): RegExp

// get short time zone RegExp e.g. +0200
getShortRegExp(): RegExp

// get time RegExp
getTimeRegExp(): RegExp

// get default date format
getDateFormat(): string

// get default date with time format
getDayTimeFormat(): string

// get default date with time and time zone format
getDayTimeFormatUtc(): string

// get uk date format
getDateFormatUK(): string

// get uk date with time format
getDayTimeFormatUK(): string

// get uk date with time and time zone format
getDayTimeFormatUtcUK(): string

// returns current date with time
getCurrentDateTime(options: Partial<DateManagerOptionsType> = {}): string

options = {
    returnFormat: string // response format.
}

// returns current date
getCurrentDate(options: Partial<DateManagerOptionsType> = {}): string

options = {
    returnFormat: string // response format.
}

// returns date with time.
getDateTime(date: any, options: Partial<DateManagerOptionsType> = {}): string

options = {
    inputFormat: OptionType|undefined // provided date parameter format.
    returnFormat: string // response format.
}

// returns current date with time and time zone
getCurrentDateTimeUtc(): string

// returns date and time with one day offset to the future
getDefaultToDateTime(): string

// returns current date and time with offset from parameters
getCurrentDateWithOffset(offset: number, options: Partial<DateManagerOptionsType> = {}): string

options = {
    type // type of offset value. Day, week, etc...
    returnFormat: string // response format.
}

// returns date and time with offset from parameters
getDateWithOffset(offset: number, date: any, options: Partial<DateManagerOptionsType> = {}): string

options = {
    type // type of offset value. Day, week, etc...
    returnFormat: string // response format.
    inputFormat: string // input format
}

// execute time zone regexp on value and return RegExp result
getTimeZoneRegex(val: string): RegExpExecArray | null

// returns date and time without time zone
getDateTimeWithoutTimezone(val: string): string

// returns first day of the month
getFirstDayOfMonth(options: Partial<DateManagerOptionsType> = {}): string

options = {
    date: any, // starting date to determine first day of the month. Default value is current date
    offset: number, // offset in months
    returnFormat: string // response format. Default format is dateFormat
    inputFormat: string // input format. Default format is dateFormat
}

// returns last day of the month
getLastDayOfMonth(options: Partial<DateManagerOptionsType> = {}): string

options = {
    date: any, // starting date to determine last day of the month. Default value is current date
    offset: number, // offset in months
    returnFormat: string // response format
    inputFormat: string // input format
}

// check if date matches iso standard
isIsoDate(str: string): boolean

// returns date in unix time format
getUnixTime(date: string, options: Partial<DateManagerOptionsType> = {}): number

options = {
    inputFormat: string // input format
}

// execute time regexp on value and return RegExp result
getTime(val: string): RegExpExecArray | null

// returns difference between two dates.
getDifference(from: string, to: string, format?: QUnitType | OpUnitType, options: Partial<DateManagerOptionsType> = {}): number

options = {
    inputFormat: string // input format
}

// check if provided date option is before before option
isBefore(date, before, options: Partial<DateManagerOptionsType> = {}): boolean

options = {
    inputFormat: string // input format
}

// check if provided date option is after after option
isAfter(date, after, options: Partial<DateManagerOptionsType> = {}): boolean

options = {
    inputFormat: string // input format
}

// returns week of the year from provided date
getWeekOfYear(date: string, options: Partial<DateManagerOptionsType> = {}): number

options = {
    inputFormat: string // input format
}

// returns week day from provided date
getWeekDay(date: string, options: Partial<DateManagerOptionsType> = {}): number

options = {
    firstDayMonday: boolean, // define if week starts from monday. Default value is true
    inputFormat: string // input format
}

// returns number of days in month from provided date
getDaysInMonth(date: string, options: Partial<DateManagerOptionsType> = {}): number

options = {
    inputFormat: string // input format
}

// check if both provided dates ar the same day
isSameDay(first: string, second: string, options: Partial<DateManagerOptionsType> = {}): boolean

options = {
    inputFormat: string // input format
}

// return utc offset
getUTCOffset(date: string, options: Partial<DateManagerOptionsType> = {}): number

options = {
    inputFormat: string // input format
}

// summer time utc offset
stdTimezoneOffset(): number

// is winter time observed
isDstObserved(date: string): boolean

// checks if date value is valid
isValid(date, format = this.dateFormat, strict = true): boolean

// returns displayable time string from provided now date to date x. e.g. 5 minutes ago
from(now, options: Partial<DateManagerOptionsType> = {})

options = {
    withoutSuffix, // remove suffix from result e.g 5 minutes / 5 minutes ago. Default is false
    to, // end point of interval from now date. Default is current date
    inputFormat: string // input format
}

// returns displayable time string from now to provided date . e.g. 5 minutes ago
fromNow(options: Partial<DateManagerOptionsType> = {})

options = {
    withoutSuffix, // remove suffix from result e.g 5 minutes / 5 minutes ago. Default is false
    to, // date for measuring interval from now. Default is current date
    inputFormat: string // input format
}

// returns displayable time string to provided date from date x. e.g. 5 minutes ago
to(to, options: Partial<DateManagerOptionsType> = {})

options = {
    withoutSuffix, // remove suffix from result e.g 5 minutes / 5 minutes ago. Default is false
    from, // starting point for measuring interval to x date. Default is current date
}

// returns displayable time string from provided date to now. e.g. 5 minutes ago
toNow(options: Partial<DateManagerOptionsType> = {})

options = {
    withoutSuffix, // remove suffix from result e.g 5 minutes / 5 minutes ago. Default is false
    from, // starting point for measuring interval to now. Default is current date
}

toLocalTimezone(date: string = this.getCurrentDateTime(), options: Partial<DateManagerOptionsType> = {})

options = {
    inputFormat: string // input format. Default format is dayTimeFormat
    returnFormat: string // response format. Default format is dayTimeFormatUtc
}
```

### File downloader

Function for file downloading

```
import { fileDownload } from '@plumtreesystems/utils';
```

### File helper

`FilesHelper` initiated class.

```
import { FilesHelper, AssetCategoryType, AssetStatsType, AssetType } from '@plumtreesystems/utils';
```

### Functionality manager

`FunctionalityManager` uninitiated class.

```
import { FunctionalityManager } from '@plumtreesystems/utils';

new FunctionalityManager(versionObject, dateManager);
```

Params:
- `versionObject: object`
- `dateManager: DateManager`

versionObject - Object. Example: `{featureName: {type: boolean, value: true}}`.

supported version types: `boolean`, `dateFrom`, `dateTo`

### Hydra

Uninitiated `Hydra` pagination object and types.

```
import { Hydra, HydraObjectType, HydraPaginationType, HydraViewObjectType, PageFilterType } from '@plumtreesystems/utils';

new Hydra(hydraObject);
```
Params:
- `hydraObject: HydraObjectType`


### Image tools

`ImageTools` initiated class.

```
import { ImageTools } from '@plumtreesystems/utils';
```

### Impersonator

`Impersonator` initiated class. Handle impersonation token actions with local storage.

```
import { Impersonator } from '@plumtreesystems/utils';
```
### Local storage manager

`LocalStorageManager` uninitiated class.

```
import { LocalStorageManager } from '@plumtreesystems/utils';

new LocalStorageManager(storeVersion, excludedModules);
```

Params:
- `storeVersion: string`
- `excludedModules: string[]`

### Mocked date manager

`MockedDateManager` uninitiated class.

```
import { MockedDateManager } from '@plumtreesystems/utils';

new MockedDateManager(currentDate);
```

Params:
- `currentDate: string = '2020-08-08 08:00'`

### Object processor

`ObjectProcessor` initiated class.

```
import { ObjectProcessor } from '@plumtreesystems/utils';
```

### Pagination helper

`PaginationHelper` initiated class.

```
import { PaginationHelper } from '@plumtreesystems/utils';
```

### Request cache

`RequestCache` initiated class and types.

```
import { RequestCache, RequestCacheFormatType, SetCachedRequestParametersType } from '@plumtreesystems/utils';
```

### Response error processor

`ResponseErrorsProcessor` uninitiated class, `errorsFormatter` function and error types.

```
import { ResponseErrorsProcessor, errorsFormatter, ErrorType, ErrorsObjectType } from '@plumtreesystems/utils';

new ResponseErrorsProcessor(registerError)
```

Params:
- `registerError: (e: any) => any`

### Scroll to top

`scrollToTop` function.

```
import { scrollToTop } from '@plumtreesystems/utils';
```

### Types overwrite

`OverwriteType` type to overwrite existing types.

```
import { OverwriteType } from '@plumtreesystems/utils';
```

### Url tools

`UrlTools` initiated class.

```
import { UrlTools } from '@plumtreesystems/utils/src/url-tools';
```

### Vuex module decorators

`CustomAction` function witch overrides default action parameters.

```
import { CustomAction } from '@plumtreesystems/utils';
```