Return

All tests / src/helpers index.ts

86.36% Statements 38/44
76.92% Branches 10/13
92.3% Functions 12/13
81.25% Lines 26/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 12718x                     87x 87x   196x   135x 135x   87x 18x     69x                         18x                     18x             18x                       18x 2x 1x   1x                       18x             18x 4x 4x                                   4x                   18x 3x 3x 2x   1x    
import { defaultLocale, MAX_STEPS } from '@/constants/site';
/**
 * @description Utility function to concatenate classes.
 *
 * @example
 *     clx('class1', 'class2', 'class3');
 *     returns 'class1 class2 class3'
 *
 * @param {(string | null | undefined)[]} classes
 * @returns {string}
 */
export const clx = (...classes: Array<string | null | undefined>) => {
    const filteredClasses = classes
        .filter((element) => {
            return element !== '' && element !== null && element !== undefined;
        })
        .map((item) => item?.trim())
        .filter((item) => item !== '');
 
    if (!filteredClasses.length) {
        return '';
    }
 
    return filteredClasses.join(' ');
};
 
/**
 * @description Utility function to check if locale is not defaultLocale.
 *
 * @example
 *     isNotEng('en');
 *     returns false
 *
 * @param {string | undefined} locale
 * @returns {boolean}
 */
export const isNotEng = (locale: string | undefined) => locale !== defaultLocale;
 
/**
 * @description Utility function to clean trailing slash of a path.
 *
 * @example
 *     cleanTrailingSlash('/');
 *     returns ''
 *
 * @param {string} path
 */
export const cleanTrailingSlash = (path: string) => (path !== '/' ? path : '');
 
/**
 * @description Serialize a value for a JSON-LD <script> block. Escaping '<'
 * prevents breaking out of the script tag (XSS) when values derive from
 * user-influenced input such as the router locale or slugs.
 */
export const jsonLdString = (value: unknown): string => JSON.stringify(value).replace(/</g, '\\u003c');
 
/**
 * @description Utility function to remove trailing slash of a string.
 *
 * @example
 *     removeTrailingSlash('string/');
 *     returns 'string'
 *
 * @param {string} str
 * @returns {string}
 */
export const removeTrailingSlash = (str: string) => {
    if (str.substr(-1) === '/') {
        return str.substr(0, str.length - 1);
    }
    return str;
};
 
/**
 * @description Utility function to return lang if not english.
 *
 * @example
 *     getLang('en');
 *     returns ''
 *
 * @param {string | undefined} lang
 */
export const getLang = (lang: string | undefined) => (isNotEng(lang) ? `/${lang}` : '');
 
/**
 *
 * @param ref  React.RefObject<HTMLDivElement>
 * @returns  interval
 */
export const setInverval = (ref: React.RefObject<HTMLDivElement | null>) => {
    let step = 0;
    const interval = setInterval(() => {
        Iif (ref.current) {
            step += 1;
            if (step < MAX_STEPS) {
                ref.current.scrollBy({
                    top: ref.current.clientHeight,
                    behavior: 'smooth',
                });
            } else {
                step = 0;
                ref.current.scrollTo({
                    top: 0,
                    behavior: 'smooth',
                });
            }
        }
    }, 15000);
 
    return interval;
};
 
/**
 * @description Utility function to use SWR with fetcher.
 * @example const { data, error } = useSWR('/api/weather', fetcher);
 * @param {string} url
 * @returns {Promise<unknown>}
 * @see https://swr.vercel.app/docs/data-fetching
 */
export const fetcher = (url: string): Promise<unknown> =>
    fetch(url).then((res) => {
        if (res.ok) {
            return res.json();
        }
        throw new Error(res.statusText);
    });