@alduino/humanizer

1.1.0 • Public • Published

This is a port of Humanizer for Javascript. Humanizer meets all your Javascript needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities. It is licensed under the MIT (an OSI approved license).

This library is currently in very early development, so most features of Humanizer aren't supported yet. The features currently documented in this readme are the ones that are supported.

Install

You can install Humanizer from NPM:

npm i @alduino/humanizer
yarn add @alduino/humanizer
pnpm i @alduino/humanizer

Alternatives

  • humanizer.node
    • Pros: More faithful port of Humanizer.NET, currently supports more features, recently updated
    • Cons: Pollutes prototypes

Features

Humanise string

import {humanize} from "@alduino/humanizer/string";
// or
const {humanize} = require("@alduino/humanizer/string");

You can call the humanize() function to turn an otherwise computerised string into a more readable human-friendly one.

humanize("PascalCaseInputStringIsTurnedIntoSentence") => "Pascal case input string is turned into sentence"
humanize("Underscored_input_string_is_turned_into_sentence") => "Underscored input string is turned into sentence"
humanize("Underscored_input_String_is_turned_INTO_sentence") => "Underscored input String is turned INTO sentence"

Dehumanise string

import {dehumanize} from "@alduino/humanizer/string";
// or
const {dehumanize} = require("@alduino/humanizer/string");

Much like you can humanise a computer friendly into human friendly string, you can dehumanise a human friendly string into a computer friendly one:

dehumanize("Pascal case input string is turned into sentence") => "PascalCaseInputStringIsTurnedIntoSentence"

Transform string

import {transform, toLowerCase} from "@alduino/humanizer/string";
// or
const {transform, toLowerCase} = require("@alduino/humanizer/string");

The transform function applies some kind of transformation to the input string. There are a few default implementations:

transform("Sentence casing", toLowerCase) => "sentence casing"
transform("Sentence casing", toSentenceCase) => "Sentence casing"
transform("Sentence casing", toTitleCase) => "Sentence Casing"
transform("Sentence casing", toUpperCase) => "SENTENCE CASING"

You can create your own transformers by implementing the IStringTransformer interface, which you can import from the same place.

Truncate string

import {truncate} from "@alduino/humanizer/string";
// or
const {truncate} = require("@alduino/humanizer/string");

You can truncate a string using the truncate method:

truncate("Long text to truncate", 10) => "Long text…"

By default, the character is used to truncate strings. The advantage of using the character over ... is that the former is only a single character long, and thus allows more text to be shown before truncation. If you want, you can override this with your own truncation string:

truncate("Long text to truncate", 10, "---") => "Long te---"

The default truncation strategy, fixedLength, is used to truncate the input stringto a specific length, including the truncation string length. There are two more default truncator strategies available: one for a fixed number of (alpha-numerical) characters, and one for a fixed number of words. To use a specific truncator when truncating, pass it after the truncation string (you can use null as the truncation string value to use its default):

import {fixedLength, fixedNumberOfCharacters, fixedNumberOfWords} from "@alduino/humanizer/string";

truncate("Long text to truncate", 10, null, fixedLength) => "Long text…"
truncate("Long text to truncate", 6, null, fixedNumberOfCharacters) => "Long t…"
truncate("Long text to truncate", 2, null, fixedNumberOfWords) => "Long text…"

You can create your own truncator by implementing the ITruncator interfacea from the same import.

There is also an option to choose whether to truncate the string from the beginning (TruncateFrom.Start) or the end (TruncateFrom.End). Default is the end as shown in the examples above, however you can truncate from the beginning by passing TruncateFrom.Start as the last parameter.

Inflector functions

Pluralize

import {pluralize} from "@alduino/humanizer/string";
// or
const {pluralize} = require("@alduino/humanizer/string");

Pluralize pluralises the provided input while taking irregular and uncountable words into consideration:

pluralize("Man") => "Men"
pluralize("string") => "strings"

Normally, you would call pluralize on a singular word, but if you are unsure you can specify the last parameter as false:

pluralize("Men", false) => "Men"
pluralize("Man", false) => "Men"
pluralize("string", false) => "strings"

Singularize

See pluralize, but reversed.

Adding words

Sometimes, you may need to add a rule from the singularisation/pluralisation vocabulary (the examples below are already in the default vocabulary):

import {defaultVocabulary} from "@alduino/humanizer/string";

// Adds a word that can't easily be matched using regex
// By default, will be matched as an ending option, so could be `person` or `salesperson`
defaultVocabulary.addIrregular("person", "people");
// To only match whole words, add a parameter `false`:
defaultVocabulary.addIrregular("person", "people", false);

// Adds an uncountable word
defaultVocabulary.addUncountable("fish");

// Adds a rule to the vocabulary that doesn't follow trivial rules
defaultVocabulary.addPlural("bus", "busses");
defaultVocabulary.addPlural("(vert|ind)ices$", "$1ex")

Dependencies (0)

    Dev Dependencies (12)

    Package Sidebar

    Install

    npm i @alduino/humanizer

    Weekly Downloads

    537

    Version

    1.1.0

    License

    MIT

    Unpacked Size

    207 kB

    Total Files

    70

    Last publish

    Collaborators

    • alduino