0.5.1 • Published 5 years ago

typographizer-js v0.5.1

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

TypographizerJS

Published version Code Coverage Build Status Dependency Status Dev Dependency Status Greenkeeper badge

Typographizer automates everything some things you don’t want (or need) to know about typography.


Acknowledgement

The JS version is inspired by the Typographizer by Frank Rausch, written in Swift. 🙇‍

Usage

Here be dragons 🐉

This package uses RegExp lookbehinds. These are not universally supported. You can use it in Node >=6.4 with enabled --harmony flag, without a flag in Node >=9.11.

You should not use this in the browser (probably). Formatting and reinserting long strings is a mayor performance hit.

In general, this is more of a fun project. So, use with caution.

Installation

npm install typographizer-js

If you are using Yarn:

yarn add typographizer-js

Import the bundle into your project:

const TypographizerJS = require('typographizer-js')

And crate a new instance:

const Typographizer = new TypographizerJS()

Note: Typographizer will preserve inline HTML Tags (a, strong, em), but should not be used to format large bunches of source code.

Once you have your Typographizer instance available you can format strings using the typographize() method.

typographize returns a Promise. So you need to either await the return value or use .then().

const str = `I introduced myself, "Hi, I'm Oscar".`

/**
 * Using await
 */
const formatted = await Typographizer.typographize(str)

console.log(formatted)
// I introduced myself, “Hi, I’m Oscar“.

/**
 * Using .then()
 */
Typographizer.typographize(str)
  .then((str) => {
    console.log(str)
    // I introduced myself, “Hi, I’m Oscar“.
  })

Typographizer uses a bunch of different methods under the hood. All of them can be called on their own. For details, take a look at the API section below.

Options

By default quotes will be formatted to match en_US style. If you need different formatting, pass a language code while initializing.

const Typographizer = new TypographizerJS('fr')

You can find a list of supported languages further down this file.

⚠️ To avoid side effects and confusion, It is not possible to change these options after you created your instance. If you want to format text in multiple languages, you need to create multiple instances.

API

All these methods return Promises. Usage instructions for typographize usage apply.

Methods

MethodDescription
fixApostroph(str)Replace ', ´ and ` used as an apostroph
fixEllipsis(str)Formats all occurences of ... to use the correct … character
fixAiryPunctuation(str)Finds punctuation that is surrounded by whitespace
formatQuotes(str)Format quotes, but not whitespace or apostrophes
formatOpeningQuotes(str)Format all opening quotes
formatClosingQuotes(str)Format all closing quotes
trimWhitespace(str)Remove whitespace from the beginning and end of a string, replaces multiple spaces with a single one
typographize(str)Takes a string and applies all optimizations

Supported languages

Language support has been ported from the original Typographizer.

Supported languages are:

LanguageShortcode
Bosnianbs
Czechcz
Danishda
Dutch, Flemishnl
British Englishen_GB
American English (default)en_US
Estonianes
Finnishfi
Frenchfr
Germande
German (Suisse)de_CH
German (Liechtenstein)de_LI
Hungarianhu
Icelandicis
Lithuanianlt
Latvianlv
Polishpl
Romanianro
Slovaksk
Sloveniansl
Swedishsw

This list is missing Japanese and Hebrew, which are supported by Typographizer. Hebrew is missing due to complicated support. Japanese requires extended Unicode checks.

0.5.1

5 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago