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

@frankhoodbs/cast-prop-to-type

v1.3.11

Published

Simple utility to convert string props to correct type

Downloads

147

Readme

Cast Prop to Type

This utility provides a straightforward mechanism to cast various types of JavaScript properties to desired data types, such as number, array, boolean, and object. In the event of a casting error or invalid input, a custom ValidationError is thrown to provide a meaningful error message.

Version License

Usage

import { castPropToType } from "@frankhoodbs/cast-prop-to-type";

const numValue = castPropToType("42", "number");  // Returns 42 as a number

API

castPropToType(value, type)

Cast the given value to the specified type.

Parameters:

  • value (any): The value to be casted.
  • type ("number" | "array" | "boolean" | "object"): The type to which the value should be casted.

Returns:

The casted value. Throws ValidationError if casting is not possible.

ValidationError

Custom error that is thrown when there's a validation error during the casting process. It extends the native JavaScript Error class.

Examples

// Casting a string to a number
const num = castPropToType("42", "number"); // 42

// Casting a string to an array
const arr = castPropToType('["a", "b", "c"]', "array"); // ["a", "b", "c"]

// Casting a string to a boolean
const boolTrue = castPropToType("true", "boolean");  // true

// Casting a string to an object
const obj = castPropToType('{"key": "value"}', "object");  // { key: "value" }

Error Handling

If the utility encounters an invalid value for the desired casting type or if the type itself is invalid, a ValidationError will be thrown:

try {
  const num = castPropToType("foo", "number");
} catch (error) {
  if (error instanceof ValidationError) {
    console.error(error.message);
  }
}