78d2f27c77d3002a51f716e244f124e8078b160c.svn-base
4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
[Grunt homepage](https://github.com/gruntjs/grunt) | [Documentation table of contents](toc.md)
# [The grunt API](api.md) / grunt.utils
Miscellaneous utilities, including Underscore.js, Async and Hooker.
See the [utils lib source](../lib/grunt/utils.js) for more information.
## The utils API
### grunt.utils.kindOf
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"`.
```javascript
grunt.utils.kindOf(value)
```
### grunt.utils.linefeed
The linefeed character, normalized for the current operating system. (`\r\n` on Windows, `\n` otherwise)
### grunt.utils.normalizelf
Given a string, return a new string with all the linefeeds normalized for the current operating system. (`\r\n` on Windows, `\n` otherwise)
```javascript
grunt.utils.normalizelf(string)
```
### grunt.utils.recurse
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.
```javascript
grunt.utils.recurse(object, callbackFunction, continueFunction)
```
See the [config lib source](../lib/grunt/config.js) for usage examples.
### grunt.utils.repeat
Return string `str` repeated `n` times.
```javascript
grunt.utils.repeat(n, str)
```
### grunt.utils.pluralize
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.
```javascript
grunt.utils.pluralize(n, str, separator)
```
### grunt.utils.spawn
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.
```javascript
grunt.utils.spawn(options, doneFunction)
```
The `options` object has these possible properties:
```javascript
var options = {
// The command to execute. It should be in the system path.
cmd: commandToExecute,
// 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
};
```
The done function accepts these arguments:
```javascript
function doneFunction(error, result, code) {
// If the exit code was non-zero and a fallback wasn't specified, the error
// object is the same as the result object.
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
}
```
See the [init task source](../tasks/init.js) and the [qunit task source](../tasks/qunit.js) for usage examples.
### grunt.utils.toArray
Given an array or array-like object, return an array. Great for converting `arguments` objects into arrays.
```javascript
grunt.utils.toArray(arrayLikeObject)
```
## Internal libraries
### grunt.utils.namespace
An internal library for resolving deeply-nested properties in objects.
### grunt.utils.task
An internal library for task running.
## External libraries
### grunt.utils._
[Underscore.js](http://underscorejs.org/) - Tons of super-useful array, function and object utility methods.
[Underscore.string](https://github.com/epeli/underscore.string) - Tons of string utility methods.
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.
### grunt.utils.async
[Async](https://github.com/caolan/async) - Async utilities for node and the browser.
### grunt.utils.hooker
[JavaScript Hooker](https://github.com/cowboy/javascript-hooker) - Monkey-patch (hook) functions for debugging and stuff.