"use strict" // Parameters we accept const RE_IDENTIFIER = /^[0-9A-Za-z\\._\\-]+$/; // Identifiers can generally be non-critical SQL characters. const RE_GUID = /^[0-9a-z\\-]+$/; // GUIDs can only be lower case a-z, numbers and dashes const exampleGuid: string = "de305d54-75b4-431b-adb2-eb6b9e546014"; export module checks { /** * Checks if a hash was is empty. * http://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object * @param {[type]} obj [description] * @return {[type]} [description] */ export function isEmpty(obj: Object | undefined | null): boolean { if (!obj) return true; return Object.keys(obj).length === 0; } /** * Checks if the given paramters is compatible with regex and length * requriements. * @param {[type]} param [description] * @param {[type]} regex [description] * @param {[type]} length [description] * @return {[type]} [description] */ export function isValidParameter(param: string | number, regex?: RegExp, length?: number): boolean { // Normal numbers get admitted instantly if (typeof param === "number") { if (isFinite(param)) return true; return false; } // Here we know what we are. const paramstring = param; regex = regex || RE_IDENTIFIER; length = length || 128; if ( !paramstring || paramstring.length > length || !regex.test(paramstring) ) return false; return true; } /** * Checks if a given session ID is actually a valid session ID and properly * formed. * @param {interfaces.sessionID} sessionID [description] * @return {boolean} [description] */ export function isValidGUID(guid: string): boolean { if (guid.length != exampleGuid.length) return false; if (!(guid.charAt(8) == "-")) return false; if (!(guid.charAt(13) == "-")) return false; if (!(guid.charAt(18) == "-")) return false; if (!(guid.charAt(23) == "-")) return false; if (!isValidParameter(guid, RE_GUID, exampleGuid.length)) return false; return true; } }