grunt.util

返回给定值的”类型”。就像运算符就会返回内部的[Class](Class/)信息。这个方法可能会返回"number""string""boolean""function""regexp""array""date""error""null""undefined"和代表一切类型的"object"

grunt.util.error

返回一个新的Error实例(它也可以抛出)与相应的消息。如果指定的是一个Error对象而不是message,会返回指定的对象。另外,如果指定一个Error对象作为origError参数,同时使用--debug 9选项运行Grunt,那么就会输出原始的Error堆栈信息。

    grunt.util.linefeed

    将换行符标准化为当前操作系统使用的形式(Window上是\r\n,否则为\n)。

    grunt.util.normalizeIf

    给定一个字符串,返回一个新字符串,原始字符串中所有的换行符都会被标准化为当前操作系统中使用的形式(Window上是\r\n,否则为\n)。

    1. grunt.util.normalizeIf(string)

    grunt.util.recurse

    以递归的方式遍历嵌套的对象和数组,然后为每个非对象类型的值执行callbackFunction(回调函数)。如果continueFunction返回,那么就会跳过给定的对象和值。

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

    grunt.util.pluralize

    给定一个"a/b"形式的str,如果n1则返回"a";否则返回"b"。如果你不能使用’/‘,你还可以指定一个自定义的分隔符。

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

    grunt.util.spawn

    生成一个子进程,跟踪它的stdout(标准输出),stderr(标准错误)和退出码代。这个方法会返回所生成的子进程的引用。当子进程退出时,就会调用done函数。

    1. grunt.util.spawn(options, doneFunction)

    options对象可以指定下面这些属性:

    1. var options = {
    2. // The command to execute. It should be in the system path.
    3. cmd: commandToExecute,
    4. // If specified, the same grunt bin that is currently running will be
    5. // spawned as the child command, instead of the "cmd" option. Defaults
    6. // to false.
    7. grunt: boolean,
    8. // An array of arguments to pass to the command.
    9. args: arrayOfArguments,
    10. // Additional options for the Node.js child_process spawn method.
    11. opts: nodeSpawnOptions,
    12. // and null will be passed as the error value.
    13. };

    done函数可以接收以下参数:

    grunt.util.toArray

    给定一个数组或者一个类数组对象,然后返回一个(新)数组。将arguments对象转换为数组是非常有用的。

    1. grunt.util.toArray(arrayLikeObject)

    grunt.util.callbackify

    1. grunt.util.callbackify(syncOrAsyncFunction)

    下面这个例子也许能够更好的说明这个问题:

    1. function add1(a, b) {
    2. return a + b;
    3. }
    4. function add2(a, b, callback) {
    5. callback(a + b);
    6. }
    7. var fn1 = grunt.util.callbackify(add1);
    8. var fn2 = grunt.util.callbackify(add2);
    9. fn1(1, 2, function(result) {
    10. console.log('1 plus 2 equals ' + result);
    11. });
    12. fn2(1, 2, function(result) {
    13. });

    一个用于解析对象内部深度嵌套的属性的内部库。

    grunt.util.task

    一个用于运行任务内部库。

    外部库

    grunt.util._

    Lo-Dash - 它带有很多超级有用的处理数组、函数和对象的实用方法。[Underscore.string] - 它就包含了很多字符串处理的实用方法。

    注意Underscore.string已经混合到grunt.util._中了,它也可以以的形式调用这个方法,但是这样做它就会与现有的Lo-Dash方法冲突。

    grunt.util.async

    grunt.util.hooker

    - 用于调试和作其他补充的补丁Monkey-patch函数。