Skip to content

informat.utils 其它工具函数

概述

使用informat.utils执行工具类操作


getPinyin

返回拼音

javascript
informat.utils.getPinyin(str,separator)
参数类型描述
strString要转换的字符串
separatorString分隔符

返回值

返回拼音

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

TIP

注意:符号不会被解析,会被保留输出,例如上述的逗号

getShortPinyin

返回拼音首字母

javascript
informat.utils.getShortPinyin(str,separator)
参数类型描述
strString要转换的字符串
separatorString分隔符

返回值

返回拼音首字母

示例

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

randomUUID

返回随机的UUID

javascript
informat.utils.randomUUID()

返回值

返回随机的UUID

示例:随机生成一个UUID

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

getEnv

返回系统的环境变量

javascript
informat.utils.getEnv()

返回值 类型Object<String,String>

返回系统环境变量

示例:返回用户当前的系统环境变量

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

将对象转换为JSON字符串

javascript
informat.utils.toJSON(obj)
参数类型描述
objObject要转换的对象

返回值

返回JSON字符串

示例:查询数据表记录,并将结果转换为JSON字符串

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

HTML格式文本转换为纯文本格式

javascript
informat.utils.html2text(html)
参数类型描述
htmlStringhtml格式文本

返回值

返回纯文本格式文本

示例:

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

stringToBytes

将字符串转换为byte数组

javascript
informat.utils.stringToBytes(str, charset)
参数类型描述
strString要转换的字符串
charsetString字符集,可选项UTF-8,ISO-8859-1,GBK等

返回值

返回Array<Byte>

示例

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

bytesToString

将byte数组转换为String

javascript
informat.utils.bytesToString(data, charset)
参数类型描述
dataArray<Byte>要转换的字符串
charsetString字符集,可选项UTF-8,ISO-8859-1,GBK等

返回值

返回String

示例

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

jsonDiff

计算两个json字符串的差异

javascript
informat.utils.jsonDiff(jsonA, jsonB)
参数类型描述
jsonAStringjson字符串a
jsonBStringjson字符串b

返回值

返回String

示例

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

jsonPatch

json补丁

javascript
informat.utils.jsonPatch(json, patch)
参数类型描述
jsonStringjson字符串
patchStringjson补丁

返回值

返回String

示例

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

urlEncode

URL编码,将特殊字符转换为可在URL中安全传输的编码方式

javascript
informat.utils.urlEncode(url)
参数类型描述
urlStringurl地址

返回值

返回String

示例

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解码,将经过URL编码的字符串恢复为其原始形式

javascript
informat.utils.urlDecode(encodedUrl)
参数类型描述
encodedUrlString编码后的url地址

返回值

返回String

示例

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

字符串转义

javascript
informat.utils.escape(content)
参数类型描述
contentString字符串

返回值

返回String

示例

js
// 假设输入的字符串包含特殊字符,需要进行转义
let inputString = 'This is a <div> tag';
informat.utils.escape(inputString);
This%20is%20a%20%3cdiv%3e%20tag

unescape

字符串反转义

javascript
informat.utils.unescape(escapedString)
参数类型描述
escapedStringString转义后的字符串

返回值

返回String

示例

js
// 假设输入的字符串包含转义字符,需要反转义
var escapedString = 'This%20is%20a%20%3cdiv%3e%20tag';
informat.utils.unescape(escapedString);
json
This is a <div> tag

js2java

将js对象转化为java对象

javascript
informat.utils.js2java(jsObject)
参数类型描述
jsObjectObjectjs对象

返回值

返回Object

示例

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

sleep

当前线程休眠

javascript
informat.utils.sleep(millis)
参数类型描述
millisInteger毫秒

示例

javascript
informat.utils.sleep(5000);//休眠5秒

htmlToMarkdown

html转化为Markdown

javascript
informat.utils.htmlToMarkdown(html)
参数类型描述
htmlStringhtml内容

示例

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

markdownToHtml

markdown转化为Html

javascript
informat.utils.markdownToHtml(markdown)
参数类型描述
markdownStringmarkdown内容

示例

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