Skip to content

informat.utils Other Utility Functions

Overview

Use informat.utils to perform utility operations


getPinyin

Returns pinyin

javascript
informat.utils.getPinyin(str, separator);
ParameterTypeDescription
strStringString to convert
separatorStringSeparator

Return Value

Returns pinyin

js
informat.utils.getPinyin('你好,世界',',');
json
"ni,hao,,,shi,jie"

TIP

Note: Symbols are not parsed and will be preserved in the output, such as the comma above

getShortPinyin

Returns pinyin initials

javascript
informat.utils.getShortPinyin(str, separator);
ParameterTypeDescription
strStringString to convert
separatorStringSeparator

Return Value

Returns pinyin initials

Example

js
informat.utils.getShortPinyin('你好,世界',',');
json
"n,h,,,s,j"

randomUUID

Returns a random UUID

javascript
informat.utils.randomUUID();

Return Value

Returns a random UUID

Example: Generate a random UUID

js
informat.utils.randomUUID();
json
"abeb540f-b4ff-45e3-89a0-151ed4d38a40"

getEnv

Returns system environment variables

javascript
informat.utils.getEnv();

Return Value TypeObject<String,String>

Returns system environment variables

Example: Return the user's current system environment variables

js
informat.utils.getEnv();
json
{
  "PATH": "/usr/local/bin:/usr/bin",
  "LESSOPEN": "||/usr/bin/lesspipe.sh %s",
  "SHELL": "/bin/bash",
  "HISTSIZE": "3000",
  "SSH_TTY": "/dev/pts/4"
}

toJSON

Convert an object to a JSON string

javascript
informat.utils.toJSON(obj);
ParameterTypeDescription
objObjectObject to convert

Return Value

Returns a JSON string

Example: Query a data table record and convert the result to a JSON string

js
let record=informat.table.queryById('staffs','yhg8b23ej2gt6');
informat.utils.toJSON(record);
json
{
  "id": "yhg8b23ej2gt6",
  "seq": 1,
  "createTime": 1700796135362,
  "grade": 5,
  "name": "李四",
  "updateUser_avatar": "pic7.png",
  "createUser": "skydu",
  "createUser_avatar": "79a487eb7f384321bfaed16eb9e020d8.jpg",
  "deptRel": "fh1snm1k18kqc",
  "age": 28,
  "status": "onjob"
}

html2text

Convert HTML formatted text to plain text

javascript
informat.utils.html2text(html);
ParameterTypeDescription
htmlStringHTML formatted text

Return Value

Returns plain text

Example:

js
let html='<html><script></script><body><h1>Hello World</h1></body>'
informat.utils.html2text(html);
json
"Hello World"

stringToBytes

Convert string to byte array

javascript
informat.utils.stringToBytes(str, charset);
ParameterTypeDescription
strStringString to convert
charsetStringCharacter set, optional UTF-8, ISO-8859-1, GBK, etc.

Return Value

ReturnsArray<Byte>

Example

js
var bytes=informat.utils.stringToBytes('测试','UTF-8');
bytes.forEach(item=>{
    console.log(item)
})
js
-26
-75
-117
-24
-81
-107

bytesToString

Convert byte array to String

javascript
informat.utils.bytesToString(data, charset);
ParameterTypeDescription
dataArray<Byte>String to convert
charsetStringCharacter set, optional UTF-8, ISO-8859-1, GBK, etc.

Return Value

ReturnsString

Example

js
let bytes=informat.utils.stringToBytes('测试','UTF-8');
informat.utils.bytesToString(bytes, 'UTF-8');
json
测试

jsonDiff

Calculate the difference between two JSON strings

javascript
informat.utils.jsonDiff(jsonA, jsonB);
ParameterTypeDescription
jsonAStringJSON string a
jsonBStringJSON string b

Return Value

ReturnsString

Example

js
informat.utils.jsonDiff('{\"a\": 0,\"b\": [1,2]}','{\"b\": [1,2,0]}');
json
[{"op":"move","path":"/b/-","from":"/a"}]

jsonPatch

JSON patch

javascript
informat.utils.jsonPatch(json, patch);
ParameterTypeDescription
jsonStringJSON string
patchStringJSON patch

Return Value

ReturnsString

Example

js
informat.utils.jsonPatch('{\"a\": 0,\"b\": [1,2]}','[{"op":"move","path":"/b/-","from":"/a"}]');
json
newJson {"b":[1,2,0]}

urlEncode

URL encoding, converts special characters to encoding that can be safely transmitted in URLs

javascript
informat.utils.urlEncode(url);
ParameterTypeDescription
urlStringURL address

Return Value

ReturnsString

Example

js
const originalUrl = "https://www.example.com/search?q=hello world&category=programming";
informat.utils.urlEncode(originalUrl);
json
https%3A%2F%2Fwww.example.com%2Fsearch%3Fq%3Dhello+world%26category%3Dprogramming

urlDecode

URL decoding, restores URL-encoded strings to their original form

javascript
informat.utils.urlDecode(encodedUrl);
ParameterTypeDescription
encodedUrlStringEncoded URL address

Return Value

ReturnsString

Example

js
const encodedUrl = "https%3A%2F%2Fwww.example.com%2Fsearch%3Fq%3Dhello%20world%26category%3Dprogramming";
informat.utils.urlDecode(encodedUrl);
json
https://www.example.com/search?q=hello world&category=programming

escape

String escape

javascript
informat.utils.escape(content);
ParameterTypeDescription
contentStringString

Return Value

ReturnsString

Example

js
// Assume the input string contains special characters that need to be escaped
let inputString = 'This is a <div> tag';
informat.utils.escape(inputString);
This%20is%20a%20%3cdiv%3e%20tag

unescape

String unescape

javascript
informat.utils.unescape(escapedString);
ParameterTypeDescription
escapedStringStringEscaped string

Return Value

ReturnsString

Example

js
// Assume the input string contains escaped characters that need to be unescaped
var escapedString = 'This%20is%20a%20%3cdiv%3e%20tag';
informat.utils.unescape(escapedString);
json
This is a <div> tag

js2java

Convert JS object to Java object

javascript
informat.utils.js2java(jsObject);
ParameterTypeDescription
jsObjectObjectJS object

Return Value

ReturnsObject

Example

js
informat.utils.js2java({
    id:"1",
    name:"test"
});
json
{name=test, id=1}

sleep

Sleep current thread

javascript
informat.utils.sleep(millis);
ParameterTypeDescription
millisIntegerMilliseconds

Example

javascript
informat.utils.sleep(5000); //Sleep for 5 seconds

htmlToMarkdown

Convert HTML to Markdown

javascript
informat.utils.htmlToMarkdown(html);
ParameterTypeDescription
htmlStringHTML content

Example

js
let html='<h3>Hello Informat!</h3>';
informat.utils.htmlToMarkdown(html);
json
### Hello Informat!

markdownToHtml

Convert Markdown to HTML

javascript
informat.utils.markdownToHtml(markdown);
ParameterTypeDescription
markdownStringMarkdown content

Example 1 Text

js
let markdown='This is *Sparta*';
informat.utils.markdownToHtml(markdown);
json
<p>This is <em>Sparta</em></p>

Example 2 Table

js
let md = `| 标题1 | 标题2 |
|---|---|
|内容1|内容2|
`
informat.utils.markdownToHtml(md);
json
<table> <thead> <tr><th>标题1</th><th>标题2</th></tr> </thead> <tbody> <tr><td>内容1</td><td>内容2</td></tr> </tbody> </table>