PHP's max in JavaScript

Here’s what our current JavaScript equivalent to PHP's max looks like.

module.exports = functionmax () {
// discuss at: https://locutus.io/php/max/
// original by: Onno Marsman (https://twitter.com/onnomarsman)
// revised by: Onno Marsman (https://twitter.com/onnomarsman)
// improved by: Jack
// note 1: 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]
let ar
let retVal
let i = 0
let n = 0
const argv = arguments
const argc = argv.length
const _obj2Array = function (obj) {
if (Object.prototype.toString.call(obj) === '[object Array]') {
return obj
} else {
const ar = []
for (const i in obj) {
if (obj.hasOwnProperty(i)) {
ar.push(obj[i])
}
}
return ar
}
}
var _compare = function (current, next) {
let i = 0
let n = 0
let tmp = 0
let nl = 0
let cl = 0
if (current === next) {
return0
} elseif (typeof current === 'object') {
if (typeof next === 'object') {
current = _obj2Array(current)
next = _obj2Array(next)
cl = current.length
nl = next.length
if (nl > cl) {
return1
} elseif (nl < cl) {
return-1
}
for (i = 0, n = cl; i < n; ++i) {
tmp = _compare(current[i], next[i])
if (tmp === 1) {
return1
} elseif (tmp === -1) {
return-1
}
}
return0
}
return-1
} elseif (typeof next === 'object') {
return1
} elseif (isNaN(next) && !isNaN(current)) {
if (current === 0) {
return0
}
return (current < 0 ? 1 : -1)
} elseif (isNaN(current) && !isNaN(next)) {
if (next === 0) {
return0
}
return (next > 0 ? 1 : -1)
}
if (next === current) {
return0
}
return (next > current ? 1 : -1)
}
if (argc === 0) {
thrownewError('At least one value should be passed to max()')
} elseif (argc === 1) {
if (typeof argv[0] === 'object') {
ar = _obj2Array(argv[0])
} else {
thrownewError('Wrong parameter count for max()')
}
if (ar.length === 0) {
thrownewError('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
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/math/max'). You could also require the math module in full so that you could access math.max instead.

If you intend to target the browser, you can then use a module bundler such as Parcel, webpack, Browserify, or rollup.js. This can be important because Locutus allows modern JavaScript in the source files, meaning it may not work in all browsers without a build/transpile step. Locutus does transpile all functions to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Notes

  • Long code cause we’re aiming for maximum PHP compatibility

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1max(1, 3, 5, 6, 7)7
2max([2, 4, 5])5
3max(0, 'hello')0
4max('hello', 0)'hello'
5max(-1, 'hello')'hello'
6max([2, 4, 8], [2, 5, 7])[2, 5, 7]

« More PHP math functions


Star