All files utils.js

100% Statements 81/81
94.59% Branches 35/37
100% Functions 8/8
100% Lines 81/81

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 821x 2x 2x 1x 1x 8x 8x 1x 1x 2x 2x 1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 151x 151x 151x 1x 1x 18x 16x 18x 15x 18x 15x 15x 15x 18x 11x 11x 18x 7x 7x 18x 5x 5x 18x 6x 6x 18x 5x 5x 18x 3x 3x 15x 15x 18x 1x 1x 17x 17x 17x 11x 11x 11x 17x 12x 12x 17x 12x 12x 17x 11x 11x 17x 10x 10x 17x 10x 10x 17x 17x 17x  
export function formatTimestamp(timestamp) {
    return new Date(timestamp).toLocaleString();
}
 
export function isNonEmptyString(value) {
    return typeof value === 'string' && value.trim().length > 0;
}
 
export function deepClone(obj) {
    return JSON.parse(JSON.stringify(obj));
}
 
export function createId() {
    return Date.now() + '-' + Math.random().toString(36).substr(2, 9);
}
 
export function isNodeEnvironment() {
    return typeof window === 'undefined';
}
 
export function randomLetter() {
    const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    return letters[Math.floor(Math.random() * 26)];
}
 
export function parseStateFromUrl(urlString) {
    if (!urlString) return {};
    const qIndex = urlString.indexOf('?');
    if (qIndex === -1) return {};
    const queryString = urlString.substring(qIndex + 1);
    if (!queryString) return {};
    const params = new URLSearchParams(queryString);
    const result = {};
 
    if (params.has('words')) {
        result.words = params.get('words').split(',');
    }
    if (params.has('width')) {
        result.grid_width = parseInt(params.get('width'), 10);
    }
    if (params.has('height')) {
        result.grid_height = parseInt(params.get('height'), 10);
    }
    if (params.has('all_cross')) {
        result.all_cross = params.get('all_cross') === 'true';
    }
    if (params.has('allow_diagonals')) {
        result.allow_diagonals = params.get('allow_diagonals') === 'true';
    }
    if (params.has('allow_reverse')) {
        result.allow_reverse = params.get('allow_reverse') === 'true';
    }
 
    return result;
}
 
export function serializeStateToUrl(state) {
    const params = new URLSearchParams();
 
    if (state.words && state.words.length > 0) {
        const wordValues = state.words.map(w => typeof w === 'string' ? w : w.value);
        params.set('words', wordValues.join(','));
    }
    if (state.grid_width !== undefined) {
        params.set('width', String(state.grid_width));
    }
    if (state.grid_height !== undefined) {
        params.set('height', String(state.grid_height));
    }
    if (state.all_cross !== undefined) {
        params.set('all_cross', String(state.all_cross));
    }
    if (state.allow_diagonals !== undefined) {
        params.set('allow_diagonals', String(state.allow_diagonals));
    }
    if (state.allow_reverse !== undefined) {
        params.set('allow_reverse', String(state.allow_reverse));
    }
 
    return params.toString();
}