Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aikoven/typescript-immutable-utils

Type-safe immutability helpers for simple objects and arrays
https://github.com/aikoven/typescript-immutable-utils

Last synced: about 2 months ago
JSON representation

Type-safe immutability helpers for simple objects and arrays

Awesome Lists containing this project

README

        

# TypeScript Immutable Utils [![npm version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url]

Type-safe immutability helpers for simple objects and arrays.

## Installation

```
npm install --save typescript-immutable-utils
```

## API

### Arrays

* `setAt(items: T[], index: number, item: T): T[]`

Returns new array with replaced item at given index.

If item is equal to the old one, original array is returned.

* `insertAt(items: T[], index: number, item: T): T[]`

Returns new array with inserted item at given index.

If index is negative, counts from the end of the array.

* `removeAt(items: T[], index: number): T[]`

Returns new array with removed item at given index.

If index is out of bounds, original array is returned.

If index is negative, counts from the end of the array.

* `removeValue(items: T[], item: T): T[]`

Returns new array with given value removed.

If value is not present in the array, it is returned unchanged.

### Objects

* `update(target: T, values: Partial): T`

Returns new object with updated values.

If all values are the same, original object is returned.

### Maps (ES-2015)

* `mapValues(src: Map, map: (value: T, key: K) => R): Map`

Returns new Map with updated values

### Dicts

Dictionary type is a simple index signature:

```ts
type Dict = {[key: string]: V};
```

* `createDict(): Dict`

Creates empty dict.

* `copyDict(dict: Dict): Dict`

Copies given dict.

* `hasKey(dict: Dict, key: any): boolean`

Checks whether dict has given key.

* `mapValues(dict: Dict, map: (value: T) => R): Dict`

Creates new dict with same keys as given dict whose values are the result of
applying mapping function on given dict values.

If every returned value is the same as in original dict, original dict is
returned.

* `union(target: Dict, source: Dict): Dict`

Creates new dict with values from both given dicts.

* `setKey(dict: Dict, key: any, value: V): Dict`

Returns new dict with given key set to given value.

If the value is the same as in original dict, original dict is returned.

* `removeKey(dict: Dict, ...keys: any[]): Dict`

Returns new dict with given keys removed.

If none of the keys are present in original dict, original dict is returned.

* `fromKeys(keys: any[], values: V | ((key: any) => V)): Dict`

If `values` is not a function, creates new dict with given keys whose values are
all the same and equal to `values`.

If `values` is function, creates new dict with given keys whose values are the
result of applying this function to the key.

[npm-image]: https://badge.fury.io/js/typescript-immutable-utils.svg
[npm-url]: https://badge.fury.io/js/typescript-immutable-utils
[travis-image]: https://travis-ci.org/aikoven/typescript-immutable-utils.svg?branch=master
[travis-url]: https://travis-ci.org/aikoven/typescript-immutable-utils