Class JSBridge

java.lang.Object
org.daiitech.naftah.playground.utils.JSBridge

public final class JSBridge extends Object
Utility bridge between Java and JavaScript (JSObject).

Provides helper methods to convert JavaScript arrays/objects into Java Map, List, and arrays.

Author:
Chakib Daii
  • Constructor Details

    • JSBridge

      private JSBridge()
      Private constructor to prevent instantiation. Throws NaftahBugError if called.
  • Method Details

    • toMap

      public static <R> Map<String,R> toMap(JSObject jsEntries, Function<Object,R> entryValueMapper)
      Converts a JavaScript array of key-value entries into a Java Map.
      Type Parameters:
      R - value type of resulting map
      Parameters:
      jsEntries - JavaScript object representing an array of entries
      entryValueMapper - function used to convert JS value into type R
      Returns:
      a LinkedHashMap preserving JS iteration order
    • toList

      public static <R> List<R> toList(JSObject jsArray, Function<Object,R> arrayValueMapper)
      Converts a JavaScript array into a Java List.
      Type Parameters:
      R - element type
      Parameters:
      jsArray - JavaScript array object
      arrayValueMapper - function to convert JS values into type R
      Returns:
      a ArrayList containing converted values
    • toArray

      public static <R> R[] toArray(JSObject jsArray, Class<R> type, Function<Object,R> arrayValueMapper)
      Converts a JavaScript array into a Java array.
      Type Parameters:
      R - element type
      Parameters:
      jsArray - JavaScript array object
      type - component type of resulting array
      arrayValueMapper - function to convert JS values into type R
      Returns:
      a Java array of type R
    • getLength

      public static int getLength(JSObject obj)
      Retrieves the length of a JavaScript array-like object.
      Parameters:
      obj - JavaScript object
      Returns:
      length as integer, or 0 if unavailable
    • getAtIndex

      public static Object getAtIndex(JSObject obj, int index)
      Retrieves an element at a given index from a JavaScript array.
      Parameters:
      obj - JavaScript array
      index - index to access
      Returns:
      value at index, or null if unavailable
    • getEntryAtIndex

      public static Object getEntryAtIndex(JSObject obj, int index)
      Retrieves a JS object entry at a given index.
      Parameters:
      obj - JavaScript array of entries
      index - index to access
      Returns:
      entry object, or null if unavailable
    • getAtIndex

      public static Object getAtIndex(JSObject obj, String methodName, int index)
      Internal helper to access a JavaScript array-like structure using a JS method.
      Parameters:
      obj - JavaScript object
      methodName - JavaScript method name to invoke
      index - index parameter
      Returns:
      result of JS call, or null if unavailable
    • callJSMethod

      private static Object callJSMethod(String methodName, Object... objs)
      Calls a JavaScript method on the given object.
      Parameters:
      methodName - JS method name
      objs - arguments (first argument must be JSObject target)
      Returns:
      result of JS call
    • getWindow

      private static JSObject getWindow(JSObject anyJsObject)
      Retrieves the JavaScript global window object.
      Parameters:
      anyJsObject - any JSObject instance
      Returns:
      globalThis JS object