0.2.8 • Published 2 years ago

shared-js-api v0.2.8

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Functions

getQueryString(name, href) ⇒ string | Array.<string>

获取 url 查询参数

Kind: global function
Returns: string | Array.<string> - 参数值 value,如果 name 为数组,则返回数组

ParamTypeDefaultDescription
namestring | Array.<string>参数 key,如果为数组,则返回多个
hrefstring"location.href"目标 url,不传默认为当前 url,即 location.href

Example

const href = 'https://www.baidu.com?name=tom&age=20'
getQueryString('name', href) // tom
getQueryString('age', href) // 20
getQueryString(['name', 'age'], href) // ['tom', '20']

fillz(val, count) ⇒ string

内容前补 0

Kind: global function
Returns: string - 补 0 后的值

ParamTypeDefaultDescription
valstring | number需要补 0 的内容
countnumber10 的个数,默认为 1

Example

fillz(1, 1) // '01'

toRawType(val) ⇒ string

获取对象类型

Kind: global function
Returns: string - 类型,Object、String 等

ParamTypeDescription
val*任意参数

Example

toRawType({}) // Object
toRawType('') // String

isString(val) ⇒ boolean

是否为字符串

Kind: global function
Returns: boolean - 结果

ParamTypeDescription
val*任意参数

Example

isString('hello') // true

isObject(val) ⇒ boolean

是否为对象

Kind: global function
Returns: boolean - 结果

ParamTypeDescription
val*任意参数

isArray(val) ⇒ boolean

是否为数组

Kind: global function
Returns: boolean - 结果

ParamTypeDescription
val*任意参数

isDate(val) ⇒ boolean

是否为日期

Kind: global function
Returns: boolean - 结果

ParamTypeDescription
val*任意参数

isNumber(val) ⇒ boolean

是否为数字

Kind: global function
Returns: boolean - 结果

ParamTypeDescription
val*任意参数

isSymbol(val) ⇒ boolean

是否为 Symbol

Kind: global function
Returns: boolean - 结果

ParamTypeDescription
val*任意参数

isFunction(val) ⇒ boolean

是否为 Function

Kind: global function
Returns: boolean - 结果

ParamTypeDescription
val*任意参数

isPromise(val) ⇒ boolean

是否为 Promise

Kind: global function
Returns: boolean - 结果

ParamTypeDescription
val*任意参数

formatDate(val, fmt) ⇒ string

Date 转字符串

Kind: global function
Returns: string - 转换后的日期

ParamTypeDefaultDescription
valdateDate 对象
fmtstring"yyyy-MM-dd"格式,默认 yyyy-MM-dd

Example

formatDate(new Date()) // 2021-02-23
formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss') // 2021-02-23 13:40:44

formatTime(val, fmt) ⇒ string

毫秒转时间

Kind: global function
Returns: string - 转换后的时间

ParamTypeDefaultDescription
valstring毫秒数
fmtstring"dd:hh:mm:ss"格式,默认 dd:hh:mm:ss

Example

formatTime(60 * 1000) // 00:00:01:00
formatTime(60 * 1000, 'dd 天 hh 小时 mm 分钟 ss 秒') // 00 天 00 小时 01 分钟 00 秒
formatTime(60 * 1000 * 1.5, 'dd 天 hh 小时 mm 分钟 ss 秒') // 00 天 00 小时 01 分钟 30 秒
formatTime(60 * 1000 * 1.5, 'mm 分钟 ss 秒') // 01 分钟 30 秒

toNumber(val) ⇒ *

将字符串转为数字,转换失败返回原参数

Kind: global function
Returns: * - 成功返回数字,失败原样返回

ParamTypeDescription
val*要转换的对象

Example

toNumber('1') // 1
toNumber('a') // 'a'

getFirstDateOfMonth(dateConfig) ⇒ string | Date

获取当月第一天

Kind: global function
Returns: string | Date - 结果

ParamTypeDescription
dateConfigobject配置对象
dateConfig.offsetnumber偏移,-1 表示上一月,1 表示下一月,默认为 0 当前月
dateConfig.fmtstring格式,不传返回 Date 对象

Example

// 假设当前为 4 月
getFirstDateOfMonth() // Thu Apr 01 2021 00:00:00 GMT+0800 (中国标准时间)
getFirstDateOfMonth({ fmt: 'yyyy-MM-dd hh:mm:ss' }) // 2021-04-01 00:00:00
getFirstDateOfMonth({ offset: 1, fmt: 'yyyy-MM-dd hh:mm:ss' }) // 2021-05-01 00:00:00

getLastDateOfMonth(dateConfig) ⇒ string | Date

