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

unjustifiable

v1.1.2

Published

Fast (enough) and beautiful text justification for the web

Downloads

9

Readme

Unjustifiable

Text justification on the web is really hideous. Browsers choose to naively cram as much as they can on each line, but this greedy algorithm leaves "loose" lines of text, with great gaping holes.

For hundreds of years, well-justified text was considered masterful and beautiful; only in the era of the browser have designers turned their back on it -- and with good reason. On a browser, justified text is ugly.

Was ugly.

With unjustifiable, text can look like this: Example

Note the dangling punctuation, and that text can change styles inline -- bold words are wider, but the text is still properly aligned. Floats and other incursions to the text are also supported.

See my blog for a live example.

Unjustifiable works best when paired with a hyphenating function; I recommend Hypher (that's what I use.)

Unjustifiable works beautifully on Chrome and safai, and well enough on Firefox, and Edge. It will leave a slightly ragged right on some mobile browsers, and it's up to you to decide what to do about that.

Installation

yarn add unjustifiable

Usage

const justify = unjustifiable();
const elements = document.querySelectorAll(".content p");
elements.forEach(justify);

Options

The unjustifiable function takes an optional options object. Defaults are shown below:

const options = {
  stretch: 15,
  shrink: -1,
  overhang: 20,
  hyphenPenalty: 1000,
  hyphenator: function(w) {
    // Returns an array of hyphenatable syllables
    return [w];
  },
};
const justify = unjustifiable(options);

hyphenator

To use Hypher for the hyphenator, install Hypher, and then:

const hyph = new Hypher(en_us);
const justify = unjustifiable({
  hyphenator: function(w) { return hyph.hyphenate(w); }
});

stretch

A measure of the default ability for a space to stretch wider, in px.

shrink

A measure of the default ability for a space to shrink smaller, in px.

overhang

The amount of space, in px, left for punctuation to dangle attractively over the margins. It's usually best to style your textblocks with

p {
  margin-right: -20px;
  margin-left: -20px;
}

(or whatever the overhang is) to ensure that the justified line of the text appears where you expect, and the hanging punctuation... hangs.

HyphenPenalty

The bigger this number is, the less likely Unjustifible is to break a line mid-word.

Internally

It's a dynamic programming algorithm, like Knuth and Plass, but it's faster than Knuth and Plass. The speed increase comes by accepting suboptimal results for one part of its cost function: the difference in compression between a line and the succeeding line.