Skip to content

Commit

Permalink
Introduce empty operator (#220)
Browse files Browse the repository at this point in the history
* feat(empty): add operator

* Update src/empty/readme.md

Co-authored-by: Sergey Sova <[email protected]>

* Update src/empty/index.ts

Co-authored-by: Sergey Sova <[email protected]>

* Update src/empty/index.ts

* chore(empty): fixes after review

Co-authored-by: Sergey Sova <[email protected]>
  • Loading branch information
igorkamyshev and sergeysova authored Aug 12, 2022
1 parent 856de14 commit e2a4ed6
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/babel-plugin-factories.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"patronum/delay",
"patronum/either",
"patronum/equals",
"patronum/empty",
"patronum/every",
"patronum/format",
"patronum/in-flight",
Expand Down
28 changes: 28 additions & 0 deletions src/empty/empty.fork.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { allSettled, createEvent, createStore, fork } from 'effector';
import { empty } from './index';

test('boolean', async () => {
const makeFalse = createEvent();
const $a = createStore<boolean | null>(null).on(makeFalse, (_) => false);
const $result = empty($a);

const scope = fork();

expect(scope.getState($result)).toBe(true);

await allSettled(makeFalse, { scope });
expect(scope.getState($result)).toBe(false);
});

test('numbers', async () => {
const increment = createEvent();
const $a = createStore<number | null>(null).on(increment, (a) => (a ?? -1) + 1);
const $result = empty($a);

const scope = fork();

expect(scope.getState($result)).toBe(true);

await allSettled(increment, { scope });
expect(scope.getState($result)).toBe(false);
});
24 changes: 24 additions & 0 deletions src/empty/empty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createEvent, createStore } from 'effector';
import { empty } from './index';

test('boolean', () => {
const makeFalse = createEvent();
const $a = createStore<boolean | null>(null).on(makeFalse, (_) => false);
const $result = empty($a);

expect($result.getState()).toBe(true);

makeFalse();
expect($result.getState()).toBe(false);
});

test('numbers', () => {
const increment = createEvent();
const $a = createStore<number | null>(null).on(increment, (a) => (a ?? -1) + 1);
const $result = empty($a);

expect($result.getState()).toBe(true);

increment();
expect($result.getState()).toBe(false);
});
5 changes: 5 additions & 0 deletions src/empty/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Store } from 'effector';

export function empty<A>(a: Store<A | null>): Store<boolean> {
return a.map((value) => value === null);
}
3 changes: 3 additions & 0 deletions src/empty/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# empty (experimental)

> Since 1.10.0

0 comments on commit e2a4ed6

Please sign in to comment.