All files / utils/string.utils string.utils.ts

100% Statements 16/16
100% Branches 3/3
100% Functions 6/6
100% Lines 14/14

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                  162x     50x   50x 162x 162x 162x     50x                       4x 5x         4x   4x 5x 10x     4x    
import { SelectedFacets } from "../../request/search/search.request.type";
import { customIdentity, customPickBy } from "../functional/functional";
 
export function camelCaseToSnackCaseObject(object: any) {
 
    /**
     * ref
     * https://stackoverflow.com/questions/54246477/how-to-convert-camelcase-to-snake-case-in-javascript
     */
    const _camelToSnakeCase = (str: string) => str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
 
 
    let newObject: any = {};
 
    Object.keys(object).forEach(key => {
        const oldKey = key;
        const newKey = _camelToSnakeCase(key);
        newObject[newKey] = object[oldKey]
    });
 
    return newObject;
}
 
 
 
 
/**
 * convert a facets object shape into a string or repeated facets string like
 * 'tags=greeting&tags=hello' 
 */
export function convertFacetsObjectIntoString(facets: SelectedFacets) {
 
    const facetsInputs = customPickBy(facets, (facetsArray) =>
        facetsArray &&
        facetsArray.length &&
        facetsArray.filter(customIdentity).length > 0
    );
 
    let facetsString = '';
 
    for (const key of Object.keys(facetsInputs))
        for (const singleValue of facets[key])
            facetsString += `${key}=${singleValue}&`;
 
    // slice remove the last `&`
    return facetsString.slice(0, facetsString.length - 1);
}