grunt.util
返回给定值的”类型”。就像运算符就会返回内部的[Class](Class/)
信息。这个方法可能会返回"number"
,"string"
,"boolean"
,"function"
,"regexp"
,"array"
,"date"
,"error"
,"null"
,"undefined"
和代表一切类型的"object"
。
grunt.util.error
返回一个新的Error实例(它也可以抛出)与相应的消息。如果指定的是一个Error对象而不是message
,会返回指定的对象。另外,如果指定一个Error对象作为origError
参数,同时使用--debug 9
选项运行Grunt,那么就会输出原始的Error堆栈信息。
grunt.util.linefeed
将换行符标准化为当前操作系统使用的形式(Window上是\r\n
,否则为\n
)。
grunt.util.normalizeIf
给定一个字符串,返回一个新字符串,原始字符串中所有的换行符都会被标准化为当前操作系统中使用的形式(Window上是\r\n
,否则为\n
)。
grunt.util.normalizeIf(string)
grunt.util.recurse
以递归的方式遍历嵌套的对象和数组,然后为每个非对象类型的值执行callbackFunction
(回调函数)。如果continueFunction
返回,那么就会跳过给定的对象和值。
grunt.util.recurse(object, callbackFunction, continueFunction)
grunt.util.pluralize
给定一个"a/b"
形式的str
,如果n
为1
则返回"a"
;否则返回"b"
。如果你不能使用’/‘,你还可以指定一个自定义的分隔符。
grunt.util.pluralize(n, str, separator)
grunt.util.spawn
生成一个子进程,跟踪它的stdout(标准输出),stderr(标准错误)和退出码代。这个方法会返回所生成的子进程的引用。当子进程退出时,就会调用done函数。
grunt.util.spawn(options, doneFunction)
options
对象可以指定下面这些属性:
var options = {
// The command to execute. It should be in the system path.
cmd: commandToExecute,
// If specified, the same grunt bin that is currently running will be
// spawned as the child command, instead of the "cmd" option. Defaults
// to false.
grunt: boolean,
// An array of arguments to pass to the command.
args: arrayOfArguments,
// Additional options for the Node.js child_process spawn method.
opts: nodeSpawnOptions,
// and null will be passed as the error value.
};
done函数可以接收以下参数:
grunt.util.toArray
给定一个数组或者一个类数组对象,然后返回一个(新)数组。将arguments
对象转换为数组是非常有用的。
grunt.util.toArray(arrayLikeObject)
grunt.util.callbackify
grunt.util.callbackify(syncOrAsyncFunction)
下面这个例子也许能够更好的说明这个问题:
function add1(a, b) {
return a + b;
}
function add2(a, b, callback) {
callback(a + b);
}
var fn1 = grunt.util.callbackify(add1);
var fn2 = grunt.util.callbackify(add2);
fn1(1, 2, function(result) {
console.log('1 plus 2 equals ' + result);
});
fn2(1, 2, function(result) {
});
一个用于解析对象内部深度嵌套的属性的内部库。
grunt.util.task
一个用于运行任务内部库。
外部库
grunt.util._
Lo-Dash - 它带有很多超级有用的处理数组、函数和对象的实用方法。[Underscore.string] - 它就包含了很多字符串处理的实用方法。
注意Underscore.string已经混合到grunt.util._
中了,它也可以以的形式调用这个方法,但是这样做它就会与现有的Lo-Dash方法冲突。
grunt.util.async
grunt.util.hooker
- 用于调试和作其他补充的补丁Monkey-patch函数。