Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@stagas/getter

Run callbacks with the property key.

@stagas/getter exports a tiny Getter helper that calls a callback every time a property is read. It is useful for APIs where the property name is the input: CSS variables, translation keys, route builders, token maps, and other compact lookup-style interfaces.

Installation

npm install @stagas/getter

Usage

import { Getter } from '@stagas/getter'

const css = Getter(key => `var(--${key})`)

css.color
// "var(--color)"

css.background
// "var(--background)"

You can also pass an existing target object when the proxy needs to preserve a specific prototype or object identity.

import { Getter } from '@stagas/getter'

const target = {}
const paths = Getter(key => `/api/${key}`, target)

paths.users
// "/api/users"

API

Getter<T>(cb, target?)

Creates a proxy that calls cb with the accessed property key and returns the callback result.

const value = Getter<T>(
  cb: (key: string) => T,
  target?: any,
)

Parameters

  • cb: Function called with the property key being accessed.
  • target: Optional proxy target. Defaults to an empty object.

Returns

A proxy typed as Record<string, T>.

License

MIT

About

Run callbacks with the property key.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages