npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@kvkit/codecs

v0.2.1

Published

Pure codecs for converting objects to/from string maps

Downloads

10

Readme

@kvkit/codecs

Core codec implementations for type-safe data serialization and transformation.

Installation

npm install @kvkit/codecs

Overview

@kvkit/codecs provides a set of composable codecs for converting JavaScript objects to/from string maps. These are particularly useful for URL parameters, localStorage, and other key-value stores.

Basic Usage

import { stringCodec, numberCodec, jsonCodec, flatCodec, prefixCodec } from '@kvkit/codecs';

// JSON codec with default key
const userCodec = jsonCodec<{ name: string; age: number }>();

const user = { name: 'John', age: 30 };
const encoded = userCodec.encode(user);
// { "data": "{\"name\":\"John\",\"age\":30}" }

const decoded = userCodec.decode(encoded);
// { name: 'John', age: 30 }

Available Codecs

Simple Value Codecs

  • stringCodec(key?) - String values
  • numberCodec(key?) - Numeric values
  • booleanCodec(key?) - Boolean values
  • dateCodec(key?) - Date objects
  • stringArrayCodec(key?) - String arrays

All simple value codecs accept an optional key parameter (default: 'value').

const nameCodec = stringCodec('name');
const ageCodec = numberCodec('age');
const activeCodec = booleanCodec('active');

const encoded = nameCodec.encode('John'); // { name: 'John' }
const name = nameCodec.decode({ name: 'John' }); // 'John'

Object Codecs

jsonCodec<T>(key?)

Serializes entire objects as JSON in a single parameter.

const userCodec = jsonCodec<{ name: string; role: string }>('user');
const encoded = userCodec.encode({ name: 'Alice', role: 'admin' });
// { user: '{"name":"Alice","role":"admin"}' }

Use cases:

  • Complex nested objects
  • When you want compact URL representation
  • Objects with dynamic properties

flatCodec<T>()

Serializes each object property as a separate key-value pair.

const filterCodec = flatCodec<{ query: string; category: string; active: boolean }>();
const encoded = filterCodec.encode({ query: 'search', category: 'tech', active: true });
// { query: 'search', category: 'tech', active: 'true' }

Use cases:

  • Form data
  • Search/filter parameters
  • When individual values need to be easily readable/editable

prefixCodec<T>(namespace, separator?)

Adds a namespace prefix to all keys to avoid conflicts.

const userCodec = prefixCodec<{ name: string; role: string }>('user');
const encoded = userCodec.encode({ name: 'Alice', role: 'admin' });
// { 'user.name': 'Alice', 'user.role': 'admin' }

// Custom separator
const configCodec = prefixCodec<{ theme: string }>('app', '_');
// Results in keys like 'app_theme'

Use cases:

  • Modular applications with multiple state sources
  • Avoiding parameter name conflicts
  • Organizing related parameters

Codec Interface

All codecs implement the Codec<T> interface:

interface Codec<T> {
  encode(value: T): Record<string, string>;
  decode(data: Record<string, string>): T;
}

Custom Codecs

Create your own codecs by implementing the interface:

import type { Codec } from '@kvkit/codecs';

const customCodec: Codec<{ lat: number; lng: number; zoom: number }> = {
  encode: (location) => ({
    location: `${location.lat},${location.lng},${location.zoom}`
  }),
  
  decode: (data) => {
    const [lat, lng, zoom] = (data.location || '0,0,1').split(',').map(Number);
    return { lat, lng, zoom };
  }
};

Generic Value Codec

For simple custom serialization, use the valueCodec helper:

import { valueCodec } from '@kvkit/codecs';

const dateCodec = valueCodec<Date>(
  (date) => date.toISOString(),
  (str) => new Date(str || Date.now()),
  'timestamp'
);

License

MIT