# 文档
# base64
# decode
decode(input, [options])
将 base64 编码的字符串解码为一个 UTF8 字符串、十六进制字符串或者字节数组。
起始版本
0.3.0
参数
input (string)
[options = {} (Object)]
[encoding = 'utf8' (string)]
:输出编码,可以是'utf8'
or'hex'
or'binary'
。
返回值
(string | Array)
示例
const input = 'aGVsbG8g5L2g5aW9'
// 返回 utf8 字符串
decode(input)
// => 'hello 你好'
// 返回十六进制字符串
decode(input, { encoding: 'hex' })
// => '68656c6c6f20e4bda0e5a5bd'
// a byte array
// 返回字节数组
decode(input, { encoding: 'binary' })
// => [104, 101, 108, 108, 111, 32, 228, 189, 160, 229, 165, 189]
# decodeFile
将一个 base64 编码字符串解码成一个 Blob (opens new window) 对象。
decodeFile(input, options)
起始版本
0.12.0
参数
input (string)
:一个 base64 编码字符串。options (Object)
type (string)
:MIME 类型,比如'plain/text'
。
返回值
(Promise<Blob>)
示例
decodeFile('aGVsbG8gd29ybGQ=', { type: 'plain/text' })
.then(blob => {
console.log(blob)
// => Blob {size: 11, type: "plain/text"}
})
# encode
将一个 UTF8 字符串、字节数组或者Uint8Array
编码为 base64 字符串。
encode(input)
起始版本
0.3.0
参数
input (string | Array | Uint8Array)
返回值
(string)
示例
// 输入字符串
encode('hello 你好')
// => 'aGVsbG8g5L2g5aW9'
// 输入字节数组
const bytes = [104, 101, 108, 108, 111, 32, 228, 189, 160, 229, 165, 189]
encode(bytes)
// => 'aGVsbG8g5L2g5aW9'
// 输入 Uint8Array
const uint8 = new Uint8Array(bytes)
encode(uint8)
// => 'aGVsbG8g5L2g5aW9'
# encodeFile
将一个 Blob (opens new window)/File (opens new window) 对象编码成一个字符串。
encodeFile(file)
起始版本
0.12.0
参数
file (Blob)
返回值
(Promise<string>)
示例
const blob = new Blob(['hello world'], { type: 'plain/text' })
encodeFile(blob)
.then((str) => {
console.log(str)
// => 'aGVsbG8gd29ybGQ='
})
# char
# isAlphabetic
isAlphabetic(ch)
判断一个字符是否是 a-z 或 A-Z。
起始版本
0.1.0
参数
ch (string)
:长度为 1 的字符串。
返回值
(boolean)
示例
isAlphabetic('a');
// => true
isAlphabetic('1');
// => false
# isAlphanumeric
isAlphanumeric(ch)
判断一个字符是否是 a-z 或 A-Z 或 0-9。
起始版本
0.1.0
参数
ch (string)
:长度为 1 的字符串。
返回值
(boolean)
示例
isAlphanumeric('a');
// => true
isAlphanumeric('1');
// => true
isAlphanumeric('*');
// => false
# isASCII
isASCII(ch)
判断一个字符是否是一个 ASCII 字符。
起始版本
0.1.0
参数
ch (string)
:长度为 1 的字符串。
返回值
(boolean)
示例
isASCII('a');
// => true
isASCII('1');
// => true
isASCII('你');
// => false
# isCJK
isCJK(ch)
判断一个字符是否是一个 CJK(中文、日文、韩文)字符。
起始版本
0.1.0
参数
ch (string)
:长度为 1 的字符串。
返回值
(boolean)
示例
isCJK('你');
// => true
isCJK('の');
// => true
# isControl
isControl(ch)
Detect a character whether it is a control character (opens new window). 判断一个字符是否是一个控制字符 (opens new window)。
起始版本
0.1.0
参数
ch (string)
:长度为 1 的字符串。
返回值
(boolean)
示例
isControl('\t');
// => true
isControl('\r');
// => true
# isDigit
isDigit(ch)
判断一个字符是否是一个十进制数字(0-9)。
起始版本
0.1.0
参数
ch (string)
:长度为 1 的字符串。
返回值
(boolean)
示例
isDigit('0');
// => true
isDigit('1');
// => true
# isGraph
isGraph(ch)
判断一个字符是否是一个图形字符 (opens new window)。
起始版本
0.1.0
参数
ch (string)
:长度为 1 的字符串。
返回值
(boolean)
示例
isGraph('0');
// => true
isGraph('a');
// => true
# isHexadecimal
isHexadecimal(ch)
判断一个字符是否是一个十六进制字符(0-9、a-f、A-F)。
起始版本
0.1.0
参数
ch (string)
:长度为 1 的字符串。
返回值
(boolean)
示例
isHexadecimal('0');
// => true
isHexadecimal('a');
// => true
isHexadecimal('A');
// => true
# isLower
isLower(ch)
判断一个字符是否是小写字符(a-z)。
起始版本
0.1.0
参数
ch (string)
:长度为 1 的字符串。
返回值
(boolean)
示例
isLower('a');
// => true
# isPrint
isPrint(ch)
判断一个字符是否是可打印字符 (opens new window)。
起始版本
0.1.0
参数
ch (string)
:长度为 1 的字符串。
返回值
(boolean)
示例
isPrint('a');
// => true
# isPunctuation
isPunctuation(ch)
Detect a character whether it is a punctuation character. 判断一个字符是否是标点字符。
起始版本
0.1.0
参数
ch (string)
:长度为 1 的字符串。
返回值
(boolean)
示例
isPunctuation(',');
// => true
# isSpace
isSpace(ch)
判断一个字符是否是一个空格字符。
起始版本
0.1.0
参数
ch (string)
:长度为 1 的字符串。
返回值
(boolean)
示例
isSpace(' ');
// => true
# isUpper
isUpper(ch)
判断一个字符是否是大写字符(A-Z)。
起始版本
0.1.0
参数
ch (string)
:长度为 1 的字符串。
返回值
(boolean)
示例
isUpper('a');
// => true
# cookie
# get
get(key)
获取指定名称的 Cookie 值,使用decodeURIComponent
解码。
起始版本
0.1.0
参数
key (string)
:Cookie 名称。
返回值
(string)
# getAll
getAll()
获取所有的 Cookie 值,使用decodeURIComponent
解码。
起始版本
0.1.0
返回值
(Object)
# getJson
getJson(key)
获取指定名称的 Cookie 值,使用JSON.parse
解码。
起始版本
0.1.0
参数
key (string)
:Cookie 名称。
返回值
(Any)
# getRaw
getRaw(key)
获取指定名称的 Cookie 值,不应用任何解码。
起始版本
0.1.0
参数
key (string)
:Cookie 名称。
返回值
(string)
# isEnabled
isEnabled()
检测 Cookie 功能是否启用。
起始版本
0.1.0
返回值
(boolean)
# remove
remove(key, [options])
移除当前域名下指定名称的 Cookie。
起始版本
0.1.0
参数
key (string)
:Cookie 名称。[options] (Object)
:Cookie 配置参数。[domain] (string)
[path = "/"] (string)
# set
set(key, value, [options])
设置一个 Cookie,value
参数将会使用encodeURIComponent
进行编码。
起始版本
0.1.0
参数
key (string)
:Cookie 名称value (string)
:Cookie 值。[options] (Object)
:Cookie 配置。[domain] (string)
:Cookie 所属域名。[path = "/"] (string)
:Cookie 所属路径。[expires] (number|string|Date)
:可以是一个Date
对象,一个可被Date.parse()
解析的日期字符串,一个整数(单位:天),或者一个带时间后缀的数值字符串。[max-age] (number)
:Cookie 持续时间。[samesite] (boolean)
[secure] (boolean)
单位后缀 | 含义 |
---|---|
Y | 年 |
M | 月 |
D | 天 |
h | 日 |
m | 分 |
s | 秒 |
# setJson
set(key, value, [options])
同set
,设置一个 Cookie,只是value
参数将会使用JSON.stringify
进行编码。
起始版本
0.1.0
参数
key (string)
:Cookie 名称value (string)
:Cookie 值。[options] (Object)
:Cookie 配置。[domain] (string)
:Cookie 所属域名。[path = "/"] (string)
:Cookie 所属路径。[expires] (number|string|Date)
:可以是一个Date
对象,一个可被Date.parse()
解析的日期字符串,一个整数(单位:天),或者一个带时间后缀的数值字符串。[max-age] (number)
:Cookie 持续时间。[samesite] (boolean)
[secure] (boolean)
# setRaw
setRaw(key, value, [options])
同set
,设置一个 Cookie,只是value
参数不使用任何编码。
起始版本
0.1.0
参数
key (string)
:Cookie 名称value (string)
:Cookie 值。[options] (Object)
:Cookie 配置。[domain] (string)
:Cookie 所属域名。[path = "/"] (string)
:Cookie 所属路径。[expires] (number|string|Date)
:可以是一个Date
对象,一个可被Date.parse()
解析的日期字符串,一个整数(单位:天),或者一个带时间后缀的数值字符串。[max-age] (number)
:Cookie 持续时间。[samesite] (boolean)
[secure] (boolean)
# date
# diff
diff(date1, date2, [unit], [roundFunc])
计算两个日期之间的差,可以指定单位及四舍五入函数。
起始版本
0.1.0
参数
date1 (number|Date|string)
:一个时间戳(单位:毫秒),或一个Date
对象,或一个可被解析的日期字符串。date2 (number|Date|string)
:一个时间戳(单位:毫秒),或一个Date
对象,或一个可被解析的日期字符串。[unit = "ms"] (string)
:一个表示时间单位的字符串,d
(表示天),详细见下面表格。[roundFunc = Math.round] (Function)
:一个四舍五入函数。
返回值
(number)
时间单位
单位值 | 表示含义 |
---|---|
Y | 年 |
M | 月 |
D | 日 |
h | 小时 |
m | 分钟 |
s | 秒 |
ms | 毫秒 |
示例
const date1 = '2019-01-01 00:00:00';
diff(date1, date2);
// => 3456000000
diff(date1, date2, 'D')
// => 40
# format
format(date, format, [isUTC])
根据指定的格式对一个日期格式化。
起始版本
0.1.0
参数
date (number|string from 0.8.0|Date)
:一个时间戳数值(单位:毫秒)或可解析的日期字符串或一个Date
对象。format (string)
:一个占位符替换字符串,为 moment format tokens (opens new window) 的一部分,具体查看下面表格。[isUTC = false] (boolean)
:是否使用协调世界时(UTC)。
返回值
(string)
Format tokens
Token | Output |
---|---|
YY | 70 71 ... 18 19 |
YYYY | 1970 1971 ... 2018 2019 |
M | 1 2 ... 11 12 |
MM | 01 02 ... 11 12 |
D | 1 2 ... 30 31 |
DD | 01 02 ... 30 31 |
H | 0 1 ... 22 23 |
HH | 00 01 ... 22 23 |
h | 1 2 ... 11 12 |
hh | 01 02 ... 11 12 |
m | 1 2 ... 58 59 |
mm | 01 02 ... 58 59 |
s | 1 2 ... 58 59 |
ss | 01 02 ... 58 59 |
示例
const date = new Date(1970, 0, 1, 13, 1, 1);
format(date, 'YYYY-MM-DD HH:mm:ss');
// => "1970-01-01 13:01:01"
format(date, 'YYYY-M-D h:m:s');
// => "1970-1-1 1:1:1"
# parse
parse(date, [isUTC])
起始版本
0.10.0
参数
date (number|string|Date)
:一个时间戳数值(单位:毫秒)或可解析的日期字符串或一个Date
对象。format (string)
:一个占位符替换字符串,为 moment format tokens (opens new window) 的一部分,具体查看下面表格。[isUTC = false] (boolean)
:是否使用协调世界时(UTC)。
返回值
(object)
示例
// In UTC+0800
j
parse(0)
// => { years: 1970, months: 0, dates: 1, hours: 8, minutes: 0, seconds: 0, milliseconds: 0 }
parse(new Date(0))
// => { years: 1970, months: 0, dates: 1, hours: 8, minutes: 0, seconds: 0, milliseconds: 0 }
parse('Thu, 01 Jan 1970 00:00:00 GMT')
// => { years: 1970, months: 0, dates: 1, hours: 8, minutes: 0, seconds: 0, milliseconds: 0 }
parse(0, true)
// => { years: 1970, months: 0, dates: 1, hours: 0, minutes: 0, seconds: 0, milliseconds: 0 }
parse(new Date(0), true)
// => { years: 1970, months: 0, dates: 1, hours: 0, minutes: 0, seconds: 0, milliseconds: 0 }
parse('Thu, 01 Jan 1970 00:00:00 GMT', true)
// => { years: 1970, months: 0, dates: 1, hours: 0, minutes: 0, seconds: 0, milliseconds: 0 }
# timeAgo
timeAgo.format(date, [locale], [nowDate])
将一个日期格式化为“5 分钟之前”之类的字符串。
起始版本
0.1.0
参数
date (number|Date|string)
:一个时间戳(单位:毫秒),或一个Date
对象,或一个可被解析的日期字符串。[locale = "en_US"] (string)
:时区,内置支持"en_US"
和"zh_CN"
。[nowDate = new Date() (Date)
:比对日期。
返回值
(string)
示例
const d = new Date();
d.setMinutes(d.getMinutes() - 10);
timeAgo.format(d);
// => "10 minutes ago"
timeAgo.format(d, 'zh_CN');
// => "10 分钟前"
# dom
# clientX
clientX(elem)
获取一个元素左边界距离视口(浏览器窗口可视区域)左边界距离。
起始版本
0.1.0
参数
elem (Element)
返回值
(number)
示例
const el = document.getElementById('el');
const x = clientX(el);
// x => 100
# clientY
clientY(elem)
获取一个元素顶部距离视口(浏览器窗口可视区域)顶部距离。
起始版本
0.1.0
参数
elem (Element)
返回值
(number)
示例
const el = document.getElementById('el');
const y = clientY(el);
// y => 100
# createEvent
createEvent(type, [options])
创建一个自定义事件对象。
起始版本
0.1.0
参数
type (string)
:事件名称。[options] (string)
:事件配置对象。[bubbles = false] (boolean)
:事件是否冒泡。[cancelable = false] (boolean)
:事件是否可被取消。[detail = null] (Object)
:附加的数据。
返回值
(Event)
示例
const btn = document.getElementById('btn');
const event = createEvent('click');
btn.dispatchEvent(event);
# domReady
domReady(callback)
延迟执行一个回调函数,直到DOMContentLoaded
事件触发或者 DOM 可访问时执行。如果domReady
执行时,DOMContentLoaded
事件已经触发或者 DOM 已经可访问,则callback
会在下一个事件循环(event loop)被执行。
起始版本
0.1.0
参数
callback (Function)
:被执行的回调函数。
示例
domReady(function () {
// ...
});
// 可以使用多次。
domReady(function () {
// 其他回调
});
# insertScript
insertScript(url, [props])
插入一个<script>
标签到文档中。
起始版本
0.1.0
参数
url (string)
:<script>
标签的src
属性。[props] (Object)
:其他<script>
标签支持的属性。
# isElement
isElement(obj)
判断一个值是否为元素对象。
起始版本
0.1.0
参数
obj (Any)
:任意值。
返回值
(boolean)
# isInViewport
isInViewport(el)
判断一个元素是否位于视口(浏览器可视区域)内。
起始版本
0.1.0
参数
el (Element)
:一个元素。
返回值
(boolean)
# isWindow
isWindow(obj)
判断一个值是否是window
对象。
起始版本
0.1.0
参数
obj (Any)
:任意值。
返回值
(boolean)
# pageHeight
pageHeight([el])
返回元素内容高度,包括由于溢出导致未显示部分。
起始版本
0.13.0
参数
[el] (Element)
返回值
(number)
示例
// 当前页面文档高度
pageHeight()
// 元素内容高度
pageHeight(document.getElementById('el1'))
# pageWidth
pageWidth([el])
返回元素内容宽度,包括由于溢出导致未显示部分。
起始版本
0.13.0
参数
[el] (Element)
返回值
(number)
示例
// 当前页面文档宽度
pageWidth()
// 元素内容宽度
pageWidth(document.getElementById('el1'))
# pageX
pageX(el)
返回一个元素距离页面左边界的水平距离。
起始版本
0.1.0
参数
el (Element)
返回值
(number)
示例
const el = document.getElementById('el')
pageX(el)
# pageY
pageY(el)
返回一个元素距离页面顶部的竖直距离。
起始版本
0.1.0
参数
el (Element)
返回值
(number)
示例
const el = document.getElementById('el')
pageY(el)
# scrollTo
scrollTo(x, y)
scrollTo(options)
scrollTo(elOrWindow, x, y)
scrollTo(elOrWindow, options)
滚动页面或元素到指定位置。
起始版本
0.1.0
参数
[x = 0] (number)
[y = 0] (number)
[elOrWindow = window] (Element | Window)
[options] (ExScrollToOptions)
[x = 0] (number)
[y = 0] (number)
[easing] (string)
:类似 CSS 的 transition-timing-function (opens new window)。 值为linear
/ease
/easeIn
/easeInOut
/easeOut
/cubic-bezier(x1, y1, x2, y2)
之一。[behavior] (string)
:值为smooth
orauto
。查看兼容性 (opens new window).
示例
// 滚动页面到 (100, 100)
scroll(100, 100)
// 平滑滚动页面到 (100, 100)
scroll({ x: 100, y: 100, behavior: 'smooth' })
// 滚动页面到 (100, 100),并且应用一个缓动函数
scroll({ x: 100, y: 100, easing: 'ease-out' })
// 滚动元素到 (100, 100)
scroll(el, 100, 100)
// 平滑滚动元素到 (100, 100)
scroll(el, { x: 100, y: 100, behavior: 'smooth' })
// 滚动元素到 (100, 100),并且应用一个缓动函数
scroll(el, { x: 100, y: 100, easing: 'ease-out' })
# scrollX
scrollX([elOrWindow])
返回一个元素或页面的水平滚动距离。
起始版本
0.1.0
参数
[elOrWindow = window] (Element|Window)
:一个元素或window
对象。
返回值
(number)
示例
// 如果不传任何参数,则返回页面的水平滚动距离。
scrollX();
const el = document.getElementById('el')
scrollX(el);
# scrolY
scrollY([elOrWindow])
返回一个元素或页面的竖直滚动距离。
起始版本
0.1.0
参数
[elOrWindow = window] (Element|Window)
:一个元素或window
对象。
返回值
(number)
示例
// 如果不传任何参数,则返回页面的竖直滚动距离。
scrollY();
const el = document.getElementById('el')
scrollY(el);
# viewport
viewport([elOrWindow])
获取一个元素或window
的视口宽度和高度。
起始版本
0.1.0
参数
[elOrWindow = window] (Element|Window)
:一个元素或window
对象。
返回值
(Object)
示例
viewport();
// => { width: 1920, height: 900 }
# easing
# cubicBezier
cubicBezier(x1, y1, x2, y2)
如同 CSS 过渡(transition)的cubic-bezier()
,该方法生成一个缓动函数(easing function)。
起始版本
0.2.0
参数
x1 (number)
y1 (number)
x2 (number)
y2 (number)
返回值
(Function)
示例
const linear = cubicBezier(0, 0, 1, 1)
linear(0.1)
// => 0.1
# ease
ease(x)
等同于cubicBezier(0.25, 0.1, 0.25, 1)
。
起始版本
0.2.0
参数
x (number)
返回值
(number)
# easeIn
easeIn(x)
等同于cubicBezier(0.42, 0, 1, 1)
。
起始版本
0.2.0
参数
x (number)
返回值
(number)
# easeInOut
easeInOut(x)
等同于cubicBezier(0.42, 0, 0.58, 1)
。
起始版本
0.2.0
参数
x (number)
返回值
(number)
# easeOut
easeOut(x)
等同于cubicBezier(0, 0, 0.58, 1)
。
起始版本
0.2.0
参数
x (number)
返回值
(number)
# linear
linear(x)
等同于cubicBezier(0, 0, 1, 1)
。
起始版本
0.2.0
参数
x (number)
返回值
(number)
示例
linear(0.1)
// => 0.1
# emitter
你可以使用micell.emitter.create()
创建一个Emitter
实例对象,每一个 Emitter 实例对象都有下面的方法:
emit
getListeners
off
on
once
micell.emitter
如同一个全局的 Emitter 实例对象,也具有上面的方法,即下面的函数实际上是调用的一个全局 Emitter 实例对象对应的方法。
micell.emitter.emit
micell.emitter.getListeners
micell.emitter.off
micell.emitter.on
micell.emitter.once
# Emitter
new Emitter()
Emitter 构造函数,不过推荐使用create()
方法创建 Emitter 实例。
起始版本
0.5.0
返回值
(Emitter)
# create
create()
创建一个 Emitter 实例。
起始版本
0.5.0
返回值
(Emitter)
示例
const emitter = create()
emitter.on('foo', () => console.log('foo'))
emitter.emit('foo')
// => 'foo'
# emit
emit(type, [arg1], [arg2], ...)
起始版本
0.5.0
参数
type (string)
[argN] (any)
micell.emitter.on('foo', (...args) => console.log(args))
micell.emitter.emit('foo', 1, 2, 3)
// => [1, 2, 3]
# getListeners
getListeners(type)
获取事件type
的所有监听器函数或者所有事件的所有监听器函数。
起始版本
0.5.0
参数
[type (string)]
返回值
(Array<Listener>)
示例
micell.emitter.on('foo', () => console.log('foo 1'))
micell.emitter.on('bar', () => console.log('bar 1'))
micell.emitter.on('foo', () => console.log('foo 2'))
// 事件 'foo' 对应的所有监听器函数
micell.emitter.getListeners('foo')
// 所有事件的所有监听器函数
micell.emitter.getListeners()
# off
off(type, listener)
- 如果参数为空,则所有事件的所有监听器函数都被移除。
- 如果只传入了
type
参数,则type
事件对应的所有监听器函数都被移除。 - 如果
type
和listener
参数都传入了,则type
事件对应的所有listener
监听器函数会被移除。
起始版本
0.5.0
参数
[type (string)]
[listener (function)]
示例
const fn1 = () => console.log(1)
const fn2 = () => console.log(2)
micell.emitter.on('foo', fn1)
micell.emitter.on('bar', fn1)
micell.emitter.on('foo', fn2)
micell.emitter.off('foo', fn1)
micell.emitter.off('foo')
micell.emitter.off()
# on
on(type, listener)
绑定一个监听器函数listener
。
起始版本
0.5.0
参数
[type (string)]
[listener (function)]
# once
once(type, listener)
和on
方法一样,绑定一个监听器函数listener
,但是监听器函数只会被执行一次。
参数
[type (string)]
[listener (function)]
# lang
# getType
getType(value)
返回value
的数据类型。
起始版本
0.14.0
参数
value (any)
返回值
(string)
示例
getType(undefined)
// => 'Undefined'
getType(null)
// => 'Null'
getType(1)
// => 'Number'
getType(true)
// => 'Boolean'
getType('')
// => 'String'
getType(Symbol('foo'))
// => 'Symbol'
getType(0n)
// => 'BigInt'
getType({})
// => 'Object'
getType([])
// => 'Array'
getType(/\s/)
// => 'RegExp'
getType(new Date())
// => 'Date'
getType(function () {})
// => 'Function'
# isArray
isArray(value)
判断一个值是否是数组类型。
起始版本
0.1.0
参数
value (any)
返回值
(boolean)
示例
isArray([]);
// => true
# isBigInt
isBigInt(value)
判断一个值是否是 BigInt 类型。
起始版本
0.8.0
参数
value (any)
返回值
(boolean)
示例
isBigInt(BigInt(1));
// => true
# isBoolean
isBoolean(value)
判断一个值是否是布尔类型。
起始版本
0.1.0
参数
value (any)
返回值
(boolean)
示例
isBoolean(true);
// => true
# isDate
isDate(value)
判断一个值是否是 Date 对象。
起始版本
0.1.0
参数
value (any)
返回值
(boolean)
示例
isDate([]);
// => true
# isError
isError(value)
判断一个值是否是 Error 类型。
起始版本
0.1.0
参数
value (any)
返回值
(boolean)
示例
isError(new Error('error message'));
// => true
# isFunction
isFunction(value)
判断一个值是否是一个函数,包括生成器函数和异步函数。
起始版本
0.1.0
参数
value (any)
返回值
(boolean)
示例
isFunction(function () {});
// => true
isFunction(function* () {});
// => true
# isNil
isNil(value)
判断一个值是否是null
或undefined
。
起始版本
0.1.0
参数
value (any)
返回值
(boolean)
示例
isNil(null);
// => true
isNil(undefined);
// => true
# isNull
isNull(value)
判断一个值是否是null
。
起始版本
0.1.0
参数
value (any)
返回值
(boolean)
示例
isNull(null);
// => true
# isNumber
isNumber(value)
判断一个值是否是数值类型。
起始版本
0.1.0
参数
value (any)
返回值
(boolean)
示例
isNumber(1);
// => true
# isObject
isObject(value)
判断一个值是否是一个对象类型。
起始版本
0.1.0
参数
value (any)
返回值
(boolean)
示例
isObject({});
// => true
isObject(null);
// => false
# isRegExp
isRegExp(value)
判断一个值是否是 RegExp 对象。
起始版本
0.1.0
参数
value (any)
返回值
(boolean)
示例
isRegExp(/\s+/);
// => true
# isString
isString(value)
判断一个值是否是字符串类型。
起始版本
0.1.0
参数
value (any)
返回值
(boolean)
示例
isString('hello world');
// => true
# isSymbol
isSymbol(value)
判断一个值是否是 Symbol 类型。
起始版本
0.8.0
参数
value (any)
返回值
(boolean)
示例
isSymbol(Symbol('foo'));
// => true
# isUndefined
isUndefined(value)
判断一个值是否是undefined
。
起始版本
0.1.0
参数
value (any)
返回值
(boolean)
示例
isUndefined(void 0);
// => true
# path
# basename
basename(str)
返回一个路径字符串的最后一部分。
起始版本
0.1.0
参数
str (string)
返回值
(string)
示例
basename('/foo/bar');
// => "bar"
# dirname
dirname(str)
返回一个路口字符串的目录部分。
起始版本
0.1.0
参数
str (string)
返回值
(string)
示例
dirname('/foo/bar');
// => "/foo"
# extname
extname(str)
返回一个路径字符串的扩展名部分。
起始版本
0.1.0
参数
str (string)
返回值
(string)
示例
extname('/foo/bar.txt');
// => ".txt"
# join
join(...args)
拼接路径字符串,使用斜线/
分隔。
起始版本
0.8.0
参数
args (string[])
返回值
(string)
示例
join()
// => ""
join('foo', 'bar')
// => "foo/bar"
join('foo/', '/bar')
// => "foo/bar"
# qs
# get
get(name)
获取查询字符串当中指定名称的值。
起始版本
0.1.0
参数
name (string)
返回值
(string|Array\<string\>)
示例
// location.search = '?hello=world'
getQuery('hello');
// => "world"
// location.search = '?fruits=apple&fruits=orange'
getQuery('fruits');
// => ["apple", "orange"]
# parse
parse(search)
解析一个查询字符串。
起始版本
0.1.0
参数
search (string)
返回值
(Object)
# stringify
stringify(query)
将对象序列化为查询字符串。
起始版本
0.1.0
参数
query (Object)
返回值
(string)
# regex
# isAscii
isAscii(str)
判断一个字符串是否只包含 ASCII 字符。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isAscii('hello world');
// => true
# isDecimal
isDecimal(str)
判断一个字符串是否是一个小数记数法表示的数值。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isDecimal('1');
// => true
isDecimal('3.14');
// => true
# isDigit
isDigit(str)
判断一个字符串是否只包含十进制数字(0-9)。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isDigit('0123');
// => true
# isDomain
isDomain(str)
判断一个字符串是否是一个域名。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isDomain('example.com');
// => true
isDomain('https://example.com');
// => false
# isEmail
isEmail(str)
判断一个字符串是否一个邮件地址。
如果你打算判断一个邮件地址是否有效,最好的办法是发送一封带有回执链接的邮件。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isEmail('alexchao1990@gmail.com');
// => true
isEmail('alexchao1990+github@gmail.com');
// => true
# isHexColor
isHexColor(str)
判断一个字符串是否是一个十六进制数字格式的颜色值。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isHexColor('#f50');
// => true
isHexColor('#ff5500');
// => true
isHexColor('#FF5500');
// => true
# isHsl
isHsl(str)
判断一个字符串是否是一个 HSL 颜色值。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isHsl('hsl(100, 50%, 50%)');
// => true
isHsl('HSL(100, 50%, 50%)');
// => true
# isHsla
isHsla(str)
判断一个字符串是否是一个 HSLA 颜色值。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isHsla('hsla(100, 50%, 50%, 0.5)');
// => true
isHsla('HSLA(100, 50%, 50%, 0.5)');
// => true
# isInteger
isInteger(str)
与isDigit
相同,判断一个字符串是否只包含十进制数字。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isInteger('0123');
// => true
# isIP
isIP(str)
判断一个字符串是否一个 IP(v4 或 v6)地址。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isIP('192.0.0.1');
// => true
isIP('2408:8100:2514:3f70:c98:15fe:9611:acdc');
// => true
# isIPv4
isIPv4(str)
判断一个字符串是否是一个 IP v4 地址。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isIPv4('192.0.0.1');
// => true
# isIPv6
isIPv6(str)
判断一个字符串是否是一个 IP v6 地址。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isIPv6('2408:8100:2514:3f70:c98:15fe:9611:acdc');
// => true
# isQQ
isQQ(str)
判断一个字符串是否是一个 QQ 号。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isQQ('10000');
// => true
# isRealNumber
isRealNumber(str)
判断一个字符串是否是一个实数。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isRealNumber('0.12');
// => true
isRealNumber('1.2e10');
// => true
isRealNumber('1.2e-10');
// => true
# isRgb
isRgb(str)
判断一个字符串是否是一个 RGB 颜色值。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isRgb('rgb(255, 255, 255)');
// => true
isRgb('RGB(255, 255, 255)');
// => true
# isRgba
isRgba(str)
判断一个字符串是否是个 RGBA 颜色值。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isRgb('rgba(255, 255, 255, 0.5)');
// => true
isRgb('RGBA(255, 255, 255, 0.5)');
// => true
# string
# countChars
countChars(str, [type])
计算一个字符串当中字符数量。
起始版本
0.1.0
参数
str (string)
[type = 0] (number)
:如果type
是0
,则返回字符串的长度值(length)。如果type
是1
,则一个全宽字符计数为 2,一个半宽字符计数为 1。如果type
是2
,则一个全宽字符计数为 1,一个半宽字符计数为 0.5,但是最终结果会向上取整。
返回值
(number)
示例
countChars('hello你好')
// => 7
countChars('hello你好', 1)
// => 9
countChars('hello你好', 2)
// => 5
# countLines
countLines(str)
计算字符串的行数(以\n
为分隔符)。
起始版本
0.1.0
参数
str (string)
返回值
(number)
示例
const str = `hello
world
1`;
countLines(str);
// => 3
# escapeRegexp
escapeRegexp(str)
对正则表达式当中的特殊字符进行转义。
起始版本
0.1.0
参数
str (string)
返回值
(string)
示例
escapeRegexp('a-z');
// => "a\-z"
# firstChar
firstChar(str)
返回一个字符串的首个字符。
起始版本
0.1.0
参数
str (string)
返回值
(string)
示例
firstChar('hello');
// => "h"
# isValidJSON
isValidJSON(str)
判断一个字符串是否是一个合法的 JSON 字符串。
起始版本
0.1.0
参数
str (string)
返回值
(boolean)
示例
isValidJSON('{"name": "Alex Chao"}');
// => true
isValidJSON('""');
// => true
# lastChar
lastChar(str)
返回一个字符串的最后一个字符。
起始版本
0.1.0
参数
str (string)
返回值
(string)
示例
lastChar('hello');
// => "o"
# truncate
truncate([str = ''], [options = {}])
返回一个裁剪之后的字符串,并可指定结尾字符串。
起始版本
0.1.0
参数
[str = ""] (string)
:被裁剪的字符串。[options = {}] (Object)
:配置对象[length] (number)
:允许的最大字符串长度,默认为str
长度。[omission = "..."] (string)
:结尾字符串。[countType = 0] (number)
:如何对半宽与全宽字符计数。0
:每个字符长度都算做 1。1
:半宽长度算作 1,全宽字符长度算作 2。2
:两个半宽字符长度算作 1,不足按 1 算,全宽字符长度算作 1。
返回值
(string)
示例
const str = 'hello, 你好,world!世界!';
truncate(str);
truncate(str, { length: 10 });
truncate(str, { omission: '***' });
truncate(str, { length: 10, countType: 1 });
# ua
# isAlipay
isAlipay()
判断运行环境是否是支付宝。
起始版本
0.14.0
返回值
(boolean)
# isAndroid
isAndroid()
判断运行环境是否是安卓。
起始版本
0.1.0
返回值
(boolean)
# isIOS
isIOS()
判断运行环境是否是 iOS。
起始版本
0.1.0
返回值
(boolean)
# isIPad
isIPad()
判断运行环境是否是 iPad。
起始版本
0.1.0
返回值
(boolean)
# isIPhone
isIPhone()
判断运行环境是否是 iPhone。
起始版本
0.1.0
返回值
(boolean)
# isLinux
isLinux()
判断运行环境是否是 Linux。
起始版本
0.1.0
返回值
(boolean)
# isMacOS
isMacOS()
判断运行环境是否是 macOS。
起始版本
0.1.0
返回值
(boolean)
# isMobile
isMobile()
判断运行环境是否是移动端。
起始版本
0.1.0
返回值
(boolean)
# isTablet
isTablet()
判断运行环境是否是平板。
起始版本
0.1.0
返回值
(boolean)
# isWechat
isWechat()
判断运行环境是否是微信。
起始版本
0.1.0
返回值
(boolean)
# isWindows
isWindows()
判断运行环境是否是 Windows。
起始版本
0.1.0
返回值
(boolean)
# url
# isAbsolute
isAbsolute(url)
判断url
是否是一个绝对 URL。
起始版本
0.6.0
参数
url (string)
返回值
(boolean)
示例
isAbsolute('example.com')
// => false
isAbsolute('https://example.com')
// => true
# join
join(...args)
拼接并正规化 URL 子字符串。
起始版本
0.8.0
参数
args (string[])
返回值
(string)
示例
join('https://example.com', 'foo', 'bar')
// => 'https://example.com/foo/bar'
join('https://example.com', 'foo', 'bar/')
// => 'https://example.com/foo/bar/'
join('https://example.com', 'foo', 'bar', '?hello=world')
// => 'https://example.com/foo/bar?hello=world'
# parse
parse(url)
起始版本
0.4.0
参数
url (string)
:被解析的 url。
返回值
(Object)
示例
parse('http://admin:123456@example.com:8080/path-to-somewhere?foo=1&lang=js&lang=css#title')
// =>
// {
// hash: '#title',
// host: 'example.com:8080',
// hostname: 'example.com',
// href: 'http://admin:123456@example.com:8080/path-to-somewhere?foo=1&lang=js&lang=css#title',
// origin: 'http://example.com:8080',
// password: '123456',
// pathname: '/path-to-somewhere',
// port: '8080',
// protocol: 'http',
// query: {
// foo: '1',
// lang: ['js', 'css']
// },
// search: '?foo=1&lang=js&lang=css',
// username: 'admin'
// }
# stringify
stringify(urlParts)
起始版本
0.4.0
参数
urlParts (Object)
:URL 配置对象。
返回值
(string)
示例
stringify({
protocol: 'http',
host: 'example.com',
pathname: '/path',
query: {
foo: '1',
lang: ['js', 'css']
}
})
// => 'http://example.com/path?foo=1&lang=js&lang=css'
# ajax
ajax(url, [options])
发送一个 AJAX 请求。
起始版本
0.1.0
参数
url (string)
:请求 URL。[options] (Object)
:配置对象。[async = true] (boolean)
:是否为异步请求。[beforeSend] (Function)
:发送请求之前的回调,如果返回false
,则不会发送请求。[data = null] (string|Object)
:发送的数据。[headers = null] (Object)
:请求头部。[method = "get"] (string)
:请求方法。[responseType = "json"] (string)
:如何解析响应值。[timeout = 0] (number)
:请求超时设置。
返回值
(Promise)
j
# classNames
classNames(...args)
根据任意数量的参数生成一个元素的className
值,和 JedWatson/classnames (opens new window) 相同。
起始版本
0.10.0
参数
[args] (any[])
:见下面示例。
返回值
(string)
示例
classNames('foo', 'bar'); // => 'foo bar'
classNames('foo', { bar: true }); // => 'foo bar'
classNames({ 'foo-bar': true }); // => 'foo-bar'
classNames({ 'foo-bar': false }); // => ''
classNames({ foo: true }, { bar: true }); // => 'foo bar'
classNames({ foo: true, bar: true }); // => 'foo bar'
// 接受各种类型参数
classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux'
// 假值(falsy)和非字符串/对象/数组值会被忽略
classNames(null, false, 'bar', undefined, 0, 1, true, { baz: null }, ''); // => 'bar'
classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'
// 数组会根据上面规则展开
const arr = ['b', { c: true, d: false }];
classNames('a', arr); // => 'a b c'
# css
css(el, [prop])
获取一个元素所有或指定属性的值。
起始版本
0.1.0
参数
el (Element|string)
:一个元素对象或者 CSS 选择器。[prop] (string)
:CSS 属性名。
返回值
(string | Object)
示例
const el = document.getElementById('el');
// 获取所有属性值
css(el);
// => CSSStyleDeclaration { ... }
// 传入一个 CSS 选择器
css('#el');
// => CSSStyleDeclaration { ... }
// 获取指定属性的值
css('#el', 'width');
// => '100px'
# delay
delay([duration])
返回一个 Promise,持续duration
时间之后 resolve。
起始版本
0.1.0
参数
[duration = 0] (number)
:持续时间。
返回值
(Promise)
# download
download(file, [fileName])
下载一个 URL 所定位的文件或Blob
(opens new window)对象或File
(opens new window)对象。该函数来自于eligrey/FileSaver.js (opens new window)。
参数
file (string | Blob | File)
:一个 URL 字符串或Blob
对象或File
对象。[fileName] (string)
:优先使用该参数作为文件名,其次,如果file
参数是File
类型,则使用file
的name
属性,最后使用'download'
字符串。
示例
// 下载 url 字符串所定位的文件
download('https://micell.org/logo.svg', 'micell-logo.svg')
// 下载 Blob 对象
const blob = new Blob(['Hello, world!'], { type: 'text/plain;charset=utf-8' })
download(blob, 'hello-world.txt')
# jsonp
jsonp(url, [options])
起始版本
0.1.0
参数
url (string)
:请求数据的 URL。[options] (Object)
:配置对象。[callback] (string)
:数据请求成功或失败的回调函数。[responseType = "json"] (string)
:对返回的数据如何处理。[timeout = 0] (number)
:请求超时配置,默认0
,表示设置请求超时。
返回值
(Promise)
# md5
md5(input)
起始版本
0.4.0
参数
input (string | number[] | Uint8Array)
返回值
(string)
示例
md5('hello 你好')
// => '429f2c0b03ebc9911455cbec2a09bc6f'
# noop
该函数为一个空函数,不接受任何参数,不返回任何值。
起始版本
0.1.0
# numberFormat
numberFormat(number, [digits], [dot], [sep])
格式化一个数值。
起始版本
0.1.0
参数
number (number)
:被格式化的数值。[digits = -1] (number)
:保留的小数位数,如果为-1
则表示保留所有小数位数,如果该值> -1
,则保留的最后一位为四舍五入所得。[dot = "."] (string)
:整数与小数部分的分隔符。[sep = ","] (string)
:千分位分隔符。
返回值
(string)
示例
numberFormat(3.1415);
// => '3.1415'
numberFormat(3.1415, 3);
// => '3.142'
# raf
raf(callback)
如果浏览器支持 requestAnimationFrame (opens new window),则和其一样。否则,使用setTimeout
来模拟。
起始版本
0.2.0
参数
callback (Function)
:在下一次重绘(repaint)之前调用的函数。一个由performance.now()
返回的类似 DOMHighResTimeStamp (opens new window) 的时间值将会作为第一个参数传入。
返回值
(number)
:用于取消下一次回调的定时器 ID。
示例
raf(time => console.log(time))
// => 120.123
const timerId = raf(() => console.log('never happen'))
raf.cancel(timerId)
# randomNumbers
randomNumbers(start, end, length)
返回一个随机整数数组,每个整数都大于或等于start
,并且小于或等于end
。数组当中每个元素互补相等。
起始版本
0.8.0
参数
start (number)
:最小整数。end (number)
:最大整数。length (number)
:整数的数量。
返回值
(numbers[])
示例
randomNumbers(1, 10, 5)
// => [9, 4, 1, 5, 7]
# randomString
randomString([len], [chars])
生成一个随机字符串,可以指定字符串的长度和组成字符串的字符集。
起始版本
0.1.0
参数
[len = 32] (number)
:随机字符串长度。[chars] (string)
:组成字符串的字符集,默认为英文大写字母、小写字母及十进制数字,即 A-Za-z0-9。
返回值
(string)
示例
randomString();
// 生成一个长度为 8 的随机字符串。
randomString(8);
// 生成一串随机数字。
randomString(undefined, '0123456789');
# uuid
uuid()
生成一个基于随机数字的 UUID,算法见 rfc4122 (opens new window)。
起始版本
0.1.0
返回值
(string)
示例
uuid();
// => 'fedb3747-4208-475d-b473-e51b583cddaf'