some
import { some } from 'patronum';// orimport { some } from 'patronum/some';
some({ predicate: Function, stores })
Motivation
Method calculates boolean value if at least one state of the store satisfies the condition in predicate
.
It is useful to check that user filled at least a single field.
Formulae
$result = some({ predicate: (value) => true, stores });
- read it as: has some predicate at least one store
$result
will betrue
if each at leastpredicate
on each store value fromvalues
returnstrue
, otherwise it will befalse
Arguments
predicate
((value: T) => boolean)
— Function to check store valuestores
(Array<Store<T>>)
— List of stores
Return
$result
(Store<boolean>)
—true
if at least one store corresponds topredicate
Example
const $width = createStore(440);const $height = createStore(820);
const $tooBig = some({ predicate: (size) => size > 800, stores: [$width, $height],});
console.assert(true === $tooBig.getState());
some({ predicate: value, stores })
Motivation
This overload compares each store to specific value in predicate
.
It is useful when you write combine
with ||
very often, for example to create an invalid form flag.
Formulae
$result = some({ predicate: value, stores });
$result
will betrue
if at least one value instores
equalsvalue
, otherwise it will befalse
Arguments
predicate
(T)
— Data to compare stores values withstores
(Array<Store<T>>)
— List of stores to compare withvalue
- type of
predicate
andstores
should should be the same
Return
$result
(Store<boolean>)
—true
if at least one store containsvalue
Example
const $isPasswordCorrect = createStore(true);const $isEmailCorrect = createStore(true);
const $isFormFailed = some({ predicate: false, stores: [$isPasswordCorrect, $isEmailCorrect],});
console.assert(false === $isFormFailed.getState());
some({ predicate: Store, stores })
Motivation
This overload compares each store to specific value in store predicate
.
It is useful when you write combine
with ||
very often, for example to create an invalid form flag.
Formulae
$result = some({ predicate: $value, stores });
$result
will betrue
if at least one value instores
equals value in$value
, otherwise it will befalse
Arguments
predicate
(Store<T>)
— Store contains value to compare values fromstores
withstores
(Array<Store<T>>)
— List of stores to compare withvalue
- type of
value
andstores
should be the same
Return
$result
(Store<boolean>)
—true
if at least one store containsvalue
Example
const $allowToCompare = createStore(true);
const $isPasswordCorrect = createStore(true);const $isEmailCorrect = createStore(true);
const $isFormFailed = some({ predicate: $allowToCompare, stores: [$isPasswordCorrect, $isEmailCorrect],});
console.assert(false === $isFormFailed.getState());
Shorthands
$result = some(stores, value);$result = some(stores, (value) => false);$result = some(stores, $predicate);
Shorthand have the same rules as the main overrides, just it uses positional arguments instead of object-form.
Arguments
stores
(Array<Store<T>>)
— List of stores to compare with predicate in the second argumentpredicate
(Store<T> | (value: T) => boolean | T)
— Predicate to compare with