获取当月最后一天

Kind: global function
Returns: string | Date - 结果

ParamTypeDescription
dateConfigobject配置对象
dateConfig.offsetnumber偏移,-1 表示上一月,1 表示下一月,默认为 0 当前月
dateConfig.fmtstring格式,不传返回 Date 对象

Example

// 假设当前为 4 月
getLastDateOfMonth() // Fri Apr 30 2021 23:59:59 GMT+0800 (中国标准时间)
getLastDateOfMonth({ fmt: 'yyyy-MM-dd hh:mm:ss' }) // 2021-04-30 23:59:59
getLastDateOfMonth({ offset: 1, fmt: 'yyyy-MM-dd hh:mm:ss' }) // 2021-05-31 23:59:59

getRangeDateOfMonth(dateConfig) ⇒ Array.<string> | Array.<Date>

获取当月日期范围

Kind: global function
Returns: Array.<string> | Array.<Date> - 结果

ParamTypeDescription
dateConfigobject配置对象
dateConfig.offsetnumber偏移,-1 表示上一月,1 表示下一月,默认为 0 当前月
dateConfig.fmtstring格式,不传返回 Date 对象

Example

// 假设当前为 4 月
getRangeDateOfMonth() // [Thu Apr 01 2021 00:00:00 GMT+0800 (中国标准时间), Fri Apr 30 2021 23:59:59 GMT+0800 (中国标准时间)]
getRangeDateOfMonth({ fmt: 'yyyy-MM-dd hh:mm:ss' }) // ['2021-04-01 00:00:00', '2021-04-30 23:59:59']
getRangeDateOfMonth({ offset: 1, fmt: 'yyyy-MM-dd hh:mm:ss' }) // ['2021-05-01 00:00:00', '2021-05-31 23:59:59']

getRangeDateOfWeek(dateConfig) ⇒ Array.<string> | Array.<Date>

获取当前日期所在周的日期范围,比如今天是 2021 年 4 月 22 日(周 4),那么就返回这周的开始日期和结束日期

Kind: global function
Returns: Array.<string> | Array.<Date> - 结果

ParamTypeDescription
dateConfigobject配置对象
dateConfig.offsetnumber偏移,-1 表示上一周,1 表示下一周,默认为 0 当前周
dateConfig.fmtstring格式,不传返回 Date 对象

Example

getRangeDateOfWeek() // [Mon Apr 19 2021 00:00:00 GMT+0800 (中国标准时间), Sun Apr 25 2021 23:59:59 GMT+0800 (中国标准时间)]
getRangeDateOfWeek({ fmt: 'yyyy-MM-dd hh:mm:ss' }) // ['2021-04-19 00:00:00', '2021-04-25 23:59:59']
getRangeDateOfWeek({ offset: 1, fmt: 'yyyy-MM-dd hh:mm:ss' }) // ['2021-04-26 00:00:00', '2021-05-02 23:59:59']

getSuffix(val, toUpperCase) ⇒ string

获取后缀

Kind: global function
Returns: string - 后缀

ParamTypeDefaultDescription
valstring路径
toUpperCasebooleanfalse是否转大写,默认 false,即小写

Example

getSuffix('xx.jpg') // jpg
getSuffix('xx.jpg', true) // JPG
getSuffix('xx.JPG') // jpg

isEmpty(val) ⇒ boolean

是否为 null、undefined 或者 ''

Kind: global function
Returns: boolean - 结果

ParamTypeDescription
val*任意参数

Example

isEmpty('') // true
isEmpty(null) // true
isEmpty(undefined) // true
isEmpty(0) // false

encodeHTML(val) ⇒ string

对 html 代码进行编码

Kind: global function
Returns: string - 编码后的字符串

ParamTypeDescription
valstringhtml 代码

Example

encodeHTML('<div>hello</div>') // &lt;div&gt;hello&lt;/div&gt;

decodeHTML(val) ⇒ string

与 encodeHTML 相反,对字符串进行 html 解码

Kind: global function
Returns: string - 解码后的 html

ParamTypeDescription
valstring要解码的字符串

Example

decodeHTML('&lt;div&gt;hello&lt;/div&gt;') // <div>hello</div>

validate(type, val) ⇒ string

验证参数

Kind: global function
Returns: string - 结果

ParamTypeDescription
typestring要验证类型,11 位手机号:mobilePhone、邮箱:email、18 位身份证:identityCard
valstring | number要验证的值

Example

validate('mobilePhone', '13122222222') // true
validate('mobilePhone', '11111111111') // false

validate('email', '32d@xx.cc') // true
validate('email', '32d.cc') // false
0.2.8

2 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.9

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago