String

Methods

(inner) byteSize(str) → {number}

Source:
获取字符串的字节长度
Example
U.byteSize('日')
// => 3

U.byteSize('12')
// => 2

U.byteSize('hello')
// => 5
Parameters:
Name Type Description
str string 字符串
Returns:
Type
number

(inner) reverseString(str) → {str}

Source:
反转字符串
Example
U.reverseString('hello!')
// => '!olleh'
Parameters:
Name Type Description
str string 字符串
Returns:
Type
str

(inner) stringifyURL(url, params) → {string}

Source:
向URL追加参数
Example
U.stringifyURL('https://www.google.com/', {name: 'john', age: 30})
// => 'https://www.google.com/?name=john&age=30'
Parameters:
Name Type Description
url string URL路径
params object 参数对象
Returns:
Type
string

(inner) parseURL(url) → {object}

Source:
解析URL参数
Example
U.parseURL('http://url.com/page?name=Adam&surname=Smith')
// => {name: 'Adam', surname: 'Smith'}

U.parseURL('https://www.google.com/')
// => {}
Parameters:
Name Type Description
url string 字符串
Returns:
Type
object

(inner) removeHTML(str) → {string}

Source:
移除字符串中的HTML标签
Example
const str = '<p>这是<em>一个</em>段落。</p>'
U.removeHTML(str)
// => '这是一个段落。'
Parameters:
Name Type Description
str string 字符串
Returns:
Type
string

(inner) escapeHTML(str) → {string}

Source:
转义特殊字符
Example
const str = '<a href="#">you & me</a>'
U.escapeHTML(str)
// => '&lt;a href=&quot;#&quot;&gt;you &amp; me&lt;/a&gt;'
Parameters:
Name Type Description
str string 字符串
Returns:
Type
string

(inner) unescapeHTML(str) → {string}

Source:
反转义特殊字符
Example
const str = '&lt;a href=&quot;#&quot;&gt;you &amp; me&lt;/a&gt;'
U.unescapeHTML(str)
// => '<a href="#">you & me</a>'
Parameters:
Name Type Description
str string 字符串
Returns:
Type
string

(inner) mask(str, startopt, endopt, maskopt) → {string}

Source:
使用指定的掩码字符替换start~end之间的所有字符
Example
U.mask(123456789) // => *********
U.mask(123456789, 3) // => 123******
U.mask(str, 0, 4) // => *****6789
U.mask(str, 3, 4) // => 123**6789
U.mask(str, 3, 4, '&') // => 123&&6789
Parameters:
Name Type Attributes Default Description
str string | number 字符串
start number <optional>
0 可选,开始位置,默认为0(即字符串开头)
end number <optional>
0 可选,结束位置,默认为0(即字符串结尾)
mask string <optional>
'*' 可选,掩码字符,默认为'*'号
Returns:
Type
string

(inner) randomHex() → {string}

Source:
随机生成16进制色值
Example
U.randomHex()
// => "#f13ba7"
Returns:
Type
string

(inner) randomRgba(minopt, maxopt, alphaopt) → {string}

Source:
随机生成rgba色值
Example
U.randomRgba()
// => rgba(223,135,252,1)

U.randomRgba(154, 211, 0.5)
// => rgba(191,178,179,0.5)
Parameters:
Name Type Attributes Default Description
min number <optional>
0 可选,最小色阶
max number <optional>
256 可选,最大色阶
alpha number <optional>
1 可选,透明度
Returns:
Type
string

(inner) extendHex(shortHex) → {string}

Source:
将3位16进制色值转为6位
Example
U.extendHex('#03f')
// => '#0033ff'

U.extendHex('05a')
// => '#0055aa'
Parameters:
Name Type Description
shortHex string 字符串
Returns:
Type
string

(inner) hexToRGB(hex, alpha) → {string}

Source:
将16进制hex色值转为rgb(或rgba)色值
Example
U.hexToRGB('#e5f')
// => rgb(238,85,255)

U.hexToRGB('e5f')
// => rgb(238,85,255)

U.hexToRGB('#e5f', 0.5)
// => rgba(238,85,255,0.5)
Parameters:
Name Type Description
hex string 字符串,16进制hex色值
alpha number 可选,色彩透明度
Returns:
Type
string

(inner) RGBToHex(rgb) → {string}

Source:
将rgb(或rgba)色值转为16进制hex色值
Example
U.RGBToHex('rgb(238,85,255)')
// => #ee55ff

U.RGBToHex('rgba(238,85,255,0.5)')
// => #ee55ff
Parameters:
Name Type Description
rgb string 字符串,rgb(或rgba)色值
Returns:
Type
string

(inner) parseCookie(str) → {object}

Source:
解析cookie字符串
Example
U.parseCookie('taken=bar; equation=E%3Dmc%5E2')
// => {taken: 'bar', equation: 'E=mc^2'}
Parameters:
Name Type Description
str string 字符串
Returns:
Type
object

(inner) stringToDate(str) → {date}

Source:
字符串转日期对象
Example
U.stringToDate('2019/5-06')
// => Mon May 06 2019 00:00:00 GMT+0800 (中国标准时间)

U.stringToDate('2019-5-06 20:21:22:500')
// => Mon May 06 2019 20:21:22 GMT+0800 (中国标准时间)
Parameters:
Name Type Description
str string 字符串
Returns:
Type
date

(inner) camelToDash(str) → {string}

Source:
驼峰字符串转横线连接字符串
Example
U.camelToDash('camelCase')
=> 'camel-case'
Parameters:
Name Type Description
str string 驼峰字符串
Returns:
Type
string

(inner) dashToCamel(str) → {string}

Source:
横线连接字符串转驼峰字符串
Example
U.camelToDash('dash-case')
=> 'dashCase'
Parameters:
Name Type Description
str string 横线连接字符串
Returns:
Type
string