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 🙏

© 2025 – Pkg Stats / Ryan Hefner

preformat

v1.1.1

Published

Log with custom formatting.

Downloads

5

Readme

preformat

npm Node.js CI

Log with custom formatting.

const logger = preformat({ success: '<DONE>' });
logger.log('Hello %s!', 'World');
logger.success('Hello %s!', 'World');
Hello World!
<DONE> Hello World!

Installation

npm install preformat

Use the module:

// ES6
import preformat from 'preformat';

// CommonJS
const { preformat } = require('preformat');

Usage

preformat (logger)

The preformat function accepts a format value (default format) or an object with format values, and it returns a Preformat object (logger).

| Properties | Description | Type | | ---------------------------------------------- | -------------------------------------------------------------------------------------------- | ----------------------------------------- | | logger.force | Force logging even with no params. | Object, methods return Preformat object | | logger.format | Format params instead of logging. | Object, methods return any[] | | logger.format.force | Force formatting even with no params. | Object, methods return any[] | | logger.handle(handler) | Set handler callback and override default logging. | Returns Preformat object | | logger[methods] | Default existing format methods: default, log, info, error, warn, debug, trace | Returns Preformat object | | logger.* | Other custom format methods. | Returns Preformat object |

Examples

  • Basic formatting:

    const logger = preformat('Prefix:');
    logger.log('Hello %s!', 'World');
    Prefix: Hello World!
  • Formatting with function:

    const logger = preformat(() => {
      const year = new Date().getFullYear();
      return `[${year}]`;
    });
    logger.log('Hello %s!', 'World');
    [2021] Hello World!
  • Multiple formatting:

    const logger = preformat({
      default: '[DEFAULT]',
      log: '[LOG]',
      info: '[INFO]',
      error: () => '[ERR]',
      warn: '[WARN]',
      debug: () => {
        const year = new Date().getFullYear();
        return `[DEUBG-${year}]`;
      }
    });
    logger
      .log('Hello %s!', 'World')
      .error('Hello %s!', 'World')
      .debug('Hello %s!', 'World');
    [LOG] Hello World!
    [ERR] Hello World!
    [DEBUG-2021] Hello World!
  • Custom formatting:

    You can apply custom format method names to the object and use them through the logger.

    const logger = preformat({ success: '<DONE>', test: () => '<TEST>' });
    logger.success('Hello %s!', 'World');
    logger.test('Hello %s!', 'World');
    <DONE> Hello World!
    <TEST> Hello World!
  • Formatting with substitution:

    const logger = preformat({
      default: 'DEFAULT: %s foo',
      error: 'ERROR: %s bar'
    });
    logger.log(200).error(400, 'error').warn('[%d]', 300, 'warn');
    DEFAULT: 200 foo
    ERROR: 400 bar error
    DEFAULT: [300] foo warn

logger.force

The logger.force object contains the format methods that will apply the format even if params is empty.

By default, the format methods will print out an empty line if there are no params similar to console.log(), but using force will always include the formatting.

const logger = preformat({ success: '<DONE>' });
logger.success();
logger.force.success();
logger.success('Hello %s!', 'World');
logger.force.success('Hello %s!', 'World');

<DONE>
<DONE> Hello World!
<DONE> Hello World!

logger.format

The logger.format object contains the format methods and each method will return the formatted result (type: any[]). Example:

const logger = preformat({ success: '<DONE>' });
const output = logger.format.success('Hello %s!', 'World');
console.log(output);
[ '<DONE> Hello %s!', 'World' ]

logger.format.force

The logger.format.force object will apply the formatting even without params. Its methods will also return the formatted result.

const logger = preformat({ success: '<DONE>' });
const value1 = logger.format.success();
const value2 = logger.format.force.success();
console.log(value1);
console.log(value2);
[]
[ '<DONE>' ]

logger.handle(handler)

The logger.handle method accepts a callback handler to handle the format method call.

By default, it uses console to log the formatted parameters. You can override this functionality. Example:

const logger = preformat({ success: '<DONE>' });

logger.handle((mode, args, defaultHandler) => {
  // mode is the format method (e.g. log, success, etc.)
  console.log('mode:', mode);
  // args is an object:
  // args.raw contains the raw parameters
  console.log('args.raw:', args.raw);
  // args.params contains the formatted parameters
  console.log('args.params:', args.params);
  // defaultHandler is the default handle function
  defaultHandler(mode, args);
});

logger.success('Hello %s!', 'World');
mode: success
args.raw: [ 'Hello %s!', 'World' ]
args.params: [ '<DONE> Hello %s!', 'World' ]
<DONE> Hello World!

You can use the default handler again by passing in null or undefined to the logger.handle call:

const logger = preformat();
logger.handle(null);
logger.log('Hello %s!', 'World');
Hello World!

format

Applies util.format() and util.inspect() to params.

import { format } from 'preformat';

const value = format('Hello %s!', 'World', ':)');
console.log(value);
[ 'Hello World! :)' ]

It will return an array with one string if params were provided. Otherwise, it will return an empty array.

console.log(format());
[]

This helps to distinguish empty params from undefined values.

isLogMethod

Checks whether a mode is part of the default format methods.

import { isLogMethod } from 'preformat';

isLogMethod('log'); // true
isLogMethod('success'); // false

This can be useful when overriding the default logging via logger.handle.

License

Licensed under the MIT License.