jsonata-functions

Extension functions for JSONata
Source:

Methods

(private, inner) decodeUuid(value, baseopt) → {String|Void}

Source:
See:
Decode the supplied shortened UUID
Example

Example usage within a JSONata transform

// returns
// result = '1b49aa30-e719-11e6-9835-f723b46a2688'
const jsonata = require('jsonata-extended');
const expr = jsonata('$decodeUuid("1m5otdkthiyq143crwujacdqg", "base36")');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = '1b49aa30-e719-11e6-9835-f723b46a2688'
const jsonata = require('jsonata-extended');
const expr = jsonata('$decodeUuid("1m5otdkthiyq143crwujacdqg", "base36")');
const result = expr.evaluate();
Parameters:
Name Type Attributes Default Description
value String Shortened UUID
base 'base2' | 'base10' | 'base16' | 'base32' | 'base36' | 'base58' | 'base62' | 'base64' | 'base64url' <optional>
'base36' A valid base to use for the process, case sensitive Encoding Options
Throws:
  • Will throw an error if the value is not a valid uuid
  • Will throw an error if the base is not a valid option
Returns:
Returns a string containing the encoded version of the uuid, or undefined for undefined input
Type
String | Void

(private, inner) encodeUuid(value, baseopt) → {String|Void}

Source:
See:
Encode the supplied UUID
Example

Example usage within a JSONata transform

// returns
// result = '1m5otdkthiyq143crwujacdqg'
const jsonata = require('jsonata-extended');
const expr = jsonata('$encodeUuid("1b49aa30-e719-11e6-9835-f723b46a2688", "base36")');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = '1m5otdkthiyq143crwujacdqg'
const jsonata = require('jsonata-extended');
const expr = jsonata('$encodeUuid("1b49aa30-e719-11e6-9835-f723b46a2688", "base36")');
const result = expr.evaluate();
Parameters:
Name Type Attributes Default Description
value String Properly formatted UUID
base 'base2' | 'base10' | 'base16' | 'base32' | 'base36' | 'base58' | 'base62' | 'base64' | 'base64url' <optional>
'base36' A valid base to use for the process, case sensitive Encoding Options
Throws:
  • Will throw an error if the value is not a valid uuid
  • Will throw an error if the base is not a valid option
Returns:
Returns a string containing the encoded version of the uuid, or undefined for undefined input
Type
String | Void

(private, inner) htmltotext(value, optionsopt) → {String|Void}

Source:
See:
Convert HTML to plain text
Example

Example usage within a JSONata transform. The <p> tags and the HTML entities are converted

