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

@traversable/json

v0.0.52

Published

<br /> <h1 align="center">แฏ“๐˜๐—ฟ๐—ฎ๐˜ƒ๐—ฒ๐—ฟ๐˜€๐—ฎ๐—ฏ๐—น๐—ฒ/๐—ท๐˜€๐—ผ๐—ป</h1> <br />

Downloads

27,631

Readme

API

Types

Json

If no type parameter is provided, describes any JSON value.

Note that, like JSON values, the Json type is recursive. If you'd like to try out the non-recursive version, provide Json a type parameter.

If a type parameter is provided, e.g. Json<string[]>, the resulting type will be:

import type { Json } from '@traversable/json'

type MyNonRecursiveJsonType = Json<string[]>
//   ^? type MyNonRecursiveJsonType = 
//        | null 
//        | boolean 
//        | number 
//        | string 
//        | string[][] 
//        | { [x: string]: string[] }
//

Q: What's the point of the non-recursive type?

The non-recursive type comes in handy when you're using an abstraction like Json.fold, which lets you implement a recursive function, without performing any recursion.

This is the main feature of Json.fold (which is made possible by Json.map) is that it fully decouples "how to recurse" from "what to do at each step".

For more information, see the docs on Json.fold.

Terms (values)

Json.map

Like Array.prototype.map, Json.map takes a function that accepts any JSON input and returns an arbitrary value, and the JSON you'd like to map over.

If the JSON value is an array, Json.map will apply the function to every element of the array, preserving the structure of the input array.

If the JSON value is an object, Json.map will apply the function to every value of the object, preserving the index signature of the input object.

Note that the function will not have any effect if its input is a scalar value (null, boolean, number or string).

Note that the function will only be applied to a single level. If you need to apply a function recursively (to every level of the JSON value), use Json.fold.

Json.fold

Applies a non-recursive function to a JSON value, recursively.

If you only want to apply the function at the top-level, use Json.map.

Note that the function is applied from the bottom-up (that is, Json.fold is a as a post-order traversal / transformation).

Examples

Here's a custom JSON serializer:

import { Json } from '@traversable/json'
import { parseKey } from '@traversable/registry'

const serialize = Json.fold((x) => {
  switch (true) {
    case Json.isScalar(x): return typeof x === 'string' ? `"${x}"` : String(x)
    case Json.isArray(x): return '[' +  x.join(', ') + ']'
    case Json.isObject(x): return '{' + Object.entries(x).map(([k, v]) => `"${k}": ${v}`).join(', ') + '}'
    // or, if you only want keys to be surrounded by quotes if the key is not a valid identifier:
    case Json.isObject(x): return '{' + Object.entries(x).map(([k, v]) => `${parseKey(k)}: ${v}`).join(', ') + '}'
  }
})

Here's a custom deserializer:

import { Json } from '@traversable/json'
import { parseKey } from '@traversable/registry'

const isValidDateString = (u: unknown): u is string => 
  typeof u === 'string' && !Number.isNaN(new Date(u).getTime())

// coerce only date strings into Date objects, leaving everything else alone
const handleDateStrings = Json.fold((x) => isValidDateString(x) ? new Date(x) : x)

const deserialize = (u: string) => {
  try {
    return handleDateStrings(JSON.parse(u))
  } catch (e) {
    console.error(e)
  }
}
Things to keep in mind

As a user of Json.fold, the function you write is not recursive.

That's the point of using Json.fold: it lets you separate concerns. Implementing recursion by hand is nuanced and error prone. And when you get it working, it's expensive to maintain, since even a seemingly innocent change can result in a different performance profile.

The main feature of Json.fold (which is made possible by Json.map) is that it fully decouples "how to recurse" from "what to do at each step".

Json.fold also comes with a few nice-to-haves. For example, when it passes control back to you, the caller, it actually gives you the correct types.

Let's take a closer look at the JSON serializer example from above:

import { Json } from '@traversable/json'
import { parseKey } from '@traversable/registry'

const serializer = (x: Json<string>) => {
  //                   ^^ here I'm using the `Json` type to specify what my JSON will be in the end (a string):

  // Notice that the type of `x` is __not recursive__, and it's also __not `string`__: 
  console.log(x)
  //          ^? const x: null | boolean | number | string | string[] | { [x: string]: string }

  // Recall that recursion happens from the bottom up. If you think about it for a minute, this type makes a lot
  // of sense. In fact, that's the only type that makes sense here -- anything else is incorrect.

  switch (true) {
    case Json.isScalar(x): return typeof x === 'string' ? `"${x}"` : String(x)
    case Json.isArray(x): return '[' +  x.join(', ') + ']'
    //                                  ^ notice that the type of `x` here is `string[]` -- this is because 
    //                                    `Json.fold` has already applied your function to the "lower" levels
    //                                    of the tree, and we're on the way back "up"

    case Json.isObject(x): return '{' + Object.entries(x).map(([k, v]) => `"${k}": ${v}`).join(', ') + '}'
    //                                                 ^ here, the type of `x` is { [x: string]: string }
  }
}

FAQ

How does it work? Why does it work?

This library actually doesn't introduce anything new. All it does is apply a few ideas from category theory (a subject I find fascinating, but do not claim to be an expert in) and ports them to JavaScript.

In the Haskell community (where these ideas first appeared in the industry), they're called recursion schemes, and they've been around since circa 1991, when a famous paper called Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire was published.

Since then, dozens of recursion schemes have been discovered / invented. If you're curious, the one that is used by Json.fold is called a "catamorphism".

It is this library's explicit goal to showcase how useful recursion schemes are, and how you can use them today to write recursive programs that:

  1. are simple, almost trivial, to implement
  2. are easy to read and understand
  3. will, over time, give you a solid intuition for recursion in general