JavaScript Utilities

From Q
(Redirected from MinWithReplacedNaN)
Jump to: navigation, search

This page contains JavaScript functions that can be used when writing JavaScript for use in Q. These functions do not relate to specific aspects of JavaScript or Q.

To make these functions available when writing a QScript or Rule see JavaScript Reference.

isNumber()

Returns true if x is a number.

asNumber(x)

This function converts a string into a number.

asNumbers(x)

This function converts an array of strings into an array of numbers

isArray(x)

This function returns true if x is an array.

minWithReplacedNaN(x, replace_NaN_with)

This function finds the minimum value of the input x. It replaced any NaN values found in x with the value specified by replace_NaN_with. If x contains elements that are also arrays (like a Q Table, for example), then it will apply itself to these arrays also, resulting in it returning the minimum element among all the arrays.

minWithNaNasNegativeInfinity(x)

This function finds the smallest element in x, where any NaN values will be replaced with the smallest number that is recognized by JavaScript, which is negative infinity.

isPositiveInteger(value)

This function returns true if the input value is a positive integer.

intersection(array_1, array_2)

This function returns an array containing values that are common between the two arrays.

difference(array_1, array_2)

This function returns an array containing values that are in array_1 but not in array_2.

unique(array)

This function returns an array containing unique values of the input array, i.e. no value appears twice in the output.

getHash(str)

This function returns a 32 bit integer hash generated from the string str.

roundDecimalNumber(x, num_decimals)

Round x to the specified number of decimals.


Source Code

//checks if it is a number
function isNumber(x) {
    return isFinite(x) && !isNaN(parseFloat(x));
}

function asNumber(x) {
   return parseFloat(x.replace(/[^0-9.-]+/g, ''))
}

function asNumbers(x) {//converts an array of strings into an array of numbers
    var numbers = [];
    for (var value in x)
        numbers.push(asNumber(x[value]))
    return numbers;
}

function isArray(x) {
    return x instanceof Array
}

// returns the minimum value of an array of any size; where NaN is replaced with another value
function minWithReplacedNaN(x, replace_NaN_with) {
    var min_value = Number.POSITIVE_INFINITY;
    var n = x.length;
    for (var i = 0; i < n; i++){
        var value = x[i];
        if (isArray(value))
            value = minWithReplacedNaN(value, replace_NaN_with);
        if (isNaN(value))
            value = replace_NaN_with;
        if (min_value > value)
            min_value = value;
    }
    return min_value;
}

// returns the minimum value of an array of any size; where NaN is replaced negative infinity - the smallest possible number
function minWithNaNasNegativeInfinity(x) {
    return minWithReplacedNaN(x, Number.NEGATIVE_INFINITY);
}

function isPositiveInteger(value) { 
    return !isNaN(parseInt(value,10)) && (parseFloat(value,10) == parseInt(value,10)) && value > 0; 
}

function intersection(array_1, array_2) {
    var intersection = [];
    for (var i = 0; i < array_1.length; i++)
        for (var j = 0; j < array_2.length; j++)
            if (array_1[i] == array_2[j]) {
               intersection.push(array_1[i]);
               break;
            }
    return unique(intersection);
}

function difference(array_1, array_2) {
    var difference = [];
    for (var i = 0; i < array_1.length; i++) {
        var is_found = false;
        for (var j = 0; j < array_2.length; j++) {
            if (array_1[i] == array_2[j]) {
                is_found = true;
                break;
            }
        }
        if (!is_found)
            difference.push(array_1[i]);
    }
    return unique(difference);
}

function unique(array) {
    var unique_array = [];
    for (var i = 0; i < array.length; i++) {
        var is_repeated = false;
        for (var j = 0; j < unique_array.length; j++) {
            if (unique_array[j] == array[i]) {
                is_repeated = true;
                break;
            }
        }
        if (!is_repeated)
            unique_array.push(array[i]);
    }
    return unique_array;
}

// Modified from http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery
function getHash(str) {
    var hash = 0;
    if (str.length == 0)
        return hash;
    for (var i = 0; i < str.length; i++) {
        var chr = str.charCodeAt(i);
        hash = ((hash << 5) - hash) + chr;
        hash |= 0; // Convert to 32 bit integer
    }
    return hash;
}

// round the number to a certain number of decimal places
function roundDecimalNumber(x, num_decimals) {
    x = parseFloat(x.toPrecision(13)); // Q rounds the raw numbers to 13 decimal places
    if (isNaN(x) || x == 0)
        return x;
    return parseFloat(x.toFixed(num_decimals));
}

See Also