PHP's min in JavaScript

How to use

You you can install via yarn add locutus and require this function via const min = require('locutus/php/math/min').

It is important to use a bundler that supports tree-shaking so that you only ship the functions that you actually use to your browser, instead of all of Locutus, which is massive. Examples are: Parcel, webpack, or rollup.js. For server-side use this is typically less of a concern.

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
1min(1, 3, 5, 6, 7)1
2min([2, 4, 5])2
3min(0, 'hello')0
4min('hello', 0)'hello'
5min(-1, 'hello')-1
6min([2, 4, 8], [2, 5, 7])[2, 4, 8]

Notes

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

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

module.exports = function min() {
// discuss at: https://locutus.io/php/min/
// 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: 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]

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
}
const ar = []
for (const i in obj) {
if (obj.hasOwnProperty(i)) {
ar.push(obj[i])
}
}
return ar
}

const _compare = function (current, next) {
let i = 0
let n = 0
let tmp = 0
let nl = 0
let 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
}

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
}

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we assimilate first iterations with imperfections, hoping for others to take issue with-and improve them. This unorthodox approach has worked very well to foster fun and fruitful collaboration, but please be reminded to use our creations at your own risk. THE SOFTWARE IS PROVIDED "AS IS" has never been more true than for Locutus.

Now go and: [ View on GitHub | Edit on GitHub | View Raw ]


« More PHP math functions


Star