1 | [Grunt homepage](https://github.com/cowboy/grunt) | [Documentation table of contents](toc.md)
|
2 |
|
3 | # [The grunt API](api.md) / grunt.utils
|
4 |
|
5 | Miscellaneous utilities, including Underscore.js, Async and Hooker.
|
6 |
|
7 | See the [utils lib source](../lib/grunt/utils.js) for more information.
|
8 |
|
9 | ## The utils API
|
10 |
|
11 | ### grunt.utils.kindOf
|
12 | Return the "kind" of a value. Like `typeof` but returns the internal `[[Class]]` value. Possible results are `"number"`, `"string"`, `"boolean"`, `"function"`, `"regexp"`, `"array"`, `"date"`, `"error"`, `"null"`, `"undefined"` and the catch-all `"object"`.
|
13 |
|
14 | ```javascript
|
15 | grunt.utils.kindOf(value)
|
16 | ```
|
17 |
|
18 | ### grunt.utils.linefeed
|
19 | The linefeed character, normalized for the current operating system. (`\r\n` on Windows, `\n` otherwise)
|
20 |
|
21 | ### grunt.utils.normalizelf
|
22 | Given a string, return a new string with all the linefeeds normalized for the current operating system. (`\r\n` on Windows, `\n` otherwise)
|
23 |
|
24 | ```javascript
|
25 | grunt.utils.normalizelf(string)
|
26 | ```
|
27 |
|
28 | ### grunt.utils.recurse
|
29 | Recurse through nested objects and arrays, executing `callbackFunction` for each non-object value. If `continueFunction` returns `false`, a given object or value will be skipped.
|
30 |
|
31 | ```javascript
|
32 | grunt.utils.recurse(object, callbackFunction, continueFunction)
|
33 | ```
|
34 |
|
35 | See the [config lib source](../lib/grunt/config.js) for usage examples.
|
36 |
|
37 | ### grunt.utils.repeat
|
38 | Return string `str` repeated `n` times.
|
39 |
|
40 | ```javascript
|
41 | grunt.utils.repeat(n, str)
|
42 | ```
|
43 |
|
44 | ### grunt.utils.pluralize
|
45 | Given `str` of `"a/b"`, If `n` is `1`, return `"a"` otherwise `"b"`. You can specify a custom separator if '/' doesn't work for you.
|
46 |
|
47 | ```javascript
|
48 | grunt.utils.pluralize(n, str, separator)
|
49 | ```
|
50 |
|
51 | ### grunt.utils.spawn
|
52 | Spawn a child process, keeping track of its stdout, stderr and exit code. The method returns a reference to the spawned child. When the child exits, the done function is called.
|
53 |
|
54 | ```javascript
|
55 | grunt.utils.spawn(options, doneFunction)
|
56 | ```
|
57 |
|
58 | The `options` object has these possible properties:
|
59 |
|
60 | ```javascript
|
61 | var options = {
|
62 | // The command to execute. It should be in the system path.
|
63 | cmd: commandToExecute,
|
64 | // An array of arguments to pass to the command.
|
65 | args: arrayOfArguments,
|
66 | // Additional options for the Node.js child_process spawn method.
|
67 | opts: nodeSpawnOptions,
|
68 | // If this value is set and an error occurs, it will be used as the value
|
69 | // and null will be passed as the error value.
|
70 | fallback: fallbackValue
|
71 | };
|
72 | ```
|
73 |
|
74 | The done function accepts these arguments:
|
75 |
|
76 | ```javascript
|
77 | function doneFunction(error, result, code) {
|
78 | // If the exit code was non-zero and a fallback wasn't specified, the error
|
79 | // object is the same as the result object.
|
80 | error
|
81 | // The result object is an object with the properties .stdout, .stderr, and
|
82 | // .code (exit code).
|
83 | result
|
84 | // When result is coerced to a string, the value is stdout if the exit code
|
85 | // was zero, the fallback if the exit code was non-zero and a fallback was
|
86 | // specified, or stderr if the exit code was non-zero and a fallback was
|
87 | // not specified.
|
88 | String(result)
|
89 | // The numeric exit code.
|
90 | code
|
91 | }
|
92 | ```
|
93 |
|
94 | See the [init task source](../tasks/init.js) and the [qunit task source](../tasks/qunit.js) for usage examples.
|
95 |
|
96 |
|
97 | ### grunt.utils.toArray
|
98 | Given an array or array-like object, return an array. Great for converting `arguments` objects into arrays.
|
99 |
|
100 | ```javascript
|
101 | grunt.utils.toArray(arrayLikeObject)
|
102 | ```
|
103 |
|
104 | ## Internal libraries
|
105 |
|
106 | ### grunt.utils.namespace
|
107 | An internal library for resolving deeply-nested properties in objects.
|
108 |
|
109 | ### grunt.utils.task
|
110 | An internal library for task running.
|
111 |
|
112 |
|
113 | ## External libraries
|
114 |
|
115 | ### grunt.utils._
|
116 | [Underscore.js](http://underscorejs.org/) - Tons of super-useful array, function and object utility methods.
|
117 | [Underscore.string](https://github.com/epeli/underscore.string) - Tons of string utility methods.
|
118 |
|
119 | Note that Underscore.string is mixed into `grunt.utils._` but is also available as `grunt.utils._.str` for methods that conflict with existing Underscore.js methods.
|
120 |
|
121 | ### grunt.utils.async
|
122 | [Async](https://github.com/caolan/async) - Async utilities for node and the browser.
|
123 |
|
124 | ### grunt.utils.hooker
|
125 | [JavaScript Hooker](https://github.com/cowboy/javascript-hooker) - Monkey-patch (hook) functions for debugging and stuff.
|
126 |
|