All files / functions index.js

96.42% Statements 27/28
65.62% Branches 21/32
100% Functions 5/5
100% Lines 27/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                                  5x           1x                   1x 1x   1x                 1x 1x   1x                 1x 1x   1x                         1x 1x 1x     1x   1x 1x 1x       1x 1x       1x 1x       1x 1x       1x 1x       1x        
/*
 * Copyright (c) 2020 the original author or authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
 
// Collection of common functions that used by almost all services
const common_functions = {
    /**
     * Construct full url from given parameters
     * @returns {String}
     */
    get_full_url(base_url, server, port) {
        return `${server}:${port}/${base_url}`;
    },
 
    /**
     * Check url
     * @param {String} image_url
     * @returns {Boolean}
     */
    isUrl(image_url){
        // regex to check passed parameter is url or relative path
        let urlRegEX = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/;
        let isUrl = urlRegEX.test(image_url);
 
        return isUrl;
    },
 
    /**
     * Check whether string is base64
     * @param {String} path
     * @returns
     */
    isBase64(image_data){
        let base64regex = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$/;
        let isBase64 = base64regex.test(image_data);
 
        return isBase64;
    },
 
    /**
     * Check whether string is relative path or not
     * @param {String} path
     * @returns
     */
    isPathRelative(path) {
        Iif(typeof path !== 'string') return false
        let isAbsolute = /^([a-zA-Z]\:|\\\\[^\/\\:*?"<>|]+\\[^\/\\:*?"<>|]+)(\\[^\/\\:*?"<>|]+)+(\.[^\/\\:*?"<>|]+)$/.test(path);
 
        return !isAbsolute;
    },
 
     /**
     * Add extra options to url
     * @param {String} url
     * @param {Object} globalOptions
     * @param {Object} localOptions
     * @param {Object} required_parameters
     * @returns {String}
     */
    add_options_to_url(url, globalOptions, localOptions, required_parameters){
        // merge options passed by localy and globally NOTE: global options will override local on if same value passed from both of them
        let uniqueOptions = {...localOptions, ...globalOptions};
        let isThereAnyOptions = Object.keys(uniqueOptions);
        let isLimitOptionExist = false;
 
        // check whether any parameters passed
        if(isThereAnyOptions.length > 0){
            // check limit parameter passed and it is allowed for particular endpoint (ex: it is not requrid for add())
            if(uniqueOptions['limit'] >= 0 && required_parameters['limit']){
                isLimitOptionExist = true;
                url = `${url}?limit=${uniqueOptions['limit']}`
            }
 
            // check det_prob_threshold parameter passed and is it allowed for particular endpoint
            if(uniqueOptions['det_prob_threshold'] >= 0 && required_parameters['det_prob_threshold']){
                url = `${url}${isLimitOptionExist ? '&' : '?'}det_prob_threshold=${uniqueOptions['det_prob_threshold']}`
            }
 
            // check prediction_count passed and is it allowed for particular endpoint
            if(uniqueOptions['prediction_count'] >= 0 && required_parameters['prediction_count']){
                url = `${url}${isLimitOptionExist ? '&' : '?'}prediction_count=${uniqueOptions['prediction_count']}`
            }
 
            // check face_plugins passed and is it allowed for particular endpoint
            if(uniqueOptions['face_plugins'] && required_parameters['face_plugins']){
                url = `${url}${isLimitOptionExist ? '&' : '?'}face_plugins=${uniqueOptions['face_plugins']}`
            }
 
            // check status passed and is it allowed for particular endpoint
            if(uniqueOptions['status'] && required_parameters['status']){
                url = `${url}${isLimitOptionExist ? '&' : '?'}status=${uniqueOptions['status']}`
            }
        }
 
        return url;
    }
}
 
export { common_functions }