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 🙏

© 2024 – Pkg Stats / Ryan Hefner

rescript-rtype

v1.0.2

Published

A super simple JSON decoder for Rescript

Downloads

4

Readme

Rtype

Rtype is a super-simple way of decoding unknown json.

Install


npm i rescript-rtype

Then add rescript-rtype as a dependency to bsconfig.json:


"bs-dependencies": [
  "rescript-rtype"
]

How it works

Let's create a value that the type system is unaware of...


%%raw(`const unknown = { greeting: "hello world" }`)
@val external unknown: 'a = "unknown"

Using Rtype (short for for Runtime Type), we can match an unknown value against a given type.

We start by creating the type that we are looking for. We'll call this the decoder-type.


type decoder = {
  greeting: string
}

We can now create the actual decoder using Rtype.
Note how similar the decoder is to the actual decoder-type above.
Also the decoder has the same type as the decoder-type.

open RescriptRtype.Rtype // Opening Rtype gives access to the match function, as well as common decoders

let decoder: decoder = {
  greeting: string
}

Simply pass the unknown value into match(decoder) and switch on the outcome.
If the value matches the decoder, the decoder-type type is inferred.


unknown
  -> match(decoder)
  -> outcome => switch outcome {
    | Some(value) => Js.log(value.greeting)
    | None => Js.log(":(")
    }

Built in decoders

We can build decoders with

  • string, int, float, bool, as well as
  • array(...) where ... can be any other decoder
  • dict(...) where ... can be any other decoder
  • as well as...
  • gt, gte, lt, lte => eg. gt(10.0) will match if it gets a float greater than 10
  • And Tuple and Record decoders can be built from all of the above.

Eg. Matching a dictionary


type compileTimeCat = { name: string }
type compileTimeDictionary = Js.Dict.t<compileTimeCat>

let runTimeTypeDictionary: compileTimeDictionary = dict({ name: string })

// create a value unknown. The type system is unaware of the actual type.
%%raw(`const unknown2 = { charlie: { name: "charlie" }}`)
@val external unknown2: 'a = "unknown2"

unknown2 -> match(runTimeTypeDictionary) -> v => switch v {
| Some(thing) => switch Js.Dict.get(thing, "charlie") {
  | Some(cat) => Js.log(cat.name)
  | None => Js.log(":(")
  }
| None => Js.log(">:(")
}

Eg. Matching a tuple


type myTuple = (string, int, bool)

let myTuple: myTuple = (string, int, bool)

%%raw(`const unknown3 = ["hi", 7, true]`)
@val external unknown3: 'a = "unknown3"

unknown3
  -> match(myTuple)
  -> outcome => switch outcome {
    | Some(str, _, _) => Js.log(str ++ " there") // hi there
    | None => Js.log(":(")
    }

Matching on literals


"hello world"
  -> match("hello world")
  -> outcome => switch outcome {
    | Some(greeting) => Js.log(greeting) // hello world
    | None => Js.log(":(")
    }