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 🙏

© 2025 – Pkg Stats / Ryan Hefner

transform-ts

v0.3.0

Published

`$ npm install transform-ts` or `$ yarn add transform-ts`

Readme

Transform.ts npm version codecov CircleCI

Powerful object serialization/deserialization library.

  • Easy to use
  • Flexible
  • Written in TypeScript

How to use

$ npm install transform-ts or $ yarn add transform-ts

Usage

Transformer

Transformer<A,B> has A to B transformation. It also has B to A (inverse) transformation.

Transformation

import $, { Transformer, ValidationResult } from 'transform-ts'

declare const fab: Transformer<A, B>

declare const validA: A
declare const invalidA: A

const result1: ValidationResult<B> = fab.transform(validA) // $.isOk(result1) === true
const result2: ValidationResult<B> = fab.transform(invalidA) // $.isError(result2) === true

const b1: B = fab.transformOrThrow(validA)
const b2: B = fab.transformOrThrow(invalidA) // Throw Error!

Composition

Transformer<A,B> is composable to Tranformer<B,C>.

declare const fab: Transformer<A, B>
declare const fbc: Transformer<B, C>

const fac: Transformer<A, C> = fab.compose(fbc)

Builtin Transformers

  • any: Transformer<unknown, unknown>(validate that input value is not undefined or null)
  • number: Transformer<unknown, number>
  • string: Transformer<unknown, string>
  • boolean: Tranformer<unknown, boolean>
const num = $.number.transformOrThrow(123 as unknown)

Transformer Combinators

nullable

converts Transformer<A, B> to Transformer<A | null, B | null>

declare const fab: Transformer<A, B>

const f: Transformer<A | null, B | null> = $.nullable(fab)

optional

converts Transformer<A, B> to Transformer<A | undefined, B | undefined>

declare const fab: Transformer<A, B>

const f: Transformer<A | undefined, B | undefined> = $.optional(fab)

array

converts Transformer<unknown, A> to Transformer<unknown, A[]>

const f: Transformer<unknown, number[]> = $.array($.number)

tuple

const f: Transformer<unknown, [number, string]> = $.tuple($.number, $.string)

obj

const f: Transformer<unknown, { a: number; b: string }> = $.obj({
  a: $.number,
  b: $.string,
})

You can construct complex Transformer with simple transformers and combinators. Try it out!

Example

user.json

{
  "id": 9,
  "name": "a",
  "screenName": "ika",
  "application": {
    "id": 13,
    "name": "V",
    "isAutomated": false
  }
}
import $, { Transformer } from 'transform-ts'

const userTransformer: Transformer<
  unknown,
  {
    id: number
    name: string
    screenName: string
    application: {
      id: number
      name: string
      isAutomated: boolean | undefined
    }
  }
> = $.obj({
  id: $.number,
  name: $.string,
  screenName: $.string,
  application: $.obj({
    id: $.number,
    name: $.string,
    isAutomated: $.optional($.boolean),
  }),
})

const user = userTransformer.transformOrThrow(require('./user.json'))

const userJson = JSON.stringify(userTransformer.invert().transformOrThrow(user))

Custom Transformer

You can write your own transformer.

/*
Transformer requires `A` to `B` and `B` to `A` transformation.
`A` to `B` transformation is a function `(a: A) => ValidationResult<B>`.
You can use `$.ok` and `$.error` to create `ValidationResult`.
Returning `$.ok(value)` represents that transformation is success with `value`.
Returning `$.error(error)` represents that error(s) has occured.
*/
import $, { Transformer, ok } from 'transform-ts'

const stringToDate = Transformer.from<string, Date>(str => ok(new Date(str)))

const date = stringToDate.transformOrThrow('2019-05-01T15:13:34.459Z')
const dateString = stringToDate.inverseTransformOrThrow(new Date())

Using custom transformers, you can transform unknown to any type you like.