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

castr

v0.2.1

Published

Cast value to any type (js)

Downloads

87

Readme

castr

Cast value to any type

import * as castr from 'castr'

castr.toNumber('1') // 1

View cheat sheet

String

Turn any value into a string (cheat sheet)

castr.toString('hello') // "hello"
function toString(value: any, options: Options = {}): string

interface Options {
  false?: string
  null?: string
  parse?: boolean
  true?: string
  undefined?: string
}

Cases

If you pass a number, it will be converted to a string:

castr.toString(-42.5) // "-42.5"

undefined or null are translated into empty strings:

castr.toString(undefined) // ""
castr.toString(null) // ""

You can overwrite this by passing a custom string:

castr.toString(undefined, { undefined: 'missing' }) // "missing"
castr.toString(null, { null: 'ignored' }) // "ignored"

Booleans are stringified like this:

castr.toString(true) // "true"
castr.toString(false) // "false"

You can also owerwrite this:

castr.toString(true, { true: 'yes' }) // "yes"
castr.toString(false, { false: 'no' }) // "no"

If you pass a plain object as a JSON string, it will be parsed:

castr.toString({ foo: 1 }) // "{\"foo\": 1}"

You can also overwrite this:

castr.toString({ foo: 1 }, { parse: false }) // "[object Object]"

In case you pass an instantiated class, the method toString will be called on it:

castr.toString(new Error('foo')) // "Error: foo"

Arrays are joined with a comma:

castr.toString([1, 2, 3]) // "1,2,3"
castr.toString([]) // ""

You can force the array to be stringified as JSON:

castr.toString([1, 2, 3], { parse: true }) // "[1,2,3]"
castr.toString([], { parse: true }) // "[]"

Number

Turn any value into a number (cheat sheet)

castr.toNumber(22) // 22
function toNumber(value: any, options: Options = {}): string

interface Options {
  Infinity?: number
  NaN?: number
  count?: boolean
}

Cases

In case the value can not be casted to a number, it will return 0 unless specified otherwise

castr.toNumber(null) // 0
castr.toNumber(null, { NaN: -1 }) // -1
castr.toNumber(Infinity, { Infinity: -1 }) // -1

If the value is a numeric string, it will be casted to a number

castr.toNumber('-1.5') // -1.5

Booleans value are represented as numbers:

castr.toNumber(true) // 1
castr.toNumber(false) // 0

Cast will return lenght if possible:

castr.toNumber('abc') // 3
castr.toNumber([1, 2, 3]) // 3
castr.toNumber({ foo: 1, bar: 2 }) // 2

Use count to overwite this

castr.toNumber('abc', { count: false }) // 0