Install via yarn add locutus and import:
import { min } from 'locutus/php/math/min'.
Or with CommonJS: const { min } = require('locutus/php/math/min')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
if (typeof current === 'string' && typeof next === 'string' && Number.isNaN(currentNum) && Number.isNaN(nextNum)) { return next > current ? 1 : -1 }
return nextNum > currentNum ? 1 : -1 }
exportfunctionmin(...args: PhpMinMaxValue[]): PhpMinMaxValue { // 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 // improved by: Thomas Hohn // 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] // example 7: min({2:'two', 1:'one', 3:'three', 5:'five', 4:'four'}) // returns 7: 'five' // example 8: min('one', 'two') // returns 8: 'one'
if (args.length === 0) { thrownewError('At least one value should be passed to min()') }
letvalues: PhpMinMaxValue[]
if (args.length === 1) { const only = args[0] if (typeof only === 'undefined' || !isCollection(only)) { thrownewError('Wrong parameter count for min()') }
values = objectToArray(only)
if (values.length === 0) { thrownewError('Array must contain at least one element for min()') } } else { values = args }
const first = values[0] if (typeof first === 'undefined') { thrownewError('Array must contain at least one element for min()') }
let result = first
for (let index = 1; index < values.length; index += 1) { const candidate = values[index] if (typeof candidate === 'undefined') { continue } if (compareValues(result, candidate) === -1) { result = candidate } }
return result }
constisCollection = (value) => typeof value === 'object' && value !== null
constobjectToArray = (value) => { if (Array.isArray(value)) { return value }
if (typeof current === 'string' && typeof next === 'string' && Number.isNaN(currentNum) && Number.isNaN(nextNum)) { return next > current ? 1 : -1 }
return nextNum > currentNum ? 1 : -1 }
exportfunctionmin(...args) { // 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 // improved by: Thomas Hohn // 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] // example 7: min({2:'two', 1:'one', 3:'three', 5:'five', 4:'four'}) // returns 7: 'five' // example 8: min('one', 'two') // returns 8: 'one'
if (args.length === 0) { thrownewError('At least one value should be passed to min()') }
let values
if (args.length === 1) { const only = args[0] if (typeof only === 'undefined' || !isCollection(only)) { thrownewError('Wrong parameter count for min()') }
values = objectToArray(only)
if (values.length === 0) { thrownewError('Array must contain at least one element for min()') } } else { values = args }
const first = values[0] if (typeof first === 'undefined') { thrownewError('Array must contain at least one element for min()') }
let result = first
for (let index = 1; index < values.length; index += 1) { const candidate = values[index] if (typeof candidate === 'undefined') { continue } if (compareValues(result, candidate) === -1) { result = candidate } }
return result }
Improve this function
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.