From e2a4ed6a05ee3279fe2916b64aa278026dfbaefa Mon Sep 17 00:00:00 2001 From: Igor Kamyshev Date: Fri, 12 Aug 2022 13:21:17 +0700 Subject: [PATCH] Introduce `empty` operator (#220) * feat(empty): add operator * Update src/empty/readme.md Co-authored-by: Sergey Sova <5620073+sergeysova@users.noreply.github.com> * Update src/empty/index.ts Co-authored-by: Sergey Sova <5620073+sergeysova@users.noreply.github.com> * Update src/empty/index.ts * chore(empty): fixes after review Co-authored-by: Sergey Sova <5620073+sergeysova@users.noreply.github.com> --- src/babel-plugin-factories.json | 1 + src/empty/empty.fork.test.ts | 28 ++++++++++++++++++++++++++++ src/empty/empty.test.ts | 24 ++++++++++++++++++++++++ src/empty/index.ts | 5 +++++ src/empty/readme.md | 3 +++ 5 files changed, 61 insertions(+) create mode 100644 src/empty/empty.fork.test.ts create mode 100644 src/empty/empty.test.ts create mode 100644 src/empty/index.ts create mode 100644 src/empty/readme.md diff --git a/src/babel-plugin-factories.json b/src/babel-plugin-factories.json index c633189f..e0b5aa2c 100644 --- a/src/babel-plugin-factories.json +++ b/src/babel-plugin-factories.json @@ -8,6 +8,7 @@ "patronum/delay", "patronum/either", "patronum/equals", + "patronum/empty", "patronum/every", "patronum/format", "patronum/in-flight", diff --git a/src/empty/empty.fork.test.ts b/src/empty/empty.fork.test.ts new file mode 100644 index 00000000..95dbab25 --- /dev/null +++ b/src/empty/empty.fork.test.ts @@ -0,0 +1,28 @@ +import { allSettled, createEvent, createStore, fork } from 'effector'; +import { empty } from './index'; + +test('boolean', async () => { + const makeFalse = createEvent(); + const $a = createStore(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(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); +}); diff --git a/src/empty/empty.test.ts b/src/empty/empty.test.ts new file mode 100644 index 00000000..f5ee62d7 --- /dev/null +++ b/src/empty/empty.test.ts @@ -0,0 +1,24 @@ +import { createEvent, createStore } from 'effector'; +import { empty } from './index'; + +test('boolean', () => { + const makeFalse = createEvent(); + const $a = createStore(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(null).on(increment, (a) => (a ?? -1) + 1); + const $result = empty($a); + + expect($result.getState()).toBe(true); + + increment(); + expect($result.getState()).toBe(false); +}); diff --git a/src/empty/index.ts b/src/empty/index.ts new file mode 100644 index 00000000..d7aa6ae7 --- /dev/null +++ b/src/empty/index.ts @@ -0,0 +1,5 @@ +import { Store } from 'effector'; + +export function empty(a: Store): Store { + return a.map((value) => value === null); +} diff --git a/src/empty/readme.md b/src/empty/readme.md new file mode 100644 index 00000000..0ef6f467 --- /dev/null +++ b/src/empty/readme.md @@ -0,0 +1,3 @@ +# empty (experimental) + +> Since 1.10.0