import * as React from "react"; import { CLAMP_MIN_MAX } from "./errors"; export * from "./utils/compareUtils"; // only accessible within this file, so use `Utils.isNodeEnv(env)` from the outside. declare var process: { env: any }; /** Returns whether `process.env.NODE_ENV` exists and equals `env`. */ export function isNodeEnv(env: string) { return typeof process !== "undefined" && process.env && process.env.NODE_ENV === env; } /** Returns whether the value is a function. Acts as a type guard. */ // tslint:disable-next-line:ban-types export function isFunction(value: any): value is Function { return typeof value === "function"; } /** * Returns true if `node` is null/undefined, false, empty string, or an array * composed of those. If `node` is an array, only one level of the array is * checked, for performance reasons. */ export function isReactNodeEmpty(node?: React.ReactNode, skipArray = false): boolean { return ( node == null || node === "" || node === false || (!skipArray && Array.isArray(node) && // only recurse one level through arrays, for performance (node.length === 0 || node.every(n => isReactNodeEmpty(n, true)))) ); } /** * Converts a React child to an element: non-empty string or number or * `React.Fragment` (React 16.3+) is wrapped in given tag name; empty strings * are discarded. */ export function ensureElement(child: React.ReactChild | undefined, tagName: keyof JSX.IntrinsicElements = "span") { if (child == null) { return undefined; } else if (typeof child === "string") { // cull whitespace strings return child.trim().length > 0 ? React.createElement(tagName, {}, child) : undefined; } else if (typeof child === "number" || typeof child.type === "symbol") { // React.Fragment has a symbol type return React.createElement(tagName, {}, child); } else { return child; } } /** * Represents anything that has a `name` property such as Functions. */ export interface INamed { name?: string; } export function getDisplayName(ComponentClass: React.ComponentType | INamed) { return (ComponentClass as React.ComponentType).displayName || (ComponentClass as INamed).name || "Unknown"; } export function isElementOfType
( element: any, ComponentClass: React.ComponentType
, ): element is React.ReactElement
{
return element != null && element.type === React.createElement(ComponentClass).type;
}
/**
* Safely invoke the function with the given arguments, if it is indeed a
* function, and return its value. Otherwise, return undefined.
*/
export function safeInvoke