Skip to content

Commit

Permalink
Create examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysova authored Apr 23, 2020
1 parent 35cb0be commit 55d27ff
Showing 1 changed file with 127 additions and 0 deletions.
127 changes: 127 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,129 @@
# patronum

☄️ Effector utility library delivering modularity and convenience

## Debounce

```ts
import { createEvent } from 'effector';
import { createDebounce } from 'patronum/debounce';

// You should call this event
const trigger = createEvent<number>();

const target = createDebounce(trigger, 200);

target.watch((payload) => console.info('debounced', payload));

trigger(1);
trigger(2);
trigger(3);
trigger(4);
// after 200ms
// => debounced 4
```

## Throttle

```ts
import { createEvent } from 'effector';
import { createThrottle } from 'patronum/throttle';

// You should call this event
const trigger = createEvent<number>();

const target = createThrottle(trigger, 200);

target.watch((payload) => console.info('throttled', payload));

trigger(1);
trigger(2);
trigger(3);
trigger(4);
// 200ms after trigger(1)
// => throttled 1
```

## Reshape

```ts
import { createStore } from 'effector';
import { reshape } from 'patronum/reshape';

const $original = createStore<string>('Hello world');

const parts = reshape($original, {
length: (string) => string.length,
first: (string) => string.split(' ')[0] || '',
second: (string) => string.split(' ')[1] || '',
});

parts.length.watch(console.info); // 11
parts.first.watch(console.log); // "Hello"
parts.second.watch(console.log); // "Second"
```

## Spread

```ts
import { createEvent, createStore } from 'effector';
import { spread } from 'patronum/spread';

const trigger = createEvent<{ first: string; second: string }>();

const $first = createStore('');
const $second = createStore('');

spread(trigger, {
first: $first,
second: $second,
});

trigger({ first: 'Hello', second: 'World' });

$first.getState(); // "Hello"
$second.getState(); // "World"
```

## SplitMap

```ts
import { createEvent } from 'effector';
import { splitMap } from 'patronum/splitmap';

const nameReceived = createEvent<string>();

const received = splitMap(nameReceived, {
firstName: (string) => string.split(' ')[0], // string | undefined
lastName: (string) => string.split(' ')[1], // string | undefined
});

received.firstName.watch((first) => console.info('firstname received', first));
received.lastName.watch((last) => console.info('lastname received', last));

nameReceived('Sergey');
// firstname received "Sergey"

nameReceived('Sergey Sova');
// firstname received "Sergey"
// lastname received "Sova"
```

## Condition

```ts
import { createEvent } from 'effector';
import { condition } from 'patronum/condition';

const trigger = createEvent<string>();

const longString = createEvent<string>();
const shortString = createEvent<string>();

condition({
source: trigger,
if: (string) => string.length > 8,
then: longString,
else: shortString,
});
```

0 comments on commit 55d27ff

Please sign in to comment.