Text formatting
formatDate
Formats a date value to a text string.
Returns the formatted date string. Throws an exception if the value or format is empty.
An invalid date does not throw an exception, but results in the text Invalid date.
let text = formatDate(date_time_value, date_time_format);
Example:
let date_value = new Date();
let text = formatDate(date_value, "DD.MM.YYYY");
log("Formatted Date :", text);
formatDateTime
Formats a date-time value to a text string with timezone conversion.
Returns the formatted date-time string. Default timezone city is Berlin, which is also used for an unknown city.
Throws an exception if the value or format is empty. An invalid date does not throw an exception, but results in the
text Invalid date.
let text = formatDateTime(date_time_value, date_time_format, target_time_zone_city);
Example:
let date_time_value = new Date();
let text = formatDateTime(date_time_value, "DD.MM.YYYY HH:mm", "Berlin");
log("Formatted DateTime :", text);
formatNumber
Formats the given numeric value. Format and language are optional, defaulting to 0.00 and en-gb.
Returns the formatted number string. Throws an exception if the value is null or the language is not supported.
A value that is not a number does not throw an exception, but is formatted as 0.
The supported formats are based on NumeralJS and work as follows:
- Use
,to indicate that you want thousands separators in the formatted string - Use
.to indicate that you want a decimal point in the formatted string - Use
$to indicate that you want a currency symbol in the formatted string - Put one or more zeroes after the decimal point to indicate the number of decimal places you want
The symbols that appear in the formatted string are dependent on the language. See the examples below.
const formattedNumber = formatNumber(value, format, language);
Example:
let result1 = formatNumber(1234.567); // => "1234.57"
let result2 = formatNumber(1234.567, "0,0.00", "en-gb"); // => "1,234.57"
let result3 = formatNumber(1234.567, "0,0.00", "de-de"); // => "1.234,57"
let result4 = formatNumber(1234.567, "0.00000", "de-de"); // => "1234,56700"
let result5 = formatNumber(1234.567, "0"); // => "1235"
let result6 = formatNumber(1234.567, "00000"); // => "01235"
let result7 = formatNumber(1234.567, "$ 0,0.00", "de-de"); // => "€ 1.234,57"
let result8 = formatNumber(1234.567, "0$", "de-de"); // => "1235€"
formatItrpNote
Formats a text with Xurrent text formatting as HTML.
Returns an HTML string. Throws an exception if the text is empty.
const html = formatItrpNote(text);
Example:
let note = "**Bold text** and _italic text_";
let html = formatItrpNote(note);
log("html:", html); // => '<b>Bold text</b> and <em>italic text</em>'
html2text
Converts HTML to plain text.
Returns the plain text string. Throws an exception if the HTML is empty.
let text = html2text(html, options);
Example:
let html = "<h1>Title</h1><p>Hello <b>World</b></p>";
let text = html2text(html, {
wordwrap: 80,
preserveNewlines: true,
tables: true,
singleNewLineParagraphs: true,
});
log("text:", text); // => 'TITLE\nHello World'
upperCaseInitials
Converts the first letter of each word in the given text to uppercase.
Returns the formatted string, or an empty string if the value is not a string.
let formattedText = upperCaseInitials(text);
Example:
let result = upperCaseInitials("hello world");
log("result:", result); // => 'Hello World'
objectToText
Converts an object's first-level properties into a text representation.
Returns the text string. Throws an exception if the object is null.
Properties starting with a _ are treated as private. Subobjects and empty values are not taken into consideration.
Only the properties that Object.keys returns are included. To select properties or use another format, use
Object.entries instead, as in example 2.
const result = objectToText(data_object);
Example 1: Default format
const ob = {
name: "Max Mustermann",
strasse_und_hausnummer: "Musterstrasse 42",
};
log(objectToText(ob));
// => Name: Max Mustermann
// Strasse Und Hausnummer: Musterstrasse 42
Example 2: Selecting properties and using another format
const ob = {
name: "Max Mustermann",
email_privat: "max.musterman@privat-test-mail.com",
email_arbeit: "max.musterman@arbeit-test-mail.com",
};
const text = Object.entries(ob)
.filter(([name]) => name.startsWith("email_"))
.map(([name, value]) => `${upperCaseInitials(name.replace(/_/g, " "))}: ${value}<br/>\n`)
.join("");
log(text);
// => Email Privat: max.musterman@privat-test-mail.com<br/>
// Email Arbeit: max.musterman@arbeit-test-mail.com<br/>