OpenVeo server API for plugins

API Docs for: 7.0.0
Show:

util

Defined in: lib/util.js:9
Module: util

Summary

Provides functions for common JavaScript operations.

// Load module "util"
var util = require('@openveo/api').util;

Methods

areSameArrays

Defined in lib/util.js:99

Syntax

areSameArrays

(
  • [array1]
  • [array2]
)
Boolean static

Summary

Compares two arrays.

Shallow validates that two arrays contains the same elements, no more no less.

Parameters:

  • [array1] Array optional

    An array

  • [array2] Array optional

    An array

Returns:

Boolean:

true if arrays are the same, false otherwise

escapeTextForRegExp

Defined in lib/util.js:616

Syntax

escapeTextForRegExp

(
  • text
)
String static

Summary

Escapes a text that will be used in a regular expression.

Parameters:

  • text String

    The text to escape

Returns:

String:

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

Defined in lib/util.js:574

Syntax

evaluateDeepObjectProperties

(
  • propertyPath
  • objectToAnalyze
)
Mixed static

Summary

Evaluates a path of properties on an object.

It does not use the JavaScript eval function.

Parameters:

  • propertyPath String

    The path of the property to retreive from the object

  • objectToAnalyze Object

    The object containing the requested property

Returns:

Mixed:

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

Defined in lib/util.js:502

Syntax

getPropertyFromArray

(
  • property
  • list
  • [recursiveProperty]
  • [startValue]
  • [shouldGetNextItems]
)
Array static

Summary

Gets values of a specific property from a structured Array and its sub Array(s).

Parameters:

  • 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] Mixed 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:

Array:

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

Defined in lib/util.js:78

Syntax

intersectArray

(
  • [array1]
  • [array2]
)
Array static

Summary

Makes intersection of two arrays.

Parameters:

  • [array1] Array optional

    An array

  • [array2] Array optional

    An array

Returns:

Array:

The intersection of the two arrays

isContained

Defined in lib/util.js:131

Syntax

isContained

(
  • expectedValue
)
Boolean static

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:

Boolean:

true if the expected value has been found in value

isEmailValid

Defined in lib/util.js:117

Syntax

isEmailValid

(
  • email
)
Boolean static

Summary

Checks if an email address is valid or not.

Parameters:

  • email String

    The email address

Returns:

Boolean:

true if the email is valid, false otherwise

joinArray

Defined in lib/util.js:63

Syntax

joinArray

(
  • [array1]
  • [array2]
)
Array static

Summary

Makes union of two arrays.

Parameters:

  • [array1] Array optional

    An array

  • [array2] Array optional

    An array

Returns:

Array:

The union of the two arrays

merge

Defined in lib/util.js:20

Syntax

merge

(
  • object1
  • object2
)
Object static

Summary

Merges, recursively, all properties of object2 in object1.

This will not create copies of objects.

Parameters:

  • object1 Object

    The JavaScript final object

  • object2 Object

    A second JavaScript object to merge into the first one

Returns:

Object:

object1

removeHtmlFromText

Defined in lib/util.js:641

Syntax

removeHtmlFromText

(
  • text
)
String static

Summary

Decodes all HTML entities and removes all HTML elements from specified text.

New lines are also replaced by spaces.

Parameters:

  • text String

    The text to sanitize

Returns:

String:

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 "&eacute; or $ccedil;" +
  '\n on several lines'
);

// 'Text with HTML tags and HTML entities like "é or ç"  on several lines'
console.log(htmlLessText);

shallowValidateObject

Defined in lib/util.js:164

Syntax

shallowValidateObject

(
  • objectToAnalyze
  • validationDescription
)
Object static

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:

  • objectToAnalyze Object

    The object to analyze

  • validationDescription Object

    The validation description object

Returns:

Object:

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

Defined in lib/util.js:419

Syntax

validateFiles

(
  • filesToAnalyze
  • validationDescription
  • callback
)
static async

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 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 Function

    The 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);