Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skipVoid to maps and combines #302

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/and/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { combine, Store } from 'effector';

export function and(...stores: Array<Store<any>>): Store<boolean> {
return combine(stores, (values) => {
for (const value of values) {
if (!value) {
return false;
return combine(
stores,
(values) => {
for (const value of values) {
if (!value) {
return false;
}
}
}
return true;
}) as Store<boolean>;
return true;
},
{ skipVoid: false },
) as Store<boolean>;
}
2 changes: 1 addition & 1 deletion src/combine-events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function combineEvents<P>({

sample({
source: eventsTrriggered,
filter: $counter.map((value) => value === 0),
filter: $counter.map((value) => value === 0, { skipVoid: false }),
target: target as UnitTargetable<any>,
});
});
Expand Down
14 changes: 12 additions & 2 deletions src/condition/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { createEvent, Effect, Event, sample, is, Store, Unit, UnitTargetable, split } from 'effector';
import {
createEvent,
Effect,
Event,
sample,
is,
Store,
Unit,
UnitTargetable,
split,
} from 'effector';

type NoInfer<T> = T & { [K in keyof T]: T[K] };
type EventAsReturnType<Payload> = any extends Payload ? Event<Payload> : never;
Expand Down Expand Up @@ -120,7 +130,7 @@ function inverse<A extends boolean, T>(
fnOrUnit: Store<boolean> | ((payload: T) => boolean),
): Store<boolean> | ((payload: T) => boolean) {
if (is.unit(fnOrUnit)) {
return fnOrUnit.map((value) => !value);
return fnOrUnit.map((value) => !value, { skipVoid: false });
}
return (value) => !fnOrUnit(value);
}
1 change: 1 addition & 0 deletions src/either/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function either<Then, Other>(
then as Store<Then>,
other as Store<Other>,
(filter, then, other) => (filter ? then : other),
{ skipVoid: false },
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/empty/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Store } from 'effector';

export function empty<A>(source: Store<A | null | undefined>): Store<boolean> {
return source.map((value) => value == null);
return source.map((value) => value == null, { skipVoid: false });
}
4 changes: 3 additions & 1 deletion src/equals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ export function equals<A, B>(
? B
: { error: 'argument b should extends a' },
): Store<boolean> {
return combine(a as Store<A>, b as Store<A>, (a, b) => a === b);
return combine(a as Store<A>, b as Store<A>, (a, b) => a === b, {
skipVoid: false,
});
}
8 changes: 6 additions & 2 deletions src/every/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export function every<T>(
if (isFunction(predicate)) {
checker = predicate;
} else if (is.store(predicate)) {
checker = predicate.map((value) => (required: T) => value === required);
checker = predicate.map((value) => (required: T) => value === required, {
skipVoid: false,
});
} else {
checker = (value: T) => value === predicate;
}
Expand All @@ -60,7 +62,9 @@ export function every<T>(
// Combine pass simple values as is
const $checker = checker as Store<(value: T) => boolean>;

return combine($checker, $values, (checker, values) => values.every(checker));
return combine($checker, $values, (checker, values) => values.every(checker), {
skipVoid: false,
});
}

function isFunction<T>(value: unknown): value is (value: T) => boolean {
Expand Down
23 changes: 13 additions & 10 deletions src/format/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ export function format<Values extends StoreOrValue<any>[]>(
strings: TemplateStringsArray,
...stores: [...Values]
): Store<string> {
return combine(stores, (stores) =>
strings.reduce(
(acc, value, index) =>
acc.concat(
isLastElement(strings, index)
? value
: `${value}${toString(stores[index])}`,
),
'',
),
return combine(
stores,
(stores) =>
strings.reduce(
(acc, value, index) =>
acc.concat(
isLastElement(strings, index)
? value
: `${value}${toString(stores[index])}`,
),
'',
),
{ skipVoid: false },
);
}

Expand Down
1 change: 1 addition & 0 deletions src/in-flight/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ export function inFlight({
return combine(
effects!.map((fx) => fx.inFlight),
(inFlights) => inFlights.reduce((all, current) => all + current, 0),
{ skipVoid: false },
);
}
12 changes: 2 additions & 10 deletions src/interval/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import {
Event,
Store,
createEvent,
createStore,
sample,
attach,
is,
} from 'effector';
import { Event, Store, createEvent, createStore, sample, attach, is } from 'effector';

export function interval<S extends unknown, F extends unknown>(config: {
timeout: number | Store<number>;
Expand Down Expand Up @@ -42,7 +34,7 @@ export function interval<S extends unknown, F extends unknown>({
const $isRunning = createStore(false);
const $timeout = toStoreNumber(timeout);

const $notRunning = $isRunning.map((running) => !running);
const $notRunning = $isRunning.map((running) => !running, { skipVoid: false });

const saveTimeout = createEvent<{
timeoutId: NodeJS.Timeout;
Expand Down
2 changes: 1 addition & 1 deletion src/not/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Store } from 'effector';

export function not<T extends unknown>(source: Store<T>): Store<boolean> {
return source.map((value) => !value);
return source.map((value) => !value, { skipVoid: false });
}
18 changes: 11 additions & 7 deletions src/or/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { combine, Store } from 'effector';

export function or(...stores: Array<Store<any>>): Store<boolean> {
return combine(stores, (values) => {
for (const value of values) {
if (value) {
return true;
return combine(
stores,
(values) => {
for (const value of values) {
if (value) {
return true;
}
}
}
return false;
}) as Store<boolean>;
return false;
},
{ skipVoid: false },
) as Store<boolean>;
}
1 change: 1 addition & 0 deletions src/pending/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ export function pending({
return combine(
effects.map((fx) => fx.pending),
strategy,
{ skipVoid: false },
);
}
11 changes: 7 additions & 4 deletions src/reshape/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ export function reshape<Type, Shape extends Record<string, unknown>>({
}

const fn = shape[key];
result[key] = source.map((state) => {
const result = fn(state);
return result === undefined ? null : result;
});
result[key] = source.map(
(state) => {
const result = fn(state);
return result === undefined ? null : result;
},
{ skipVoid: false },
);
}

return result as any;
Expand Down
8 changes: 6 additions & 2 deletions src/some/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export function some<T>(
if (isFunction(predicate)) {
checker = predicate;
} else if (is.store(predicate)) {
checker = predicate.map((value) => (required: T) => value === required);
checker = predicate.map((value) => (required: T) => value === required, {
skipVoid: false,
});
} else {
checker = (value: T) => value === predicate;
}
Expand All @@ -57,7 +59,9 @@ export function some<T>(
// Combine pass simple values as is
const $checker = checker as Store<(value: T) => boolean>;

return combine($checker, $values, (checker, values) => values.some(checker));
return combine($checker, $values, (checker, values) => values.some(checker), {
skipVoid: false,
});
}

function isFunction<T>(value: unknown): value is (value: T) => boolean {
Expand Down
Loading