UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

187 lines (169 loc) 4.57 kB
--- layout: page title: "JavaScript max function" comments: true sharing: true footer: true alias: - /functions/view/max:468 - /functions/view/max - /functions/view/468 - /functions/max:468 - /functions/468 --- <!-- Generated by Rakefile:build --> A JavaScript equivalent of PHP's max {% codeblock math/max.js lang:js https://raw.github.com/kvz/phpjs/master/functions/math/max.js raw on github %} function max () { // From: http://phpjs.org/functions // + original by: Onno Marsman // + revised by: Onno Marsman // + tweaked by: Jack // % note: Long code cause we're aiming for maximum PHP compatibility // * example 1: max(1, 3, 5, 6, 7); // * returns 1: 7 // * example 2: max([2, 4, 5]); // * returns 2: 5 // * example 3: max(0, 'hello'); // * returns 3: 0 // * example 4: max('hello', 0); // * returns 4: 'hello' // * example 5: max(-1, 'hello'); // * returns 5: 'hello' // * example 6: max([2, 4, 8], [2, 5, 7]); // * returns 6: [2, 5, 7] 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; } else { 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 max()'); } else if (argc === 1) { if (typeof argv[0] === 'object') { ar = _obj2Array(argv[0]); } else { throw new Error('Wrong parameter count for max()'); } if (ar.length === 0) { throw new Error('Array must contain at least one element for max()'); } } 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; } {% endcodeblock %} - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/math/max.js) Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are the closest match to this hashtable-like data structure. Please also note that php.js offers community built functions and goes by the [McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just: - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/math/max.js) ### Example 1 This code {% codeblock lang:js example %} max(1, 3, 5, 6, 7); {% endcodeblock %} Should return {% codeblock lang:js returns %} 7 {% endcodeblock %} ### Example 2 This code {% codeblock lang:js example %} max([2, 4, 5]); {% endcodeblock %} Should return {% codeblock lang:js returns %} 5 {% endcodeblock %} ### Example 3 This code {% codeblock lang:js example %} max(0, 'hello'); {% endcodeblock %} Should return {% codeblock lang:js returns %} 0 {% endcodeblock %} ### Other PHP functions in the math extension {% render_partial _includes/custom/math.html %}