repos / starfx

a micro-mvc framework for react apps
git clone https://github.com/neurosnap/starfx.git

starfx / src / store / slice
Eric Bower  ·  2025-06-06

str.ts

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