or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

environment-utilities.mdimmutable-arrays.mdimmutable-objects.mdindex.mdmath-operations.mdreactive-utilities.mdtype-definitions.md
tile.json

tessl/npm-solid-primitives--utils

A bunch of reactive utility types and functions, for building primitives with Solid.js

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@solid-primitives/utils@6.3.x

To install, run

npx @tessl/cli install tessl/npm-solid-primitives--utils@6.3.0

index.mddocs/

@solid-primitives/utils

@solid-primitives/utils is a comprehensive TypeScript utility library designed specifically for building reactive primitives with Solid.js. It provides general-purpose reactive utilities for signal management, immutable data manipulation helpers, mathematical operations, and type definitions that maintain functional programming principles with non-mutating operations.

Package Information

  • Package Name: @solid-primitives/utils
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @solid-primitives/utils or pnpm add @solid-primitives/utils
  • Peer Dependencies: solid-js ^1.6.12

Core Imports

import { 
  access, asAccessor, createHydratableSignal, 
  isClient, isServer, noop, clamp 
} from "@solid-primitives/utils";

Immutable helpers:

import { 
  push, omit, pick, update, merge, add, subtract 
} from "@solid-primitives/utils/immutable";

For CommonJS:

const { access, isClient, createHydratableSignal } = require("@solid-primitives/utils");
const { push, omit, pick, update } = require("@solid-primitives/utils/immutable");

Basic Usage

import { createSignal } from "solid-js";
import { access, createHydratableSignal } from "@solid-primitives/utils";
import { push, update } from "@solid-primitives/utils/immutable";

// Using accessor utilities
const getValue = (maybeAccessor: string | (() => string)) => {
  return access(maybeAccessor); // Works with both values and functions
};

// Creating hydratable signals
const [serverState, setServerState] = createHydratableSignal(
  "initial server value",
  () => "updated client value"
);

// Using immutable helpers with signals
const [list, setList] = createSignal([1, 2, 3]);
setList(current => push(current, 4, 5)); // [1, 2, 3, 4, 5]

const [user, setUser] = createSignal({
  name: "John",
  address: { street: "Main St", number: 123 }
});
setUser(current => update(current, "address", "number", 456));

Architecture

@solid-primitives/utils is organized into several key functional areas:

  • Reactive Utilities: Functions for working with Solid.js signals, accessors, and reactivity patterns
  • Environment Detection: Client/server and development/production environment helpers
  • Type System: Comprehensive TypeScript types and utility types for enhanced type safety
  • Immutable Operations: Functional programming helpers for non-mutating data manipulation
  • Object/Array Utilities: General-purpose utilities for working with JavaScript data structures
  • Mathematical Operations: Basic math functions designed for immutable workflows

Capabilities

Reactive Utilities

Core utilities for working with Solid.js reactivity system, including accessor manipulation, signal creation, and lifecycle management.

function access<T extends MaybeAccessor<any>>(v: T): MaybeAccessorValue<T>;
function asAccessor<A extends MaybeAccessor<unknown>>(v: A): Accessor<MaybeAccessorValue<A>>;
function createHydratableSignal<T>(
  serverValue: T,
  update: () => T,
  options?: SignalOptions<T>
): ReturnType<typeof createSignal<T>>;
function defer<S, Next extends Prev, Prev = Next>(
  deps: AccessorArray<S> | Accessor<S>,
  fn: (input: S, prevInput: S, prev: undefined | NoInfer<Prev>) => Next,
  initialValue?: Next
): EffectFunction<undefined | NoInfer<Next>>;

type MaybeAccessor<T> = T | Accessor<T>;
type MaybeAccessorValue<T extends MaybeAccessor<any>> = T extends () => any ? ReturnType<T> : T;

Reactive Utilities

Immutable Array Operations

Non-mutating array manipulation functions that return new arrays without modifying the original data structures.

function push<T>(list: readonly T[], ...items: T[]): T[];
function filter<T>(list: readonly T[], predicate: Predicate<T>): T[] & { removed: number };
function sort<T>(list: T[], compareFn?: (a: T, b: T) => number): T[];
function splice<T>(list: readonly T[], start: number, deleteCount?: number, ...items: T[]): T[];
function concat<A extends any[], V extends ItemsOf<A>>(...a: A): Array<V extends any[] ? ItemsOf<V> : V>;

type Predicate<T> = (item: T, index: number, array: readonly T[]) => boolean;

Immutable Arrays

Immutable Object Operations

Non-mutating object manipulation functions for property selection, modification, merging, and copying operations.

function omit<O extends object, K extends keyof O>(object: O, ...keys: K[]): Omit<O, K>;
function pick<O extends object, K extends keyof O>(object: O, ...keys: K[]): Pick<O, K>;
function merge<A extends object, B extends object>(a: A, b: B): Modify<A, B>;
function get<O extends object, K extends keyof O>(obj: O, key: K): O[K];
function update<O extends object, K extends keyof O, V>(
  object: O,
  key: K,
  setter: UpdateSetter<O, K, V>
): ModifyValue<O, K, V>;
function shallowArrayCopy<T>(array: readonly T[]): T[];
function shallowObjectCopy<T extends object>(object: T): T;
function withArrayCopy<T>(array: readonly T[], mutator: (copy: T[]) => void): T[];
function withObjectCopy<T extends object>(object: T, mutator: (copy: T) => void): T;

type UpdateSetter<O, K extends keyof O, V> = V | ((prev: O[K]) => V);
type Modify<T, R> = Omit<T, keyof R> & R;
type ModifyValue<O, K extends keyof O, V> = Omit<O, K> & { [key in K]: V };

Immutable Objects

Mathematical Operations

Immutable mathematical functions for basic arithmetic operations, designed to work well with functional programming patterns.

function add(...a: number[]): number;
function add(...a: string[]): string;
function substract(a: number, ...b: number[]): number;
function multiply(a: number, ...b: number[]): number;
function divide(a: number, ...b: number[]): number;
function power(a: number, ...b: number[]): number;
function clamp(n: number, min: number, max: number): number;

Mathematical Operations

Environment and Utility Functions

Environment detection, utility functions, and general-purpose helpers for common programming tasks.

const isServer: boolean;
const isClient: boolean;
const isDev: boolean;
const isProd: boolean;

function noop(...a: any[]): void;
function isObject(value: any): value is AnyObject;
function arrayEquals(a: readonly unknown[], b: readonly unknown[]): boolean;
function chain<Args extends [] | any[]>(callbacks: {
  [Symbol.iterator](): IterableIterator<((...args: Args) => any) | undefined>;
}): (...args: Args) => void;

type AnyObject = Record<PropertyKey, any>;

Environment and Utilities

Type Definitions

Comprehensive TypeScript type utilities and definitions for enhanced type safety and developer experience.

type Many<T> = T | T[];
type ItemsOf<T> = T extends (infer E)[] ? E : never;
type Directive<P = true> = (el: Element, props: Accessor<P>) => void;
type Truthy<T> = Exclude<T, FalsyValue>;
type Falsy<T> = Extract<T, FalsyValue>;
type Position = { x: number; y: number; };
type Size = { width: number; height: number; };
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;

type FalsyValue = false | 0 | "" | null | undefined;
type PrimitiveValue = PropertyKey | boolean | bigint | null | undefined;

Type Definitions