Appearance
informat.utils 其它工具函数
概述
使用informat.utils执行工具类操作
getPinyin
返回拼音
javascript
informat.utils.getPinyin(str, separator)| 参数 | 类型 | 描述 |
|---|---|---|
| str | String | 要转换的字符串 |
| separator | String | 分隔符 |
返回值
返回拼音
js
informat.utils.getPinyin('你好,世界',',');json
"ni,hao,,,shi,jie"TIP
注意:符号不会被解析,会被保留输出,例如上述的逗号
getShortPinyin
返回拼音首字母
javascript
informat.utils.getShortPinyin(str, separator)| 参数 | 类型 | 描述 |
|---|---|---|
| str | String | 要转换的字符串 |
| separator | String | 分隔符 |
返回值
返回拼音首字母
示例
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)| 参数 | 类型 | 描述 |
|---|---|---|
| obj | Object | 要转换的对象 |
返回值
返回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)| 参数 | 类型 | 描述 |
|---|---|---|
| html | String | html格式文本 |
返回值
返回纯文本格式文本
示例:
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)| 参数 | 类型 | 描述 |
|---|---|---|
| str | String | 要转换的字符串 |
| charset | String | 字符集,可选项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
-107bytesToString
将byte数组转换为String
javascript
informat.utils.bytesToString(data, charset)| 参数 | 类型 | 描述 |
|---|---|---|
| data | Array<Byte> | 要转换的字符串 |
| charset | String | 字符集,可选项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)| 参数 | 类型 | 描述 |
|---|---|---|
| jsonA | String | json字符串a |
| jsonB | String | json字符串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)| 参数 | 类型 | 描述 |
|---|---|---|
| json | String | json字符串 |
| patch | String | json补丁 |
返回值
返回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)| 参数 | 类型 | 描述 |
|---|---|---|
| url | String | url地址 |
返回值
返回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%3DprogrammingurlDecode
URL解码,将经过URL编码的字符串恢复为其原始形式
javascript
informat.utils.urlDecode(encodedUrl)| 参数 | 类型 | 描述 |
|---|---|---|
| encodedUrl | String | 编码后的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=programmingescape
字符串转义
javascript
informat.utils.escape(content)| 参数 | 类型 | 描述 |
|---|---|---|
| content | String | 字符串 |
返回值
返回String
示例
js
// 假设输入的字符串包含特殊字符,需要进行转义
let inputString = 'This is a <div> tag';
informat.utils.escape(inputString);This%20is%20a%20%3cdiv%3e%20tagunescape
字符串反转义
javascript
informat.utils.unescape(escapedString)| 参数 | 类型 | 描述 |
|---|---|---|
| escapedString | String | 转义后的字符串 |
返回值
返回String
示例
js
// 假设输入的字符串包含转义字符,需要反转义
var escapedString = 'This%20is%20a%20%3cdiv%3e%20tag';
informat.utils.unescape(escapedString);json
This is a <div> tagjs2java
将js对象转化为java对象
javascript
informat.utils.js2java(jsObject)| 参数 | 类型 | 描述 |
|---|---|---|
| jsObject | Object | js对象 |
返回值
返回Object
示例
js
informat.utils.js2java({
id:"1",
name:"test"
});json
{name=test, id=1}sleep
当前线程休眠
javascript
informat.utils.sleep(millis)| 参数 | 类型 | 描述 |
|---|---|---|
| millis | Integer | 毫秒 |
示例
javascript
informat.utils.sleep(5000);//休眠5秒htmlToMarkdown
html转化为Markdown
javascript
informat.utils.htmlToMarkdown(html)| 参数 | 类型 | 描述 |
|---|---|---|
| html | String | html内容 |
示例
js
let html='<h3>Hello Informat!</h3>';
informat.utils.htmlToMarkdown(html);json
### Hello Informat!markdownToHtml
markdown转化为Html
javascript
informat.utils.markdownToHtml(markdown)| 参数 | 类型 | 描述 |
|---|---|---|
| markdown | String | markdown内容 |
示例1 文本
js
let markdown='This is *Sparta*';
informat.utils.markdownToHtml(markdown);json
<p>This is <em>Sparta</em></p>示例2 表格
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>
