written@v0.2.2 | Deno

5 min read Original article ↗

written

Build Status

written provides a set of utilities for manipulating text, with a focus on providing typographic tools rather than pure string manipulation. It can be added as a set of mixins to Underscore or used in it’s own right, both in front and back end contexts.

Deno or ESM
import * as written from "https://deno.land/x/written/written.ts";


import { writtenNumber } from "https://deno.land/x/written/written.ts";
Browser
<script type="module" src=></script>
NPM

As this is a Deno fork, the original NPM version is published by @stephenhutchings from the repo stephenhutchings/written. Note that the Node version doesn’t have types.


Setup

written can be used in any environment that supports both ES Modules and TypeScript.

Some style guides prefer the numbers 12 and under to be written, so we’ll include those in here. If more or fewer numbers need to be added, or those from another language, see Language Support.

Following the APA style guide (for ease and practicality) conjunctions, articles, and short prepositions of less than four letters will be left in lowercase when calling capitalizeAll().

A rule is needed to determine the correct ordinal for any number. For English, we use match in such a way that the first value in the matching array is returned, unless it is 11, 12 or 13. We use this number to determine the correct ordinal form.


Capitalization

Capitalize the first letter of a string.

Examples:

w.capitalize("obviously");                       

Capitalize all words in a string apart from some common lower case words. This can be tested with the internal noncaps regular expression, which are stored by language code, or by passing a regular expression of your own.

Examples:

w.capitalizeAll("this and that");                
w.capitalizeAll("the cat in the hat");           

Utilities

enclose wraps a string within two other strings, repeating the first if needs be. cleanJoin joins an array of words with falsy, non-string values removed with some glue. Both are used internally but are offered in case of their external value.

Examples:

w.enclose("'", "string");                        
w.cleanJoin(["this", null, "that"], " and ");    

Collapse

Replace all white-space in a string with a single space character

Examples:

w.collapse("this   \t\t and \n    that");        

Cases

Transform strings between common code cases.

Examples:

w.camelCase("some-thing");                       
w.hyphenCase("some_thing");                      
w.snakeCase("someThing");                        
w.humanCase("fromA_to-Z");                       

This helps to split “cased” words into their constituent parts…

Tags

Enclose a string inside an HTML tag.

Examples:

w.wrapInTag("Hello world!");                     
w.wrapInTag("Hello world!", "em");               
w.wrapInTag(                                     
  "Link",
  "a",
  {
    href: "/url",
    class: ["b"],
    disabled: true,
  },
);

Lists

Group strings into a grammatically correct list with an arbitrary limit. The final example shows all the possible options available.

Examples:

w.prettyList(["Ben", "Bob"])                     
w.prettyList(["Ben", "Bob", "Bill"])             
w.prettyList(["Ben", "Bob", "Bill", "Max"], 2)   
w.prettyList(["Ben", "Bob"], 1, {more: "other"}) 
w.prettyList([                                   
  {file: "Document 1"},
  {file: "Document 2"},
  {file: "Document 3"}
], 1, {
  amp: "&"
  written: true,
  more: "other file",
  quantify: true,
  key: "file"
})

Hyphenation

Add soft hyphens every n characters so that the CSS attribute hyphens: manual will allow for nice breaks in long strings of text. This is especially useful on mobile devices, where long strings can break the layout.

Examples:

w.hyphenate("antidisestablishmentarianism"); 

Quantify

Add an “s” to a string when an amount is non-singular, disregarding the order of the arguments passsed. If an array or collection is passed, it’s length will be used as the numerical input.

Examples:

w.quantify("monkey", 1);                         
w.quantify(1, "monkey");                         
w.quantify("monkey", 9, { written: true });      
w.quantify("person", 9, { plural: "people" });   
w.quantify([1, 2, 3], "number");                 

Written Numbers

Convert numbers between one and twelve into their written counter-parts.

Examples:

w.writtenNumber(1);                              
w.writtenNumber(2, "DE");                        

Quotes

Wrap a string in single or double quotes or guillemets (angle quotes).

Examples:

w.quote("pastry chef", "s");                     
w.quote("cats cradle");                          
w.quote("tres chic", "a");                       
w.quote("Gol", "!");                             
w.quote("Cómo estás", "?");                      

Ordinals

Convert a number from it’s cardinal to ordinal equivalent.

Examples:

w.ordinal(1);                                    
w.ordinal(2, { written: true });                 
w.ordinal(3, { wrap: true });                    
w.ordinal(4, { wrap: "em" });                    

Numbers

Format a number in various ways and parse one from a string.

Examples:

w.prettyNumber(1000);                            
w.prettyNumber(10.5, 2);                         
w.prettyNumber(9999, " ", 2, ",");               

w.prettyPrice(4);                                
w.prettyPrice(1200, "£");                        
w.prettyPrice(                                   
  4,
  {
    currency: "€",
    wrap: "sup",
  },
);

w.prettyPercent(0.5);                            
w.prettyPercent(1, 4);                           
w.prettyPercent(1, 3, 2);                        

w.parseNumber(1000);                             
w.parseNumber("1,000.00");                       
w.parseNumber("99%");                            
w.parseNumber("some 44,000 participants");       

Glyphs

Provide quick access to different typographic glyphs without the need commit them to memory or look at a reference table.

Examples:

w.glyphs();                                      
w.glyph("!");                                    

Language Support

Set cardinal and ordinal numbers and non-caps words for different languages as appropriate. Please note that only partial support for French, German, Italian, Spanish and Swedish is currently implemented. If using in the browser, ensure that the document’s charset is set to UTF-8. Pull requests which extend language support are encouraged.

To load and install a locale, do:

import * as written from "https://deno.land/x/written/written.ts";
import * as DE from "https://deno.land/x/written/lang/written.de.ts";


written.setLanguage(DE.dico, DE.code);


w.writtenNumber(1);                              


w.writtenNumber(2, "EN");                        

Aliases

Pack up the written object (with some aliases…)

  • dasherize -> hyphenCase
  • dashify -> hyphenCase
  • slugify -> snakeCase
  • underscore -> snakeCase
  • numerate -> quantify
  • count -> quantify
  • titleCase -> capitalizeAll