// returns
// result = 'Leadership has a dark side; a "leadership shadow" that often creates an unknown; lurking fear.'
const jsonata = require('jsonata-extended');
const expr = jsonata('$htmltotext("<p>Leadership has a dark side; a &#34;leadership shadow&#34; that often creates an unknown; lurking fear.</p>")');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = 'Leadership has a dark side; a "leadership shadow" that often creates an unknown; lurking fear.'
const jsonata = require('jsonata-extended');
const expr = jsonata('$htmltotext("<p>Leadership has a dark side; a &#34;leadership shadow&#34; that often creates an unknown; lurking fear.</p>")');
const result = expr.evaluate();
Parameters:
Name Type Attributes Default Description
value String The HTML string to convert
options Object <optional>
{} The options object. For more information see the html-to-text NPM
Properties
Name Type Attributes Default Description
tables Array.<String> | String | Boolean <optional>
[] allows to select certain tables by the 'class' or 'id' attribute from the HTML document. This is necessary because the majority of HTML E-Mails uses a table based layout. Prefix your table selectors with an '.' for the 'class' and with a '#' for the 'id' attribute. All other tables are ignored. You can assign 'true' to this attribute to select all tables.
wordwrap Number <optional>
80 defines after how many chars a line break should follow in 'p' elements. Set to 'null' or 'false' to disable word-wrapping.
linkHrefBaseUrl String <optional>
null allows you to specify the server host for href attributes, where the links start at the root ('/'). For example, 'linkHrefBaseUrl = 'http://asdf.com'' and '...' the link in the text will be 'http://asdf.com/dir/subdir'. Keep in mind that 'linkHrefBaseUrl' shouldn't end with a '/'.
hideLinkHrefIfSameAsText Boolean <optional>
false allows you to specify the server host for href attributes, where the links start at the root ('/'). For example, 'linkHrefBaseUrl = 'http://asdf.com'' and '...' the link in the text will be 'http://asdf.com/dir/subdir'. Keep in mind that 'linkHrefBaseUrl' shouldn't end with a '/'.
noLinkBrackets Boolean <optional>
false dont print brackets around the link if 'true'
ignoreHref Boolean <optional>
false ignore all document links if 'true'
ignoreImage Boolean <optional>
false ignore all document images if 'true'.
preserveNewlines Boolean <optional>
false by default, any newlines \n in a block of text will be removed. If true, these newlines will not be removed.
uppercaseHeadings Boolean <optional>
true by default, headings (h1, h2, etc) are uppercased. Set to 'false' to leave headings as they are.
singleNewLineParagraphs Boolean <optional>
false by default, paragraphs are converted with two newlines ('\n\n'). Set to 'true' to convert to a single newline.
baseElement String | Array.<String> <optional>
'body' defines the tags whose text content will be captured from the html. All content will be captured below the baseElement tags and added to the resulting text output. This option allows the user to specify an array of elements as base elements using a single tag with css class and id parameters e.g. ['p.class1.class2#id1#id2', 'p.class1.class2#id1#id2'].
returnDomByDefault Boolean <optional>
true onvert the entire document if we don't find the tag we're looking for if 'true'
unorderedListItemPrefix String <optional>
' * ' defines the string that is used as item prefix for unordered lists '<ol>'
longWordSplit Object <optional>
{} describes how to wrap long words
Properties
Name Type Attributes Default Description
wrapCharacters Array.<String> <optional>
[] is an array containing the characters that may be wrapped on, these are used in order
forceWrapOnLimit Boolean <optional>
false defines whether to break long words on the limit if 'true'
decodeOptions Object <optional>
{} defines the text decoding options given to 'he.decode'. For more informations see the HE NPM.
Properties
Name Type Attributes Default Description
isAttributeValue Boolean <optional>
false false means that decode() will decode the string as if it were used in a text context in an HTML document. HTML has different rules for parsing character references in attribute values — set this option to true to treat the input string as if it were used as an attribute value.
strict Boolean <optional>
false false means that decode() will decode any HTML text content you feed it, even if it contains any entities that cause parse errors. To throw an error when such invalid HTML is encountered, set the strict option to true. This option makes it possible to use he as part of HTML parsers and HTML validators.
format Object <optional>
{}} pass an object to enable custom formatting for specific elements. By using the format option, you can specify formatting for these elements: text, image, lineBreak, paragraph, anchor, heading, table, orderedList, unorderedList, listItem, horizontalLine. Each key must be a function which eventually receive elem (the current elem), fn (the next formatting function) and options (the options passed to html-to-text).
whitespaceCharacters String <optional>
' \t\r\n\f\u200b\u00a0' A string of characters that are recognized as HTML whitespace.
Returns:
Returns a string of the plain text, or undefined for undefined input
Type
String | Void

(private, inner) languageInfo(value) → {Array.<Object>|Void}

Source:
See:
Get detailed information based on an RFC5646 tag for region and language
Examples

ample usage within a JSONata transform to get detailed info with single tag with language only no locale

