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

string-converters

v1.0.1

Published

A utility library for converting data types and working with arrays. Provides converters for boolean, number, and string values, along with array conversion functions and support for values with predefined options.

Downloads

47

Readme

string-converters

A lightweight and versatile utility for converting various data types. It provides a set of converters that can be used to parse and stringify different values.

Installation

using npm:

npm install --save string-converters

or yarn:

yarn add string-converters

API

Converter

type Converter<T> = {
  /**
   * Serializes the specified value into a string.
   *
   * @param value - The value to be serialized.
   * @returns The serialized value as a string.
   */
  stringify(value: T): string;
  /**
   * Parses the specified string and returns the deserialized value.
   *
   * @param value - The string to be parsed.
   * @returns The deserialized value.
   */
  parse(value: string): T;
};

booleanConverter

const booleanConverter: Converter<boolean>;

The booleanConverter is a utility that provides parsing and stringifying functionality for boolean values.

import booleanConverter from "string-converters/booleanConverter";

const value = "false";
const parsedValue = booleanConverter.parse(value);
console.log(parsedValue); // Output: false (boolean)

const booleanValue = true;
const stringValue = booleanConverter.stringify(booleanValue);
console.log(stringValue); // Output: "true" (string)

numberConverter

const numberConverter: Converter<number>;

The numberConverter is a utility that provides parsing and stringifying functionality for number values.

import numberConverter from "string-converters/numberConverter";

const value = "3.14";
const parsedValue = numberConverter.parse(value);
console.log(parsedValue); // Output: 3.14 (number)

const numberValue = 42;
const stringValue = numberConverter.stringify(numberValue);
console.log(stringValue); // Output: "42" (string)

// Throws an error because 'NaN' is not a number
numberConverter.parse("NaN");

stringConverter

const stringConverter: Converter<string>;

The stringConverter is a utility that provides a passthrough functionality for string values. It doesn't perform any conversion and simply returns the input value as-is.

import stringConverter from "string-converters/stringConverter";

const value = "Hello, World!";
const parsedValue = stringConverter.parse(value);
console.log(parsedValue); // Output: "Hello, World!" (string)

const stringValue = "Hello, World!";
const result = stringConverter.stringify(stringValue);
console.log(result); // Output: "Hello, World!" (string)

createArrayConverter

const createArrayConverter: <T>(
  itemConverter: Converter<T>,
  separator?: string
) => Converter<T[]>;

The createArrayConverter is a utility function that creates a converter for arrays. It provides parsing and stringifying functionality for array values, using a specified item converter and separator.

import createArrayConverter from "string-converters/createArrayConverter";
import stringConverter from "string-converters/stringConverter";

const itemConverter = stringConverter; // The converter for array items
const separator = ";"; // Custom separator

const arrayConverter = createArrayConverter(itemConverter, separator);

const value = "apple;banana;cherry";
const parsedValue = arrayConverter.parse(value);
console.log(parsedValue); // Output: ['apple', 'banana', 'cherry'] (array)

const arrayValue = ["apple", "banana", "cherry"];
const stringValue = arrayConverter.stringify(arrayValue);
console.log(stringValue); // Output: "apple;banana;cherry" (string)

createOneOfConverter

type AnyArray<T> = Array<T> | ReadonlyArray<T>;
type ValueOf<T> = T extends AnyArray<infer K>
  ? K
  : T extends {}
  ? T[keyof T]
  : never;
type Generalize<T> = T extends string ? string : T extends number ? number : T;

const createOneOfConverter: <const T extends {} | []>(
  generalConverter: Converter<Generalize<ValueOf<T>>>,
  possibleValues: T
) => Converter<ValueOf<T>>;

The createOneOfConverter is a utility function that creates a converter for values that can only be one of the specified options. It provides parsing and stringifying functionality for such values, using a general converter and a set of possible values.

import createOneOfConverter from "string-converters/createOneOfConverter";
import stringConverter from "string-converters/stringConverter";

const generalConverter = stringConverter; // The general converter for the value type
const possibleValues = ["option1", "option2", "option3"];
const anotherPossibleValues = {
  OPTION_1: "option1",
  OPTION_2: "option2",
  OPTION_1: "option3",
};

const oneOfConverter = createOneOfConverter(generalConverter, possibleValues);

const value = "option2";
const parsedValue = oneOfConverter.parse(value);
console.log(parsedValue); // Output: 'option2'

const optionValue = "option1";
const stringValue = oneOfConverter.stringify(optionValue);
console.log(stringValue); // Output: 'option1'

// Throws an error because 'option4' does not exist in possibleValues
oneOfConverter.parse("option4");

License

MIT © Krombik