Util Class
Provides a set of utility functions used throughout the code base
Item Index
Methods
- ane static
- arrayPushAll static
- arrayToHash static
- arrayToObj static
- cb static
- clone static
- copyArray static
- deepMerge static
- escapeRegExp static deprecated
- forEach static
- getDirectories static
- getExtension static
- getFileExtensionFilter static
- getFiles static
- getTasks static
- hashToArray static
- inherits static
- initArray static
- invertHash static
- isBoolean static
- isFunction static
- isNullOrUndefined static
- isObject static
- isString static
- merge
- mkdirsSync static
- objArrayToHash static
- union static
- uniqueId static
- wrapTask static
- wrapTimedTask static
Properties
- TIME static
Methods
ane
-
obj
Checks if the supplied object is an errof. If the object is an error the function will throw the error.
Parameters:
-
obj
ObjectThe object to check
arrayPushAll
-
from
-
to
Pushes all of one array's values into another
Parameters:
-
from
Array -
to
Array
Returns:
FALSE when the parameters are not Arrays
arrayToHash
-
array
-
[defaultVal=true]
Hashes an array
Parameters:
-
array
ArrayThe array to hash
-
[defaultVal=true]
optionalDefault value if the hashing fails
Returns:
Hash
arrayToObj
-
array
-
keyFieldOrTransform
-
[valFieldOrTransform]
Converts an array to an object.
Parameters:
-
array
ArrayThe array of items to transform from an array to an object
-
keyFieldOrTransform
String | FunctionWhen this field is a string it is expected that the array contains objects and that the objects contain a property that the string represents. The value of that field will be used as the property name in the new object. When this parameter is a function it is passed two parameters: the array being operated on and the index of the current item. It is expected that the function will return a value representing the key in the new object.
-
[valFieldOrTransform]
String | Function optionalWhen this value is a string it is expected that the array contains objects and that the objects contain a property that the string represents. The value of that field will be used as the property value in the new object. When this parameter is a function it is passed two parameters: the array being operated on and the index of the current item. It is expected that the function return a value representing the value of the derived property for that item.
Returns:
The converted array.
cb
()
static
Empty callback function just used as a place holder if a callback is required and the result is not needed.
clone
-
object
Clones an object by serializing it and then re-parsing it. WARNING: Objects with circular dependencies will cause an error to be thrown.
Parameters:
-
object
ObjectThe object to clone
Returns:
Cloned object
copyArray
-
array
Clones an array
Parameters:
-
array
Array
Returns:
Cloned array
deepMerge
-
from
-
to
Performs a deep merge and returns the result. NOTE: DO NOT ATTEMPT TO MERGE PROPERTIES THAT REFERENCE OTHER PROPERTIES. This could have unintended side-effects as well as cause errors due to circular dependencies.
Parameters:
-
from
Object -
to
Object
Returns:
escapeRegExp
-
The
Escapes a regular expression.
Parameters:
-
The
Stringexpression to escape
Returns:
Escaped regular expression.
forEach
-
iterable
-
handler
Provides an implementation of for each that accepts an array or object.
Parameters:
-
iterable
Object | Array -
handler
FunctionA function that accepts 4 parameters. The value of the current property or index. The current index (property name if object). The iterable. Finally, the numerical index if the iterable is an object.
getDirectories
-
dirPath
-
cb
Retrieves the subdirectories of a path
Parameters:
-
dirPath
StringThe starting path
-
cb
FunctionCallback function
getExtension
-
filePath
-
[options]
Retrieves the extension off of the end of a string that represents a URI to a resource
Parameters:
-
filePath
StringURI to the resource
-
[options]
Object optional-
[lower=false]
Boolean optionalWhen TRUE the extension will be returned as lower case
-
[sep]
String optionalThe file path separator used in the path. Defaults to the OS default.
-
Returns:
The value after the last '.' character
getFileExtensionFilter
-
extension
Creates a filter function to be used with the getFiles function to skip files that are not of the specified type
Parameters:
-
extension
Object
Returns:
getFiles
-
dirPath
-
[options]
-
cb
Retrieves file and/or directorie absolute paths under a given directory path.
Parameters:
-
dirPath
StringThe path to the directory to be examined
-
[options]
Object optionalOptions that customize the results
-
[recursive=false]
Boolean optionalA flag that indicates if directories should be recursively searched.
-
[filter]
Function optionalA function that returns a boolean indicating if the file should be included in the result set. The function should take two parameters. The first is a string value representing the absolute path of the file. The second is the stat object for the file.
-
-
cb
FunctionA callback that takes two parameters. The first is an Error, if occurred. The second is an array of strings representing the absolute paths for files that met the criteria specified by the filter function.
getTasks
-
iterable
-
getTaskFunction
Creates a set of tasks that can be executed by the "async" module.
Parameters:
-
iterable
ArrayThe array of items to build tasks for
-
getTaskFunction
FunctionThe function that creates and returns the task to be executed.
Example:
var items = ['apple', 'orange'];
var tasks = util.getTasks(items, function(items, i) {
return function(callback) {
console.log(items[i]);
callback(null, null);
};
});
async.series(tasks, util.cb);
hashToArray
-
obj
-
[hashKeyProp]
Converts a hash to an array. When provided, the hashKeyProp will be the property name of each object in the array that holds the hash key.
Parameters:
-
obj
ObjectThe object to convert
-
[hashKeyProp]
String optionalThe property name that will hold the hash key.
Returns:
An array of each property value in the hash.
inherits
-
Type1
-
Type2
Overrides the basic inherit functionality to include static functions and properties of prototypes
Parameters:
-
Type1
Function -
Type2
Function
initArray
-
cnt
-
val
Initializes an array with the specified number of values. The value at each index can be static or a function may be provided. In the event that a function is provided the function will be called for each item to be placed into the array. The return value of the function will be placed into the array.
Parameters:
-
cnt
IntegerThe length of the array to create
-
val
Function | String | NumberThe value to initialize each index of the array
Returns:
The initialized array
invertHash
-
obj
Inverts a hash
Parameters:
-
obj
ObjectHash object
Returns:
Inverted hash
isBoolean
-
value
Tests if a value is a boolean
Parameters:
-
value
Returns:
isFunction
-
value
Tests if a value is a function
Parameters:
-
value
Returns:
isNullOrUndefined
-
value
Tests if a value is NULL or undefined
Parameters:
-
value
Returns:
isObject
-
value
Tests if a value is an object
Parameters:
-
value
Returns:
isString
-
value
Tests if a value is an string
Parameters:
-
value
Returns:
merge
-
from
-
to
Merges the properties from the first parameter into the second. This modifies the second parameter instead of creating a new object.
Parameters:
-
from
Object -
to
Object
Returns:
The 'to' variable
mkdirsSync
-
absoluteDirPath
-
isFileName
Synchronously makes the specified directory structure.
Parameters:
-
absoluteDirPath
StringThe absolute path of the directory structure to be created
-
isFileName
BooleanWhen true the value after the last file separator is treated as a file. This means that a directory with that value will not be created.
objArrayToHash
-
array
-
hashProp
Converts an array of objects into a hash where the key the value of the specified property. If multiple objects in the array have the same value for the specified value then the last one found will be kept.
Parameters:
-
array
ArrayThe array to convert
-
hashProp
StringThe property who's value will be used as the key for each object in the array.
Returns:
A hash of the values in the array
union
()
Object
static
Creates an object that has both the properties of "a" and "b". When both objects have a property with the same name, "b"'s value will be preserved.
Returns:
The union of properties from both a and b.
uniqueId
()
String
static
Creates a unique Id
Returns:
Unique Id Object
wrapTask
-
context
-
func
-
[argArray]
Wraps a function in an anonymous function. The wrapper function will call the wrapped function with the provided context. This comes in handy when creating your own task arrays in conjunction with the async function when a prototype function needs to be called with a specific context.
Parameters:
-
context
The value of "this" for the function to be called
-
func
FunctionThe function to be executed
-
[argArray]
Array optionalThe arguments to be supplied to the func parameter when executed.
Returns:
wrapTimedTask
-
context
-
func
-
[name]
-
[argArray]
Wraps a task in a context as well as a function to mark the start and end time. The result of the task will be provided in the callback as the "result" property of the result object. The time of execution can be found as the "time" property.
Parameters:
-
context
The value of "this" for the function to be called
-
func
FunctionThe function to be executed
-
[name]
String optionalThe task's name
-
[argArray]
Array optionalThe arguments to be supplied to the func parameter when executed.
Returns:
Properties
TIME
Object
static
Provides typical conversions for time