May 25, 2021 Grunt
Various tool functions/libraries, including Lo-Dash, Async, and Hooker.
Returns the type (kind) of the given value.
typeof
but it returns an internal
[Class](class/)
value.
The results that
"number"
"string"
"boolean"
"function"
"regexp"
"array"
"date"
"error"
"null"
"undefined"
"object"
can represent all types.
grunt.util.kindOf(value)
Returns a new Error instance (which can also be thrown) with the corresponding message.
If you specify an Error object instead
message
the object is returned.
In addition, if
origError
is specified as an Error object
--stack
run using the --stack option, the original Error stack is output.
grunt.util.error(message [, origError])
Convert line breaks to the form of the current system
\r\n
Window, on the other systems,
\n
.
For a given string, convert all its line breaks to the form that the current system takes, and then return a new string.
(On Window,
\r\n
and on the other system, it's
\n
grunt.util.normalizelf(string)
Recursive nested objects and arrays that perform
callbackFunction
If
continueFunction
false
the given object or value will be skipped.
grunt.util.recurse(object, callbackFunction, continueFunction)
Returns the
n
that was
str
grunt.util.repeat(n, str)
Given a
str
"a/b"
n
1
"a"
otherwise
"b"
If you can't use '/', you can also specify a custom separator.
grunt.util.pluralize(n, str, separator)
Build a child process and track its stdout, stderr, and exit codes. T
his method returns a reference to the child process.
When the child process exits,
doneFunction
function is called.
grunt.util.spawn(options, doneFunction)
options
can specify the following properties:
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
can receive the following parameters:
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
}
For incoming arrays or class array objects, an array is returned.
It is useful
arguments
objects to arrays.
grunt.util.toArray(arrayLikeObject)
Functions that standardize "return values" and "pass results to callbacks" always pass a result to the specified callback function. I f the original function returns a value, it is immediately passed to the callback function and specified as the last argument, after all predefined parameters. If the original function passes a value to the callback function, it will continue to do the same.
grunt.util.callbackify(syncOrAsyncFunction)
The following example might be a better illustration:
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);
});
This internal tool library is used to resolve deeply nested properties in an object.
An internal tool library for task execution.
Not recommended
All external tool libraries listed below are no longer recommended.
Use npm to manage the dependencies on third-party tool libraries in your project.
For example, if you need
to use Lo-Dash,
install it first with
npm install lodash
and then use it in the
Gruntfile
var _ = require('lodash');
Not recommended
Lo-Dash - Many useful methods of arrays, functions, and object tools. Underscore.string - Many useful string tool functions.
grunt.util._.str
is available for methods that conflict with existing Lo-Dash methods.
Not recommended
Async - Asynchronous tool for both node and browser.
Not recommended
JavaScript Hooker - Hook function for debugging and doing other things.