PHP's range in JavaScript

How to use

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

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
1range ( 0, 12 )[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
2range( 0, 100, 10 )[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
3range( 'a', 'i' )['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
4range( 'c', 'a' )['c', 'b', 'a']

PHP arrays and JavaScript

Please note that Locutus uses JavaScript objects as substitutes for PHP arrays, they are the closest we can get to this hashtable-like data structure without rolling our own. While many JavaScript implementations preserve the order of object properties, the ECMAScript Language Specification explicitly states that:

The mechanics and order of enumerating the properties is not specified.

So don't use this for anything serious if you rely on the order to be consistent accross platforms.

To influence how Locutus treats objects to arrays, you can check out the `locutus.objectsAsArrays` setting.

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

module.exports = function range(low, high, step) {
// discuss at: https://locutus.io/php/range/
// original by: Waldo Malqui Silva (https://waldo.malqui.info)
// example 1: range ( 0, 12 )
// returns 1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
// example 2: range( 0, 100, 10 )
// returns 2: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
// example 3: range( 'a', 'i' )
// returns 3: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
// example 4: range( 'c', 'a' )
// returns 4: ['c', 'b', 'a']

const matrix = []
let iVal
let endval
let plus
const walker = step || 1
let chars = false

if (!isNaN(low) && !isNaN(high)) {
iVal = low
endval = high
} else if (isNaN(low) && isNaN(high)) {
chars = true
iVal = low.charCodeAt(0)
endval = high.charCodeAt(0)
} else {
iVal = isNaN(low) ? 0 : low
endval = isNaN(high) ? 0 : high
}

plus = !(iVal > endval)
if (plus) {
while (iVal <= endval) {
matrix.push(chars ? String.fromCharCode(iVal) : iVal)
iVal += walker
}
} else {
while (iVal >= endval) {
matrix.push(chars ? String.fromCharCode(iVal) : iVal)
iVal -= walker
}
}

return matrix
}

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 array functions


Star