grunt.util

2018-11-02 14:03 更新

grunt.util

各色工具函數(shù)/庫,包括 Lo-Dash、Async 和 Hooker。

grunt.util.kindOf

返回給定值的"類型(kind)"。就像typeof,但是其返回的是內(nèi)部的[Class](class/)值??赡芊祷氐慕Y(jié)果是"number""string"、"boolean""function"、"regexp"、"array"、"date"、"error""null"、"undefined"和可以表示一切類型的 "object"。

grunt.util.kindOf(value)

grunt.util.error

返回一個新的Error實例(也可以拋出)與相應(yīng)的消息。如果指定的是Error對象而不是message,則返回對象。

另外,如果為 origError 參數(shù)指定的是Error對象,并且使用 --stack 選項運行Grunt,則輸出原始的Error堆棧。

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

grunt.util.linefeed

將換行符轉(zhuǎn)換為當(dāng)前系統(tǒng)所采用的形式(Window上是\r\n,其他系統(tǒng)為\n)。

grunt.util.normalizelf

對于一個給定的字符串,將其所有換行符轉(zhuǎn)換為當(dāng)前系統(tǒng)所采用的形式,然后返回一個新的字符串。(Window上是\r\n,其他系統(tǒng)為\n

grunt.util.normalizelf(string)

grunt.util.recurse

遞歸嵌套的對象和數(shù)組,為每個非對象值執(zhí)行callbackFunction。如果continueFunction返回false, 給定的對象或值將會被跳過。

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

grunt.util.repeat

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

grunt.util.repeat(n, str)

grunt.util.pluralize

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

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

grunt.util.spawn

生成一個子進(jìn)程,并跟蹤其stdout、stderr和退出碼。此方法返回子進(jìn)程的引用。當(dāng)子進(jìn)程退出時,doneFunction 函數(shù)被調(diào)用。

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 函數(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

對于傳入的數(shù)組或類數(shù)組對象,返回一個數(shù)組。對于將arguments對象轉(zhuǎn)換為數(shù)組是非常有用的。

grunt.util.toArray(arrayLikeObject)

grunt.util.callbackify

標(biāo)準(zhǔn)化"返回值"和"傳遞結(jié)果給回調(diào)"的函數(shù),總是傳遞一個結(jié)果給指定的回調(diào)函數(shù)。如果原始函數(shù)返回一個值,該值將即刻傳遞給回調(diào)函數(shù),,并指定為最后一個參數(shù),在所有的預(yù)定義參數(shù)之后。如果原始函數(shù)傳遞一個值給回調(diào)函數(shù),,它也會繼續(xù)照樣如此。

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

grunt.util.namespace

此內(nèi)部工具庫用于解析對象中深度嵌套的屬性。

grunt.util.task

用于task執(zhí)行的內(nèi)部工具庫。

外部工具庫

不建議使用

下面列出的所有外部工具庫已經(jīng)不再建議使用了。

請使用 npm 管理項目中對第三方工具庫的依賴。

例如,如果你需要使用 Lo-Dash,首先通過 npm install lodash 安裝,然后在 Gruntfile 文件中使用即可: var _ = require('lodash');

grunt.util._

不建議使用

Lo-Dash - 很多有用的數(shù)組、函數(shù)和對象工具方法。 Underscore.string - 很多實用的字符串工具函數(shù)。

grunt.util._.str is available for methods that conflict with existing Lo-Dash methods.

grunt.util.async

不建議使用

Async - 對node和瀏覽器都適用的異步工具。

grunt.util.hooker

不建議使用

JavaScript Hooker - 用于調(diào)試和做些其他事情的鉤子(hook)函數(shù)。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號