OpenVeo server API for plugins

API Docs for: 4.3.1
Show:

util

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

Summary

Provides functions for common JavaScript operations.

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

Item Index

Methods

areSameArrays

Defined in lib/util.js:92

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

evaluateDeepObjectProperties

Defined in lib/util.js:552

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:510

Syntax

getPropertyFromArray

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

Summary

Gets a specific property from an Array of Objects.

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

Returns:

Array:

The list of values for the given property

Example:

// Get util
var util = require('@openveo/api').util;

// Get property 'id' of each objects of the array
var params = util.getPropertyFromArray('id', [
  {id: 0},
  {id: 1},
  {id: 2, items: [{id: 3}]}
], 'items');

// [0, 1, 2, 3]
console.log(params);

intersectArray

Defined in lib/util.js:77

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:124

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:110

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:62

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:19

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

shallowValidateObject

Defined in lib/util.js:157

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:427

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