zhao-tools
v1.0.2
Published
一款常用业务tools工具包 - 包括JSONP文本转JSON html标签按需截取 时间处理 字符处理 url解析 按需匹配字符串 replaceAll方法 等等.......
Downloads
16
Maintainers
Readme
Classes
使用实例
const ztools = require('zhao-tools');
## 网络相关
// JSONP文本转JSON
ztools.z_request.jsonptojson()
## url路径相关
// 解析路径得到内容
ztools.z_url.parseUrl()
## 时间相关
//将时间戳格式化为各种时间格式,
ztoolss.z_time.formatTimestamp()
## js相关
ztoolss.z_js.getType()
## 字符串相关
ztoolss.z_str.truncateString()//返回一个截断到指定长度的字符串,
..............replaceAll()//ES2020的replaceAll进行ES5重写
.............getAllTextBetween()//指定开始值和结束值之间的所有字符串
更多
ztools
class工具类
Kind: global class
ztools.z_request
网络请求相关
Kind: instance property of ztools
ztools.z_url
处理网址相关
Kind: instance property of ztools
ztools.z_time
处理时间格式化相关
Kind: instance property of ztools
ztools.z_js
js相关
Kind: instance property of ztools
ztools.z_str
字符串相关
Kind: instance property of ztools
ztools.z_html
html相关
Kind: instance property of ztools
ztools.z_regex
正则表达式相关
Kind: instance property of ztools
jsonptojson(jsonpStr) ⇒ object
jsonp文本 转 json对象
Kind: global function 注意,此函数只适用于标准的 JSONP 格式,即 callbackName(jsonData) 形式的字符串。如果你需要处理其他格式的跨域请求响应,请自行修改代码。 法的 JSONP 格式,则函数会返回 null。
| Param | Type | Description | | --- | --- | --- | | jsonpStr | string | jsonp格式字符串 |
parseUrl(url) ⇒ object
解析 URL,返回包含其参数键值对的对象,以及路径、查询参数、主机名
Kind: global function Returns: object - - 包含路径、查询参数、主机名和参数键值对的对象
| Param | Type | Description | | --- | --- | --- | | url | string | 要解析的 URL |
formatTimestamp(timestamp) ⇒ object
将时间戳格式化为各种时间格式,并计算出距离当前时间的时间差,并以键值对形式返回
Kind: global function Returns: object - - 包含各种时间格式和发布时间的键值对
| Param | Type | Description | | --- | --- | --- | | timestamp | number | 要格式化的时间戳(Unix 时间戳,单位为毫秒) |
getType(variable) ⇒ string
判断变量的类型,并返回类型字符串
Kind: global function Returns: string - - 变量的类型字符串
| Param | Type | Description | | --- | --- | --- | | variable | * | 要检查类型的变量 |
truncateString(str, maxLength) ⇒ string
返回一个截断到指定长度的字符串,如果字符串长度超过指定长度,则添加省略号。
Kind: global function Returns: string - - 截断后的字符串。 Throws:
- TypeError 如果输入的参数不是字符串或数字类型,则抛出异常。
| Param | Type | Description | | --- | --- | --- | | str | string | 要截断的字符串。 | | maxLength | number | 最大允许的字符串长度。 |
replaceAll(str, find, replace) ⇒ string
将源字符串中的所有出现的子字符串替换为指定的目标字符串。
Kind: global function Returns: string - - 一个新字符串,其中所有匹配项都被替换为目标字符串。 Throws:
- TypeError 如果任何一个参数不是字符串类型,则抛出异常。
- Error 如果查找字符串为空,则抛出异常。
| Param | Type | Description | | --- | --- | --- | | str | string | 要进行替换操作的源字符串。 | | find | string | 要查找和替换的子字符串。 | | replace | string | 用于替换每个匹配项的目标字符串。 |
getAllTextBetween(text, start, end) ⇒ array
获取文本中指定开始值和结束值之间的所有子字符串(全局匹配)
Kind: global function Returns: array - - 匹配到的所有子字符串组成的数组,如果未找到任何匹配项,则返回空数组。
| Param | Type | Description | | --- | --- | --- | | text | string | 要搜索的文本 | | start | string | 搜索范围的开始值 | | end | string | 搜索范围的结束值 |
getTagAttrValues(html, tag, [attr], [limit]) ⇒ array | object
在给定的 HTML 片段中,查找前 n 个指定标签的指定属性的值
Kind: global function Returns: array | object - - 如果未指定属性名称,则返回包含每个匹配项的所有属性的对象。否则,返回包含匹配项的属性值组成的数组。
| Param | Type | Description | | --- | --- | --- | | html | string | 要搜索的 HTML 片段 | | tag | string | 要搜索的 HTML 标签名称 | | [attr] | string | 要搜索的 HTML 属性名称(可选) | | [limit] | number | 要返回的值的数量(可选) |
validate(optionsObject) ⇒ Object
验证给定的对象是否符合指定的格式要求
Kind: global function Returns: Object - 包含每个属性是否符合对应格式要求的结果的对象 Throws:
- TypeError 如果参数不是一个对象类型,则抛出一个类型错误
| Param | Type | Description | | --- | --- | --- | | optionsObject | Object | 包含需要验证的属性和值的对象 |