grunt.util


本文整理自网络,侵删。

grunt.util

各色工具函数/库,包括 Lo-Dash、Async 和 Hooker。

grunt.util.kindOf

返回给定值的"类型(kind)"。就像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对象,并且使用 --stack 选项运行Grunt,则输出原始的Error堆栈。

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

grunt.util.linefeed

将换行符转换为当前系统所采用的形式(Window上是\r\n,其他系统为\n)。

grunt.util.normalizelf

对于一个给定的字符串,将其所有换行符转换为当前系统所采用的形式,然后返回一个新的字符串。(Window上是\r\n,其他系统为\n

grunt.util.normalizelf(string)

grunt.util.recurse

递归嵌套的对象和数组,为每个非对象值执行callbackFunction。如果continueFunction返回false, 给定的对象或值将会被跳过。

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

grunt.util.repeat

返回被重复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

生成一个子进程,并跟踪其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

用于task执行的内部工具库。

外部工具库

不建议使用

阅读剩余部分

相关阅读 >>

Grunt 常见问题

Grunt.config

Grunt.file

Grunt 快速入门

Grunt 深入任务内幕

Grunt.task

Grunt 退出码

安装 Grunt

Grunt.option

Grunt 创建插件

更多相关阅读请进入《Grunt》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...