// returns
// result = {
//     "rfc5646": "es",
//     "region": null,
//     "language": {
//       "name": "Spanish",
//       "native": "Español"
//     }
//   }
// }
const jsonata = require('jsonata-extended');
const expr = jsonata('$languageInfo("es")');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = {
//     "rfc5646": "es",
//     "region": null,
//     "language": {
//       "name": "Spanish",
//       "native": "Español"
//     }
//   }
// }
const jsonata = require('jsonata-extended');
const expr = jsonata('$languageInfo("es")');
const result = expr.evaluate();

Example usage within a JSONata transform to get the region native language name for a locale

// returns
// result = "中文"
const jsonata = require('jsonata-extended');
const expr = jsonata('$languageInfo("zh-CN").language.native');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = "中文"
const jsonata = require('jsonata-extended');
const expr = jsonata('$languageInfo("zh-CN").language.native');
const result = expr.evaluate();

Example usage within a JSONata transform to get the information for three locales

// returns
// result = [
//      {
//         "region": {
//          "name": "United States",
//          "native": "United States",
//          "phone": "1",
//          "continent": "NA",
//          "capital": "Washington D.C.",
//          "currency": "USD,USN,USS",
//          "languages": [
//            "en"
//          ],
//          "emoji": "🇺🇸",
//          "emojiU": "U+1F1FA U+1F1F8"
//        },
//        "language": {
//          "name": "English",
//          "native": "English"
//        },
//        "rfc5646": "en-US"
//      },
//      {
//        "region": {
//          "name": "France",
//          "native": "France",
//          "phone": "33",
//          "continent": "EU",
//          "capital": "Paris",
//          "currency": "EUR",
//          "languages": [
//            "fr"
//          ],
//          "emoji": "🇫🇷",
//          "emojiU": "U+1F1EB U+1F1F7"
//        },
//        "language": {
//          "name": "French",
//          "native": "Français"
//        },
//        "rfc5646": "fr-FR"
//      },
//      {
//        "region": {
//          "name": "China",
//          "native": "中国",
//          "phone": "86",
//          "continent": "AS",
//          "capital": "Beijing",
//          "currency": "CNY",
//          "languages": [
//            "zh"
//          ],
//          "emoji": "🇨🇳",
//          "emojiU": "U+1F1E8 U+1F1F3"
//        },
//        "language": {
//          "name": "Chinese",
//          "native": "中文"
//        },
//        "rfc5646": "zh-CN"
//      }
//    ]
const jsonata = require('jsonata-extended');
const expr = jsonata('( $allLocales:= ["en-US", "fr-FR", "zh-CN"]; $map($allLocales, function($v, $i, $a) { $languageInfo($v) }) )');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = [
//      {
//         "region": {
//          "name": "United States",
//          "native": "United States",
//          "phone": "1",
//          "continent": "NA",
//          "capital": "Washington D.C.",
//          "currency": "USD,USN,USS",
//          "languages": [
//            "en"
//          ],
//          "emoji": "🇺🇸",
//          "emojiU": "U+1F1FA U+1F1F8"
//        },
//        "language": {
//          "name": "English",
//          "native": "English"
//        },
//        "rfc5646": "en-US"
//      },
//      {
//        "region": {
//          "name": "France",
//          "native": "France",
//          "phone": "33",
//          "continent": "EU",
//          "capital": "Paris",
//          "currency": "EUR",
//          "languages": [
//            "fr"
//          ],
//          "emoji": "🇫🇷",
//          "emojiU": "U+1F1EB U+1F1F7"
//        },
//        "language": {
//          "name": "French",
//          "native": "Français"
//        },
//        "rfc5646": "fr-FR"
//      },
//      {
//        "region": {
//          "name": "China",
//          "native": "中国",
//          "phone": "86",
//          "continent": "AS",
//          "capital": "Beijing",
//          "currency": "CNY",
//          "languages": [
//            "zh"
//          ],
//          "emoji": "🇨🇳",
//          "emojiU": "U+1F1E8 U+1F1F3"
//        },
//        "language": {
//          "name": "Chinese",
//          "native": "中文"
//        },
//        "rfc5646": "zh-CN"
//      }
//    ]
const jsonata = require('jsonata-extended');
const expr = jsonata('( $allLocales:= ["en-US", "fr-FR", "zh-CN"]; $map($allLocales, function($v, $i, $a) { $languageInfo($v) }) )');
const result = expr.evaluate();

Example usage within a JSONata transform to get a new object with the RFC5646 tag, and a string 'region (language)' representing the native locale for three locales

// returns
// result = [
//        {
//        "rfc5646": "en-US",
//        "locale": "United States (English)"
//        },
//        {
//        "rfc5646": "fr-FR",
//        "locale": "France (Français)"
//        },
//        {
//        "rfc5646": "zh-CN",
//        "locale": "中国 (中文)"
//        }
//      ]
const jsonata = require('jsonata-extended');
const expr = jsonata('( $allLocales:= ["en-US", "fr-FR", "zh-CN"]; $map($allLocales, function($v, $i, $a) { ( $lang := $languageInfo($v); { "rfc5646": $lang.rfc5646, "locale" : $lang.region.native & " (" & $lang.language.native &")" })}) )');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = [
//        {
//        "rfc5646": "en-US",
//        "locale": "United States (English)"
//        },
//        {
//        "rfc5646": "fr-FR",
//        "locale": "France (Français)"
//        },
//        {
//        "rfc5646": "zh-CN",
//        "locale": "中国 (中文)"
//        }
//      ]
const jsonata = require('jsonata-extended');
const expr = jsonata('( $allLocales:= ["en-US", "fr-FR", "zh-CN"]; $map($allLocales, function($v, $i, $a) { ( $lang := $languageInfo($v); { "rfc5646": $lang.rfc5646, "locale" : $lang.region.native & " (" & $lang.language.native &")" })}) )');
const result = expr.evaluate();
Parameters:
Name Type Description
value String The RFC5646 locales to retrieve detailed information on
Throws:
Will throw an error if the value is not a valid RFC5646 language tag
Returns:
object contain details on the region and language, region is null if language only passed. Returns undefined for undefined input
Type
Array.<Object> | Void

(private, inner) moment(…args) → {Object}

Source:
See:
Wrapper around MomentJS Moment NPM
Examples

Example usage within a JSONata transform to get the year of a ISO8601 string

// returns
// result = "2017"
const jsonata = require('jsonata-extended');
const expr = jsonata('$moment("2017-02-02T15:49:06Z").year()');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = "2017"
const jsonata = require('jsonata-extended');
const expr = jsonata('$moment("2017-02-02T15:49:06Z").year()');
const result = expr.evaluate();

Example usage within a JSONata transform to get the localised Italian month name for a date

// returns
// result = "dicembre"
const jsonata = require('jsonata-extended');
const expr = jsonata('( $momentit := $moment().locale(\"it\"); $momentit.localeData().months($moment(\"12-12-1995\",\"MM-DD-YYYY\")) )');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = "dicembre"
const jsonata = require('jsonata-extended');
const expr = jsonata('( $momentit := $moment().locale(\"it\"); $momentit.localeData().months($moment(\"12-12-1995\",\"MM-DD-YYYY\")) )');
const result = expr.evaluate();
Parameters:
Name Type Attributes Description
args * <repeatable>
The moment parse argument options
Returns:
A moment object
Type
Object

(private, inner) momentDuration(…args) → {Object}

Source:
See:
Wrapper around MomentJS Duration Moment NPM
Examples

Example usage within a JSONata transform to get the total seconds of an ISO8601 duration string

// returns
// result = 3730
const jsonata = require('jsonata-extended');
const expr = jsonata('$momentDuration(\"PT1H2M10S\").asSeconds()');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = 3730
const jsonata = require('jsonata-extended');
const expr = jsonata('$momentDuration(\"PT1H2M10S\").asSeconds()');
const result = expr.evaluate();

Example usage within a JSONata transform to convert ISO8601 duration string to hh:mm:ss

// returns
// result = "74:02:10"
const jsonata = require('jsonata-extended');
const expr = jsonata('( $duration := $momentDuration(\"P3DT2H2M10S\"); $formatNumber($floor($duration.asHours()),\"#00\") & \":\" & $formatNumber($floor($duration.minutes()),\"00\") & \":\" & $formatNumber($floor($duration.seconds()),\"00\") )');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = "74:02:10"
const jsonata = require('jsonata-extended');
const expr = jsonata('( $duration := $momentDuration(\"P3DT2H2M10S\"); $formatNumber($floor($duration.asHours()),\"#00\") & \":\" & $formatNumber($floor($duration.minutes()),\"00\") & \":\" & $formatNumber($floor($duration.seconds()),\"00\") )');
const result = expr.evaluate();
Parameters:
Name Type Attributes Description
args * <repeatable>
The moment.duration argument options
Returns:
A moment duration object
Type
Object

(private, inner) mustache(value=opt, template=opt) → {String|Void}

Source:
See:
Render a mustache template
Examples

Example usage within a JSONata transform for converting object

// returns
// result = 'Martin Holden is a Learner'
const jsonata = require('jsonata-extended');
const expr = jsonata('$mustache( { "name" : "Martin Holden" , "role" : "Learner"}, \"{{name}} is a {{role}}\")');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = 'Martin Holden is a Learner'
const jsonata = require('jsonata-extended');
const expr = jsonata('$mustache( { "name" : "Martin Holden" , "role" : "Learner"}, \"{{name}} is a {{role}}\")');
const result = expr.evaluate();

Example usage within a JSONata transform for converting more complex object

// returns
// result = '<h1>Nincompoopery: Why Your Customers Hate You--And How to Fix It</h1><h2>Authors:</h2><ul><li>John R. Brandt</li></ul><p>HarperCollins Leadership&copy;2019</p>'
const jsonata = require('jsonata-extended');
const expr = jsonata('$mustache({ "title" : "Nincompoopery: Why Your Customers Hate You--And How to Fix It", "publication" : { "copyrightYear": 2019, "isbn": "9781400213672", "publisher": "HarperCollins Leadership" } , "authors" : ["John R. Brandt"]}, "<h1>{{title}}</h1>{{#authors.length}}<h2>Authors:</h2><ul>{{#authors}}<li>{{.}}</li>{{/authors}}</ul>{{/authors.length}}{{#publication}}<p>{{publisher}}&copy;{{copyrightYear}}</p>{{/publication}}")');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = '<h1>Nincompoopery: Why Your Customers Hate You--And How to Fix It</h1><h2>Authors:</h2><ul><li>John R. Brandt</li></ul><p>HarperCollins Leadership&copy;2019</p>'
const jsonata = require('jsonata-extended');
const expr = jsonata('$mustache({ "title" : "Nincompoopery: Why Your Customers Hate You--And How to Fix It", "publication" : { "copyrightYear": 2019, "isbn": "9781400213672", "publisher": "HarperCollins Leadership" } , "authors" : ["John R. Brandt"]}, "<h1>{{title}}</h1>{{#authors.length}}<h2>Authors:</h2><ul>{{#authors}}<li>{{.}}</li>{{/authors}}</ul>{{/authors.length}}{{#publication}}<p>{{publisher}}&copy;{{copyrightYear}}</p>{{/publication}}")');
const result = expr.evaluate();
Parameters:
Name Type Attributes Description
value= Object <optional>
The object to use with the template
template= String <optional>
The template object. For more information see the mustache NPM
Returns:
Returns the resulting string, or undefined for undefined value or value for undefined template
Type
String | Void

(private, inner) parsePath(value) → {Object|Void}

Source:
See:
Parse the supplied path and return the parsed values
Examples

Example usage within a JSONata transform

// returns
// result = {
//     "base": "page.html",
//     "dir": "/p/a/t",
//     "ext": ".html",
//     "name": "page",
//     "root": "/"
// }
const jsonata = require('jsonata-extended');
const expr = jsonata('$parsePath("/p/a/t/page.html")');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = {
//     "base": "page.html",
//     "dir": "/p/a/t",
//     "ext": ".html",
//     "name": "page",
//     "root": "/"
// }
const jsonata = require('jsonata-extended');
const expr = jsonata('$parsePath("/p/a/t/page.html")');
const result = expr.evaluate();

Example usage within a JSONata transform to get the file extension

// returns
// result = ".html"

const jsonata = require('jsonata-extended');
const expr = jsonata('$parsePath("/p/a/t/page.html").ext');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = ".html"

const jsonata = require('jsonata-extended');
const expr = jsonata('$parsePath("/p/a/t/page.html").ext');
const result = expr.evaluate();
Parameters:
Name Type Description
value String Properly formatted path
Throws:
Will throw an error if the value is not a valid url
Returns:
Returns an object containing the parsed components of the path, or undefined for undefined input
Type
Object | Void

(private, inner) parseUrl(value) → {Object|Void}

Source:
See:
Parse the supplied URL and return the parsed values
Examples

Example usage within a JSONata transform

// returns
// result = {
//   "slashes": true,
//   "protocol": "http:",
//   "hash": "#hash",
//   "query": {
//     "param1": "string",
//     "param2": "true"
//   },
//   "pathname": "/p/a/t/page.html",
//   "auth": "user:pass",
//   "host": "host.com:8080",
//   "port": "8080",
//   "hostname": "host.com",
//   "password": "pass",
//   "username": "user",
//   "origin": "http://host.com:8080",
//   "href": "http://user:pass@host.com:8080/p/a/t/page.html?param1=string&param2=true#hash"
// }
const jsonata = require('jsonata-extended');
const expr = jsonata('$parseUrl("http://user:pass@host.com:8080/p/a/t/page.html?param1=string&param2=true#hash")');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = {
//   "slashes": true,
//   "protocol": "http:",
//   "hash": "#hash",
//   "query": {
//     "param1": "string",
//     "param2": "true"
//   },
//   "pathname": "/p/a/t/page.html",
//   "auth": "user:pass",
//   "host": "host.com:8080",
//   "port": "8080",
//   "hostname": "host.com",
//   "password": "pass",
//   "username": "user",
//   "origin": "http://host.com:8080",
//   "href": "http://user:pass@host.com:8080/p/a/t/page.html?param1=string&param2=true#hash"
// }
const jsonata = require('jsonata-extended');
const expr = jsonata('$parseUrl("http://user:pass@host.com:8080/p/a/t/page.html?param1=string&param2=true#hash")');
const result = expr.evaluate();

Example usage within a JSONata transform to get the hostname

// returns
// result = "host.com"

const jsonata = require('jsonata-extended');
const expr = jsonata('$parseUrl("http://user:pass@host.com:8080/p/a/t/page.html?param1=string&param2=true#hash").hostname');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = "host.com"

const jsonata = require('jsonata-extended');
const expr = jsonata('$parseUrl("http://user:pass@host.com:8080/p/a/t/page.html?param1=string&param2=true#hash").hostname');
const result = expr.evaluate();
Parameters:
Name Type Description
value String Properly formatted URL
Throws:
Will throw an error if the value is not a valid url
Returns:
Returns an object containing the parsed components of the URL, or undefined for undefined input
Type
Object | Void

(private, inner) registerWithJSONATA(expression)

Source:
See:
Register the functions in this library to the JSONata expression
Example

Example usage to add the functions in this library to a JSONata expression object, and utilise them

const jsonata = require('jsonata');
const jsonataFunctions = require('jsonata-functions');

const expression = jsonata('$htmltotext("<p>Leadership has a dark side; a &#34;leadership shadow&#34; that often creates an unknown; lurking fear.</p>")');
jsonataFunctions.registerWithJSONATA(expression);
const result = expression.evaluate();
Try on RUNKIT
const jsonata = require('jsonata');
const jsonataFunctions = require('jsonata-functions');

const expression = jsonata('$htmltotext("<p>Leadership has a dark side; a &#34;leadership shadow&#34; that often creates an unknown; lurking fear.</p>")');
jsonataFunctions.registerWithJSONATA(expression);
const result = expression.evaluate();
Parameters:
Name Type Description
expression Object The JSONata Expression Object JSONata NPM
Throws:
expression must be a JSONata expression object
Type
TypeError

(private, inner) shortenUuid(value, baseopt) → {String|Void}

Source:
See:
Shorten the supplied UUID
Example

Example usage within a JSONata transform

// returns
// result = '1m5otdkthiyq143crwujacdqg'
const jsonata = require('jsonata-extended');
const expr = jsonata('$shortenUuid("1b49aa30-e719-11e6-9835-f723b46a2688", "base36")');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = '1m5otdkthiyq143crwujacdqg'
const jsonata = require('jsonata-extended');
const expr = jsonata('$shortenUuid("1b49aa30-e719-11e6-9835-f723b46a2688", "base36")');
const result = expr.evaluate();
Parameters:
Name Type Attributes Default Description
value String Properly formatted UUID
base 'base2' | 'base10' | 'base16' | 'base32' | 'base36' | 'base58' | 'base62' | 'base64' | 'base64url' <optional>
'base36' A valid base to use for the process, case sensitive Encoding Options
Throws:
  • Will throw an error if the value is not a valid uuid
  • Will throw an error if the base is not a valid option
Returns:
Returns a string containing the encoded version of the uuid, or undefined for undefined input
Type
String | Void

(private, inner) truncate(valueopt, optionsopt) → {String|Void}

Source:
Truncate the string
Examples

Example usage within a JSONata transform for length of 13

// returns
// result = '1234567890...'
const jsonata = require('jsonata-extended');
const expr = jsonata('$truncate("1234567890123456789012345678901234567890", {"length": 13, "omission": "..."})');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = '1234567890...'
const jsonata = require('jsonata-extended');
const expr = jsonata('$truncate("1234567890123456789012345678901234567890", {"length": 13, "omission": "..."})');
const result = expr.evaluate();

Example usage within a JSONata transform for length of 20, and with a list of wordbreaks (these will be trimmed from end if present)

// returns
// result = '1234567890123456....'
const jsonata = require('jsonata-extended');
const expr = jsonata('$truncate('1234567890123456 , 789012345678901234567890', { 'length': 20 , 'omission': '...', 'wordbreakregex': '[\\\\s\\\\.%,:;\\\\?_]'})');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = '1234567890123456....'
const jsonata = require('jsonata-extended');
const expr = jsonata('$truncate('1234567890123456 , 789012345678901234567890', { 'length': 20 , 'omission': '...', 'wordbreakregex': '[\\\\s\\\\.%,:;\\\\?_]'})');
const result = expr.evaluate();

Example usage within a JSONata transform for length of 20, and with a single wordbreak (this will be trimmed from end if present)

// returns
// result = '1234567890123456....'
const jsonata = require('jsonata-extended');
const expr = jsonata('$truncate('1234567890123456 , 789012345678901234567890', { 'length': 20 , 'omission': '...', 'wordbreak': ' ' })');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = '1234567890123456....'
const jsonata = require('jsonata-extended');
const expr = jsonata('$truncate('1234567890123456 , 789012345678901234567890', { 'length': 20 , 'omission': '...', 'wordbreak': ' ' })');
const result = expr.evaluate();
Parameters:
Name Type Attributes Default Description
value String <optional>
'' The string to truncate.
options Object <optional>
{} The options object.
Properties
Name Type Attributes Default Description
length number <optional>
30 The maximum string length.
omission String <optional>
'...' The string to indicate text is omitted.
wordbreak String <optional>
The wordbreak strings to truncate to.
wordbreakregex String <optional>
The string representation of the regex
Returns:
Returns the truncated string, or undefined for undefined input
Type
String | Void

(private, inner) unicodeToASCII(valueopt, optionsopt) → {String|Void}

Source:
See:
Convert UNICODE String to ASCII Converts alphabetic, numeric, and symbolic Unicode characters which are not in the first 127 ASCII characters (the "Basic Latin" Unicode block) into their ASCII equivalents.
Examples

Example usage within a JSONata transform

// returns
// result = 'Lorem 🤧 eripuit'
const jsonata = require('jsonata-extended');
const expr = jsonata('$unicodeToASCII("Lörem 🤧 ëripuît")');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = 'Lorem 🤧 eripuit'
const jsonata = require('jsonata-extended');
const expr = jsonata('$unicodeToASCII("Lörem 🤧 ëripuît")');
const result = expr.evaluate();

Example usage within a JSONata transform with replacement of Unknown characters

// returns
// result = 'Lorem  eripuit'
const jsonata = require('jsonata-extended');
const expr = jsonata('$unicodeToASCII("Lörem 🤧 ëripuît", { "replacement": ""})');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = 'Lorem  eripuit'
const jsonata = require('jsonata-extended');
const expr = jsonata('$unicodeToASCII("Lörem 🤧 ëripuît", { "replacement": ""})');
const result = expr.evaluate();

Example usage within a JSONata transform with replacement of Unknown characters, most UNICODE characters are 2 bytes long the UTF8 Encoding for this emoji is 0xF0 0x9F 0xA4 0xA7

// returns
// result = 'Lorem XX eripuit'
const jsonata = require('jsonata-extended');
const expr = jsonata('$unicodeToASCII("Lörem 🤧 ëripuît", { "replacement": "X"})');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = 'Lorem XX eripuit'
const jsonata = require('jsonata-extended');
const expr = jsonata('$unicodeToASCII("Lörem 🤧 ëripuît", { "replacement": "X"})');
const result = expr.evaluate();
Parameters:
Name Type Attributes Default Description
value String <optional>
'' The UNICODE string to convert.
options Object <optional>
{} The options object.
Properties
Name Type Attributes Default Description
replacement String <optional>
Void The string to replace unknown UNICODE characters with, each byte of the UNICODE string is replaced if Void then unknown characters are maintained.
Returns:
Returns the truncated string, or undefined for undefined input
Type
String | Void

(private, inner) unshortenUuid(value, baseopt) → {String|Void}

Source:
See:
UnShorten the supplied shortened UUID
Example

Example usage within a JSONata transform

// returns
// result = '1b49aa30-e719-11e6-9835-f723b46a2688'
const jsonata = require('jsonata-extended');
const expr = jsonata('$unshortenUuid("1m5otdkthiyq143crwujacdqg", "base36")');
const result = expr.evaluate();
Try on RUNKIT
// returns
// result = '1b49aa30-e719-11e6-9835-f723b46a2688'
const jsonata = require('jsonata-extended');
const expr = jsonata('$unshortenUuid("1m5otdkthiyq143crwujacdqg", "base36")');
const result = expr.evaluate();
Parameters:
Name Type Attributes Default Description
value String Shortened UUID
base 'base2' | 'base10' | 'base16' | 'base32' | 'base36' | 'base58' | 'base62' | 'base64' | 'base64url' <optional>
'base36' A valid base to use for the process, case sensitive Encoding Options
Throws:
  • Will throw an error if the value is not a valid uuid
  • Will throw an error if the base is not a valid option
Returns:
Returns a string containing the encoded version of the uuid, or undefined for undefined input
Type
String | Void