Some useful function to make working with parameters more reliable and less painful in JavaScript.
- Copyright:
- Kozalo.Ru, 2016
- Source:
Methods
(static) checkMissingParameters(parameters, funcName, paramNames)
Checks if all required parameters have been passed into the function. Throws an exception if any of them are missing.
Parameters:
Name | Type | Description |
---|---|---|
parameters |
Array.<object> | object | Either an array of variables or a variable to check. |
funcName |
string | function | The name of the function. Used to make the description of an exception more detailed. If you pass a function, extracts its name automatically. |
paramNames |
Array.<string>= | string | Either an array of strings or a string. Pass it in the same order as parameters! Used to make the description of the exception more detailed as well. |
- Source:
Throws:
KozExceptions.missingArgument or a string exception.
Example
function foo(bar) {
// KozUtils.checkMissingParameters(bar);
// KozUtils.checkMissingParameters([bar], "foo");
// KozUtils.checkMissingParameters([bar], "foo", ['bar']);
KozUtils.checkMissingParameters(bar, "foo", "bar");
document.writeln(bar);
}
(static) checkParameters(parameters, types, funcName, paramNames)
Not only checks if all required parameters have been passed into the function, but also checks their types. Throws exceptions if any of them are missing or invalid.
Parameters:
Name | Type | Description |
---|---|---|
parameters |
Array.<object> | object | Either an array of variables or a variable to check. |
types |
Array.<string> | string | String representations of expected types for typeof-comparison. |
funcName |
string | function | The name of the function. Used to make the description of an exception more detailed. If you pass a function, extracts its name automatically. |
paramNames |
Array.<string>= | string | Either an array of strings or a string. Pass it in the same order as parameters! Used to make the description of the exception more detailed as well. |
- Source:
Throws:
KozExceptions.missingArgument, KozExceptions.invalidArgument or their equivalents.
Example
function foo(bar) {
// KozUtils.checkParameters(bar, "string");
// KozUtils.checkParameters([bar], "number", "foo");
// KozUtils.checkParameters([bar], "string", "foo", ['bar']);
KozUtils.checkParameters(bar, "number", "foo", "bar");
document.writeln(bar);
}
(static) getActualSize(element) → {Object}
Gets the actual current size of an HTMLElement.
Parameters:
Name | Type | Description |
---|---|---|
element |
HTMLElement | Any HTMLElement. |
- Source:
Throws:
KozExceptions.invalidArgument or a string exception.
Returns:
The object has 2 fields: 'width' and 'height'.
- Type
- Object
(static) initOptions(options, defaultOptions) → {Object}
Copies given options into the defaultOptions object and returns the result.
Parameters:
Name | Type | Description |
---|---|---|
options |
Object | Object with user settings. |
defaultOptions |
Object | Specify a list of default parameters here. |
- Source:
Returns:
The members of the options object overrides ones of the defaultOptions to get the result.
- Type
- Object