util

fis. util

fis 中工具类操作集合。lodash 中所有方法都挂载在此名字空间下面。

Source:

Example

/a/b//c\d/ -> /a/b/c/d

Methods

(static) applyMatches(path, matches, allowed)

将 matches 规则应用到某个对象上面。

Parameters:
Name Type Description
path String

路径。用来与 match 规则匹配

matches Array

规则数组

allowed Array

可以用来过滤掉不关心的字段。

Source:

(static) base64(data) → {String}

生成base64串

Parameters:
Name Type Description
data String | Buffer | Array

数据源

Source:
Returns:

base64串

Type
String

(static) camelcase(str) → {String}

驼峰写法转换

Parameters:
Name Type Description
str String

待转换字符串

Source:
Returns:

转换后的字符串

Type
String

(static) clone(source) → {any}

clone一个变量

Parameters:
Name Type Description
source any

变量

Source:
Returns:

clone值

Type
any

(static) copy(rSource, target, include, exclude, uncover, move)

复制符合include和exclude规则的文件到目标目录,若rSource为文件夹则递归其下属每个文件

Parameters:
Name Type Description
rSource String

源路径

target String

目标路径

include Array

包含匹配规则

exclude Array

排除匹配规则

uncover Boolean

是否覆盖

move Boolean

是否移动

Source:

(static) del()

删除指定目录下面的文件。

Source:

(static) download(url, callback, extract, opt)

下载功能实现

Parameters:
Name Type Description
url String

下载的url

callback function

回调

extract String

压缩提取路径

opt Object

配置

Source:

(static) escapeReg(str) → {String}

正则串编码转义

Parameters:
Name Type Description
str String

正则串

Source:
Returns:

普通字符串

Type
String

(static) escapeShellArg(命令)

shell编码转义

Parameters:
Name Type Description
命令 String
Source:

(static) escapeShellCmd(命令)

shell命令编码转义

Parameters:
Name Type Description
命令 String
Source:

(static) exist(filepath)

判断文件是否存在。

Parameters:
Name Type Description
filepath String

文件路径。

Source:

(static) ext(str) → {Object}

path处理

Parameters:
Name Type Description
str String

待处理的路径

Source:
Returns:
Type
Object
Example
str = /a.b.c/f.php?kw=%B2%E5%BB%AD#addfhubqwek
                     {
                        origin: '/a.b.c/f.php?kw=%B2%E5%BB%AD#addfhubqwek',
                        rest: '/a.b.c/f',
                        hash: '#addfhubqwek',
                        query: '?kw=%B2%E5%BB%AD',
                        fullname: '/a.b.c/f.php',
                        dirname: '/a.b.c',
                        ext: '.php',
                        filename: 'f',
                        basename: 'f.php'
                     }

(static) filter(str, include, exclude) → {Boolean}

str过滤处理,判断include中匹配str为true,exclude中不匹配str为true

Parameters:
Name Type Description
str String

待处理的字符串

include Array

include匹配规则

exclude Array

exclude匹配规则

Source:
Returns:

是否匹配

Type
Boolean

(static) find(rPath, include, exclude, root) → {Array}

若rPath为文件夹,夹遍历目录下符合include和exclude规则的全部文件;若rPath为文件,直接匹配该文件路径是否符合include和exclude规则

Parameters:
Name Type Description
rPath String

要查找的目录

include Array

包含匹配正则集合,可传null

exclude Array

排除匹配正则集合,可传null

root String

根目录

Source:
Returns:

符合规则的文件路径的集合

Type
Array

(static) getMimeType(ext) → {String}

匹配文件后缀所属MimeType类型

Parameters:
Name Type Description
ext String

文件后缀

Source:
Returns:

MimeType类型

Type
String

(static) glob(pattern, str, options) → {Boolean|RegExp}

模拟linux glob文法实现,但()为捕获匹配模式

Parameters:
Name Type Description
pattern String

符合fis匹配文法的正则串

str String

待匹配的字符串

options Object

匹配设置参数 @see minimatch.makeRe

Source:
Returns:

若str参数为String则返回str是否可被pattern匹配
若str参数不为String,则返回正则表达式

Type
Boolean | RegExp

(static) install(name, version, opt)

读取fis组件安装

Parameters:
Name Type Description
name String

组件名称

version String

版本标识

opt Object

安装配置 { remote, extract, before, error, done, }

Source:

(static) isAbsolute(path) → {Boolean}

判断路径是否为绝对路径

Parameters:
Name Type Description
path String

路径

Source:
Returns:

true为是

Type
Boolean

(static) isDir(path) → {Boolean}

是否为文件夹

Parameters:
Name Type Description
path String

路径

Source:
Returns:

true为是

Type
Boolean

(static) isEmpty(obj) → {Boolean}

判断对象是否为null,[],{},0

Parameters:
Name Type Description
obj Object

待测对象

Source:
Returns:

是否为空

Type
Boolean

(static) isFile(path) → {Boolean}

是否为一个文件

Parameters:
Name Type Description
path String

路径

Source:
Returns:

true为是

Type
Boolean

(static) isImageFile(path) → {Boolean}

是否为配置中的image文件类型

Parameters:
Name Type Description
path String

路径

Source:
Returns:
Type
Boolean

(static) isTextFile(path) → {Boolean}

是否为配置中的text文件类型

Parameters:
Name Type Description
path String

路径

