鍍金池/ 教程/ 物聯(lián)網(wǎng)/ grunt.util
深入任務(wù)內(nèi)幕
grunt.option
退出碼
創(chuàng)建插件
grunt.file
grunt
快速入門
項(xiàng)目腳手架
使用命令行工具
Gruntfile 實(shí)例
配置任務(wù)
創(chuàng)建任務(wù)
grunt.log
安裝Grunt
grunt.util
grunt.event
常見問題
grunt.config
grunt.task
grunt.template
grunt.fail

grunt.util

各種實(shí)用工具,包括Lo-Dash,Async和Hooker等。

grunt.util.kindOf

返回給定值的"類型"。就像typeof運(yùn)算符就會返回內(nèi)部的[Class](Class/)信息。這個(gè)方法可能會返回"number""string","boolean""function","regexp","array","date""error","null","undefined"和代表一切類型的"object"

grunt.util.kindOf(value)

grunt.util.error

返回一個(gè)新的Error實(shí)例(它也可以拋出)與相應(yīng)的消息。如果指定的是一個(gè)Error對象而不是message,會返回指定的對象。另外,如果指定一個(gè)Error對象作為origError參數(shù),同時(shí)使用--debug 9選項(xiàng)運(yùn)行Grunt,那么就會輸出原始的Error堆棧信息。

grunt.util.error(message [, origError])

grunt.util.linefeed

將換行符標(biāo)準(zhǔn)化為當(dāng)前操作系統(tǒng)使用的形式(Window上是\r\n,否則為\n)。

grunt.util.normalizeIf

給定一個(gè)字符串,返回一個(gè)新字符串,原始字符串中所有的換行符都會被標(biāo)準(zhǔn)化為當(dāng)前操作系統(tǒng)中使用的形式(Window上是\r\n,否則為\n)。

grunt.util.normalizeIf(string)

grunt.util.recurse

以遞歸的方式遍歷嵌套的對象和數(shù)組,然后為每個(gè)非對象類型的值執(zhí)行callbackFunction(回調(diào)函數(shù))。如果continueFunction返回false,那么就會跳過給定的對象和值。

grunt.util.recurse(object, callbackFunction, continueFunction)

grunt.util.repeat

返回重復(fù)n次的字符串str

grunt.util.repeat(n, str)

grunt.util.pluralize

給定一個(gè)"a/b"形式的str,如果n1則返回"a";否則返回"b"。如果你不能使用'/',你還可以指定一個(gè)自定義的分隔符。

grunt.util.pluralize(n, str, separator)

grunt.util.spawn

生成一個(gè)子進(jìn)程,跟蹤它的stdout(標(biāo)準(zhǔn)輸出),stderr(標(biāo)準(zhǔn)錯(cuò)誤)和退出碼代。這個(gè)方法會返回所生成的子進(jìn)程的引用。當(dāng)子進(jìn)程退出時(shí),就會調(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
}

grunt.util.toArray

給定一個(gè)數(shù)組或者一個(gè)類數(shù)組對象,然后返回一個(gè)(新)數(shù)組。將arguments對象轉(zhuǎn)換為數(shù)組是非常有用的。

grunt.util.toArray(arrayLikeObject)

grunt.util.callbackify

標(biāo)準(zhǔn)化"返回值"和"傳遞結(jié)果給回調(diào)"的函數(shù),并且總是傳遞一個(gè)結(jié)果給指定的回調(diào)函數(shù)。如果原始函數(shù)返回一個(gè)值,那么這個(gè)返回值會立即傳遞給回調(diào)函數(shù),同時(shí)指定為回調(diào)函數(shù)的最后一個(gè)參數(shù),在所有預(yù)定義的參數(shù)之后。如果原始函數(shù)給回調(diào)函數(shù)傳遞一個(gè)值,它也會這么做。

grunt.util.callbackify(syncOrAsyncFunction)

下面這個(gè)例子也許能夠更好的說明這個(gè)問題:

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)部庫

grunt.util.namespace

一個(gè)用于解析對象內(nèi)部深度嵌套的屬性的內(nèi)部庫。

grunt.util.task

一個(gè)用于運(yùn)行任務(wù)內(nèi)部庫。

外部庫

grunt.util._

Lo-Dash - 它帶有很多超級有用的處理數(shù)組、函數(shù)和對象的實(shí)用方法。[Underscore.string] - 它就包含了很多字符串處理的實(shí)用方法。

注意Underscore.string已經(jīng)混合到grunt.util._中了,它也可以以grunt.util._.str的形式調(diào)用這個(gè)方法,但是這樣做它就會與現(xiàn)有的Lo-Dash方法沖突。

grunt.util.async

Async - 用于Node.js和瀏覽器異步操作的實(shí)用工具。

grunt.util.hooker

JavaScript Hooker - 用于調(diào)試和作其他補(bǔ)充的補(bǔ)丁 Monkey-patch 函數(shù)。

上一篇:安裝Grunt