g.js
g.js is a very small library (0.4kb minified, 0.2kb minified and gzipped) which provide a simple method to iterate over a suit of numbers. It can be used with Node.js or in the browser.
Installing
Download the latest version here, then import the script:
Node:
var g = ;
Browser:
Usage
g.from(<from>).to(<to>) [.excluded()] [.by(<step>)] { ._do(<fn>) | .to_a() }
-
<from>
: first number -
<to>
: last number (included) -
<step>
: increment for each step (may be negative; default:1
) -
<fn>
: function executed with every number -
.excluded()
: exclude the last number -
.by(…)
: use a custom step -
.to_a()
: return an array of numbers -
._do(…)
: iterate over the numbers suit with a function. It can be chained.
Examples
Basic:
> g;12345
Excluding final step:
> g;1234
Negative step:
> g;3210
chained ._do(…)
:
> var {console;}> g0101
Get an array:
> g12345678910