各種實用工具,包括Lo-Dash,Async和Hooker等。
返回給定值的"類型"。就像typeof運算符就會返回內(nèi)部的[Class](Class/)信息。這個方法可能會返回"number","string","boolean","function","regexp","array","date","error","null","undefined"和代表一切類型的"object"。
grunt.util.kindOf(value)
返回一個新的Error實例(它也可以拋出)與相應(yīng)的消息。如果指定的是一個Error對象而不是message,會返回指定的對象。另外,如果指定一個Error對象作為origError參數(shù),同時使用--debug 9選項運行Grunt,那么就會輸出原始的Error堆棧信息。
grunt.util.error(message [, origError])
將換行符標(biāo)準(zhǔn)化為當(dāng)前操作系統(tǒng)使用的形式(Window上是\r\n,否則為\n)。
給定一個字符串,返回一個新字符串,原始字符串中所有的換行符都會被標(biāo)準(zhǔn)化為當(dāng)前操作系統(tǒng)中使用的形式(Window上是\r\n,否則為\n)。
grunt.util.normalizeIf(string)
以遞歸的方式遍歷嵌套的對象和數(shù)組,然后為每個非對象類型的值執(zhí)行callbackFunction(回調(diào)函數(shù))。如果continueFunction返回false,那么就會跳過給定的對象和值。
grunt.util.recurse(object, callbackFunction, continueFunction)
返回重復(fù)n次的字符串str。
grunt.util.repeat(n, str)
給定一個"a/b"形式的str,如果n為1則返回"a";否則返回"b"。如果你不能使用'/',你還可以指定一個自定義的分隔符。
grunt.util.pluralize(n, str, separator)
生成一個子進程,跟蹤它的stdout(標(biāo)準(zhǔn)輸出),stderr(標(biāo)準(zhǔn)錯誤)和退出碼代。這個方法會返回所生成的子進程的引用。當(dāng)子進程退出時,就會調(diào)用done函數(shù)。
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,
// If this value is set and an error occurs, it will be used as the value
// and null will be passed as the error value.
fallback: fallbackValue
};
done函數(shù)可以接收以下參數(shù):
function doneFunction(error, result, code) {
// If the exit code was non-zero and a fallback wasn't specified, an Error
// object, otherwise null.
error
// The result object is an object with the properties .stdout, .stderr, and
// .code (exit code).
result
// When result is coerced to a string, the value is stdout if the exit code
// was zero, the fallback if the exit code was non-zero and a fallback was
// specified, or stderr if the exit code was non-zero and a fallback was
// not specified.
String(result)
// The numeric exit code.
code
}
給定一個數(shù)組或者一個類數(shù)組對象,然后返回一個(新)數(shù)組。將arguments對象轉(zhuǎn)換為數(shù)組是非常有用的。
grunt.util.toArray(arrayLikeObject)
標(biāo)準(zhǔn)化"返回值"和"傳遞結(jié)果給回調(diào)"的函數(shù),并且總是傳遞一個結(jié)果給指定的回調(diào)函數(shù)。如果原始函數(shù)返回一個值,那么這個返回值會立即傳遞給回調(diào)函數(shù),同時指定為回調(diào)函數(shù)的最后一個參數(shù),在所有預(yù)定義的參數(shù)之后。如果原始函數(shù)給回調(diào)函數(shù)傳遞一個值,它也會這么做。
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) {
console.log('1 plus 2 equals ' + result);
});
一個用于解析對象內(nèi)部深度嵌套的屬性的內(nèi)部庫。
一個用于運行任務(wù)內(nèi)部庫。
Lo-Dash - 它帶有很多超級有用的處理數(shù)組、函數(shù)和對象的實用方法。[Underscore.string] - 它就包含了很多字符串處理的實用方法。
注意Underscore.string已經(jīng)混合到grunt.util._中了,它也可以以grunt.util._.str的形式調(diào)用這個方法,但是這樣做它就會與現(xiàn)有的Lo-Dash方法沖突。
Async - 用于Node.js和瀏覽器異步操作的實用工具。
JavaScript Hooker - 用于調(diào)試和作其他補充的補丁 Monkey-patch 函數(shù)。