Skip to content

Commit

Permalink
Rename previousValue to previous
Browse files Browse the repository at this point in the history
  • Loading branch information
zerobias committed Dec 15, 2023
1 parent 223ad4b commit 949a1fe
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/babel-plugin-factories.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"patronum/once",
"patronum/or",
"patronum/pending",
"patronum/previous-value",
"patronum/previous",
"patronum/reset",
"patronum/reshape",
"patronum/snapshot",
Expand Down Expand Up @@ -45,7 +45,7 @@
"once": "once",
"or": "or",
"pending": "pending",
"previousValue": "previous-value",
"previous": "previous",
"reset": "reset",
"reshape": "reshape",
"snapshot": "snapshot",
Expand Down
2 changes: 1 addition & 1 deletion src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ All methods split into categories.
- [snapshot](./snapshot/readme.md) — Create store value snapshot.
- [splitMap](./split-map/readme.md) — Split event to different events and map data.
- [spread](./spread/readme.md) — Send fields from object to same targets.
- [previousValue](./previous-value/readme.md) - Get previous value of store.
- [previous](./previous/readme.md) - Get previous value of store.

### Debug

Expand Down
8 changes: 4 additions & 4 deletions src/previous-value/index.ts → src/previous/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Node, Store, createStore, launch, step, is } from 'effector';

