Return

All tests / src/helpers index.ts

84.21% Statements 32/38
75% Branches 9/12
90.9% Functions 10/11
77.77% Lines 21/27

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 12216x                     122x 122x   303x   192x 192x   122x 26x     96x                         19x                     22x             16x                       18x 18x 17x   1x                       16x             16x 5x 5x                                   5x                        
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;
};
 
/*
 * SDD-L07: the generic `fetcher` used to live here. It returned `Promise<unknown>`, so both of its
 * callers cast the result at the call site, and it competed with six hand-rolled copies of the same
 * function in the hooks. All eight are now `helpers/createFetcher.ts`, which takes the route's schema
 * and returns a value that has actually been checked against it.
 *
 * It is gone rather than deprecated on purpose: leaving an unvalidated fetcher exported from the
 * barrel is how the next hook would quietly get written the old way.
 */