Skip to content
On this page

super-tools-lib - v1.74.0 / Modules / string

Module: string

Table of contents

Functions

Functions

decode

decode(token): string

解码JWT令牌

Since

1.62.0

Example

ts
import { decode } from 'super-tools-lib'

decode(`
   eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
   eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0I
   joxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
`); // '{"sub":"1234567890","name":"John Doe","iat":1516239022}'

Parameters

NameTypeDescription
tokenstring字符串

Returns

string

返回解析后的数据


getTextWidth

getTextWidth(text, font): number

计算文字宽度

Since

1.62.0

Example

ts
import { getTextWidth } from 'super-tools-lib'

getTextWidth('哈哈哈哈', '14px sans-serif') // 56

Parameters

NameTypeDescription
textstring文字
fontstring'14px sans-serif' 字号 字体

Returns

number


initialToCapitali

initialToCapitali(name): string

字符串首位是字母转大写

Since

1.62.0

Example

ts
import { values } from 'super-tools-lib'

values({a:1,b:2}) // [1, 2]

Parameters

NameTypeDescription
namestring字符串

Returns

string

返回首字母转换成大写的字符串


letterToEmoji

letterToEmoji(c): string

转换字母以关联表情符号

Since

1.62.0

Example

ts
import { letterToEmoji } from 'super-tools-lib'

letterToEmoji("a"); // '🇦'

Parameters

NameTypeDescription
cstring要操作的字符串

Returns

string

返回和字母关联的表情符号


removeSpaces

removeSpaces(str): string

删除字符串中的空格

Since

1.62.0

Example

ts
import { removeSpaces } from 'super-tools-lib'

removeSpaces("s s s k "); // sssk

Parameters

NameTypeDescription
strstring要操作的字符串

Returns

string

返回去除空格后的新字符串


repeat

repeat(str, n): string

传入字符串,和重复次数,返回结果字符串

Since

1.62.0

Example

ts
import { repeat } from 'super-tools-lib'

repeat('abc', 3) // abcabcabc

Parameters

NameTypeDescription
strstring字符串
nnumber重复次数

Returns

string

返回字符串*重复次数的字符串


replaceText

replaceText(str, range, mask): string

替换指定位置字符串内容

Since

1.62.0

Example

ts
import { replaceText } from 'super-tools-lib'

replaceText("17778780909", [3, 7], "*"); // '177****0909'

Parameters

NameTypeDescription
strstring要操作的字符串
range[number, number] | [number]替换字符串的区间
maskstring替换内容

Returns

string

返回替换后的新字符串


strInversion

strInversion(str): string

反转字符串

Since

1.62.0

Example

ts
import { strInversion } from 'super-tools-lib'

strInversion('abc') // cba

Parameters

NameTypeDescription
strstring字符串

Returns

string

返回反转后的字符串


toFullHexColor

toFullHexColor(color): string

将3位数颜色转换为6位数颜色

Since

1.62.0

Example

ts
import { toFullHexColor } from 'super-tools-lib'

toFullHexColor('#FFF') // #FFFFFF

Parameters

NameTypeDescription
colorstring颜色值 #FFF

Returns

string

返回6位数的颜色值 例如:#FFFFFF


toLine

toLine(val, isLowercase?): string

驼峰转下划线

Since

1.62.0

Example

ts
import { toLine } from 'super-tools-lib'

toLine("contBeginDate", false); // 'CONT_BEGIN_DATE'

Parameters

NameTypeDefault valueDescription
valstringundefined要操作的字符串
isLowercasebooleantrue是否转换为小写字母 默认值 true

Returns

string

返回转换后的字符串


toPascalCase

toPascalCase(str): string

将字符串转换为PascalCase

Since

1.62.0

Example

ts
import { toPascalCase } from 'super-tools-lib'

toPascalCase("hello world"); // 'HelloWorld'
toPascalCase("hello.world"); // 'HelloWorld'
toPascalCase("foo_bar-baz"); // FooBarBaz

Parameters

NameTypeDescription
strstring要操作的字符串

Returns

string


transFormat

transFormat(str, oldChar, newChar): string

字符替换

Since

1.62.0

Example

ts
import { transFormat } from 'super-tools-lib'

transFormat("2019-12-13", "-", "/") // 2019/12/13

Parameters

NameTypeDescription
strstring表示将要替换的字符串
oldCharstring表示你将要替换的字符
newCharstring表示你想要替换的字符

Returns

string

返回替换后的字符串


trim

trim(str): string

去除字符串首尾空格

Since

1.62.0

Example

ts
import { trim } from 'super-tools-lib'

trim(' AA ') // 'AA'

Parameters

NameTypeDescription
strstring字符串

Returns

string

返回去除首尾空格后的字符串