repos / starfx

supercharged async flow control library.
git clone https://github.com/neurosnap/starfx.git

starfx / store / slice
Vlad · 29 Jul 23

str.ts

 1import type { AnyState } from "../../types.ts";
 2
 3import type { BaseSchema } from "../types.ts";
 4
 5export interface StrOutput<S extends AnyState = AnyState>
 6  extends BaseSchema<string> {
 7  schema: "str";
 8  initialState: string;
 9  set: (v: string) => (s: S) => void;
10  reset: () => (s: S) => void;
11  select: (s: S) => string;
12}
13
14export function createStr<S extends AnyState = AnyState>(
15  { name, initialState = "" }: { name: keyof S; initialState?: string },
16): StrOutput<S> {
17  return {
18    schema: "str",
19    name: name as string,
20    initialState,
21    set: (value) => (state) => {
22      // deno-lint-ignore no-explicit-any
23      (state as any)[name] = value;
24    },
25    reset: () => (state) => {
26      // deno-lint-ignore no-explicit-any
27      (state as any)[name] = initialState;
28    },
29    select: (state) => {
30      // deno-lint-ignore no-explicit-any
31      return (state as any)[name];
32    },
33  };
34}
35
36export function str(initialState?: string) {
37  return (name: string) => createStr<AnyState>({ name, initialState });
38}