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

fast-word-wrap

v1.1.0

Published

a fast and simple node.js package for getting a paragraph wrapped at a fixed line length

Downloads

740

Readme

fast-word-wrap

a fast and simple node.js package for getting a paragraph wrapped at a fixed line length

fast-word-wrap has been proven (see benchmark) to be faster than wordwrap (28x), wrap-text (10x) and word-wrap (8x)

toc

installation

$ npm install fast-word-wrap

usage

const wrap = require("fast-word-wrap");

const str = "The quick brown fox jumps over the lazy dog."; // the newline-free string to wrap
const cpl = 10; // the maximum number of characters per line

result = wrap(str, cpl);

console.log(result); // "The quick\nbrown fox\njumps over\nthe lazy\ndog.\n"

outputs

The quick
brown fox
jumps over
the lazy
dog.

wrapping strings containing newlines

fast-word-wrap won't work correctly if the input string contains newlines; if that's the case, simply do

partials = [];
for (p of str.split("\n").filter(s => s)) {
    partials.push(wrap(p, cpl));
}

result = partials.join("");

testing

unit

$ npm test test/unit.js

outputs


> [email protected] test
> ava --verbose "test/unit.js"


  ✔ no wrap
    ℹ The quick brown fox jumps over the lazy dog.
      
  ✔ wrap
    ℹ The quick
      brown fox
      jumps over
      the lazy
      dog.

  ✔ do your best
    ℹ The
      quick
      brown
      fox
      jumps
      over
      the
      lazy
      dog.
      
  ✔ wrap a fragment of "El inmortal" by Jorge Luis Borges
    ℹ [output omitted for brevity in readme]
      
  ─

  4 tests passed

benchmark

$ npm test test/benchmark.js

outputs in my laptop


> [email protected] test
> ava --verbose test/benchmark.js


  ✔ wrap faster than others (2.5s)
    ℹ 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
    ℹ wrapping 10000 times with each package a fragment of "El inmortal" by Jorge Luis Borges...
    ℹ wordwrap took 1500.74 ms
    ℹ wrap-text took 532.84 ms
    ℹ word-wrap took 506.52 ms
    ℹ fast-word-wrap took 47.02 ms
  ─

  1 test passed

barh

about the sample text and the golden master

the fragment used for testing is a transcription I did from my copy of "Nueva antología personal" by Jorge Luis Borges (ed. Emecé, 1st edition, 1968); I manually wrapped it at 80 characters per line to get the golden master