grunt.util
Gruntfile 和任務的雜項公用程式。
grunt.util.kindOf
傳回值的「種類」。類似 typeof
,但傳回內部的 [Class](Class/)
值。可能的結果為 "number"
、"string"
、"boolean"
、"function"
、"regexp"
、"array"
、"date"
、"error"
、"null"
、"undefined"
和萬用 "object"
。
grunt.util.kindOf(value)
grunt.util.error
傳回新的 Error 實例(可以拋出),並附上適當的訊息。如果指定 Error 物件而不是 message
,則會傳回該物件。此外,如果為 origError
指定 Error 物件,且 Grunt 以 --stack
選項執行,則會傾印原始 Error 堆疊。
grunt.util.error(message [, origError])
grunt.util.linefeed
換行字元,已針對目前作業系統正規化。(Windows 為 \r\n
,其他為 \n
)
grunt.util.normalizelf
給定字串,傳回新的字串,其中所有換行字元已針對目前作業系統正規化。(Windows 為 \r\n
,其他為 \n
)
grunt.util.normalizelf(string)
grunt.util.recurse
遞迴巢狀物件和陣列,對每個非物件值執行 callbackFunction
。如果 continueFunction
傳回 false
,則會略過給定的物件或值。
grunt.util.recurse(object, callbackFunction, continueFunction)
grunt.util.repeat
傳回字串 str
,重複 n
次。
grunt.util.repeat(n, str)
grunt.util.pluralize
給定 "a/b"
的 str
,如果 n
為 1
,則傳回 "a"
,否則傳回 "b"
。如果「/」對您不起作用,您可以指定自訂分隔符號。
grunt.util.pluralize(n, str, separator)
grunt.util.spawn
產生一個子程序,追蹤其 stdout、stderr 和結束代碼。此方法會傳回一個參照至已產生的子程序。當子程序結束時,會呼叫 doneFunction
。
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
};
doneFunction
接受下列引數
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
給定一個陣列或類似陣列的物件,傳回一個陣列。非常適合將 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) {
console.log('1 plus 2 equals ' + result);
});
內部函式庫
grunt.util.namespace
一個用於解析物件中深度巢狀屬性的內部函式庫。
grunt.util.task
一個用於執行任務的內部函式庫。
外部函式庫
已棄用
下列列出的所有外部函式庫現在都已棄用。
請使用 npm 來管理專案相依性中的這些外部函式庫。
例如,如果您想要使用 Lo-Dash,請先安裝它:npm install lodash
,然後在您的 Gruntfile
中使用它:var _ = require('lodash');
。
grunt.util._
已棄用
grunt.util._.str
可用於與現有 Lo-Dash 方法衝突的方法。
grunt.util.async
已棄用
Async - 給 node 和瀏覽器的非同步公用程式。
grunt.util.hooker
已棄用
JavaScript Hooker - 用於偵錯等用途的猴子修補(掛鉤)函式。