ItemGenerator.brs

' /**
'  * @module ItemGenerator
'  */
' /**
'  * @memberof module:ItemGenerator
'  * @name ItemGenerator
'  * @function
'  * @instance
'  * @param {Dynamic} scheme  - A scheme with desired object structure. Can be
'  * any simple type, array of types or associative array in form
'  *
'  * ``` { propertyName1 : "propertyType1"
'  *   propertyName2 : "propertyType2"
'  *   ...
'  *   propertyNameN : "propertyTypeN" }
'  * ```
'  * @description Creates an ItemGenerator instance
'  * @returns {Object} An object according to specified scheme or invalid, if scheme is not valid.
'  */
function __Rooibos_ItemGenerator_builder()
    instance = {}
    instance.new = function()
        m.isValid = Rooibos_Common_isValid(scheme)
    end function
    ' /**
    '  * @memberof module:ItemGenerator
    '  * @name GetItem
    '  * @function
    '  * @instance
    '  * @param {Dynamic} scheme  - A scheme with desired object structure. Can be
    '  * any simple type, array of types or associative array in form
    '  *
    '  * ``` { propertyName1 : "propertyType1"
    '  *   propertyName2 : "propertyType2"
    '  *   ...
    '  *   propertyNameN : "propertyTypeN" }
    '  * ```
    '  * @description Gets an item according to the schem
    '  * @returns {Object}  An object according to specified scheme or invalid if scheme is not one of simple type, array or associative array.
    '  */
    instance.GetItem = function(scheme as object) as object
        item = invalid
        if Rooibos_Common_isAssociativeArray(scheme) then
            item = m.getAssocArray(scheme)
        else if Rooibos_Common_isArray(scheme) then
            item = m.getArray(scheme)
        else if Rooibos_Common_isString(scheme) then
            item = m.getSimpleType(lCase(scheme))
        end if
        return item
    end function
    ' /**
    '  * @memberof module:ItemGenerator
    '  * @name GetAssocArray
    '  * @function
    '  * @instance
    '  * @param {Dynamic} scheme  - A scheme with desired object structure. Can be
    '  * any simple type, array of types or associative array in form
    '  *
    '  * ``` { propertyName1 : "propertyType1"
    '  *   propertyName2 : "propertyType2"
    '  *   ...
    '  *   propertyNameN : "propertyTypeN" }
    '  * ```
    '  * @description Generates associative array according to specified scheme.
    '  * @returns {Object} An associative array according to specified scheme.
    '  */
    instance.GetAssocArray = function(scheme as object) as object
        item = {}
        for each key in scheme
            if not item.DoesExist(key) then
                item[key] = m.getItem(scheme[key])
            end if
        end for
        return item
    end function
    ' /**
    '  * @memberof module:ItemGenerator
    '  * @name GetArray
    '  * @function
    '  * @instance
    '  * @param {Dynamic} An array with desired object types.
    '  *
    '  * ``` { propertyName1 : "propertyType1"
    '  *   propertyName2 : "propertyType2"
    '  *   ...
    '  *   propertyNameN : "propertyTypeN" }
    '  * ```
    '  * @description Generates array according to specified scheme.
    '  * @returns {Object} An array according to specified scheme.
    '  */
    instance.GetArray = function(scheme as object) as object
        item = []
        for each key in scheme
            item.Push(m.getItem(key))
        end for
        return item
    end function
    ' /**
    '  * @memberof module:ItemGenerator
    '  * @name GetSimpleType
    '  * @function
    '  * @instance
    '  * @description Generates random value of specified type
    '  * @param {Dynamic} typeStr - name of desired object type.
    '  * @returns {Object} A simple type object or invalid if type is not supported.
    '  */
    instance.GetSimpleType = function(typeStr as string) as object
        item = invalid
        if typeStr = "integer" or typeStr = "int" or typeStr = "roint" then
            item = m.getInteger()
        else if typeStr = "float" or typeStr = "rofloat" then
            item = m.getFloat()
        else if typeStr = "string" or typeStr = "rostring" then
            item = m.getString(10)
        else if typeStr = "boolean" or typeStr = "roboolean" then
            item = m.getBoolean()
        end if
        return item
    end function
    ' /**
    '  * @memberof module:ItemGenerator
    '  * @name GetBoolean
    '  * @function
    '  * @instance
    '  * @description Generates random boolean value.
    '  * @returns {Boolean} A random boolean value.
    '  */
    instance.GetBoolean = function() as boolean
        return Rooibos_Common_asBoolean(Rnd(2) \ Rnd(2))
    end function
    ' /**
    '  * @memberof module:ItemGenerator
    '  * @name GetInteger
    '  * @function
    '  * @instance
    '  * @description Generates random integer value from 1 to specified seed value.
    '  * @param {Dynamic} seed - seed value for Rnd function.
    '  * @returns {integer} A random integer value.
    '  */
    instance.GetInteger = function(seed = 100) as integer
        return Rnd(seed)
    end function
    ' /**
    '  * @memberof module:ItemGenerator
    '  * @name GetFloat
    '  * @function
    '  * @instance
    '  * @description Generates random float value.
    '  * @returns {float} A random float value.
    '  */
    instance.GetFloat = function() as float
        return Rnd(0)
    end function
    ' /**
    '  * @memberof module:ItemGenerator
    '  * @name GetString
    '  * @function
    '  * @instance
    '  * @description Generates random string with specified length.
    '  * @param {Dynamic} seed - A string length
    '  * @returns {string} A random string value or empty string if seed is 0.
    '  */
    instance.GetString = function(seed as integer) as string
        item = ""
        if seed > 0 then
            stringLength = Rnd(seed)
            for i = 0 to stringLength
                chType = Rnd(3)
                if chType = 1 then 'Chr(48-57) - numbers
                    chNumber = 47 + Rnd(10)
                else if chType = 2 then 'Chr(65-90) - Uppercase Letters
                    chNumber = 64 + Rnd(26)
                else 'Chr(97-122) - Lowercase Letters
                    chNumber = 96 + Rnd(26)
                end if
                item = item + Chr(chNumber)
            end for
        end if
        return item
    end function
    return instance
end function
function Rooibos_ItemGenerator()
    instance = __Rooibos_ItemGenerator_builder()
    instance.new()
    return instance
end function