Query range, parse numeric from/to values

Covered cases: 10, >10, <10, <=10, >=10, 10..15, 10.., ..10
To validate user input:
    boolean isCorrectNumRange(str _rangeValue)
    {
        boolean ret = false;
        if (match("^[0-9. -=<>]+$", _rangeValue))
        {
            ret = true;
        }
        return ret;
    }

    container getRangeValue(str _rangeValue)
    {
        container   tmp;
        int         len, rangeLen = strLen(_rangeValue);
        InventQty   qtyFrom = 0, qtyTo = 0, qtyTmp;
        boolean     fromSet = false, toSet = false;
        str         val;
        _rangeValue = strLRTrim(_rangeValue);
        if (1 == strFind(_rangeValue, '>', 1, 1))
        {
            if (2 == strFind(_rangeValue, '=', 2, 1))
            {
                tmp = [subStr(_rangeValue, 3, rangeLen - 1), ''];
            }
            else
            {
                tmp = [int2str(str2int(subStr(_rangeValue, 2, rangeLen - 1))+1), ''];
            }
        }
        else if (1 == strFind(_rangeValue, '<', 1, 1))
        {
            if (2 == strFind(_rangeValue, '=', 2, 1))
            {
                tmp = ['', subStr(_rangeValue, 3, rangeLen - 1)];
            }
            else
            {
                tmp = ['', int2str(str2int(subStr(_rangeValue, 2, rangeLen - 1))-1)];
            }
            
        }
        else
        {
            tmp = str2con(_rangeValue, '..');
        }
        len     = conLen(tmp);
        val     = strLRTrim(conPeek(tmp, 1));
        if (val)
        {
            fromSet = true;
        }
        qtyFrom = str2int(val);
        if (len > 1)
        {
            val     = strLRTrim(conPeek(tmp, 2));
            if (val)
            {
                toSet = true;
            }
            qtyTo   = str2int(val);
        }
        else
        {
            qtyTo = qtyFrom;
            toSet = true;
        }
        if (fromSet && toSet && qtyFrom > qtyTo)
        {
            qtyTmp  = qtyFrom;
            qtyFrom = qtyTo;
            qtyTo   = qtyTmp;
        }
        return [qtyFrom, qtyTo, fromSet, toSet];
    }

 

Search

About

DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics AX.

Authors are allowed to set their own AdSense units and "buy me a coffee" link.
Join us.

Blog Tags