Provides functions for common JavaScript operations.
Example
// Load module "util"
var util = require('@openveo/api').util;
Methods
(static) areSameArrays(array1, array2) → {Boolean}
Compares two arrays.
Shallow validates that two arrays contains the same elements, no more no less.
Parameters:
Name | Type | Description |
---|---|---|
array1 |
Array | An array |
array2 |
Array | An array |
Returns:
true if arrays are the same, false otherwise
- Type
- Boolean
(static) escapeTextForRegExp(text) → {String}
Escapes a text that will be used in a regular expression.
Parameters:
Name | Type | Description |
---|---|---|
text |
String | The text to escape |
Returns:
The escaped text
- Type
- String
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);
(static) evaluateDeepObjectProperties(propertyPath, objectToAnalyze) → {*}
Evaluates a path of properties on an object.
It does not use the JavaScript eval function.
Parameters:
Name | Type | Description |
---|---|---|
propertyPath |
String | The path of the property to retreive from the object |
objectToAnalyze |
Object | The object containing the requested property |
Returns:
The value of the property
- Type
- *
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);
(static) getPropertyFromArray(property, list, recursivePropertyopt, startValueopt, shouldGetNextItemsopt) → {Array}
Gets values of a specific property from a structured Array and its sub Array(s).
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
property |
String | The name of the property to fetch |
|
list |
Array | The list of objects to look into |
|
recursiveProperty |
String |
<optional> |
The name of the recursive property to look into |
startValue |
* |
<optional> |
The value of the searched property to start collecting values from |
shouldGetNextItems |
Boolean |
<optional> |
For 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)
- Type
- Array
Examples
// 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);
(static) intersectArray(array1, array2) → {Array}
Makes intersection of two arrays.
Parameters:
Name | Type | Description |
---|---|---|
array1 |
Array | An array |
array2 |
Array | An array |
Returns:
The intersection of the two arrays
- Type
- Array
(static) isContained(expectedValue) → {Boolean}
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:
Name | Type | Description |
---|---|---|
expectedValue |
Object | Number | String | Array | The value expecting to be found in "value" |
Returns:
true if the expected value has been found in value
- Type
- Boolean
(static) isEmailValid(email) → {Boolean}
Checks if an email address is valid or not.
Parameters:
Name | Type | Description |
---|---|---|
email |
String | The email address |
Returns:
true if the email is valid, false otherwise
- Type
- Boolean
(static) joinArray(array1, array2) → {Array}
Makes union of two arrays.
Parameters:
Name | Type | Description |
---|---|---|
array1 |
Array | An array |
array2 |
Array | An array |
Returns:
The union of the two arrays
- Type
- Array
(static) merge(object1, object2) → {Object}
Merges, recursively, all properties of object2 in object1.
This will not create copies of objects.
Parameters:
Name | Type | Description |
---|---|---|
object1 |
Object | The JavaScript final object |
object2 |
Object | A second JavaScript object to merge into the first one |
Returns:
object1
- Type
- Object
(static) removeHtmlFromText(text) → {String}
Decodes all HTML entities and removes all HTML elements from specified text.
New lines are also replaced by spaces.
Parameters:
Name | Type | Description |
---|---|---|
text |
String | The text to sanitize |
Returns:
The sanitized text
- Type
- String
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);
(static) shallowValidateObject(objectToAnalyze, validationDescription) → {Object}
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:
Name | Type | Description |
---|---|---|
objectToAnalyze |
Object | The object to analyze |
validationDescription |
Object | The validation description object |
Throws:
-
An error if a property does not respect its associated rules
- Type
- Error
Returns:
A new object with the list of properties as expected
- Type
- Object
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);
(static) validateFiles(filesToAnalyze, validationDescription, callback)
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:
Name | Type | Description |
---|---|---|
filesToAnalyze |
Object | Files to validate with keys as files identifiers and values as files absolute paths |
validationDescription |
Object | The validation description object with keys as files identifiers and values as validation objects |
callback |
module:util~validateFilesCallback | The function to call when done |
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);
Type Definitions
validateFilesCallback(error, files)
Parameters:
Name | Type | Description |
---|---|---|
error |
Error | null | The error if an error occurred, null otherwise |
files |
Object | undefined | Files with keys as the files identifiers and values as Objects containing validation
information: isValid and |