Class JavaScriptUtil

java.lang.Object
io.github.xrickastley.sevenelements.util.JavaScriptUtil

public final class JavaScriptUtil extends Object
Utility class for JavaScript's "quirks".
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    isFalsy(@Nullable Object any)
    Mimics JavaScript's falsy coercion, returning true if the value is considered falsy.
    static boolean
    isTruthy(@Nullable Object any)
    Mimics JavaScript's truthy coercion, returning true if the value is considered truthy.
    static <T> T
    logicalOR(@Nullable T... values)
    Mimics JavaScript's "logical OR" (||) operator, returning the first truthy value as determined by JavaScriptUtil#isTruthy or the last falsy value as determined by JavaScriptUtil#isFalsy in the provided array.
    static <T> T
    nullishCoalesing(@Nullable T... values)
    Mimics JavaScript's "nullish coalesing" (??) operator, returning the first non-null value or null if all provided values are null.
    static <T> T
    nullishCoalesingFn(Supplier<? extends T>... suppliers)
    Mimics JavaScript's "nullish coalesing" (??) operator, returning the first Supplier to produce a non-null value or null if all provided values are null.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JavaScriptUtil

      public JavaScriptUtil()
  • Method Details

    • logicalOR

      public static <T> T logicalOR(@Nullable @Nullable T... values)
      Mimics JavaScript's "logical OR" (||) operator, returning the first truthy value as determined by JavaScriptUtil#isTruthy or the last falsy value as determined by JavaScriptUtil#isFalsy in the provided array.
      Type Parameters:
      T - The type of the array.
      Parameters:
      values - The values to perform the logical OR operation on.
    • nullishCoalesing

      @Nullable public static <T> T nullishCoalesing(@Nullable @Nullable T... values)
      Mimics JavaScript's "nullish coalesing" (??) operator, returning the first non-null value or null if all provided values are null.
      Type Parameters:
      T - The type of the array.
      Parameters:
      values - The values to perform the nullish coalesing operation on.
    • nullishCoalesingFn

      @Nullable public static <T> T nullishCoalesingFn(Supplier<? extends T>... suppliers)
      Mimics JavaScript's "nullish coalesing" (??) operator, returning the first Supplier to produce a non-null value or null if all provided values are null.

      Provided Supplier instances are evaluated in insertion order. If a Supplier is encountered, such that Supplier#get returns a non-null value, the value is immediately returned and evaluation is ended.
      Type Parameters:
      T - The type of the array.
      Parameters:
      suppliers - The Suppliers to perform the nullish coalesing operation on.
    • isFalsy

      public static boolean isFalsy(@Nullable @Nullable Object any)
      Mimics JavaScript's falsy coercion, returning true if the value is considered falsy.

      A value is considered falsy if:
      • it is null,
      • it is false,
      • it is an empty string (""), or
      • it is 0, -0 or fulfills Double#isNaN.
      If any of the provided conditions are fulfilled, the value is considered falsy.
      Parameters:
      any - The value to test.
    • isTruthy

      public static boolean isTruthy(@Nullable @Nullable Object any)
      Mimics JavaScript's truthy coercion, returning true if the value is considered truthy.

      A value is considered truthy if isFalsy(value) is false, as truthy is a direct negation of falsy.
      Parameters:
      any - The value to test.