npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

shared-js-api

v0.2.8

Published

<dl> <dt><a href="#getQueryString">getQueryString(name, [href])</a> ⇒ <code>string</code> | <code>Array.&lt;string&gt;</code></dt> <dd><p>获取 url 查询参数</p> </dd> <dt><a href="#fillz">fillz(val, [count])</a> ⇒ <code>string</code></dt> <dd><p>内容前补 0</p> </dd>

Downloads

29

Readme

Functions

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', '[email protected]') // true
validate('email', '32d.cc') // false