All files / src utils.ts

100% Statements 24/24
87.5% Branches 7/8
100% Functions 6/6
100% Lines 23/23

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                                        8x                 4x 8x 2x 4x 4x 4x 4x 4x 5x     4x     2x     6x             4x 14x 14x 14x 22x 22x 22x       14x 11x     3x    
/**
 * Copyright (c) 2020
 *
 * Common utility functions for ThoughtSpot Embed UI SDK
 *
 * @summary Utils
 * @author Ayon Ghosh <ayon.ghosh@thoughtspot.com>
 */
 
import { QueryParams, RuntimeFilter } from './types';
 
/**
 * A simple ID generator that meets the following goals:
 * 1. Provide a way to generate a string guaranteed to be unique when compared
 *    to other strings generated by this function.
 * 2. Make the string complex enough that it is highly unlikely to be
 *    accidentally duplicated by hand (this is key if you're using `ID`
 *    as a private/protected name on an object).
 * Hat-tip: https://gist.github.com/gordonbrander/2230317
 */
export const id = (): string => Math.random().toString(36).substr(2, 9);
 
/**
 * Construct a runtime filters query string from the given filters
 * Refer to the following docs for more details on runtime filter syntax:
 * https://docs.thoughtspot.com/6.2/app-integrate/runtime-filters/apply-runtime-filter.html
 * https://docs.thoughtspot.com/6.2/app-integrate/runtime-filters/runtime-filter-operators.html#
 * @param runtimeFilters
 */
export const getFilterQuery = (runtimeFilters: RuntimeFilter[]): string => {
    if (runtimeFilters && runtimeFilters.length) {
        const filters = runtimeFilters.map((filter, valueIndex) => {
            const index = valueIndex + 1;
            const filterExpr = [];
            filterExpr.push(`col${index}=${filter.columnName}`);
            filterExpr.push(`op${index}=${filter.operator}`);
            filterExpr.push(
                filter.values.map((value) => `val${index}=${value}`).join('&'),
            );
 
            return filterExpr.join('&');
        });
 
        return `**${filters.join('&')}**`;
    }
 
    return null;
};
 
/**
 * Return a query param string composed from the given params object
 * @param queryParams
 */
export const getQueryParamString = (queryParams: QueryParams): string => {
    const qp: string[] = [];
    const params = Object.keys(queryParams);
    params.forEach((key) => {
        const val = queryParams[key];
        Eif (val !== undefined) {
            qp.push(`${key}=${val}`);
        }
    });
 
    if (qp.length) {
        return qp.join('&');
    }
 
    return null;
};