shared-js-api
TypeScript icon, indicating that this package has built-in type declarations

0.2.8 • Public • Published

Functions

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

获取 url 查询参数

fillz(val, [count])string

内容前补 0

toRawType(val)string

获取对象类型

isString(val)boolean

是否为字符串

isObject(val)boolean

是否为对象

isArray(val)boolean

是否为数组

isDate(val)boolean

是否为日期

isNumber(val)boolean

是否为数字

isSymbol(val)boolean

是否为 Symbol

isFunction(val)boolean

是否为 Function

isPromise(val)boolean

是否为 Promise

formatDate(val, [fmt])string

Date 转字符串

formatTime(val, [fmt])string

毫秒转时间

toNumber(val)*

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

getFirstDateOfMonth(dateConfig)string | Date

获取当月第一天

getLastDateOfMonth(dateConfig)string | Date

获取当月最后一天

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

获取当月日期范围

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

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

getSuffix(val, [toUpperCase])string

获取后缀

isEmpty(val)boolean

是否为 null、undefined 或者 ''

encodeHTML(val)string

对 html 代码进行编码

decodeHTML(val)string

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

validate(type, val)string

验证参数

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

获取 url 查询参数

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

Param Type Default Description
name string | Array.<string> 参数 key,如果为数组,则返回多个
[href] string "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 后的值

Param Type Default Description
val string | number 需要补 0 的内容
[count] number 1 0 的个数,默认为 1

Example

fillz(1, 1) // '01'

toRawType(val) ⇒ string

获取对象类型

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

Param Type Description
val * 任意参数

Example

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

isString(val) ⇒ boolean

是否为字符串

Kind: global function
Returns: boolean - 结果

Param Type Description
val * 任意参数

Example

isString('hello') // true

isObject(val) ⇒ boolean

是否为对象

Kind: global function
Returns: boolean - 结果

Param Type Description
val * 任意参数

isArray(val) ⇒ boolean

是否为数组

Kind: global function
Returns: boolean - 结果

Param Type Description
val * 任意参数

isDate(val) ⇒ boolean

是否为日期

Kind: global function
Returns: boolean - 结果

Param Type Description
val * 任意参数

isNumber(val) ⇒ boolean

是否为数字

Kind: global function
Returns: boolean - 结果

Param Type Description
val * 任意参数

isSymbol(val) ⇒ boolean

是否为 Symbol

Kind: global function
Returns: boolean - 结果

Param Type Description
val * 任意参数

isFunction(val) ⇒ boolean

是否为 Function

Kind: global function
Returns: boolean - 结果

Param Type Description
val * 任意参数

isPromise(val) ⇒ boolean

是否为 Promise

Kind: global function
Returns: boolean - 结果

Param Type Description
val * 任意参数

formatDate(val, [fmt]) ⇒ string

Date 转字符串

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

Param Type Default Description
val date Date 对象
[fmt] string "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 - 转换后的时间

Param Type Default Description
val string 毫秒数
[fmt] string "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: * - 成功返回数字,失败原样返回

Param Type Description
val * 要转换的对象

Example

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

getFirstDateOfMonth(dateConfig) ⇒ string | Date

获取当月第一天

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

Param Type Description
dateConfig object 配置对象
dateConfig.offset number 偏移,-1 表示上一月,1 表示下一月,默认为 0 当前月
dateConfig.fmt string 格式,不传返回 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 - 结果

Param Type Description
dateConfig object 配置对象
dateConfig.offset number 偏移,-1 表示上一月,1 表示下一月,默认为 0 当前月
dateConfig.fmt string 格式,不传返回 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> - 结果

Param Type Description
dateConfig object 配置对象
dateConfig.offset number 偏移,-1 表示上一月,1 表示下一月,默认为 0 当前月
dateConfig.fmt string 格式,不传返回 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> - 结果

Param Type Description
dateConfig object 配置对象
dateConfig.offset number 偏移,-1 表示上一周,1 表示下一周,默认为 0 当前周
dateConfig.fmt string 格式,不传返回 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 - 后缀

Param Type Default Description
val string 路径
[toUpperCase] boolean false 是否转大写,默认 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 - 结果

Param Type Description
val * 任意参数

Example

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

encodeHTML(val) ⇒ string

对 html 代码进行编码

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

Param Type Description
val string html 代码

Example

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

decodeHTML(val) ⇒ string

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

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

Param Type Description
val string 要解码的字符串

Example

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

validate(type, val) ⇒ string

验证参数

Kind: global function
Returns: string - 结果

Param Type Description
type string 要验证类型,11 位手机号:mobilePhone、邮箱:email、18 位身份证:identityCard
val string | number 要验证的值

Example

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

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

Package Sidebar

Install

npm i shared-js-api

Weekly Downloads

25

Version

0.2.8

License

MIT

Unpacked Size

48.5 kB

Total Files

6

Last publish

Collaborators

  • huangzhaoping