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

stringable

v0.2.4

Published

Stringify any values to got something which render well in a log

Downloads

2

Readme

Stringable

experimental

Branch : release version npm version

Build Status Coverage Status

Dependency Status devDependency Status

Stringify any values to got something which render well in a log

Introduction

Why ?

You need to format as string a value that you ignore the type. However, in JS, some value doesn't have nice stringified version, and even sometimes, trying to stringify a value can throw an error. For example, if you try to stringify a Symbol using a string template like that:

const aSymbol = Symbol('a'); console.log(`${aSymbol}`);

You will get an error like: "Cannot convert a Symbol value to a string". To avoid issues like this one, Stringable converts any value in a string which aims to be readable, verbose, and useful in dev mode.

How to install ?

npm i stringable

How to use ?

const stringable = require('stringable');

const loggableObject = stringable({ firstName: 'Will', lastName: 'Wheeler', age: 13 });

console.log(loggableObject);
// (object => {
//   firstName: (string => 'Will'),
//   lastName: (string => 'Wheeler'),
//   age: (number: integer => 13)
// })

This is a basic example. You can use stringable with any value like functions, array, Symbols and even Node instance from the DOM API. Some value however, maybe worth to have a better and/or more specific formatted version. Don't hesitate to open issues.

Eventually, if you don't like the default formatting of some values, you can create a variant of stringable passing a custom formatter function to it (see the documentation below). The formatter will receive some data about the value to format and just have to return the formated value as a string.

Documentation

Table of Contents

stringable

The function you get when requiring the stringable module. Formats a value.

Parameters

  • value any The value to format.
  • formatter function (data: object): string The formatter to use in order to format the value. To create a custom one, look at the defaultFormatter documentation to see which data it will receive. (optional, default defaultFormatter)

Returns string The value formatted using the formatter.

defaultFormatter

Can't be required directly. The formatter used by default when using stringable. It formats a value from data provided by stringable.

Parameters

  • data object The data object sent to the formatter.
    • data.value any The value to format.
    • data.type string The value type from typeof operator.
    • data.stringifiedValue string A default and not optimal stringified version of the value.
    • data.isInteger boolean true if the value is an integer, else false.
    • data.isFloat boolean true if the value is a float, else false.
    • data.simpleQuoteString string If value is a string, this will be a representation of the string quotted with simple quotes.
    • data.doubleQuoteString string If value is a string, this will be a representation of the string quotted with double quotes.
    • data.defaultFormatter function The default formatter used by stringable.
    • data.constructorName string The name of the value constructor.
    • data.keys Array<string> The list of the value properties (or attributes names of the value is a DOM Node)
    • data.functionName string The name of the function is the value is a function.
    • data.isAsync boolean true if the value is an async function, else false.
    • data.isGenerator boolean true if the value is a generator function (using the syntax function*), else false.
    • data.isClass boolean true if the value is an es class, else false.
  • parents Array Used internally. An array containing the owners objects of the value (if value is a property). (optional, default [])
  • deepness number Used internally. The displayed nesting indentation level. (optional, default 0)
  • useNestTab boolean Used internally. If the nesting indentation must be used at this level. (optional, default true)

Returns string The formatted value as a string.

License

stringable is released under MIT. Copyright (c) 2017-present Alexis Tessier