util
Summary
Provides functions for common JavaScript operations.
// Load module "util"
var util = require('@openveo/api').util;
Item Index
Methods
- areSameArrays static
- escapeTextForRegExp static
- evaluateDeepObjectProperties static
- getPropertyFromArray static
- intersectArray static
- isContained static
- isEmailValid static
- joinArray static
- merge static
- removeHtmlFromText static
- shallowValidateObject static
- validateFiles static
Methods
areSameArrays
Syntax
Summary
Compares two arrays.
Shallow validates that two arrays contains the same elements, no more no less.
Returns:
true if arrays are the same, false otherwise
escapeTextForRegExp
Syntax
Summary
Escapes a text that will be used in a regular expression.
Parameters:
-
text
StringThe text to escape
Returns:
The escaped text
Example:
// Get util
var util = require('@openveo/api').util;
var escapedText = util.escapeTextForRegExp(
'Text with characters interpreted by JavaScript regular expressions: [](){}?*+.^$/\\|'
);
// "Text with characters interpreted by JavaScript regular expressions:
// \\[\\]\\(\\)\\{\\}\\?\\*\\+\\.\\^\\$\/\\\|"
console.log(escapedText);
evaluateDeepObjectProperties
Syntax
evaluateDeepObjectProperties
-
propertyPath
-
objectToAnalyze
Summary
Evaluates a path of properties on an object.
It does not use the JavaScript eval function.
Parameters:
Returns:
The value of the property
Example:
// Get util
var util = require('@openveo/api').util;
// Get property 'my.deep.property' of the object
var value = util.evaluateDeepObjectProperties('my.deep.property', {
my {
deep {
property: 'My deep property value'
}
}
});
// "My deep property value"
console.log(value);
getPropertyFromArray
Syntax
getPropertyFromArray
-
property
-
list
-
[recursiveProperty]
-
[startValue]
-
[shouldGetNextItems]
Summary
Gets values of a specific property from a structured Array and its sub Array(s).
Parameters:
-
property
StringThe name of the property to fetch
-
list
ArrayThe list of objects to look into
-
[recursiveProperty]
String optionalThe name of the recursive property to look into
-
[startValue]
Mixed optionalThe value of the searched property to start collecting values from
-
[shouldGetNextItems]
Boolean optionalFor internal use, it indicates if the values must be collected or not, for the given list. If true all values of the Array and sub Array(s) will be collected
Returns:
The list of values for the given property in the Array and its sub Array(s)
Example:
// Get util
var util = require('@openveo/api').util;
// Get values of property "id" for each Array and sub Array(s) items
var params = util.getPropertyFromArray('id', [
{id: 0},
{id: 1},
{id: 2, items: [{id: 3}]}
], 'items');
// [0, 1, 2, 3]
console.log(params);
// Get util
var util = require('@openveo/api').util;
// Get values of property "id" for each Array and sub Array(s) items starting at the item where "id" equal 2
var params = util.getPropertyFromArray('id', [
{id: 0},
{id: 1},
{id: 2, items: [
{id: 3, items: [{id: 4}]}
]
}
], 'items', 2);
// [3, 4]
console.log(params);
intersectArray
Syntax
Summary
Makes intersection of two arrays.
Returns:
The intersection of the two arrays
isContained
Syntax
Summary
Checks if a value is isContained into another comparing primitive types.
All values in expectedValue must be found in value to pass the test.
Parameters:
Returns:
true if the expected value has been found in value
isEmailValid
Syntax
Summary
Checks if an email address is valid or not.
Parameters:
-
email
StringThe email address
Returns:
true if the email is valid, false otherwise
merge
Syntax
Summary
Merges, recursively, all properties of object2 in object1.
This will not create copies of objects.
Parameters:
Returns:
object1
removeHtmlFromText
Syntax
Summary
Decodes all HTML entities and removes all HTML elements from specified text.
New lines are also replaced by spaces.
Parameters:
-
text
StringThe text to sanitize
Returns:
The sanitized text
Example:
// Get util
var util = require('@openveo/api').util;
var htmlLessText = util.removeHtmlFromText(
'Text with <strong style="color: orange">HTML tags</strong> and HTML entities like "é or $ccedil;" +
'\n on several lines'
);
// 'Text with HTML tags and HTML entities like "é or ç" on several lines'
console.log(htmlLessText);
shallowValidateObject
Syntax
Summary
Validates first level object properties using the given validation description object.
It helps validating that an object, coming from a request query string parameters correspond to the expected type, if it has to be required, if it must be contained into a list of values etc.
Available features by types :
- string
- default Specify a default value
- required Boolean to indicate if the value is required (if default is specified, value will always be set)
- in Specify an array of strings to validate that the value is inside this array
- number
- default Specify a default value
- required Boolean to indicate if the value is required (if default is specified, value will always be set)
- in Specify an array of numbers to validate that the value is inside this array
- gt Specify a number to validate that the value is greater than this number
- lt Specify a number to validate that the value is lesser than this number
- gte Specify a number to validate that the value is greater or equal to this number
- lte Specify a number to validate that the value is lesser or equal to this number
- array<string>
- required Boolean to indicate if the value is required (an empty array is not an error)
- in Specify an array of values to validate that each value of the array is inside this array
- array<number>
- required Boolean to indicate if the value is required (an empty array is not an error)
- in Specify an array of values to validate that each value of the array is inside this array
- array<object>
- required Boolean to indicate if the value is required (an empty array is not an error)
- date
- required Boolean to indicate if the value is required
- gt Specify a date to validate that the value is greater than this date
- lt Specify a date to validate that the value is lesser than this date
- gte Specify a date to validate that the value is greater or equal to this date
- lte Specify a date to validate that the value is lesser or equal to this date
- object
- default Specify a default value
- required Boolean to indicate if the value is required (if default is specified, value will always be set)
- boolean
- default Specify a default value
- required Boolean to indicate if the value is required (if default is specified, value will always be set)
- file
- required Boolean to indicate if the value is required
- in Specify an array of types to validate that the file's type is inside this array
Parameters:
Returns:
A new object with the list of properties as expected
Example:
// Get util
var util = require('@openveo/api').util;
var fileSystem = require('@openveo/api').fileSystem;
// Validate parameters
var params = util.shallowValidateObject({
myStringProperty: 'my value',
myNumberProperty: 25,
myArrayStringProperty: ['value1', 'value2'],
myArrayNumberProperty: [10, 5],
myArrayObjectProperty: [{}, {}],
myDateProperty: '02/25/2016',
myObjectProperty: {firstKey: 'firstValue'},
myBooleanProperty: true,
myFileProperty: 88 13 70 17 // At least the first 300 bytes of the file
}, {
myStringProperty: {type: 'string', required: true, default: 'default', in: ['my value', 'value']},
myNumberProperty: {type: 'number', required: true, default: 0, in: [0, 5, 10], gte: 0, lte: 5},
myArrayStringProperty: {type: 'array<string>', required: true, in: ['value1', 'value2']},
myArrayNumberProperty: {type: 'array<number>', required: true, in: [42, 43]},
myArrayObjectProperty: {type: 'array<object>', required: true},
myDateProperty: {type: 'date', required: true, gte: '02/20/2016', lte: '03/30/2016'},
myObjectProperty: {type: 'object', required: true},
myBooleanProperty: {type: 'boolean', required: true},
myFileProperty: {type: 'file', required: true, in: [
fileSystem.FILE_TYPES.JPG,
fileSystem.FILE_TYPES.PNG,
fileSystem.FILE_TYPES.GIF,
fileSystem.FILE_TYPES.MP4,
fileSystem.FILE_TYPES.TAR
]}
});
console.log(params);
validateFiles
Syntax
validateFiles
-
filesToAnalyze
-
validationDescription
-
callback
Summary
Validates that files are in the expected type.
Available features for validation object:
- in Specify an array of types to validate that the file type is inside this array
Parameters:
-
filesToAnalyze
ObjectFiles to validate with keys as files identifiers and values as files absolute paths
-
validationDescription
ObjectThe validation description object with keys as files identifiers and values as validation objects
-
callback
FunctionThe function to call when done
- Error The error if an error occurred, null otherwise
- Object Files with keys as the files identifiers and values as Objects containing validation information: isValid and type (from util.FILE_TYPES)
Example:
// Get util
var util = require('@openveo/api').util;
var fileSystem = require('@openveo/api').fileSystem;
// Validate parameters
var params = util.validateFiles({
myFirstFile: '/tmp/myFirstFile.mp4',
mySecondFile: '/tmp/mySecondFile.tar'
}, {
myFirstFile: {in: [fileSystem.FILE_TYPES.MP4]},
mySecondFile: {in: [fileSystem.FILE_TYPES.TAR]}
}, function(error, files) {
if (error) {
console.log('An error occurred during validation with message: ' + error.message);
}
console.log('Is file valid ? ' + files.myFirstFile.isValid);
console.log('File type: ' + files.myFirstFile.type);
});
console.log(params);