textfunctions v2.18.0
TextFunctions
A module with text formatting methods for template engines such as Pug.
Installation
npm install git+ssh://git@gitlab.com:newsworthy/textfunctions.git
Usage
Basic usage:
const t = require("textfunctions")("tr")
t.upper("istanbul")
// 'İSTANBUL'
This module takes arguments:
const t = require("textfunctions")("no")
...or, if you prefer:
const TextFunctions = require("textfunctions")
const t = TextFunctions("sv")
You can add an array of languages to use as a fallback chain, as well as
a translation dictionary for use with the t()
method:
const dictionary = {
oslo: {en: "Oslo municipality", nn: "Oslo kommune"},
}
const t = require("textfunctions")(["no", "nn"], dictionary)
t.t("oslo")
// 'Oslo kommune'
With PugJS
const textFunctions = require("textfunctions")("sv")
const {renderFile} = require("pug")
let context = textFunctions.assign({
days_left: 12,
})
let html = renderFile("myTemplateFile", context)
p Today, #{pl(days_left, "it is", "there are")} #{numberPretty(days_left)} #{pl(days_left, "day", "days")} until Christmas.
Note that there is an assign
method in textFunctions, that is a recommended alternative to Object.assign
, in case you want to have data and functions in the same namespace. This method will throw an error in case of a naming conflict, to avoid potential confusion. In other words, rather than doing like this:
let context = Object.assign({}, data, textFunctions)
...we recommend this:
let context = textFunctions.assign(data)
If you let your data live in the same namespace as the methods, in this way, we recommend using PascalCase for variables, as all textfunctions methods are lowerCamelCase.
Change log
The change log has moved to CHANGELOG
We use semver style version numbers, but only functional changes are considered when changing the major and minor version numbers. Linguistic data can (and do!) change as part of patch updates. This means that any update may affect the output of your templates. (Otherwise, due to the nature of this project, all updates would have to be considered major.)
A change in the X.Y.Z version number means:
- X: The API changed.
- Y: A new method was added, or the behavior of an individual method changed.
- Z: A bug fix or data change.
Roadmap
- The current piping support is very hacky, and will never work in all environments. It couls be phased out as soon as we feel ready to require users to use http://tc39.github.io/proposal-pipeline-operator/, making
numberText(x).upper()
becomenumberText(x) |> upper
. - We add quite a few properties to methods, for unified I/O handling. These should replace by decorators as soon as we have them in Node: https://tc39.es/proposal-decorators/
5 years ago