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

async-to-string

v1.1.0

Published

Turn every (async or sync) value into a string

Downloads

1

Readme

async-to-string

Turn every (async or sync) value into a string by calling asyncToString or by creating a tag function.

  • Installation
  • [Method usage](#method usage)
  • [Tag usage](#tag usage)

Installation

npm i async-to-string

Method usage

Any value passed into the asyncToString method will be converted into a string.

import { asyncToString } from 'async-to-string';

console.log(await asyncToString(1)); // -> '1'
console.log(await asyncToString(null)); // -> ''

Promise values will be awaited and the result will be passed into the asyncToString method and returned.

import { asyncToString } from 'async-to-string';

console.log(await asyncToString(Promise.resolve(1))); // -> '1'

Async iterables will be looped and the last yield will be passed into the asyncToString method and returned.

import { asyncToString } from 'async-to-string';

async function* generate() {
  yield 1;
  yield 2;
  yield 3;
}

console.log(await asyncToString(generate())); // -> '3'

Iterables will be joined together after every value was be passed into the asyncToString method.

import { asyncToString } from 'async-to-string';

async function* generate() {
  yield 1;
  yield 2;
  yield 3;
}

const list = [1, Promise.resolve(2), generate()];

console.log(await asyncToString(list)); // -> '123'

Even methods can be passed in, they will be called and their return value will be passed into the asyncToString method before being returned.

import { asyncToString } from 'async-to-string';

async function* generate() {
  yield 1;
  yield 2;
  yield 3;
}

console.log(await asyncToString(generate)); // -> '3'

An optional args iterable can be provided in the second options argument, it's items will be used as the arguments for calling any methods.

import { asyncToString } from 'async-to-string';

console.log(await asyncToString((x, y) => x + y, { args: [6, 3] })); // -> '9'

Values will be passed on recursively so deeply nested hierarchies will all be turned into a single string.

import { asyncToString } from 'async-to-string';

console.log(
  await asyncToString(
    [
      Promise.resolve(1),
      [(x, y) => x + y, Promise.resolve(['string', (x, y) => x - y])],
    ],
    { args: [6, 3] }
  )
); // -> '19string3'

If string encoding is required an encode option can be provided as method for instance to generate html safe strings.

import { asyncToString } from 'async-to-string';
import { encode } from 'html-entities';

console.log(await asyncToString('< > " \' &', { encode })); // -> '&lt; &gt; &quot; &apos; &amp;'

To exclude a string from being encoded it can be marked as "safe".

import { asyncToString, safeString } from 'async-to-string';
import { encode } from 'html-entities';

console.log(
    await asyncToString([
      '< > " \' &',
      safeString('< > " \' &'),
    ]
    { encode }
  )
); // -> '&lt; &gt; &quot; &apos; &amp;< > " \' &'

Tag usage

The tag function tagAsyncToString returns a method that can be called with the options argument of the asyncToString method.

import { tagAsyncToString } from 'async-to-string';

const template = tagAsyncToString`This is some string with an ${Promise.resolve('async replacement')}`;

console.log(
  await template({ /* asyncToString options can be provided here */ })
)
); // -> 'This is some string with an async replacement'

All values that asyncToString will convert to strings can be used as placeholders

import { tagAsyncToString } from 'async-to-string';

const template = tagAsyncToString`This is some string with an ${Promise.resolve(
  'async replacement'
)}
Just showing off: ${[
  (x, y) => x + y,
  Promise.resolve(['string', (x, y) => x - y]),
]}`;

console.log(await template({ args: [6, 3] }));
// -> 'This is some string with an async replacement
// -> 'Just showing off: 19string3'