Transformers.js documentation

utils/core

You are viewing main version, which requires installation from source. If you'd like regular npm install, checkout the latest stable version (v3.0.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

utils/core

Core utility functions/classes for Transformers.js.

These are only used internally, meaning an end-user shouldn’t need to access anything here.


utils/core.reverseDictionary(data) β‡’ <code> Object </code>

Reverses the keys and values of an object.

Kind: static method of utils/core
Returns: Object - The reversed object.
See: https://ultimatecourses.com/blog/reverse-object-keys-and-values-in-javascript

ParamTypeDescription
dataObject

The object to reverse.


utils/core.escapeRegExp(string) β‡’ <code> string </code>

Escapes regular expression special characters from a string by replacing them with their escaped counterparts.

Kind: static method of utils/core
Returns: string - The escaped string.

ParamTypeDescription
stringstring

The string to escape.


utils/core.isTypedArray(val) β‡’ <code> boolean </code>

Check if a value is a typed array.

Kind: static method of utils/core
Returns: boolean - True if the value is a TypedArray, false otherwise.

Adapted from https://stackoverflow.com/a/71091338/13989043

ParamTypeDescription
val*

The value to check.


utils/core.isIntegralNumber(x) β‡’ <code> boolean </code>

Check if a value is an integer.

Kind: static method of utils/core
Returns: boolean - True if the value is a string, false otherwise.

ParamTypeDescription
x*

The value to check.


utils/core.isNullishDimension(x) β‡’ <code> boolean </code>

Determine if a provided width or height is nullish.

Kind: static method of utils/core
Returns: boolean - True if the value is null, undefined or -1, false otherwise.

ParamTypeDescription
x*

The value to check.


utils/core.calculateDimensions(arr) β‡’ <code> Array. < number > </code>

Calculates the dimensions of a nested array.

Kind: static method of utils/core
Returns: Array.<number> - An array containing the dimensions of the input array.

ParamTypeDescription
arrArray.<any>

The nested array to calculate dimensions for.


utils/core.pop(obj, key, defaultValue) β‡’ <code> * </code>

Replicate python’s .pop() method for objects.

Kind: static method of utils/core
Returns: * - The value of the popped key.
Throws:

  • Error If the key does not exist and no default value is provided.
ParamTypeDescription
objObject

The object to pop from.

keystring

The key to pop.

defaultValue*

The default value to return if the key does not exist.


utils/core.mergeArrays(arrs) β‡’ <code> Array </code>

Efficiently merge arrays, creating a new copy. Adapted from https://stackoverflow.com/a/6768642/13989043

Kind: static method of utils/core
Returns: Array - The merged array.

ParamTypeDescription
arrsArray.<Array>

Arrays to merge.


utils/core.calculateReflectOffset(i, w) β‡’ <code> number </code>

Calculates the index offset for a given index and window size.

Kind: static method of utils/core
Returns: number - The index offset.

ParamTypeDescription
inumber

The index.

wnumber

The window size.


utils/core.pick(o, props) β‡’ <code> Object </code>

Kind: static method of utils/core

ParamType
oObject
propsArray.<string>

utils/core.len(s) β‡’ <code> number </code>

Calculate the length of a string, taking multi-byte characters into account. This mimics the behavior of Python’s len function.

Kind: static method of utils/core
Returns: number - The length of the string.

ParamTypeDescription
sstring

The string to calculate the length of.


utils/core.count(arr, value)

Count the occurrences of a value in an array or string. This mimics the behavior of Python’s count method.

Kind: static method of utils/core

ParamTypeDescription
arrArray<any> | string

The array or string to search.

valueany

The value to count.


utils/core~InitiateProgressInfo : <code> Object </code>

Kind: inner typedef of utils/core
Properties

NameTypeDescription
status'initiate'
namestring

The model id or directory path.

filestring

The name of the file.


utils/core~DownloadProgressInfo : <code> Object </code>

Kind: inner typedef of utils/core
Properties

NameTypeDescription
status'download'
namestring

The model id or directory path.

filestring

The name of the file.


utils/core~ProgressStatusInfo : <code> Object </code>

Kind: inner typedef of utils/core
Properties

NameTypeDescription
status'progress'
namestring

The model id or directory path.

filestring

The name of the file.

progressnumber

A number between 0 and 100.

loadednumber

The number of bytes loaded.

totalnumber

The total number of bytes to be loaded.


utils/core~DoneProgressInfo : <code> Object </code>

Kind: inner typedef of utils/core
Properties

NameTypeDescription
status'done'
namestring

The model id or directory path.

filestring

The name of the file.


utils/core~ReadyProgressInfo : <code> Object </code>

Kind: inner typedef of utils/core
Properties

NameTypeDescription
status'ready'
taskstring

The loaded task.

modelstring

The loaded model.


utils/core~ProgressInfo : <code> InitiateProgressInfo </code> | <code> DownloadProgressInfo </code> | <code> ProgressStatusInfo </code> | <code> DoneProgressInfo </code> | <code> ReadyProgressInfo </code>

Kind: inner typedef of utils/core


utils/core~ProgressCallback β‡’ <code> void </code>

A callback function that is called with progress information.

Kind: inner typedef of utils/core

ParamType
progressInfoProgressInfo

< > Update on GitHub