Namespace: Utilities

Utilities

Miscellaneous app/browser helpers that do not yet fit better specialized common modules.
Since:
  • 2.14.0
Source:

Methods

awaitMetacatUI(optionsopt) → {Promise.<*>}

Wait for the global MetacatUI object to be available, and optionally for a specific property on it to be defined.
Parameters:
Name Type Attributes Description
options object <optional>
Options object.
Properties
Name Type Attributes Description
maxAttempts number <optional>
Maximum number of attempts.
delay number <optional>
Delay between attempts in ms.
property string <optional>
Optional property name to wait for.
appName string <optional>
Optional MetacatUI property containing the object to wait for.
Since:
  • 2.39.0
Source:
Throws:
If the requested value is not available in time.
Type
Error
Returns:
Promise resolving to the requested global object or property value.
Type
Promise.<*>

awaitObjectFormats() → {Promise.<ObjectFormats>}

Get the list of object formats from the Coordinating Node.
Since:
  • 2.39.0
Source:
Returns:
Promise resolving to the object formats collection.
Type
Promise.<ObjectFormats>

buildInstanceKey(options, keys, normalizersopt, separatoropt, encodeopt) → {string}

Build a unique string to represent a class instance or options set.
Parameters:
Name Type Attributes Description
options object The object containing key values.
keys Array.<string> Field names required to build the key.
normalizers object <optional>
Optional field normalizers.
separator string <optional>
Separator to use between key parts.
encode boolean <optional>
Whether to md5-encode the key.
Since:
  • 2.37.0
Source:
Throws:
If keys is not a non-empty array.
Type
Error
Returns:
The generated instance key.
Type
string

deepEqual(a, b) → {boolean}

Checks if two objects are deeply equal. Simpler than the _.isEqual function.
Parameters:
Name Type Description
a object The first object to compare
b object The second object to compare
Since:
  • 2.31.0
Source:
Returns:
True if the objects are deeply equal
Type
boolean

encodeHTML(s) → {string}

HTML-encodes the given string so it can be inserted into an HTML page without running any embedded Javascript.
Parameters:
Name Type Description
s string String to be encoded.
Source:
Returns:
HTML encoded string.
Type
string

formatFixedNumber(value, digitsopt, fallbackopt) → {string}

Format a finite number using a fixed number of decimal places.
Parameters:
Name Type Attributes Default Description
value number The number value to be formatted.
digits number <optional>
2 The number of decimal places to display.
fallback string <optional>
"" The value to return when `value` is not finite.
Since:
  • 2.37.0
Source:
Returns:
A fixed-decimal number string or the fallback value.
Type
string

formatNumber(value, range) → {string}

Format the number into a string with better readability, based on the magnitude of a range this number falls in.
Parameters:
Name Type Description
value number The number value to be formatted.
range number The range of numerics this value can fall in.
Since:
  • 2.30.0
Source:
Returns:
A formatted number based on the magnitude of `range`.
Type
string

getMaxConcurrent(maxConcurrentopt) → {number}

Resolve a positive concurrency limit from a caller value or the project default.
Parameters:
Name Type Attributes Description
maxConcurrent number <optional>
Preferred limit
Since:
  • 2.39.0
Source:
Returns:
Positive integer limit.
Type
number

getMetacatUIProperty(property, appopt) → {*}

Read a MetacatUI property directly, via Backbone's `get`, or from a nested appModel.
Parameters:
Name Type Attributes Description
property string Property name to retrieve.
app object <optional>
MetacatUI object.
Since:
  • 2.39.0
Source:
Returns:
Property value, or `undefined` when not present.
Type
*

getSingleton(ClassRef, optionsopt, buildInstanceKey) → {object}

Get or create a singleton instance for a class.
Parameters:
Name Type Attributes Description
ClassRef function Class reference.
options object <optional>
Options passed to the class constructor.
buildInstanceKey function Function that builds a unique key.
Since:
  • 2.37.0
Source:
Returns:
The singleton instance.
Type
object

isValidDOI(identifier) → {boolean}

Validates that the given string is a valid DOI
Parameters:
Name Type Description
identifier string String to be validated.
Since:
  • 2.15.0
Source:
Returns:
True if identifier is a valid DOI.
Type
boolean

normalizeUrl(url, fallbackopt) → {string}

Normalize a URL string by trimming whitespace and removing trailing slashes.
Parameters:
Name Type Attributes Description
url string The URL to normalize.
fallback string <optional>
A fallback URL to use if url is empty.
Since:
  • 2.37.0
Source:
Returns:
Normalized URL, or empty string if not available.
Type
string

processConcurrently(items, worker, optionsopt) → {Promise.<object>}

Run work with one concurrency limit and no batch abstraction.
Parameters:
Name Type Attributes Description
items Array.<*> Items to process.
worker function Async item worker.
options object <optional>
Processing options.
Properties
Name Type Attributes Description
maxConcurrent number <optional>
Worker limit.
signal AbortSignal <optional>
Abort signal.
stopOnError boolean <optional>
Stop scheduling after an error.
onItemComplete function <optional>
Called after each item settles.
Since:
  • 2.39.0
Source:
Returns:
Collected errors.
Type
Promise.<object>

readSlice(file, context, callback, bytes)

Read the first part of a file
Parameters:
Name Type Description
file File A reference to a file
context Backbone.View The View to bind `callback` to
callback function A function to run after the read is complete. The function is bound to `context`.
bytes number The number of bytes to read from the start of the file
Since:
  • 2.15.0
Source:

stableStringify(val, optionsopt) → {string}

Stringify any value deterministically, where order doesn't impact the result.
Parameters:
Name Type Attributes Description
val * The value to stringify.
options object <optional>
Options object.
Properties
Name Type Attributes Description
ignoreCase boolean <optional>
Whether to convert all string values to lower case before stringifying.
processed WeakSet <optional>
WeakSet tracking visited objects.
orderMatters boolean <optional>
Whether to preserve array order during stringification.
Since:
  • 2.37.0
Source:
Throws:
If a circular reference is detected.
Type
Error
Returns:
A stable string representation.
Type
string

toJSONWithoutDefaults(model, removePropsopt) → {object}

Removes default values from a model's JSON representation
Parameters:
Name Type Attributes Description
model Backbone.Model The model to remove defaults from
removeProps Array.<string> <optional>
Additional properties to remove
Since:
  • 2.31.0
Source:
Returns:
The JSON representation with defaults removed
Type
object

tryParseCSVHeader(text) → {Array}

Attempt to parse the header/column names from a chunk of a CSV file Doesn't handle: - Commas inside quoted headers
Parameters:
Name Type Description
text string A chunk of a file
Since:
  • 2.15.0
Source:
Returns:
A list of names
Type
Array

wildcardToRegex(pattern) → {RegExp}

Convert a wildcard pattern to a safe RegExp.
Parameters:
Name Type Description
pattern string A simple wildcard pattern
Source:
Returns:
Regex for case-insensitive matching
Type
RegExp