export function previousValue<State>(store: Store<State>): Store<State | null>;
export function previousValue<State, Init>(
export function previous<State>(store: Store<State>): Store<State | null>;
export function previous<State, Init>(
store: Store<State>,
initialValue: Init,
): Store<State | Init>;
export function previousValue<State, Init = null>(
export function previous<State, Init = null>(
...args: [store: Store<State>, defaultValue?: Init]
) {
const [store] = args;
const initialValue = (args.length < 2 ? null : args[1]) as Init | null;
if (!is.store(store)) {
throw Error('previousValue first argument should be a store');
throw Error('previous first argument should be a store');
}
const $prevValue = createStore<State | Init | null>(initialValue, {
serialize: 'ignore',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import {
sample,
} from 'effector';

import { previousValue } from './index';
import { previous } from './index';

it('has null when store is not changed', () => {
const scope = fork();
const $initalStore = createStore(10);
const $prevValue = previousValue($initalStore);
const $prevValue = previous($initalStore);

expect(scope.getState($prevValue)).toBe(null);
});

it('has initial value when defined', () => {
const scope = fork();
const $initalStore = createStore(10);
const $prevValue = previousValue($initalStore, 0);
const $prevValue = previous($initalStore, 0);

expect(scope.getState($prevValue)).toBe(0);
});
Expand All @@ -30,7 +30,7 @@ it('has first value on update', async () => {
const changeInitialStore = createEvent<number>();
const $initalStore = restore(changeInitialStore, 10);

const $prevValue = previousValue($initalStore);
const $prevValue = previous($initalStore);

await allSettled(changeInitialStore, { scope, params: 20 });

Expand All @@ -42,7 +42,7 @@ it('has previous value on multiple updates', async () => {
const changeInitialStore = createEvent<number>();
const $initalStore = restore(changeInitialStore, 10);

const $prevValue = previousValue($initalStore);
const $prevValue = previous($initalStore);

await allSettled(changeInitialStore, { scope, params: 20 });
await allSettled(changeInitialStore, { scope, params: 30 });
Expand All @@ -54,7 +54,7 @@ it('has previous value on multiple updates', async () => {
it('has first scope value after first update', async () => {
const inc = createEvent();
const $initalStore = createStore(0);
const $prevValue = previousValue($initalStore, -1);
const $prevValue = previous($initalStore, -1);
$initalStore.on(inc, (x) => x + 1);
const scope = fork({ values: [[$initalStore, 10]] });
await allSettled(inc, { scope });
Expand All @@ -64,7 +64,7 @@ it('has first scope value after first update', async () => {
test('undefined support', async () => {
const changeInitialStore = createEvent<string | void>();
const $initialStore = createStore<string | void>('a', { skipVoid: false });
const $prevValue = previousValue($initialStore);
const $prevValue = previous($initialStore);

sample({ clock: changeInitialStore, target: $initialStore });

Expand All @@ -78,7 +78,7 @@ test('undefined support', async () => {
test('undefined as defaultValue support', () => {
const changeInitialStore = createEvent<string | void>();
const $initialStore = createStore<string | void>('a', { skipVoid: false });
const $prevValue = previousValue($initialStore, undefined);
const $prevValue = previous($initialStore, undefined);

sample({ clock: changeInitialStore, target: $initialStore });

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { createEvent, createStore, restore, sample } from 'effector';

import { previousValue } from './index';
import { previous } from './index';

it('has null when store is not changed', () => {
const $initalStore = createStore(10);
const $prevValue = previousValue($initalStore);
const $prevValue = previous($initalStore);

expect($prevValue.getState()).toBe(null);
});

it('has initial value when defined', () => {
const $initalStore = createStore(10);
const $prevValue = previousValue($initalStore, 0);
const $prevValue = previous($initalStore, 0);

expect($prevValue.getState()).toBe(0);
});
Expand All @@ -20,7 +20,7 @@ it('has first value on update', () => {
const changeInitialStore = createEvent<number>();
const $initalStore = restore(changeInitialStore, 10);

const $prevValue = previousValue($initalStore);
const $prevValue = previous($initalStore);

changeInitialStore(20);

Expand All @@ -31,7 +31,7 @@ it('has previous value on multiple updates', () => {
const changeInitialStore = createEvent<number>();
const $initalStore = restore(changeInitialStore, 10);

const $prevValue = previousValue($initalStore);
const $prevValue = previous($initalStore);

changeInitialStore(20);
changeInitialStore(30);
Expand All @@ -43,7 +43,7 @@ it('has previous value on multiple updates', () => {
test('undefined support', () => {
const changeInitialStore = createEvent<string | void>();
const $initialStore = createStore<string | void>('a', { skipVoid: false });
const $prevValue = previousValue($initialStore);
const $prevValue = previous($initialStore);

sample({ clock: changeInitialStore, target: $initialStore });

Expand All @@ -56,7 +56,7 @@ test('undefined support', () => {
test('undefined as defaultValue support', () => {
const changeInitialStore = createEvent<string | void>();
const $initialStore = createStore<string | void>('a', { skipVoid: false });
const $prevValue = previousValue($initialStore, undefined);
const $prevValue = previous($initialStore, undefined);

sample({ clock: changeInitialStore, target: $initialStore });

Expand All @@ -66,8 +66,8 @@ test('undefined as defaultValue support', () => {
test('store validation', () => {
expect(() => {
// @ts-expect-error
previousValue(null);
previous(null);
}).toThrowErrorMatchingInlineSnapshot(
`"previousValue first argument should be a store"`,
`"previous first argument should be a store"`,
);
});
14 changes: 7 additions & 7 deletions src/previous-value/readme.md → src/previous/readme.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# previousValue
# previous

:::note since
patronum 2.1.0
:::

```ts
import { previousValue } from 'patronum';
import { previous } from 'patronum';
// or
import { previousValue } from 'patronum/previous-value';
import { previous } from 'patronum/previous-value';
```

### Motivation
Expand All @@ -17,8 +17,8 @@ The method allows to get previous value of given store. Usually need for analyti
### Formulae

```ts
$target = previousValue($source);
$target = previousValue($source, 'initial value');
$target = previous($source);
$target = previous($source, 'initial value');
```

### Arguments
Expand All @@ -36,11 +36,11 @@ Push analytics with route transition:

```ts
import { createStore, createEvent, createEffect, sample } from 'effector';
import { previousValue } from 'patronum';
import { previous } from 'patronum';

const openNewRoute = createEvent<string>();
const $currentRoute = createStore('main_page');
const $previousRoute = previousValue($currentRoute);
const $previousRoute = previous($currentRoute);

const sendRouteTransitionFx = createEffect(async ({ prevRoute, nextRoute }) => {
console.log(prevRoute, '->', newRoute)
Expand Down
12 changes: 6 additions & 6 deletions test-typings/previous-value.ts → test-typings/previous.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import { expectType } from 'tsd';
import { Store, createStore, createEvent } from 'effector';
import { previousValue } from '../dist/previous-value';
import { previous } from '../dist/previous';

{
const $foo = createStore('a');
const $fooPrev = previousValue($foo);
const $fooPrev = previous($foo);

expectType<Store<string | null>>($fooPrev);
}
{
const $foo = createStore('a');
const $fooPrev = previousValue($foo, 'b');
const $fooPrev = previous($foo, 'b');

expectType<Store<string>>($fooPrev);
}
{
const $foo = createStore('a');
const $fooPrev = previousValue($foo, 0);
const $fooPrev = previous($foo, 0);

expectType<Store<string | number>>($fooPrev);
}
{
const $foo = createStore('a');
const $fooPrev = previousValue($foo, undefined);
const $fooPrev = previous($foo, undefined);

expectType<Store<string | void>>($fooPrev);
}
{
const foo = createEvent();

// @ts-expect-error
previousValue(foo, 0);
previous(foo, 0);
}

0 comments on commit 949a1fe

Please sign in to comment.