Source:
Returns:
Type
Boolean

(static) isUtf8(bytes) → {Boolean}

判断Buffer是否为utf8

Parameters:
Name Type Description
bytes Buffer

待检数据

Source:
Returns:

true为utf8

Type
Boolean

(static) isWin() → {Boolean}

是否为windows系统

Source:
Returns:
Type
Boolean

(static) map(obj, callback, merge)

对象枚举元素遍历,若merge为true则进行_.assign(obj, callback),若为false则回调元素的key value index

Parameters:
Name Type Description
obj Object

源对象

callback function | Object

回调函数|目标对象

merge Boolean

是否为对象赋值模式

Source:

(static) md5(data, len) → {String}

按位数生成md5串

Parameters:
Name Type Description
data String | Buffer

数据源

len Number

长度

Source:
Returns:

md5串

Type
String

(static) merge(source, target) → {Object}

将target合并到source上,新值为undefiend一样会覆盖掉原有数据

Parameters:
Name Type Description
source Object

源对象

target Object

目标对象

Source:
Returns:

合并后的对象

Type
Object

(static) mkdir(path, mode)

递归创建文件夹

Parameters:
Name Type Description
path String

路径

mode Number

创建模式

Source:

(static) mtime(path) → {Date}

获取路径最近修改时间

Parameters:
Name Type Description
path String

路径

Source:
Returns:

时间(GMT+0800)

Type
Date

(static) nohup(cmd, options, callback)

调起nohup命令

Parameters:
Name Type Description
cmd String

执行的语句

options Object

配置参数,可传可不传 @see child_process.exec options

callback function

nohup执行完毕的回调函数

Source:

(static) pad(str, len, fill, pre) → {String}

固定长度字符前后缀填补方法(fillZero)

Parameters:
Name Type Description
str String

初始字符串

len Number

固定长度

fill String

填补的缀

pre Boolean

前缀还是后缀

Source:
Returns:

填补后的字符串

Type
String

(static) parseUrl(url, opt) → {Object}

url解析函数,规则类似require('url').parse

Parameters:
Name Type Description
url String

待解析的url

opt Object

解析配置参数 { host|hostname, port, path, method, agent }

Source:
Returns:

{ protocol, host, port, path, method, agent }

Type
Object

(static) pathinfo(path) → {Object}

生成路径信息

Parameters:
Name Type Description
path String | Array

路径,可使用多参数传递:pathinfo('a', 'b', 'c')

Source:
Returns:

@see ext()

Type
Object

(static) pipe(type, callback, def)

加载处理fis模块下的全部插件,如fis3-plugin-*

Parameters:
Name Type Description
type String

模块名

callback function

回调

def Object

模块获取的默认值 @see fis.config.get def

Source:

(static) query(str) → {Object}

path处理,提取path中rest部分(?之前)、query部分(?#之间)、hash部分(#之后)

Parameters:
Name Type Description
str String

待处理的url

Source:
Returns:

{
origin: 原始串
rest: path部分(?之前)
query: query部分(?#之间)
hash: hash部分(#之后)
}

Type
Object

(static) read(path, convert) → {String}

读取文件内容

Parameters:
Name Type Description
path String

路径

convert Boolean

是否使用text方式转换文件内容的编码 @see readBuffer

Source:
Returns:

文件内容

Type
String

(static) readBuffer(buffer) → {String}

处理Buffer编码方式

Parameters:
Name Type Description
buffer Buffer

待读取的Buffer

Source:
Returns:

判断若为utf8可识别的编码则去掉bom返回utf8编码后的String,若不为utf8可识别编码则返回gbk编码后的String

Type
String

(static) readJson(path) → {Object}

读取JSON文件

Parameters:
Name Type Description
path String

路径

Source:
Returns:

JSON文件内容JSON.parse后得到的对象

Type
Object

(static) realpath(path) → {String}

返回path的绝对路径,若path不存在则返回false

Parameters:
Name Type Description
path String

路径

Source:
Returns:

绝对路径

Type
String

(static) realpathSafe(path) → {String}

多功能path处理

Parameters:
Name Type Description
path String

路径

Source:
Returns:

处理后的路径

Type
String

(static) stringQuote(str, quotes) → {Object}

提取字符串中的引号和一对引号包围的内容

Parameters:
Name Type Description
str String

待处理字符串

quotes String

初始引号可选范围,缺省为[',"]

Source:
Returns:

{
origin: 源字符串
rest: 引号包围的文字内容
quote: 引号类型
}

Type
Object

(static) toEncoding(str, encoding) → {String}

字符串编码转换

Parameters:
Name Type Description
str String | Number | Array | Buffer

待处理的字符串

encoding String

编码格式

Source:
Returns:

编码转换后的字符串

Type
String

(static) touch(path, mtime)

修改文件时间戳

Parameters:
Name Type Description
path String

路径

mtime Date | Number

时间戳

Source:

(static) upload(url, opt, data, content, subpath, callback)

遵从RFC规范的文件上传功能实现

Parameters:
Name Type Description
url String

上传的url

opt Object

配置

data Object

要上传的formdata,可传null

content String

上传文件的内容

subpath String

上传文件的文件名

callback function

上传后的回调

Source:

(static) write(path, data, charset, append)

写文件,若路径不存在则创建

Parameters:
Name Type Description
path String

路径

data String

写入内容

charset String

编码方式

append Boolean

是否为追加模式

Source: