Use PHP functions in JavaScript

JavaScript min

Return the lowest value in an array or a series of arguments

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
8081
82
83
84
8586
87
88
89
9091
92
93
94
9596
97
98
99
100101
102
103
104
105106
107
108
109
110111
112
113
114
115116
117
118
119
120
function min () {
    // Return the lowest value in an array or a series of arguments  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/min    // +   original by: Onno Marsman
    // +    revised by: Onno Marsman
    // +    tweaked by: Jack
    // %          note: Long code cause we're aiming for maximum PHP compatibility
    // *     example 1: min(1, 3, 5, 6, 7);    // *     returns 1: 1
    // *     example 2: min([2, 4, 5]);
    // *     returns 2: 2
    // *     example 3: min(0, 'hello');
    // *     returns 3: 0    // *     example 4: min('hello', 0);
    // *     returns 4: 'hello'
    // *     example 5: min(-1, 'hello');
    // *     returns 5: -1
    // *     example 6: min([2, 4, 8], [2, 5, 7]);    // *     returns 6: [2, 4, 8]
    var ar, retVal, i = 0,
        n = 0,
        argv = arguments,
        argc = argv.length,        _obj2Array = function (obj) {
            if (Object.prototype.toString.call(obj) === '[object Array]') {
                return obj;
            }
            var ar = [];            for (var i in obj) {
                if (obj.hasOwnProperty(i)) {
                    ar.push(obj[i]);
                }
            }            return ar;
        }, //function _obj2Array
        _compare = function (current, next) {
            var i = 0,
                n = 0,                tmp = 0,
                nl = 0,
                cl = 0;
 
            if (current === next) {                return 0;
            }
            else if (typeof current === 'object') {
                if (typeof next === 'object') {
                    current = _obj2Array(current);                    next = _obj2Array(next);
                    cl = current.length;
                    nl = next.length;
                    if (nl > cl) {
                        return 1;                    } 
                    else if (nl < cl) {
                        return -1;
                    }
                    for (i = 0, n = cl; i < n; ++i) {                        tmp = _compare(current[i], next[i]);
                        if (tmp == 1) {
                            return 1;
                        }
                        else if (tmp == -1) {                            return -1;
                        }
                    }
                    return 0;
                }                return -1;
            }
            else if (typeof next == 'object') {
                return 1;
            }            else if (isNaN(next) && !isNaN(current)) {
                if (current == 0) {
                    return 0;
                }
                return (current < 0 ? 1 : -1);            }
            else if (isNaN(current) && !isNaN(next)) {
                if (next == 0) {
                    return 0;
                }                return (next > 0 ? 1 : -1);
            }
            
            if (next == current) {
                return 0;            }
            return (next > current ? 1 : -1);
        }; //function _compare
    if (argc === 0) {
        throw new Error('At least one value should be passed to min()');    }
    else if (argc === 1) {
        if (typeof argv[0] === 'object') {
            ar = _obj2Array(argv[0]);
        }        else {
            throw new Error('Wrong parameter count for min()');
        }
        if (ar.length === 0) {
            throw new Error('Array must contain at least one element for min()');        }
    }
    else {
        ar = argv;
    } 
    retVal = ar[0];
    for (i = 1, n = ar.length; i < n; ++i) {
        if (_compare(retVal, ar[i]) == -1) {
            retVal = ar[i];        }
    }
 
    return retVal;
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
min(1, 3, 5, 6, 7);

Should return

1
1

» Example 2

Running

1
min([2, 4, 5]);

Should return

1
2

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have min goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Onno Marsman
11 Sep '08 Permalink

q  Kevin: I've sent a greatly improved version through email.


Contribute a New function

More functions

In this category

abs
acos
acosh
asin
asinh
atan
atan2
atanh
base_convert
bindec
ceil
cos
cosh
decbin
dechex
decoct
deg2rad
exp
expm1
floor
fmod
getrandmax
hexdec
hypot
is_finite
is_infinite
is_nan
lcg_value
log
log10
log1p
max
» min
mt_getrandmax
mt_rand
octdec
pi
pow
rad2deg
rand
round
sin
sinh
sqrt
tan
tanh

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !