grunt.util
各色工具/库, Lo-Dash、Async 和 Hooker。
返回给定值的"类型(kind)"。就像typeof,但是其返回的是内部的[Class](class/)值。可能返回的结果是"number"、"string"、"boolean"、"function"、"regexp"、"array"、"date"、"error"、"null"、"undefined"和可以表示一切类型的 "object"。
grunt.util.kindOf(value)
返回新的Error实例(也可以抛出)与相应的消息。如果指定的是Error对象而不是message,则返回对象。
另外,如果为 origError 参数指定的是Error对象,并且使用 --stack 选项运行Grunt,则原始的Error堆栈。
grunt.util.error(message [, origError])
将换行符转换为当前系统所采用的形式(Window上是\r\n,其他系统为\n)。
对于给定的字符串,将其所有换行符转换为当前系统所采用的形式,然后返回新的字符串。(Window上是\r\n,其他系统为\n)
grunt.util.normalizelf(string)
递归嵌套的对象和数组,为每个非对象值执行callbackFunction。如果continueFunction返回false, 给定的对象或值将会被跳过。
grunt.util.recurse(object, callbackFunction, continueFunction)
返回被重复n次的字符串str。
grunt.util.repeat(n, str)
给定"a/b"形式的str,如果n为1,返回"a",否则返回"b"。如果不能使用'/',也可以指定的分隔符。
grunt.util.pluralize(n, str, separator)
子进程,并跟踪其stdout、stderr和码。此返回子进程的引用。当子进程时,doneFunction 被。
grunt.util.spawn(options, doneFunction)
options 对象可以指定以下:
var options = { // The command to execute. It should be in the sy 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 }
对于传入的数组或类数组对象,返回数组。对于将arguments对象转换为数组是非常有用的。
grunt.util.toArray(arrayLikeObject)
标准化"返回值"和"传递结果给回调"的,总是传递结果给指定的回调。如果原始返回值,该值将即刻传递给回调,,并指定为最后参数,在所有的预之后。如果原始传递值给回调,,它也会继续照样如此。
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); });
内部工具库
此内部工具库用于解析对象中深度嵌套的。
用于task执行的内部工具库。
外部工具库
不建议使用
下面列出的所有外部工具库已经不再建议使用了。
请使用 npm 管理项目中对第三方工具库的依赖。
例如,如果你需要使用 ,首先通过 npm install lodash 安装,然后在 Gruntfile 中使用即可: var _ = require('lodash');
不建议使用
- 很多有用的数组、和对象工具。 - 很多实用的字符串工具。
grunt.util._.str 可用于与现有Lo-Dash冲突的。
不建议使用
- 对node和浏览器都适用的异步工具。
不建议使用
- 用于调试和做些其他事情的钩子(hook)。