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-table-fixed-chinese

v0.0.2

Published

fork form package:string-table.Fixed chinese string length bug.

Downloads

6

Readme

stringTable.js

Build Status

A groundbreaking, innovative JavaScript library to do something that's literally never been attempted before: formatting an array of data objects as a textual table.

Installation

npm install string-table

Example

var users = [
  { name: 'Dan', gender: 'M', age: 29 },
  { name: 'Adam', gender: 'M', age: 31 },
  { name: 'Lauren', gender: 'F', age: 33 }
];

stringTable.create(users);

/*
 * Result:
 *
 * | name   | gender | age |
 * -------------------------
 * | Dan    | M      |  29 |
 * | Adam   | M      |  31 |
 * | Lauren | F      |  33 |
 */

It works with multi-line strings, too!

# This example is in CoffeeScript for readability.

books = [
  {
    title: 'The Cat in the Hat',
    opening:
      """
      The sun did not shine.
      It was too wet to play.
      So we sat in the house
      All that cold, cold, wet day.
      """
  },
  {
    title: 'Green Eggs and Ham',
    opening:
      """
      I am Sam.
      Sam I am.
      Do you like green eggs and ham?
      """
  }
]

stringTable.create(books)

#
# Result:
#
# | title              | opening                         |
# --------------------------------------------------------
# | The Cat in the Hat | The sun did not shine.          |
# |                    | It was too wet to play.         |
# |                    | So we sat in the house          |
# |                    | All that cold, cold, wet day.   |
# | Green Eggs and Ham | I am Sam.                       |
# |                    | Sam I am.                       |
# |                    | Do you like green eggs and ham? |
#

You can also specify options to customize how the table is formatted:

var table = stringTable.create(users, options);

The available options are summarized below.

Options

headers

An array of strings indicating which column headers to include (and in what order)

Default: every property from the first object in the list

Example

stringTable.create(users, { headers: ['age', 'name'] });

/*
 * Result:
 *
 * | age | name   |
 * ----------------
 * |  29 | Dan    |
 * |  31 | Adam   |
 * |  33 | Lauren |
 */

capitalizeHeaders

Whether or not to capitalize the table's column headers

Default: false

Example

stringTable.create(users, { capitalizeHeaders: true });

/*
 * Result:
 *
 * | Name   | Gender | Age |
 * -------------------------
 * | Dan    | M      |  29 |
 * | Adam   | M      |  31 |
 * | Lauren | F      |  33 |
 */

formatters

An object mapping column names to formatter functions, which will accept (value, header) arguments

Default: none

Example

stringTable.create(users, {
  formatters: {
    name: function(value, header) { return value.toUpperCase(); }
  }
});

/*
 * Result:
 *
 * | name   | gender | age |
 * -------------------------
 * | DAN    | M      |  29 |
 * | ADAM   | M      |  31 |
 * | LAUREN | F      |  33 |
 */

A formatter may also return an object with the properties { value, format }, where format in turn can have the properties { color, alignment }.

stringTable.create(users, {
  formatters: {
    gender: function(value, header) {
      return {
        value: value,
        format: {
          color: value === 'M' ? 'cyan' : 'magenta',
          alignment: 'right'
        }
      };
    }
  }
});

/*
 * Result:
 *
 * | name   | gender |    age |
 * ----------------------------
 * | Dan    |      M |  29.00 |
 * | Adam   |      M |  31.00 |
 * | Lauren |      F |  33.00 |
 *
 * (Imagine the Ms are cyan and the F is magenta above.)
 */

typeFormatters

An object mapping data types ('string', 'number', 'boolean', etc.) to formatter functions (has lower precedence than formatters option)

Default: none

Example

stringTable.create(users, {
  typeFormatters: {
    number: function(value, header) { return value.toFixed(2); }
  }
});

/*
 * Result:
 *
 * | name   | gender |    age |
 * ----------------------------
 * | Dan    | M      |  29.00 |
 * | Adam   | M      |  31.00 |
 * | Lauren | F      |  33.00 |
 */

outerBorder and innerBorder

The character(s) used to enclose the table and to delimit cells within the table, respectively

Defaults: '|' for both

Example

stringTable.create(users, {
  outerBorder: '%',
  innerBorder: '$'
});

/*
 * Result:
 *
 * % name   $ gender $ age %
 * -------------------------
 * % Dan    $ M      $  29 %
 * % Adam   $ M      $  31 %
 * % Lauren $ F      $  33 %
 */

rowSeparator

The character used to separate rows in the table

Default: none

Example

stringTable.create(users, { rowSeparator: '~' });

/*
 * Result:
 *
 * | name   | gender | age |
 * -------------------------
 * | Dan    | M      |  29 |
 * ~~~~~~~~~~~~~~~~~~~~~~~~~
 * | Adam   | M      |  31 |
 * ~~~~~~~~~~~~~~~~~~~~~~~~~
 * | Lauren | F      |  33 |
 */

headerSeparator

The character used to separate the header row from the table body

Default: '-'

Example

stringTable.create(users, { headerSeparator: '*' });

/*
 * Result:
 *
 * | name   | gender | age |
 * *************************
 * | Dan    | M      |  29 |
 * | Adam   | M      |  31 |
 * | Lauren | F      |  33 |
 */