A module for utility functions.

Methods

(static) batches(path) → {Array}

A function to read the contents of a directory and to extract all files with the file extension *.json.

Parameters:
NameTypeDescription
pathString

A given path as string.

Returns:

Returns all JSON files contained in path.

Type: 
Array

(static) cookie(r)

A function to retrieve all cookie fields of an HTTP/S request.

Parameters:
NameTypeDescription
rObject

An HTTP/S request retrieved from the webRequest interface. See webRequest.onBeforeRequest() for more details.

Returns:

Returns an array of all cookie fields in the format:

[ [field_1, value_1], [field_2, value_2], ..., [field_n, value_n] ]

Returns an empty array in case no cookie is present.

(static) count(condition, value) → {Number}

Implementation of a conditional counter.

Parameters:
NameTypeDescription
conditionBoolean

Either true or false. Note, the condition is not evaluated in the function.

valueNumber

The current count (of a certain feature).

Returns:

Returns value + 1 in case condition == true or value otherwise.

Type: 
Number

(static) header(headers, key) → {Object}

A function to extract a header field from the HTTP/S header of a request. NOTE: The matching is case insensitive.

Parameters:
NameTypeDescription
headersArray

An array of HTTP/S header fields in the format

{ name: "key", value: "value" }
keyString

An HTTP/S header key like Referer or Origin.

Returns:

The requested HTTP/S header in the format:

{ name: "key", value: "value" }

Returns undefined in case header with name === key is not present.

Type: 
Object

(static) isDir(path) → {Boolean}

A function to check whether a path is a directory. This is a wrapper function for fs.lstatSync(path).isDirectory().

Parameters:
NameTypeDescription
pathString

A given path as string.

Returns:
Type: 
Boolean

(static) max(x, y) → {Number}

A wrapper function for Math.max().

Parameters:
NameTypeDescription
xNumber

The first number.

yNumber

The second number.

Returns:

Returns the greater number.

Type: 
Number

(static) params(r) → {Array}

A function to retrieve all query parameters of an HTTP/S request.

Parameters:
NameTypeDescription
rObject

An HTTP/S request retrieved from the webRequest interface. See webRequest.onBeforeRequest() for more details.

Returns:

Returns an array of all query parameters in the format:

[ [param_1, value_1], [param_2, value_2], ..., [param_n, value_n] ]

Returns an empty array in case no query parameters are present.

Type: 
Array

(static) path(dir) → {String}

A function to check whether an absolute or relative path exists.

Parameters:
NameTypeDescription
dirString

A given path as string.

Returns:

Returns the path in case it exists. Returns null otherwise.

Type: 
String

(static) ratio(dividend, divisor) → {Number}

A function to compute the ratio of two numbers. NOTE: Returns 0 in case that the divisor is 0.

Parameters:
NameTypeDescription
dividendNumber

The dividend of the ratio.

divisorNumber

The divisor of the ratio.

Returns:

The ratio between two numbers.

Type: 
Number

(static) source(r) → {String}

A function to extract the source of an HTTP/S request. The function will use the initiator property of the request. If this property is absent, the function tries to extract the source from the referer or origin HTTP/S header.

Parameters:
NameTypeDescription
rObject

An HTTP/S request retrieved from the webRequest interface. See webRequest.onBeforeRequest() for more details.

Returns:

Returns the full source URL as string. Returns undefined in case no source could be extracted.

Type: 
String

(static) target(r) → {String}

A function to extract the target URL of an HTTP/S request. The function will extract the url property of the request and falls back to the url property of the response in case no url property is present in the request.

Parameters:
NameTypeDescription
rObject

An HTTP/S request retrieved from the webRequest interface. See webRequest.onBeforeRequest() for more details.

Returns:

Returns the full target URL as string. Returns undefined in case the url property is neither present in the request nor the response.

Type: 
String

(static) zeroOrOne(condition) → {Number}

A function to encode boolean values as number (1 for true, 0 for false). The encoding is later needed, when the data is fed into the classifier.

Parameters:
NameTypeDescription
conditionBoolean

Either true or false. Note, the condition is not evaluated in the function.

Returns:

Returns 1 in case true is passed, 0 otherwise.

Type: 
Number