informat.utils Other Utility Functions
Overview
Use informat.utils to perform utility operations
getPinyin
Returns pinyin
informat.utils.getPinyin(str, separator);| Parameter | Type | Description |
|---|---|---|
| str | String | String to convert |
| separator | String | Separator |
Return Value
Returns pinyin
informat.utils.getPinyin('你好,世界',',');"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
informat.utils.getShortPinyin(str, separator);| Parameter | Type | Description |
|---|---|---|
| str | String | String to convert |
| separator | String | Separator |
Return Value
Returns pinyin initials
Example
informat.utils.getShortPinyin('你好,世界',',');"n,h,,,s,j"randomUUID
Returns a random UUID
informat.utils.randomUUID();Return Value
Returns a random UUID
Example: Generate a random UUID
informat.utils.randomUUID();"abeb540f-b4ff-45e3-89a0-151ed4d38a40"getEnv
Returns system environment variables
informat.utils.getEnv();Return Value TypeObject<String,String>
Returns system environment variables
Example: Return the user's current system environment variables
informat.utils.getEnv();{
"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
informat.utils.toJSON(obj);| Parameter | Type | Description |
|---|---|---|
| obj | Object | Object to convert |
Return Value
Returns a JSON string
Example: Query a data table record and convert the result to a JSON string
let record=informat.table.queryById('staffs','yhg8b23ej2gt6');
informat.utils.toJSON(record);{
"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
informat.utils.html2text(html);| Parameter | Type | Description |
|---|---|---|
| html | String | HTML formatted text |
Return Value
Returns plain text
Example:
let html='<html><script></script><body><h1>Hello World</h1></body>'
informat.utils.html2text(html);"Hello World"stringToBytes
Convert string to byte array
informat.utils.stringToBytes(str, charset);| Parameter | Type | Description |
|---|---|---|
| str | String | String to convert |
| charset | String | Character set, optional UTF-8, ISO-8859-1, GBK, etc. |
Return Value
ReturnsArray<Byte>
Example
var bytes=informat.utils.stringToBytes('测试','UTF-8');
bytes.forEach(item=>{
console.log(item)
})-26
-75
-117
-24
-81
-107bytesToString
Convert byte array to String
informat.utils.bytesToString(data, charset);| Parameter | Type | Description |
|---|---|---|
| data | Array<Byte> | String to convert |
| charset | String | Character set, optional UTF-8, ISO-8859-1, GBK, etc. |
Return Value
ReturnsString
Example
let bytes=informat.utils.stringToBytes('测试','UTF-8');
informat.utils.bytesToString(bytes, 'UTF-8');测试jsonDiff
Calculate the difference between two JSON strings
informat.utils.jsonDiff(jsonA, jsonB);| Parameter | Type | Description |
|---|---|---|
| jsonA | String | JSON string a |
| jsonB | String | JSON string b |
Return Value
ReturnsString
Example
informat.utils.jsonDiff('{\"a\": 0,\"b\": [1,2]}','{\"b\": [1,2,0]}');[{"op":"move","path":"/b/-","from":"/a"}]jsonPatch
JSON patch
informat.utils.jsonPatch(json, patch);| Parameter | Type | Description |
|---|---|---|
| json | String | JSON string |
| patch | String | JSON patch |
Return Value
ReturnsString
Example
informat.utils.jsonPatch('{\"a\": 0,\"b\": [1,2]}','[{"op":"move","path":"/b/-","from":"/a"}]');newJson {"b":[1,2,0]}urlEncode
URL encoding, converts special characters to encoding that can be safely transmitted in URLs
informat.utils.urlEncode(url);| Parameter | Type | Description |
|---|---|---|
| url | String | URL address |
Return Value
ReturnsString
Example
const originalUrl = "https://www.example.com/search?q=hello world&category=programming";
informat.utils.urlEncode(originalUrl);https%3A%2F%2Fwww.example.com%2Fsearch%3Fq%3Dhello+world%26category%3DprogrammingurlDecode
URL decoding, restores URL-encoded strings to their original form
informat.utils.urlDecode(encodedUrl);| Parameter | Type | Description |
|---|---|---|
| encodedUrl | String | Encoded URL address |
Return Value
ReturnsString
Example
const encodedUrl = "https%3A%2F%2Fwww.example.com%2Fsearch%3Fq%3Dhello%20world%26category%3Dprogramming";
informat.utils.urlDecode(encodedUrl);https://www.example.com/search?q=hello world&category=programmingescape
String escape
informat.utils.escape(content);| Parameter | Type | Description |
|---|---|---|
| content | String | String |
Return Value
ReturnsString
Example
// 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%20tagunescape
String unescape
informat.utils.unescape(escapedString);| Parameter | Type | Description |
|---|---|---|
| escapedString | String | Escaped string |
Return Value
ReturnsString
Example
// 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);This is a <div> tagjs2java
Convert JS object to Java object
informat.utils.js2java(jsObject);| Parameter | Type | Description |
|---|---|---|
| jsObject | Object | JS object |
Return Value
ReturnsObject
Example
informat.utils.js2java({
id:"1",
name:"test"
});{name=test, id=1}sleep
Sleep current thread
informat.utils.sleep(millis);| Parameter | Type | Description |
|---|---|---|
| millis | Integer | Milliseconds |
Example
informat.utils.sleep(5000); //Sleep for 5 secondshtmlToMarkdown
Convert HTML to Markdown
informat.utils.htmlToMarkdown(html);| Parameter | Type | Description |
|---|---|---|
| html | String | HTML content |
Example
let html='<h3>Hello Informat!</h3>';
informat.utils.htmlToMarkdown(html);### Hello Informat!markdownToHtml
Convert Markdown to HTML
informat.utils.markdownToHtml(markdown);| Parameter | Type | Description |
|---|---|---|
| markdown | String | Markdown content |
Example 1 Text
let markdown='This is *Sparta*';
informat.utils.markdownToHtml(markdown);<p>This is <em>Sparta</em></p>Example 2 Table
let md = `| 标题1 | 标题2 |
|---|---|
|内容1|内容2|
`
informat.utils.markdownToHtml(md);<table> <thead> <tr><th>标题1</th><th>标题2</th></tr> </thead> <tbody> <tr><td>内容1</td><td>内容2</td></tr> </tbody> </table>
