All files index.js

95.65% Statements 22/23
70.83% Branches 17/24
100% Functions 4/4
100% Lines 22/22

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                                  4x           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 relative path or not
     * @param {String} path 
     * @returns 
     */
    isPathRelative(path) {
        Iif(typeof path !== 'string') return false 
        let isAbsolute = /^(?:\/|[a-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);
        
        // check whether any parameters passed
        Eif(isThereAnyOptions.length > 0){
            // check whether limit parameter passed and it is allowed for particular endpoint (ex: it is not requrid for add())
            Eif(uniqueOptions['limit'] >= 0 && required_parameters['limit']){
                url = `${url}?limit=${uniqueOptions['limit']}`
            }
 
            // check whether det_prob_threshold parameter passed and is it allowed for particular endpoint
            Eif(uniqueOptions['det_prob_threshold'] >= 0 && required_parameters['det_prob_threshold']){
                url = `${url}&det_prob_threshold=${uniqueOptions['det_prob_threshold']}`
            }
 
            // check whether prediction_count passed and is it allowed for particular endpoint
            Eif(uniqueOptions['prediction_count'] >= 0 && required_parameters['prediction_count']){
                url = `${url}&prediction_count=${uniqueOptions['prediction_count']}`
            }
 
            // check whether face_plugins passed and is it allowed for particular endpoint
            Eif(uniqueOptions['face_plugins'] && required_parameters['face_plugins']){
                url = `${url}&face_plugins=${uniqueOptions['face_plugins']}`
            }
 
            // check whether status passed and is it allowed for particular endpoint
            Eif(uniqueOptions['status'] && required_parameters['status']){
                url = `${url}&status=${uniqueOptions['status']}`
            }
        }
 
        return url;
    }
}
 
export { common_functions }