@soleil-se/app-util
Version:
Utility functions for WebApps, RESTApps and Widgets in Sitevision.
72 lines (66 loc) • 2.31 kB
JavaScript
/* Make native requires work with @sitevision/sitevision-scripts that uses Webpack */
/* eslint-disable camelcase, no-undef */
/**
* Require a module natively, bypassing Webpack bundling.
* This function behaves like CommonJS require() and is used to import
* system packages in the Rhino runtime environment where Webpack bundling interferes
* with native module resolution.
*
* @param {string} module - The module identifier
* @returns {any} The exported module
* @example
* // Import a Sitevision API package
* const PortletContextUtil = nativeRequire('PortletContextUtil');
*/
export function nativeRequire(module) {
if (typeof __non_webpack_require__ !== 'undefined') return __non_webpack_require__(module);
return require(module);
}
/* eslint-enable camelcase, no-undef */
/**
* @typedef {import('@sitevision/api/types/javax/jcr/Node').Node} Node
* @typedef {import('@sitevision/api/types/javax/jcr/NodeIterator').NodeIterator} NodeIterator
* @typedef {import('@sitevision/api/types/java/util/Iterator').Iterator} Iterator
* @typedef {import('@sitevision/api/types/java/util/List').List} List
* @typedef {import('@sitevision/api/types/java/util/Set').Set} Set
*/
/**
* Converts an Iterator to an array
* @template T
* @param {Iterator} iterator - The Iterator to convert
* @returns {T[]} Array containing all items from the iterator
*/
export function iteratorToArray(iterator) {
const array = [];
if (!iterator) return array;
while (iterator?.hasNext()) {
array.push(iterator?.next());
}
return array;
}
/**
* Converts a NodeIterator to an array of Nodes
* @param {NodeIterator} nodeIterator - The NodeIterator to convert
* @returns {Node[]} Array containing all nodes from the iterator
*/
export function nodeIteratorToArray(nodeIterator) {
return iteratorToArray(nodeIterator);
}
/**
* Converts a List to an array
* @template T
* @param {List} list - The List to convert
* @returns {T[]} Array containing all items from the list
*/
export function listToArray(list) {
return iteratorToArray(list?.iterator());
}
/**
* Converts a Set to an array
* @template T
* @param {Set} set - The Set to convert
* @returns {T[]} Array containing all items from the set
*/
export function setToArray(set) {
return iteratorToArray(set?.iterator());
}