All files util.ts

100% Statements 64/64
100% Branches 24/24
100% Functions 8/8
100% Lines 64/64

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    1x   1x                     1x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x   1x 26x 26x 110x 110x 26x 32x 32x 26x 26x   1x 141x 141x 141x 141x 141x 507x 507x 507x 141x 141x   1x 82x 71x 71x 82x   82x   1x 58x   58x 5x 5x   46x 46x 46x 44x 43x 4x 58x 3x 3x   43x 43x  
import type { Route, Router } from '@esmx/router';
import type { Ref } from 'vue';
import { version } from 'vue';
 
export const isVue2 = version.startsWith('2.');
 
/**
 * Define $router and $route properties on a target object.
 * Used to set up global properties for Vue components.
 *
 * @param target - The target object to define properties on (e.g., globalProperties or prototype)
 * @param routerGetter - Getter function for $router (can use `this` in Vue 2)
 * @param routeGetter - Getter function for $route (can use `this` in Vue 2)
 * @param configurable - Whether the properties should be configurable (default: false)
 */
export function defineRouterProperties(
    target: Record<string, unknown>,
    routerGetter: (this: unknown) => Router,
    routeGetter: (this: unknown) => Route,
    configurable = false
): void {
    Object.defineProperties(target, {
        $router: {
            configurable,
            enumerable: false,
            get: routerGetter
        },
        $route: {
            configurable,
            enumerable: false,
            get: routeGetter
        }
    });
}
 
export function createSymbolProperty<T>(symbol: symbol) {
    return {
        set(instance: any, value: T): void {
            instance[symbol] = value;
        },
        get(instance: any): T | undefined {
            return symbol in instance ? instance[symbol] : void 0;
        }
    } as const;
}
 
export function createDependentProxy<T extends object>(
    obj: T,
    dep: Ref<any>
): T {
    return new Proxy(obj, {
        get(target, prop, receiver) {
            dep.value;
            return Reflect.get(target, prop, receiver);
        }
    });
}
 
export function isESModule(obj: unknown): obj is Record<string | symbol, any> {
    if (!obj || typeof obj !== 'object') return false;
    const module = obj as Record<string | symbol, any>;
    return (
        Boolean(module.__esModule) || module[Symbol.toStringTag] === 'Module'
    );
}
 
export function resolveComponent(component: unknown): unknown {
    if (!component) return null;
 
    if (isESModule(component)) {
        return component.default || component;
    }
 
    if (
        component &&
        typeof component === 'object' &&
        !Array.isArray(component) &&
        'default' in component &&
        Object.keys(component).length === 1
    ) {
        return component.default;
    }
 
    return component;
}