Core / Utils

assertNotNil

import { assertNotNil } from "@uxf/core/utils/assert-not-nil";

const testObject: { value: number | null } = { value: 10 };

assertNotNil(testObject.value);

// is the same as

if (isNil(testObject.value)) {
    throw new Error("Value is null");
}

camelCaseToDash

import { camelCaseToDash } from "@uxf/core/utils/camelCaseToDash";

const example = camelCaseToDash("fooBar"); /* returns "foo-bar" */

capitalize

import { capitalize } from "@uxf/core/utils/capitalize";

const example = capitalize("hello world"); /* returns "Hello world" */

composeRefs

import { composeRefs } from "@uxf/core/utils/composeRefs";

const firstRef = useRef<HTMLDivElement>(null);
const secondRef = useRef<HTMLDivElement>(null);

const example = <div ref={composeRefs(firstRef, secondRef)} />;

cx

import { cx } from "@uxf/core/utils/cx";

/* fork of https://github.com/lukeed/clsx */
const example = cx("foo", true && "bar", "baz", false && "bam", undefined); /* returns "foo bar baz" */

filterNullish

import { filterNullish } from "@uxf/core/utils/filter-nullish";

filterNullish([0, "text", null, undefined, [], {}]); /* returns [0, "text", [], {}]  */

forwardRef

import { forwardRef } from "@uxf/core/utils/forwardRef";

interface Props {
    className?: string;
}

// will enforce 'displayName'
export const ExampleComponent = forwardRef<HTMLDivElement, Props>("ExampleComponent", (props, ref) => {
    return <div className={props.className} ref={ref} />;
});

isEmpty

import { isEmpty } from "@uxf/core/utils/is-empty";

isEmpty("not-empty"); /* returns false */
isEmpty(""); /* returns true */
isEmpty(["1"]); /* returns false */
isEmpty([]); /* returns true */

isBrowser / isServer

import { isBrowser } from "@uxf/core/utils/isBrowser";
import { isServer } from "@uxf/core/utils/isServer";

const browserExample = isBrowser; /* returns true if DOM is available */
const serverExample = isServer; /* returns true if DOM is NOT available */

isNil

import { isNil } from "@uxf/core/utils/is-nil";

isNil(null);      /* returns true */
isNil(undefined); /* returns true */
isNil(true);      /* returns false */
isNil(1);         /* returns false */
isNil(0);         /* returns false */
isNil([]);        /* returns false */
isNil("string");  /* returns false */

isNotNil

import { isNotNil } from "@uxf/core/utils/is-not-nil";

isNotNil(null);      /* returns false */
isNotNil(undefined); /* returns false */
isNotNil(true);      /* returns true */
isNotNil(1);         /* returns true */
isNotNil(0);         /* returns true */
isNotNil([]);        /* returns true */
isNotNil("string");  /* returns true */
import { last } from "@uxf/core/utils/last";

last([1, 2]); /* returns 2 */
last([]);     /* returns undefined */

slugify

import { slugify } from "@uxf/core/utils/slugify";

const example = slugify("Jak se dnes máte?"); /* returns "jak-se-dnes-mate" */

trimTrailingZeros

import { trimTrailingZeros } from "@uxf/core/utils/trimTrailingZeros";

const example = trimTrailingZeros("120,450"); /* returns "120,45" */