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

format-to-jsx

v2.0.4

Published

React (JS) string formatting

Downloads

8,039

Readme

npm npm bundle size Build Status codecov dependabot badge semantic-release Commitizen friendly

format-to-jsx

👉 Take note that this package is in still early stage of development, and there might be breaking changes introduced while we are finalizing on the API

String formatter with support of injecting JSX elements as parameters.

yarn add format-to-jsx

# or with npm

npm install format-to-jsx --save

Example

import { format } from 'format-to-jsx'

format('This is a string template with a {0} based placeholder', 'number')

// This is a string template with a number based placeholder

Supported template strings

format-to-jsx supports two ways of doing template strings:

Index based template placeholders

Those are template strings where placeholders are based on {X} format where X is a positive integer based index. Each placeholder will be replaced by a corresponding parameter value provided, respecting order of parameters:

format('Example {1} with different placeholders order {0}', 1, 2)

// Example 2 with different placeholders order 1

String based template placeholders

Those are template strings where placeholders are based on {some_string} format where some_string is a no-white space string. Each placeholder will be replaced by corresponding parameter passed via an object - object keys are used as lookup:

const params = { one: 1, two: 2 }

format('Example {two} with different placeholders order {one}', params)

// Example 2 with different placeholders order 1

JSX arguments

Values passed as arguments, either with index or string based placeholders can be of type string, number or React.ReactNode. That enables you to create complex HTML while not injecting and manipulating the DOM outside of React scope. Result of a formatting with JSX will be a fragment instead of string:

format('Example {1} with different placeholders order {0}', 1, <strong>2</strong>)

// <>Example <strong>2</strong> with different placeholders order 1<>

Template string with placeholders provided with {} braces. Index based placeholders starting with 0 OR string based placeholders are supported.

API

Params

template

| Type | Required | | -------- | -------- | | string | true |

Template string with placeholders provided with {} braces. Index based placeholders starting with 0 OR string based placeholders are supported.

...args[]

| Type | Required | | ----------------------------------------------------------------------------------------------------- | -------- | | (string \| number \| React.ReactNode)[] OR { [key: string]: string \| number \| React.ReactNode } | true |

When working with index based params / placeholders, each parameter will replace corresponding index placeholder. Values can be passed as primitives or React.ReactNode. When working with string based params / placeholders, object with matching keys should be used.

return
  • string - if only primitive params were used, returned value is a string.
  • React.Fragment - if any of parameters provided was a React.ReactNode, returned value will be a React Fragment.

Error and warnings:

When running in dev mode (process.env.NODE_ENV !== 'production') the format-to-jsx will throw some console.warn to help you spotting incorrect formatting issues like passing different number of arguments than placeholders in the template or when matching placeholder was not paired with a parameter.

If template passed is undefined or null will throw and exception.