All files / wdclib ApprovedOrigins.js

100% Statements 18/18
83.33% Branches 5/6
100% Functions 4/4
100% Lines 18/18
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    1x 1x   1x           6x   6x                   3x   3x     3x   3x                   3x 3x   3x     3x                     6x   6x 3x     3x   3x    
import * as Cookies from 'cookies-js';
 
const SEPARATOR = ',';
export const APPROVED_ORIGINS_KEY = 'wdc_approved_origins';
// alias of Cookies for testing ( and for future replacement? )
export let CookiesLib = Cookies;
 
/**
 * @returns {*}
 */
function _getApprovedOriginsValue () {
    let result = CookiesLib.get(APPROVED_ORIGINS_KEY);
 
    return result;
}
 
/**
 *
 * @param {Array<String>} originArray
 * @returns {*}
 */
function _saveApprovedOrigins (originArray) {
 
    const APPROVED_ORIGINS = originArray.join(SEPARATOR);
 
    console.log(`Saving approved origins  ${APPROVED_ORIGINS} `);
 
    // We could potentially make this a longer term cookie instead of just for the current session
    let result = CookiesLib.set(APPROVED_ORIGINS_KEY, APPROVED_ORIGINS);
 
    return result;
}
 
/**
 * Adds an approved origin to the list already saved in a session cookie
 * @param {String} origin
 * @returns {Object|Undefined}
 */
export function addApprovedOrigin (origin) {
 
    Eif (origin) {
        let origins = getApprovedOrigins();
 
        origins.push(origin);
 
        // pass along the output of the private function
        return _saveApprovedOrigins(origins);
    }
 
}
 
/**
 * Retrieves the origins which have already been approved by the user
 * @returns {Array<String>}
 */
export function getApprovedOrigins () {
 
    let originsString = _getApprovedOriginsValue();
 
    if (!originsString || originsString.length === 0) {
        return [];
    }
 
    let origins = originsString.split(SEPARATOR);
 
    return origins;
}