Class JavaScriptUtil
java.lang.Object
io.github.xrickastley.sevenelements.util.JavaScriptUtil
Utility class for JavaScript's "quirks".
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanMimics JavaScript's falsy coercion, returningtrueif the value is considered falsy.static booleanMimics JavaScript's truthy coercion, returningtrueif the value is considered truthy.static <T> TlogicalOR(@Nullable T... values) Mimics JavaScript's "logical OR" (||) operator, returning the first truthy value as determined byJavaScriptUtil#isTruthyor the last falsy value as determined byJavaScriptUtil#isFalsyin the provided array.static <T> TnullishCoalesing(@Nullable T... values) Mimics JavaScript's "nullish coalesing" (??) operator, returning the first non-null value ornullif all provided values arenull.static <T> TnullishCoalesingFn(Supplier<? extends T>... suppliers) Mimics JavaScript's "nullish coalesing" (??) operator, returning the firstSupplierto produce a non-null value ornullif all provided values arenull.
-
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 byJavaScriptUtil#isTruthyor the last falsy value as determined byJavaScriptUtil#isFalsyin 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 ornullif all provided values arenull.- Type Parameters:
T- The type of the array.- Parameters:
values- The values to perform the nullish coalesing operation on.
-
nullishCoalesingFn
Mimics JavaScript's "nullish coalesing" (??) operator, returning the firstSupplierto produce a non-null value ornullif all provided values arenull.
ProvidedSupplierinstances are evaluated in insertion order. If aSupplieris encountered, such thatSupplier#getreturns a non-null value, the value is immediately returned and evaluation is ended.- Type Parameters:
T- The type of the array.- Parameters:
suppliers- TheSuppliersto perform the nullish coalesing operation on.
-
isFalsy
Mimics JavaScript's falsy coercion, returningtrueif 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,-0or fulfillsDouble#isNaN.
- Parameters:
any- The value to test.
- it is
-
isTruthy
Mimics JavaScript's truthy coercion, returningtrueif the value is considered truthy.
A value is considered truthy ifisFalsy(value)isfalse, as truthy is a direct negation of falsy.- Parameters:
any- The value to test.
-